blob: 813709d4143eb083cddd68d45f53e78cc7c24d30 [file] [log] [blame]
Denis Vlasenko04c63862006-11-17 18:58:49 +00001/* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
2/* TODO: depends on runit_lib.c - review and reduce/eliminate */
3
4#include <sys/poll.h>
5#include <sys/file.h>
6#include "busybox.h"
7#include "runit_lib.h"
8
9static int selfpipe[2];
10
11/* state */
12#define S_DOWN 0
13#define S_RUN 1
14#define S_FINISH 2
15/* ctrl */
16#define C_NOOP 0
17#define C_TERM 1
18#define C_PAUSE 2
19/* want */
20#define W_UP 0
21#define W_DOWN 1
22#define W_EXIT 2
23
24struct svdir {
25 int pid;
Denis Vlasenko3aba6662007-03-09 22:46:06 +000026 smallint state;
27 smallint ctrl;
28 smallint want;
29 smallint islog;
Denis Vlasenko04c63862006-11-17 18:58:49 +000030 struct taia start;
31 int fdlock;
32 int fdcontrol;
33 int fdcontrolwrite;
Denis Vlasenko04c63862006-11-17 18:58:49 +000034};
35static struct svdir svd[2];
36
Denis Vlasenko3aba6662007-03-09 22:46:06 +000037static smallint sigterm;
38static smallint haslog;
39static smallint pidchanged = 1;
Denis Vlasenko04c63862006-11-17 18:58:49 +000040static int logpipe[2];
41static char *dir;
42
43#define usage() bb_show_usage()
44
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000045static void fatal2_cannot(const char *m1, const char *m2)
Denis Vlasenko04c63862006-11-17 18:58:49 +000046{
47 bb_perror_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2);
48 /* was exiting 111 */
49}
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000050static void fatal_cannot(const char *m)
Denis Vlasenko04c63862006-11-17 18:58:49 +000051{
52 fatal2_cannot(m, "");
53 /* was exiting 111 */
54}
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000055static void fatal2x_cannot(const char *m1, const char *m2)
Denis Vlasenko04c63862006-11-17 18:58:49 +000056{
57 bb_error_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2);
58 /* was exiting 111 */
59}
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000060static void warn_cannot(const char *m)
Denis Vlasenko04c63862006-11-17 18:58:49 +000061{
62 bb_perror_msg("%s: warning: cannot %s", dir, m);
63}
Denis Vlasenkoab2aea42007-01-29 22:51:58 +000064static void warnx_cannot(const char *m)
Denis Vlasenko04c63862006-11-17 18:58:49 +000065{
66 bb_error_msg("%s: warning: cannot %s", dir, m);
67}
68
Denis Vlasenko04c63862006-11-17 18:58:49 +000069static void s_child(int sig_no)
70{
71 write(selfpipe[1], "", 1);
72}
73
74static void s_term(int sig_no)
75{
76 sigterm = 1;
77 write(selfpipe[1], "", 1); /* XXX */
78}
79
80static char *add_str(char *p, const char *to_add)
81{
82 while ((*p = *to_add) != '\0') {
83 p++;
84 to_add++;
85 }
86 return p;
87}
88
89static int open_trunc_or_warn(const char *name)
90{
91 int fd = open_trunc(name);
92 if (fd < 0)
93 bb_perror_msg("%s: warning: cannot open %s",
94 dir, name);
95 return fd;
96}
97
98static int rename_or_warn(const char *old, const char *new)
99{
100 if (rename(old, new) == -1) {
101 bb_perror_msg("%s: warning: cannot rename %s to %s",
102 dir, old, new);
103 return -1;
104 }
105 return 0;
106}
107
108static void update_status(struct svdir *s)
109{
110 unsigned long l;
111 int fd;
112 char status[20];
113
114 /* pid */
115 if (pidchanged) {
116 fd = open_trunc_or_warn("supervise/pid.new");
117 if (fd < 0)
118 return;
119 if (s->pid) {
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000120 char spid[sizeof(int)*3 + 2];
121 int size = sprintf(spid, "%u\n", (unsigned)s->pid);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000122 write(fd, spid, size);
123 }
124 close(fd);
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000125 if (rename_or_warn("supervise/pid.new",
126 s->islog ? "log/supervise/pid" : "log/supervise/pid"+4))
Denis Vlasenko04c63862006-11-17 18:58:49 +0000127 return;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000128 pidchanged = 0;
129 }
130
131 /* stat */
132 fd = open_trunc_or_warn("supervise/stat.new");
133 if (fd < -1)
134 return;
135
136 {
137 char stat_buf[sizeof("finish, paused, got TERM, want down\n")];
138 char *p = stat_buf;
139 switch (s->state) {
140 case S_DOWN:
141 p = add_str(p, "down");
142 break;
143 case S_RUN:
144 p = add_str(p, "run");
145 break;
146 case S_FINISH:
147 p = add_str(p, "finish");
148 break;
149 }
150 if (s->ctrl & C_PAUSE) p = add_str(p, ", paused");
151 if (s->ctrl & C_TERM) p = add_str(p, ", got TERM");
152 if (s->state != S_DOWN)
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000153 switch (s->want) {
Denis Vlasenko04c63862006-11-17 18:58:49 +0000154 case W_DOWN:
155 p = add_str(p, ", want down");
156 break;
157 case W_EXIT:
158 p = add_str(p, ", want exit");
159 break;
160 }
161 *p++ = '\n';
162 write(fd, stat_buf, p - stat_buf);
163 close(fd);
164 }
165
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000166 rename_or_warn("supervise/stat.new",
167 s->islog ? "log/supervise/stat" : "log/supervise/stat"+4);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000168
169 /* supervise compatibility */
170 taia_pack(status, &s->start);
171 l = (unsigned long)s->pid;
172 status[12] = l; l >>=8;
173 status[13] = l; l >>=8;
174 status[14] = l; l >>=8;
175 status[15] = l;
176 if (s->ctrl & C_PAUSE)
177 status[16] = 1;
178 else
179 status[16] = 0;
180 if (s->want == W_UP)
181 status[17] = 'u';
182 else
183 status[17] = 'd';
184 if (s->ctrl & C_TERM)
185 status[18] = 1;
186 else
187 status[18] = 0;
188 status[19] = s->state;
189 fd = open_trunc_or_warn("supervise/status.new");
190 if (fd < 0)
191 return;
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000192 l = write(fd, status, sizeof(status));
Denis Vlasenko04c63862006-11-17 18:58:49 +0000193 if (l < 0) {
194 warn_cannot("write supervise/status.new");
195 close(fd);
196 unlink("supervise/status.new");
197 return;
198 }
199 close(fd);
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000200 if (l < sizeof(status)) {
Denis Vlasenko04c63862006-11-17 18:58:49 +0000201 warnx_cannot("write supervise/status.new: partial write");
202 return;
203 }
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000204 rename_or_warn("supervise/status.new",
205 s->islog ? "log/supervise/status" : "log/supervise/status"+4);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000206}
207
208static unsigned custom(struct svdir *s, char c)
209{
210 int pid;
211 int w;
212 char a[10];
213 struct stat st;
214 char *prog[2];
215
216 if (s->islog) return 0;
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000217 strcpy(a, "control/?");
Denis Vlasenko04c63862006-11-17 18:58:49 +0000218 a[8] = c;
219 if (stat(a, &st) == 0) {
220 if (st.st_mode & S_IXUSR) {
221 pid = fork();
222 if (pid == -1) {
223 warn_cannot("fork for control/?");
224 return 0;
225 }
226 if (!pid) {
227 if (haslog && fd_copy(1, logpipe[1]) == -1)
228 warn_cannot("setup stdout for control/?");
229 prog[0] = a;
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000230 prog[1] = NULL;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000231 execve(a, prog, environ);
232 fatal_cannot("run control/?");
233 }
234 while (wait_pid(&w, pid) == -1) {
235 if (errno == EINTR) continue;
236 warn_cannot("wait for child control/?");
237 return 0;
238 }
239 return !wait_exitcode(w);
240 }
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000241 } else {
242 if (errno != ENOENT)
243 warn_cannot("stat control/?");
Denis Vlasenko04c63862006-11-17 18:58:49 +0000244 }
245 return 0;
246}
247
248static void stopservice(struct svdir *s)
249{
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000250 if (s->pid && !custom(s, 't')) {
Denis Vlasenko04c63862006-11-17 18:58:49 +0000251 kill(s->pid, SIGTERM);
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000252 s->ctrl |= C_TERM;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000253 update_status(s);
254 }
255 if (s->want == W_DOWN) {
256 kill(s->pid, SIGCONT);
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000257 custom(s, 'd');
258 return;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000259 }
260 if (s->want == W_EXIT) {
261 kill(s->pid, SIGCONT);
262 custom(s, 'x');
263 }
264}
265
266static void startservice(struct svdir *s)
267{
268 int p;
269 char *run[2];
270
271 if (s->state == S_FINISH)
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000272 run[0] = (char*)"./finish";
Denis Vlasenko04c63862006-11-17 18:58:49 +0000273 else {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000274 run[0] = (char*)"./run";
Denis Vlasenko04c63862006-11-17 18:58:49 +0000275 custom(s, 'u');
276 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000277 run[1] = NULL;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000278
279 if (s->pid != 0) stopservice(s); /* should never happen */
280 while ((p = fork()) == -1) {
281 warn_cannot("fork, sleeping");
282 sleep(5);
283 }
284 if (p == 0) {
285 /* child */
286 if (haslog) {
287 if (s->islog) {
288 if (fd_copy(0, logpipe[0]) == -1)
289 fatal_cannot("setup filedescriptor for ./log/run");
290 close(logpipe[1]);
291 if (chdir("./log") == -1)
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000292 fatal_cannot("change directory to ./log");
Denis Vlasenko04c63862006-11-17 18:58:49 +0000293 } else {
294 if (fd_copy(1, logpipe[1]) == -1)
295 fatal_cannot("setup filedescriptor for ./run");
296 close(logpipe[0]);
297 }
298 }
Denis Vlasenko8c783952007-01-27 22:21:52 +0000299 sig_uncatch(SIGCHLD);
300 sig_unblock(SIGCHLD);
301 sig_uncatch(SIGTERM);
302 sig_unblock(SIGTERM);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000303 execve(*run, run, environ);
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000304 fatal2_cannot(s->islog ? "start log/" : "start ", *run);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000305 }
306 if (s->state != S_FINISH) {
307 taia_now(&s->start);
308 s->state = S_RUN;
309 }
310 s->pid = p;
311 pidchanged = 1;
312 s->ctrl = C_NOOP;
313 update_status(s);
314}
315
316static int ctrl(struct svdir *s, char c)
317{
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000318 int sig;
319
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000320 switch (c) {
Denis Vlasenko04c63862006-11-17 18:58:49 +0000321 case 'd': /* down */
322 s->want = W_DOWN;
323 update_status(s);
324 if (s->pid && s->state != S_FINISH) stopservice(s);
325 break;
326 case 'u': /* up */
327 s->want = W_UP;
328 update_status(s);
329 if (s->pid == 0) startservice(s);
330 break;
331 case 'x': /* exit */
332 if (s->islog) break;
333 s->want = W_EXIT;
334 update_status(s);
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000335 /* FALLTHROUGH */
Denis Vlasenko04c63862006-11-17 18:58:49 +0000336 case 't': /* sig term */
337 if (s->pid && s->state != S_FINISH) stopservice(s);
338 break;
339 case 'k': /* sig kill */
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000340 if (s->pid && !custom(s, c)) kill(s->pid, SIGKILL);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000341 s->state = S_DOWN;
342 break;
343 case 'p': /* sig pause */
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000344 if (s->pid && !custom(s, c)) kill(s->pid, SIGSTOP);
345 s->ctrl |= C_PAUSE;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000346 update_status(s);
347 break;
348 case 'c': /* sig cont */
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000349 if (s->pid && !custom(s, c)) kill(s->pid, SIGCONT);
350 if (s->ctrl & C_PAUSE) s->ctrl &= ~C_PAUSE;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000351 update_status(s);
352 break;
353 case 'o': /* once */
354 s->want = W_DOWN;
355 update_status(s);
356 if (!s->pid) startservice(s);
357 break;
358 case 'a': /* sig alarm */
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000359 sig = SIGALRM;
360 goto sendsig;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000361 case 'h': /* sig hup */
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000362 sig = SIGHUP;
363 goto sendsig;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000364 case 'i': /* sig int */
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000365 sig = SIGINT;
366 goto sendsig;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000367 case 'q': /* sig quit */
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000368 sig = SIGQUIT;
369 goto sendsig;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000370 case '1': /* sig usr1 */
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000371 sig = SIGUSR1;
372 goto sendsig;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000373 case '2': /* sig usr2 */
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000374 sig = SIGUSR2;
375 goto sendsig;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000376 }
377 return 1;
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000378 sendsig:
379 if (s->pid && !custom(s, c))
380 kill(s->pid, sig);
381 return 1;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000382}
383
Denis Vlasenko06af2162007-02-03 17:28:39 +0000384int runsv_main(int argc, char **argv);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000385int runsv_main(int argc, char **argv)
386{
387 struct stat s;
388 int fd;
389 int r;
390 char buf[256];
391
392 if (!argv[1] || argv[2]) usage();
393 dir = argv[1];
394
395 if (pipe(selfpipe) == -1) fatal_cannot("create selfpipe");
396 coe(selfpipe[0]);
397 coe(selfpipe[1]);
398 ndelay_on(selfpipe[0]);
399 ndelay_on(selfpipe[1]);
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000400
Denis Vlasenko8c783952007-01-27 22:21:52 +0000401 sig_block(SIGCHLD);
402 sig_catch(SIGCHLD, s_child);
403 sig_block(SIGTERM);
404 sig_catch(SIGTERM, s_term);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000405
406 xchdir(dir);
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000407 /* bss: svd[0].pid = 0; */
408 if (S_DOWN) svd[0].state = S_DOWN; /* otherwise already 0 (bss) */
409 if (C_NOOP) svd[0].ctrl = C_NOOP;
410 if (W_UP) svd[0].want = W_UP;
411 /* bss: svd[0].islog = 0; */
412 /* bss: svd[1].pid = 0; */
Denis Vlasenko04c63862006-11-17 18:58:49 +0000413 taia_now(&svd[0].start);
414 if (stat("down", &s) != -1) svd[0].want = W_DOWN;
415
416 if (stat("log", &s) == -1) {
417 if (errno != ENOENT)
418 warn_cannot("stat ./log");
419 } else {
420 if (!S_ISDIR(s.st_mode))
421 warnx_cannot("stat log/down: log is not a directory");
422 else {
423 haslog = 1;
424 svd[1].state = S_DOWN;
425 svd[1].ctrl = C_NOOP;
426 svd[1].want = W_UP;
427 svd[1].islog = 1;
428 taia_now(&svd[1].start);
429 if (stat("log/down", &s) != -1)
430 svd[1].want = W_DOWN;
431 if (pipe(logpipe) == -1)
432 fatal_cannot("create log pipe");
433 coe(logpipe[0]);
434 coe(logpipe[1]);
435 }
436 }
437
438 if (mkdir("supervise", 0700) == -1) {
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000439 r = readlink("supervise", buf, sizeof(buf));
Denis Vlasenko04c63862006-11-17 18:58:49 +0000440 if (r != -1) {
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000441 if (r == sizeof(buf))
442 fatal2x_cannot("readlink ./supervise", ": name too long");
Denis Vlasenko04c63862006-11-17 18:58:49 +0000443 buf[r] = 0;
444 mkdir(buf, 0700);
445 } else {
446 if ((errno != ENOENT) && (errno != EINVAL))
447 fatal_cannot("readlink ./supervise");
448 }
449 }
450 svd[0].fdlock = xopen3("log/supervise/lock"+4,
451 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
452 if (lock_exnb(svd[0].fdlock) == -1)
453 fatal_cannot("lock supervise/lock");
454 coe(svd[0].fdlock);
455 if (haslog) {
456 if (mkdir("log/supervise", 0700) == -1) {
457 r = readlink("log/supervise", buf, 256);
458 if (r != -1) {
459 if (r == 256)
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000460 fatal2x_cannot("readlink ./log/supervise", ": name too long");
Denis Vlasenko04c63862006-11-17 18:58:49 +0000461 buf[r] = 0;
462 fd = xopen(".", O_RDONLY|O_NDELAY);
463 xchdir("./log");
464 mkdir(buf, 0700);
465 if (fchdir(fd) == -1)
466 fatal_cannot("change back to service directory");
467 close(fd);
468 }
469 else {
470 if ((errno != ENOENT) && (errno != EINVAL))
471 fatal_cannot("readlink ./log/supervise");
472 }
473 }
474 svd[1].fdlock = xopen3("log/supervise/lock",
475 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
476 if (lock_ex(svd[1].fdlock) == -1)
477 fatal_cannot("lock log/supervise/lock");
478 coe(svd[1].fdlock);
479 }
480
Denis Vlasenkoe2473f82007-01-27 22:22:17 +0000481 mkfifo("log/supervise/control"+4, 0600);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000482 svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY);
483 coe(svd[0].fdcontrol);
484 svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY);
485 coe(svd[0].fdcontrolwrite);
486 update_status(&svd[0]);
487 if (haslog) {
Denis Vlasenkoe2473f82007-01-27 22:22:17 +0000488 mkfifo("log/supervise/control", 0600);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000489 svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY);
490 coe(svd[1].fdcontrol);
491 svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY);
492 coe(svd[1].fdcontrolwrite);
493 update_status(&svd[1]);
494 }
Denis Vlasenkoe2473f82007-01-27 22:22:17 +0000495 mkfifo("log/supervise/ok"+4, 0600);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000496 fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY);
497 coe(fd);
498 if (haslog) {
Denis Vlasenkoe2473f82007-01-27 22:22:17 +0000499 mkfifo("log/supervise/ok", 0600);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000500 fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY);
501 coe(fd);
502 }
503 for (;;) {
504 iopause_fd x[3];
505 struct taia deadline;
506 struct taia now;
507 char ch;
508
509 if (haslog)
510 if (!svd[1].pid && svd[1].want == W_UP)
511 startservice(&svd[1]);
512 if (!svd[0].pid)
513 if (svd[0].want == W_UP || svd[0].state == S_FINISH)
514 startservice(&svd[0]);
515
516 x[0].fd = selfpipe[0];
517 x[0].events = IOPAUSE_READ;
518 x[1].fd = svd[0].fdcontrol;
519 x[1].events = IOPAUSE_READ;
520 if (haslog) {
521 x[2].fd = svd[1].fdcontrol;
522 x[2].events = IOPAUSE_READ;
523 }
524 taia_now(&now);
525 taia_uint(&deadline, 3600);
526 taia_add(&deadline, &now, &deadline);
527
Denis Vlasenko8c783952007-01-27 22:21:52 +0000528 sig_unblock(SIGTERM);
529 sig_unblock(SIGCHLD);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000530 iopause(x, 2+haslog, &deadline, &now);
Denis Vlasenko8c783952007-01-27 22:21:52 +0000531 sig_block(SIGTERM);
532 sig_block(SIGCHLD);
Denis Vlasenko04c63862006-11-17 18:58:49 +0000533
534 while (read(selfpipe[0], &ch, 1) == 1)
535 ;
536 for (;;) {
537 int child;
538 int wstat;
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000539
Denis Vlasenko04c63862006-11-17 18:58:49 +0000540 child = wait_nohang(&wstat);
541 if (!child) break;
542 if ((child == -1) && (errno != EINTR)) break;
543 if (child == svd[0].pid) {
544 svd[0].pid = 0;
545 pidchanged = 1;
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000546 svd[0].ctrl &=~ C_TERM;
Denis Vlasenkod0762e32007-02-18 11:07:43 +0000547 if (svd[0].state != S_FINISH) {
Denis Vlasenko04c63862006-11-17 18:58:49 +0000548 fd = open_read("finish");
549 if (fd != -1) {
550 close(fd);
551 svd[0].state = S_FINISH;
552 update_status(&svd[0]);
553 continue;
554 }
Denis Vlasenkod0762e32007-02-18 11:07:43 +0000555 }
Denis Vlasenko04c63862006-11-17 18:58:49 +0000556 svd[0].state = S_DOWN;
557 taia_uint(&deadline, 1);
558 taia_add(&deadline, &svd[0].start, &deadline);
559 taia_now(&svd[0].start);
560 update_status(&svd[0]);
561 if (taia_less(&svd[0].start, &deadline)) sleep(1);
562 }
563 if (haslog) {
564 if (child == svd[1].pid) {
565 svd[1].pid = 0;
566 pidchanged = 1;
567 svd[1].state = S_DOWN;
Denis Vlasenko3aba6662007-03-09 22:46:06 +0000568 svd[1].ctrl &= ~C_TERM;
Denis Vlasenko04c63862006-11-17 18:58:49 +0000569 taia_uint(&deadline, 1);
570 taia_add(&deadline, &svd[1].start, &deadline);
571 taia_now(&svd[1].start);
572 update_status(&svd[1]);
573 if (taia_less(&svd[1].start, &deadline)) sleep(1);
574 }
575 }
576 }
577 if (read(svd[0].fdcontrol, &ch, 1) == 1)
578 ctrl(&svd[0], ch);
579 if (haslog)
580 if (read(svd[1].fdcontrol, &ch, 1) == 1)
581 ctrl(&svd[1], ch);
582
583 if (sigterm) {
584 ctrl(&svd[0], 'x');
585 sigterm = 0;
586 }
587
588 if (svd[0].want == W_EXIT && svd[0].state == S_DOWN) {
589 if (svd[1].pid == 0)
590 _exit(0);
591 if (svd[1].want != W_EXIT) {
592 svd[1].want = W_EXIT;
593 /* stopservice(&svd[1]); */
594 update_status(&svd[1]);
595 close(logpipe[1]);
596 close(logpipe[0]);
597 //if (close(logpipe[1]) == -1)
598 // warn_cannot("close logpipe[1]");
599 //if (close(logpipe[0]) == -1)
600 // warn_cannot("close logpipe[0]");
601 }
602 }
603 }
604 /* not reached */
605 return 0;
606}