summaryrefslogtreecommitdiffstats
path: root/emulator/subprocess.c
blob: 969ae1b68b4e45b8cefe8a28541f34e9fa6fb281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 *   AVR emulator
 *   Emulator subprocess management
 *
 *   Copyright (C) 2007 Michael Buesch <mb@bu3sch.de>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "subprocess.h"
#include "devprocess.h"

#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>


static int fork_device_instance(struct avremu_host_instance *host)
{
	struct avremu_device_instance *dev;
	int err;
	pid_t pid;
	struct avrmsg_to_host hostmsg;

	dev = avr_malloc(sizeof(*dev));
	if (!dev)
		return -ENOMEM;
	dev->type = host->type;
	err = ipc_pipe_create(&host->msg_to_device, &dev->msg_from_host);
	if (err) {
		err = -ENOMEM;
		goto err_free_dev;
	}
	err = ipc_pipe_create(&dev->msg_to_host, &host->msg_from_device);
	if (err) {
		err = -ENOMEM;
		goto err_destroy1;
	}
	err = ipc_pipe_create(&dev->status_to_host, &host->status_from_device);
	if (err) {
		err = -ENOMEM;
		goto err_destroy2;
	}

	pid = fork();
	if (pid == (pid_t)0) {
		/* DEVICE process */
		ipc_pipe_destroy_tx(&host->msg_to_device);
		ipc_pipe_destroy_rx(&host->msg_from_device);
		ipc_pipe_destroy_rx(&host->status_from_device);
		host = NULL;

		/* Map stderr and stdout to the status pipe.
		 * Also enable line buffering. */
		ipc_dup2(&dev->status_to_host, STDERR_FILENO);
		ipc_dup2(&dev->status_to_host, STDOUT_FILENO);
		setvbuf(stdout, NULL, _IOLBF, 0);
		setvbuf(stderr, NULL, _IOLBF, 0);

		/* Run the device process. */
		err = avr_device_process_run(dev);
		sendnotify_to_host(dev, HOSTMSG_EXIT);
		exit(err >= 0 ? err : -err);
	} else if (pid < (pid_t)0) {
		/* Failure */
		err = -ENOMEM;
		goto err_destroy3;
	} else {
		/* HOST process */
		ipc_pipe_destroy_tx(&dev->msg_to_host);
		ipc_pipe_destroy_rx(&dev->msg_from_host);
		ipc_pipe_destroy_tx(&dev->status_to_host);
		free(dev);
		dev = NULL;

		host->device_pid = pid;
		/* Wait for the device to initialize itself. */
		while (1) {
			err = pollmsg_from_device(host, &hostmsg);
			if (err < 0) {
				err = -EIO;
				goto host_error;
			}
			if (err > 0) {
				if (hostmsg.msg == HOSTMSG_READY)
					break;
				if (hostmsg.msg == HOSTMSG_EXIT) {
					if (waitpid(pid, &err, 0) != pid)
						err = -ESRCH;
					else
						err = WEXITSTATUS(err);
					goto host_error;
				}
			}
		}
		host->device_running = 1;
		return 0;

	host_error:
		ipc_pipe_destroy_tx(&host->msg_to_device);
		ipc_pipe_destroy_rx(&host->msg_from_device);
		ipc_pipe_destroy_rx(&host->status_from_device);
		return err;
	}

	return 0;

err_destroy3:
	ipc_pipe_destroy(&dev->status_to_host, &host->status_from_device);
err_destroy2:
	ipc_pipe_destroy(&dev->msg_to_host, &host->msg_from_device);
err_destroy1:
	ipc_pipe_destroy(&host->msg_to_device, &dev->msg_from_host);
err_free_dev:
	free(dev);

	return err;
}

static void kill_device_instance(struct avremu_host_instance *host)
{
	int err;

printf("KILLING INSTANCE\n");
	if (!host->device_running)
		return;
printf("SEND TERM\n");
	err = kill(host->device_pid, SIGTERM);
	if (err) {
		perror("Kill device process");
		return;
	}
printf("WAITING\n");
	waitpid(host->device_pid, NULL, 0);
	host->device_running = 0;
printf("WAITING DONE.\n");
}

int avremu_instance_create(struct avremu_host_instance *host,
			   enum avr_setup_type type)
{
	int err;

	host->type = type;
	err = fork_device_instance(host);

	return err;
}

void avremu_instance_destroy(struct avremu_host_instance *host)
{
	kill_device_instance(host);
}
bues.ch cgit interface