-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
multirun.c
245 lines (221 loc) · 6.58 KB
/
multirun.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#if __linux__ || __unix__
#include <sys/prctl.h>
#endif
#ifndef PROJECT_VERSION
#define PROJECT_VERSION "0.0.0"
#endif
#ifdef PR_SET_CHILD_SUBREAPER
#define SUBREAPER 1
#else
#define SUBREAPER 0
#endif
typedef struct {
pid_t pid;
const char* command;
int up;
int error;
} subprocess;
void print_help();
void launch_processes();
void sub_exec(const char * command);
void kill_all(int signal);
void sig_receive(int signum);
void reap_zombies();
int verbose = 0;
char* const* commands;
int nbr_processes = 0;
subprocess* subprocesses = NULL;
int closing = 0;
int main(int argc, char *const *argv) {
int opt;
while ((opt = getopt(argc, argv, "vh")) != -1) {
switch (opt) {
case 'v':
verbose = 1;
break;
case 'h':
print_help();
exit(0);
break;
default:
printf("Unknown option: %c\n", (char)optopt);
print_help();
exit(-2);
}
}
if (optind >= argc) {
print_help();
exit(-2);
}
commands = &argv[optind];
nbr_processes = argc - optind;
launch_processes();
}
void launch_processes() {
int subreaper = 0;
#if SUBREAPER
if (prctl(PR_SET_CHILD_SUBREAPER, 1) == 0) {
subreaper = 1;
}
#endif
if (!subreaper && getpid() != 1) {
if (verbose) {
printf("multirun: failed to register as subreaper, probably because the plaform doesn't support it. Subchildren exit status will be ignored.\n");
}
}
if (signal(SIGINT, sig_receive) == SIG_ERR) {
fprintf(stderr, "multirun: error registering signals\n");
exit(-2);
}
if (signal(SIGTERM, sig_receive) == SIG_ERR) {
fprintf(stderr, "multirun: error registering signals\n");
exit(-2);
}
subprocesses = malloc(sizeof(subprocess) * nbr_processes);
for (int i = 0; i < nbr_processes; i++) {
pid_t pid = fork();
if (pid == 0) {
// declare a new group
setpgrp();
// execute subcommand
sub_exec(commands[i]);
exit(-2); // should not happen
} else {
if (verbose) {
printf("multirun: launched command %s with pid %d\n", commands[i], pid);
}
subprocess new_sub;
new_sub.pid = pid;
new_sub.command = commands[i];
new_sub.up = 1;
new_sub.error = 0;
subprocesses[i] = new_sub;
}
}
while (1) {
int wstatus;
pid_t pid = waitpid(-1, &wstatus, 0);
if (pid == -1) {
if (errno == ECHILD) {
break; // no more children
} if (errno == EINTR) {
continue; // interrupted
} else {
fprintf(stderr, "multirun: error during wait: %d\n", errno);
exit(-2);
}
} else if (pid == 0) {
break; // no more children
}
subprocess* process = NULL;
for (int i = 0; i < nbr_processes; i++) {
if (subprocesses[i].pid == pid) {
process = &subprocesses[i];
break;
}
}
if (process != NULL) {
// check if process is down
if (WIFEXITED(wstatus) || WIFSIGNALED(wstatus)) {
process->up = 0;
if ((WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0)
|| (WIFSIGNALED(wstatus) && WTERMSIG(wstatus) != SIGINT && WTERMSIG(wstatus) != SIGTERM)) {
process->error = 1;
if (verbose) {
printf("multirun: command %s with pid %d exited abnormally\n", process->command, process->pid);
}
} else {
if (verbose) {
printf("multirun: command %s with pid %d exited normally\n", process->command, process->pid);
}
}
if (! closing) {
closing = 1;
// activate Goebbels mode
if (verbose) {
printf("multirun: sending SIGTERM to all subprocesses\n");
}
kill_all(SIGTERM);
}
}
} else {
if (verbose) {
printf("multirun: subchild process with pid %d ended\n", pid);
}
}
}
// check if there were errors
int error = 0;
for (int i = 0; i < nbr_processes; i++) {
if (subprocesses[i].error) {
error = 1;
break;
}
}
if (error) {
fprintf(stderr, "multirun: one or more of the provided commands ended abnormally\n");
exit(-1);
} else {
if (verbose) {
printf("multirun: all subprocesses exited without errors\n");
}
exit(0);
}
}
void print_help() {
printf("Usage: multirun <options> command...\n");
printf("\n");
printf("Version: %s\n", PROJECT_VERSION);
printf("\n");
printf("multirun is a small utility to run multiple commands concurrently.\n");
printf("\n");
printf("If one of these commands terminate it will kill all the others and wait for all its children and subchildren to exit before exiting.\n");
printf("\n");
printf("Options:\n");
printf(" -v verbose mode\n");
printf(" -h display help\n");
}
void sub_exec(const char* command) {
int ret;
const char* pre = "exec ";
char* ccommand;
ccommand = malloc((sizeof(char) * (strlen(pre) + strlen(command))) + 1);
ccommand[0] = 0;
strcat(ccommand, pre);
strcat(ccommand, command);
ret = execlp("sh", "sh", "-c", ccommand, (char*) NULL);
if (ret != 0) {
fprintf(stderr, "multirun: error launching the subprocess: %s\n", strerror(errno));
exit(-2);
}
}
void kill_all(int signal) {
for (int i = 0; i < nbr_processes; i++) {
int ret = kill(-subprocesses[i].pid, signal);
if (ret != 0) {
if (errno == ESRCH) {
// ignore
} else {
fprintf(stderr, "multirun: error %d while killing processes\n", errno);
exit(-2);
}
}
}
}
void sig_receive(int signum) {
if (verbose) {
printf("multirun: received signal %s, propagating to all subprocesses\n", strsignal(signum));
}
if (signum == SIGTERM) {
closing = 1;
}
kill_all(signum);
}