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
|
#include <errno.h>
#include <signal.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
gen_argv(struct proc *proc)
{
int i;
char *saveptr, *tmp;
char **argv = malloc(sizeof(char*) * 1024);
char **head = argv;
char *cmd2 = strdup(proc->cmd);
// XXX: Check malloc/strdup return
// XXX: Check for overflow in tmp
for (i = 0; i < 1024; i++)
argv[i] = NULL;
tmp = strtok_r(cmd2, " \t", &saveptr);
while (tmp != NULL) {
*(head++) = strdup(tmp);
tmp = strtok_r(NULL, " \t", &saveptr);
}
*head = NULL;
free(cmd2);
proc->argv = argv;
}
int
run_cmd(struct proc *proc)
{
pid_t cpid;
cpid = fork();
if (cpid == 0) {
if (proc->_stdin != -1) {
close(0);
dup2(proc->_stdin, 0);
}
if (proc->_stdout != -1) {
close(1);
dup2(proc->_stdout, 1);
}
if (proc->_stderr != -1) {
close(2);
dup2(proc->_stderr, 2);
}
close(fds[0]);
close(fds[1]);
execvp(proc->argv[0], proc->argv);
perror("execvp");
} else if (cpid == -1) {
// error D:
return -1;
} else {
proc->pid = cpid;
}
return 0;
}
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;
sigaction(SIGINT, &act, NULL);
sigaction(SIGHUP, &act, NULL);
sigaction(SIGTERM, &act, NULL);
act.sa_sigaction = &handle_nonfatal;
sigaction(SIGUSR1, &act, NULL);
sigaction(SIGUSR2, &act, NULL);
procs[0].cmd = strdup(argv[1]);
procs[1].cmd = strdup(argv[2]);
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]);
}
pipe(fds); // XXX: Check return code
procs[0]._stdout = fds[1];
procs[0]._stderr = fds[1];
procs[1]._stdin = fds[0];
run_cmd(&procs[0]);
run_cmd(&procs[1]); // XXX: Check return codes
while (have_child()) {
int status;
pid_t pid;
if (sendsignal != 0)
signal_procs();
pid = wait(&status);
if (pid == -1 && errno == EINTR)
continue;
struct proc *p = proc_by_pid(pid); // XXX: Check return code
p->pid = -1;
if (!terminate)
run_cmd(p); // XXX: Check return code
}
return 0;
}
|