forked from nicolas-van/multirun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultirun.c
206 lines (188 loc) · 6.14 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
/*
Copyright (c) 2016 Nicolas Vanhoren
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#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>
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);
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: /* '?' */
print_help();
exit(-1);
}
}
if (optind >= argc) {
print_help();
exit(-1);
}
commands = &argv[optind];
nbr_processes = argc - optind;
launch_processes();
}
void launch_processes() {
int wstatus;
struct sigaction ssig;
subprocesses = malloc(sizeof(subprocess) * nbr_processes);
for (int i = 0; i < nbr_processes; i++) {
pid_t pid = fork();
if (pid == 0) {
sub_exec(commands[i]);
exit(-1); // 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;
}
}
ssig.sa_handler = sig_receive;
if (sigaction(SIGINT, &ssig, NULL))
exit(-1);
if (sigaction(SIGTERM, &ssig, NULL))
exit(-1);
while (1) {
pid_t pid = wait(&wstatus);
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, pid);
}
} else {
if (verbose) {
printf("multirun: command %s with pid %d exited normally\n", process->command, pid);
}
}
if (! closing) {
closing = 1;
// activate Goebbels mode
if (verbose) {
printf("multirun: sending SIGTERM to all subprocesses\n");
}
kill_all(SIGTERM);
}
}
}
// check if all processes are stopped
int running = 0;
for (int i = 0; i < nbr_processes; i++) {
if (subprocesses[i].up) {
running = 1;
break;
}
}
if (! running)
break;
}
// check if there are 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("multirun is a small utility to run multiple commands concurrently. ");
printf("If one of these commands terminate it will kill all the others.\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(-1);
}
}
void kill_all(int signal) {
for (int i = 0; i < nbr_processes; i++) {
kill(subprocesses[i].pid, signal);
}
}
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);
};