summaryrefslogtreecommitdiffstats
path: root/piper.c
blob: cc176e4e3bbb7aaa64e75ff1860c4f2dc378e201 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define NUM_PROCS 2

struct proc {
	pid_t pid;
	char *cmd;
	char **argv;
	int  _stdin;
	int  _stdout;
	int  _stderr;
} procs[NUM_PROCS];

int fds[2];

volatile sig_atomic_t terminate = 0;
volatile sig_atomic_t sendsignal = 0;

void
reap_all()
{
	int i;
	for (i = 0; i < NUM_PROCS; i++) {
		if (procs[i].pid != -1)
			kill(procs[i].pid, SIGTERM);
	}

	sleep(5);

	for (i = 0; i < NUM_PROCS; i++) {
		if (procs[i].pid != -1)
			kill(procs[i].pid, SIGKILL);
	}
}

void
fail(int exit, const char *msg, ...)
{
	reap_all();

	va_list args;
	va_start(args, msg);
	verr(exit, msg, args);
	va_end(args); // Not reached?
}

void
failx(int exit, const char *msg, ...)
{
	reap_all();

	va_list args;
	va_start(args, msg);
	verrx(exit, msg, args);
	va_end(args); // Not reached?
}

void
gen_argv(struct proc *proc)
{
	int i;
	char *saveptr, *tmp, *cmd;
	char **argv, **head;

	// XXX: Check for overflow in argv
	argv = malloc(sizeof(char*) * 1024);
	if (argv == NULL)
		err(1, "malloc");

	cmd = strdup(proc->cmd);
	if (cmd == NULL)
		err(1, "strdup");

	head = argv;
	for (i = 0; i < 1024; i++)
		argv[i] = NULL;

	tmp = strtok_r(cmd, " \t", &saveptr);
	while (tmp != NULL) {
		*(head++) = strdup(tmp);
		tmp = strtok_r(NULL, " \t", &saveptr);
	}
	*head = NULL;

	free(cmd);

	proc->argv = argv;
}

void
run_cmd(struct proc *proc)
{
	pid_t cpid;

	cpid = fork();
	if (cpid == 0) {
		if (proc->_stdin != -1) {
			if (close(0) == -1)
				fail(1, "close");
			if (dup2(proc->_stdin, 0) == -1)
				fail(1, "dup2");
		}
		if (proc->_stdout != -1) {
			if (close(1) == -1)
				fail(1, "close");
			if (dup2(proc->_stdout, 1) == -1)
				fail(1, "dup2");
		}
		if (proc->_stderr != -1) {
			if (close(2) == -1)
				fail(1, "close");
			if (dup2(proc->_stderr, 2) == -1)
				fail(1, "dup2");
		}
		close(fds[0]); // These aren't very important if they fail
		close(fds[1]);

		execvp(proc->argv[0], proc->argv);

		fail(1, "execvp");
	} else if (cpid == -1) {
		fail(1, "fork");
	} else {
		proc->pid = cpid;
	}
}

int
have_child()
{
	int i;
	for (i = 0; i < NUM_PROCS; i++) {
		if (procs[i].pid != -1)
			return 1;
	}

	return 0;
}

struct proc *
proc_by_pid(pid_t pid)
{
	int i;
	for (i = 0; i < NUM_PROCS; i++) {
		if (procs[i].pid == pid)
			return &procs[i];
	}

	return NULL;
}

void
signal_procs()
{
	int i;
	for (i = 0; i < NUM_PROCS; i++) {
		if (procs[i].pid != -1)
			kill(procs[i].pid, sendsignal); //XXX: Check return?
	}

	sendsignal = 0;
}

void
handle_nonfatal(int sig, siginfo_t *siginfo, void *ucontext)
{
	sendsignal = sig;
}

void
handle_fatal(int sig, siginfo_t *siginfo, void *ucontext)
{
	terminate = 1;
	sendsignal = sig;
}

int
main(int argc, char **argv)
{
	if (argc < 2) {
		fprintf(stderr, "Usage: piper <cmd1> <cmd2>\n");
		return 1;
	}

	struct sigaction act;
	memset(&act, 0, sizeof(act));

	act.sa_sigaction = &handle_fatal;
	act.sa_flags = SA_SIGINFO;

	if (sigaction(SIGINT,  &act, NULL) == -1)
		err(1, "sigaction");
	if (sigaction(SIGHUP,  &act, NULL) == -1)
		err(1, "sigaction");
	if (sigaction(SIGTERM, &act, NULL) == -1)
		err(1, "sigaction");

	act.sa_sigaction = &handle_nonfatal;
	if (sigaction(SIGUSR1, &act, NULL) == -1)
		err(1, "sigaction");
	if (sigaction(SIGUSR2, &act, NULL) == -1)
		err(1, "sigaction");

	procs[0].cmd = strdup(argv[1]);
	if (procs[0].cmd == NULL)
		err(1, "strdup");

	procs[1].cmd = strdup(argv[2]);
	if (procs[1].cmd == NULL)
		err(1, "strdup");

	int i;
	for (i = 0; i < NUM_PROCS; i++) {
		procs[i].pid = -1;
		procs[i]._stdin = -1;
		procs[i]._stdout = -1;
		procs[i]._stderr = -1;
		gen_argv(&procs[i]);
	}

	if (pipe(fds) == -1)
		err(1, "pipe");

	procs[0]._stdout = fds[1];
	procs[0]._stderr = fds[1];
	procs[1]._stdin  = fds[0];

	run_cmd(&procs[0]);
	run_cmd(&procs[1]);
	
	while (have_child()) {
		int status;
		pid_t pid;

		if (sendsignal != 0)
			signal_procs();

		pid = wait(&status);
		if (pid == -1) {
			if (errno == EINTR)
				continue;
			else
				fail(1, "wait");
		}

		struct proc *p = proc_by_pid(pid);
		if (p == NULL)
			continue; // XXX: Log something here?
		p->pid = -1;

		if (!terminate)
			run_cmd(p);
	}

	return 0;
}