3
0

runsv.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. Copyright (c) 2001-2006, Gerrit Pape
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. The name of the author may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  14. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  16. EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  19. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  20. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  21. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
  25. //config:config RUNSV
  26. //config: bool "runsv"
  27. //config: default y
  28. //config: help
  29. //config: runsv starts and monitors a service and optionally an appendant log
  30. //config: service.
  31. //applet:IF_RUNSV(APPLET(runsv, BB_DIR_USR_BIN, BB_SUID_DROP))
  32. //kbuild:lib-$(CONFIG_RUNSV) += runsv.o
  33. //usage:#define runsv_trivial_usage
  34. //usage: "DIR"
  35. //usage:#define runsv_full_usage "\n\n"
  36. //usage: "Start and monitor a service and optionally an appendant log service"
  37. #include <sys/file.h>
  38. #include "libbb.h"
  39. #include "common_bufsiz.h"
  40. #include "runit_lib.h"
  41. #if ENABLE_MONOTONIC_SYSCALL
  42. #include <sys/syscall.h>
  43. /* libc has incredibly messy way of doing this,
  44. * typically requiring -lrt. We just skip all this mess */
  45. static void gettimeofday_ns(struct timespec *ts)
  46. {
  47. syscall(__NR_clock_gettime, CLOCK_REALTIME, ts);
  48. }
  49. #else
  50. static void gettimeofday_ns(struct timespec *ts)
  51. {
  52. BUILD_BUG_ON(sizeof(struct timeval) != sizeof(struct timespec));
  53. BUILD_BUG_ON(sizeof(((struct timeval*)ts)->tv_usec) != sizeof(ts->tv_nsec));
  54. /* Cheat */
  55. gettimeofday((void*)ts, NULL);
  56. ts->tv_nsec *= 1000;
  57. }
  58. #endif
  59. /* Compare possibly overflowing unsigned counters */
  60. #define LESS(a,b) ((int)((unsigned)(b) - (unsigned)(a)) > 0)
  61. /* state */
  62. #define S_DOWN 0
  63. #define S_RUN 1
  64. #define S_FINISH 2
  65. /* ctrl */
  66. #define C_NOOP 0
  67. #define C_TERM 1
  68. #define C_PAUSE 2
  69. /* want */
  70. #define W_UP 0
  71. #define W_DOWN 1
  72. #define W_EXIT 2
  73. struct svdir {
  74. int pid;
  75. smallint state;
  76. smallint ctrl;
  77. smallint sd_want;
  78. smallint islog;
  79. struct timespec start;
  80. int fdlock;
  81. int fdcontrol;
  82. int fdcontrolwrite;
  83. int wstat;
  84. };
  85. struct globals {
  86. smallint haslog;
  87. smallint sigterm;
  88. smallint pidchanged;
  89. struct fd_pair selfpipe;
  90. struct fd_pair logpipe;
  91. char *dir;
  92. struct svdir svd[2];
  93. } FIX_ALIASING;
  94. #define G (*(struct globals*)bb_common_bufsiz1)
  95. #define haslog (G.haslog )
  96. #define sigterm (G.sigterm )
  97. #define pidchanged (G.pidchanged )
  98. #define selfpipe (G.selfpipe )
  99. #define logpipe (G.logpipe )
  100. #define dir (G.dir )
  101. #define svd (G.svd )
  102. #define INIT_G() do { \
  103. setup_common_bufsiz(); \
  104. pidchanged = 1; \
  105. } while (0)
  106. static void fatal2_cannot(const char *m1, const char *m2)
  107. {
  108. bb_perror_msg_and_die("%s: fatal: can't %s%s", dir, m1, m2);
  109. /* was exiting 111 */
  110. }
  111. static void fatal_cannot(const char *m)
  112. {
  113. fatal2_cannot(m, "");
  114. /* was exiting 111 */
  115. }
  116. static void fatal2x_cannot(const char *m1, const char *m2)
  117. {
  118. bb_error_msg_and_die("%s: fatal: can't %s%s", dir, m1, m2);
  119. /* was exiting 111 */
  120. }
  121. static void warn2_cannot(const char *m1, const char *m2)
  122. {
  123. bb_perror_msg("%s: warning: can't %s%s", dir, m1, m2);
  124. }
  125. static void warn_cannot(const char *m)
  126. {
  127. warn2_cannot(m, "");
  128. }
  129. static void s_child(int sig_no UNUSED_PARAM)
  130. {
  131. write(selfpipe.wr, "", 1);
  132. }
  133. static void s_term(int sig_no UNUSED_PARAM)
  134. {
  135. sigterm = 1;
  136. write(selfpipe.wr, "", 1); /* XXX */
  137. }
  138. static int open_trunc_or_warn(const char *name)
  139. {
  140. /* Why O_NDELAY? */
  141. int fd = open(name, O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT, 0644);
  142. if (fd < 0)
  143. bb_perror_msg("%s: warning: cannot open %s",
  144. dir, name);
  145. return fd;
  146. }
  147. static void update_status(struct svdir *s)
  148. {
  149. ssize_t sz;
  150. int fd;
  151. svstatus_t status;
  152. const char *fstatus ="log/supervise/status";
  153. const char *fstatusnew ="log/supervise/status.new";
  154. const char *f_stat ="log/supervise/stat";
  155. const char *fstatnew ="log/supervise/stat.new";
  156. const char *fpid ="log/supervise/pid";
  157. const char *fpidnew ="log/supervise/pid.new";
  158. if (!s->islog) {
  159. fstatus += 4;
  160. fstatusnew += 4;
  161. f_stat += 4;
  162. fstatnew += 4;
  163. fpid += 4;
  164. fpidnew += 4;
  165. }
  166. /* pid */
  167. if (pidchanged) {
  168. fd = open_trunc_or_warn(fpidnew);
  169. if (fd < 0)
  170. return;
  171. if (s->pid) {
  172. char spid[sizeof(int)*3 + 2];
  173. int size = sprintf(spid, "%u\n", (unsigned)s->pid);
  174. write(fd, spid, size);
  175. }
  176. close(fd);
  177. if (rename_or_warn(fpidnew, fpid))
  178. return;
  179. pidchanged = 0;
  180. }
  181. /* stat */
  182. fd = open_trunc_or_warn(fstatnew);
  183. if (fd < -1)
  184. return;
  185. {
  186. char stat_buf[sizeof("finish, paused, got TERM, want down\n")];
  187. char *p = stat_buf;
  188. switch (s->state) {
  189. case S_DOWN:
  190. p = stpcpy(p, "down");
  191. break;
  192. case S_RUN:
  193. p = stpcpy(p, "run");
  194. break;
  195. case S_FINISH:
  196. p = stpcpy(p, "finish");
  197. break;
  198. }
  199. if (s->ctrl & C_PAUSE)
  200. p = stpcpy(p, ", paused");
  201. if (s->ctrl & C_TERM)
  202. p = stpcpy(p, ", got TERM");
  203. if (s->state != S_DOWN)
  204. switch (s->sd_want) {
  205. case W_DOWN:
  206. p = stpcpy(p, ", want down");
  207. break;
  208. case W_EXIT:
  209. p = stpcpy(p, ", want exit");
  210. break;
  211. }
  212. *p++ = '\n';
  213. write(fd, stat_buf, p - stat_buf);
  214. close(fd);
  215. }
  216. rename_or_warn(fstatnew, f_stat);
  217. /* supervise compatibility */
  218. memset(&status, 0, sizeof(status));
  219. status.time_be64 = SWAP_BE64(s->start.tv_sec + 0x400000000000000aULL);
  220. status.time_nsec_be32 = SWAP_BE32(s->start.tv_nsec);
  221. status.pid_le32 = SWAP_LE32(s->pid);
  222. if (s->ctrl & C_PAUSE)
  223. status.paused = 1;
  224. if (s->sd_want == W_UP)
  225. status.want = 'u';
  226. else
  227. status.want = 'd';
  228. if (s->ctrl & C_TERM)
  229. status.got_term = 1;
  230. status.run_or_finish = s->state;
  231. fd = open_trunc_or_warn(fstatusnew);
  232. if (fd < 0)
  233. return;
  234. sz = write(fd, &status, sizeof(status));
  235. close(fd);
  236. if (sz != sizeof(status)) {
  237. warn2_cannot("write ", fstatusnew);
  238. unlink(fstatusnew);
  239. return;
  240. }
  241. rename_or_warn(fstatusnew, fstatus);
  242. }
  243. static unsigned custom(struct svdir *s, char c)
  244. {
  245. pid_t pid;
  246. int w;
  247. char a[10];
  248. struct stat st;
  249. if (s->islog)
  250. return 0;
  251. strcpy(a, "control/?");
  252. a[8] = c; /* replace '?' */
  253. if (stat(a, &st) == 0) {
  254. if (st.st_mode & S_IXUSR) {
  255. pid = vfork();
  256. if (pid == -1) {
  257. warn2_cannot("vfork for ", a);
  258. return 0;
  259. }
  260. if (pid == 0) {
  261. /* child */
  262. if (haslog && dup2(logpipe.wr, 1) == -1)
  263. warn2_cannot("setup stdout for ", a);
  264. execl(a, a, (char *) NULL);
  265. fatal2_cannot("run ", a);
  266. }
  267. /* parent */
  268. if (safe_waitpid(pid, &w, 0) == -1) {
  269. warn2_cannot("wait for child ", a);
  270. return 0;
  271. }
  272. return WEXITSTATUS(w) == 0;
  273. }
  274. } else {
  275. if (errno != ENOENT)
  276. warn2_cannot("stat ", a);
  277. }
  278. return 0;
  279. }
  280. static void stopservice(struct svdir *s)
  281. {
  282. if (s->pid && !custom(s, 't')) {
  283. kill(s->pid, SIGTERM);
  284. s->ctrl |= C_TERM;
  285. update_status(s);
  286. }
  287. if (s->sd_want == W_DOWN) {
  288. kill(s->pid, SIGCONT);
  289. custom(s, 'd');
  290. return;
  291. }
  292. if (s->sd_want == W_EXIT) {
  293. kill(s->pid, SIGCONT);
  294. custom(s, 'x');
  295. }
  296. }
  297. static void startservice(struct svdir *s)
  298. {
  299. int p;
  300. const char *arg[4];
  301. char exitcode[sizeof(int)*3 + 2];
  302. if (s->state == S_FINISH) {
  303. /* Two arguments are given to ./finish. The first one is ./run exit code,
  304. * or -1 if ./run didnt exit normally. The second one is
  305. * the least significant byte of the exit status as determined by waitpid;
  306. * for instance it is 0 if ./run exited normally, and the signal number
  307. * if ./run was terminated by a signal. If runsv cannot start ./run
  308. * for some reason, the exit code is 111 and the status is 0.
  309. */
  310. arg[0] = "./finish";
  311. arg[1] = "-1";
  312. if (WIFEXITED(s->wstat)) {
  313. *utoa_to_buf(WEXITSTATUS(s->wstat), exitcode, sizeof(exitcode)) = '\0';
  314. arg[1] = exitcode;
  315. }
  316. //arg[2] = "0";
  317. //if (WIFSIGNALED(s->wstat)) {
  318. arg[2] = utoa(WTERMSIG(s->wstat));
  319. //}
  320. arg[3] = NULL;
  321. } else {
  322. arg[0] = "./run";
  323. arg[1] = NULL;
  324. custom(s, 'u');
  325. }
  326. if (s->pid != 0)
  327. stopservice(s); /* should never happen */
  328. while ((p = vfork()) == -1) {
  329. warn_cannot("vfork, sleeping");
  330. sleep(5);
  331. }
  332. if (p == 0) {
  333. /* child */
  334. if (haslog) {
  335. /* NB: bug alert! right order is close, then dup2 */
  336. if (s->islog) {
  337. xchdir("./log");
  338. close(logpipe.wr);
  339. xdup2(logpipe.rd, 0);
  340. } else {
  341. close(logpipe.rd);
  342. xdup2(logpipe.wr, 1);
  343. }
  344. }
  345. /* Non-ignored signals revert to SIG_DFL on exec anyway */
  346. /*bb_signals(0
  347. + (1 << SIGCHLD)
  348. + (1 << SIGTERM)
  349. , SIG_DFL);*/
  350. sig_unblock(SIGCHLD);
  351. sig_unblock(SIGTERM);
  352. execv(arg[0], (char**) arg);
  353. fatal2_cannot(s->islog ? "start log/" : "start ", arg[0]);
  354. }
  355. /* parent */
  356. if (s->state != S_FINISH) {
  357. gettimeofday_ns(&s->start);
  358. s->state = S_RUN;
  359. }
  360. s->pid = p;
  361. pidchanged = 1;
  362. s->ctrl = C_NOOP;
  363. update_status(s);
  364. }
  365. static int ctrl(struct svdir *s, char c)
  366. {
  367. int sig;
  368. switch (c) {
  369. case 'd': /* down */
  370. s->sd_want = W_DOWN;
  371. update_status(s);
  372. if (s->state == S_RUN)
  373. stopservice(s);
  374. break;
  375. case 'u': /* up */
  376. s->sd_want = W_UP;
  377. update_status(s);
  378. if (s->state == S_DOWN)
  379. startservice(s);
  380. break;
  381. case 'x': /* exit */
  382. if (s->islog)
  383. break;
  384. s->sd_want = W_EXIT;
  385. update_status(s);
  386. /* FALLTHROUGH */
  387. case 't': /* sig term */
  388. if (s->state == S_RUN)
  389. stopservice(s);
  390. break;
  391. case 'k': /* sig kill */
  392. if ((s->state == S_RUN) && !custom(s, c))
  393. kill(s->pid, SIGKILL);
  394. s->state = S_DOWN;
  395. break;
  396. case 'p': /* sig pause */
  397. if ((s->state == S_RUN) && !custom(s, c))
  398. kill(s->pid, SIGSTOP);
  399. s->ctrl |= C_PAUSE;
  400. update_status(s);
  401. break;
  402. case 'c': /* sig cont */
  403. if ((s->state == S_RUN) && !custom(s, c))
  404. kill(s->pid, SIGCONT);
  405. s->ctrl &= ~C_PAUSE;
  406. update_status(s);
  407. break;
  408. case 'o': /* once */
  409. s->sd_want = W_DOWN;
  410. update_status(s);
  411. if (s->state == S_DOWN)
  412. startservice(s);
  413. break;
  414. case 'a': /* sig alarm */
  415. sig = SIGALRM;
  416. goto sendsig;
  417. case 'h': /* sig hup */
  418. sig = SIGHUP;
  419. goto sendsig;
  420. case 'i': /* sig int */
  421. sig = SIGINT;
  422. goto sendsig;
  423. case 'q': /* sig quit */
  424. sig = SIGQUIT;
  425. goto sendsig;
  426. case '1': /* sig usr1 */
  427. sig = SIGUSR1;
  428. goto sendsig;
  429. case '2': /* sig usr2 */
  430. sig = SIGUSR2;
  431. goto sendsig;
  432. }
  433. return 1;
  434. sendsig:
  435. if ((s->state == S_RUN) && !custom(s, c))
  436. kill(s->pid, sig);
  437. return 1;
  438. }
  439. static void open_control(const char *f, struct svdir *s)
  440. {
  441. struct stat st;
  442. mkfifo(f, 0600);
  443. if (stat(f, &st) == -1)
  444. fatal2_cannot("stat ", f);
  445. if (!S_ISFIFO(st.st_mode))
  446. bb_error_msg_and_die("%s: fatal: %s exists but is not a fifo", dir, f);
  447. s->fdcontrol = xopen(f, O_RDONLY|O_NDELAY);
  448. close_on_exec_on(s->fdcontrol);
  449. s->fdcontrolwrite = xopen(f, O_WRONLY|O_NDELAY);
  450. close_on_exec_on(s->fdcontrolwrite);
  451. update_status(s);
  452. }
  453. int runsv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  454. int runsv_main(int argc UNUSED_PARAM, char **argv)
  455. {
  456. struct stat s;
  457. int fd;
  458. int r;
  459. char buf[256];
  460. INIT_G();
  461. dir = single_argv(argv);
  462. xpiped_pair(selfpipe);
  463. close_on_exec_on(selfpipe.rd);
  464. close_on_exec_on(selfpipe.wr);
  465. ndelay_on(selfpipe.rd);
  466. ndelay_on(selfpipe.wr);
  467. sig_block(SIGCHLD);
  468. bb_signals_recursive_norestart(1 << SIGCHLD, s_child);
  469. sig_block(SIGTERM);
  470. bb_signals_recursive_norestart(1 << SIGTERM, s_term);
  471. xchdir(dir);
  472. /* bss: svd[0].pid = 0; */
  473. if (S_DOWN) svd[0].state = S_DOWN; /* otherwise already 0 (bss) */
  474. if (C_NOOP) svd[0].ctrl = C_NOOP;
  475. if (W_UP) svd[0].sd_want = W_UP;
  476. /* bss: svd[0].islog = 0; */
  477. /* bss: svd[1].pid = 0; */
  478. gettimeofday_ns(&svd[0].start);
  479. if (stat("down", &s) != -1)
  480. svd[0].sd_want = W_DOWN;
  481. if (stat("log", &s) == -1) {
  482. if (errno != ENOENT)
  483. warn_cannot("stat ./log");
  484. } else {
  485. if (!S_ISDIR(s.st_mode)) {
  486. errno = 0;
  487. warn_cannot("stat log/down: log is not a directory");
  488. } else {
  489. haslog = 1;
  490. svd[1].state = S_DOWN;
  491. svd[1].ctrl = C_NOOP;
  492. svd[1].sd_want = W_UP;
  493. svd[1].islog = 1;
  494. gettimeofday_ns(&svd[1].start);
  495. if (stat("log/down", &s) != -1)
  496. svd[1].sd_want = W_DOWN;
  497. xpiped_pair(logpipe);
  498. close_on_exec_on(logpipe.rd);
  499. close_on_exec_on(logpipe.wr);
  500. }
  501. }
  502. if (mkdir("supervise", 0700) == -1) {
  503. r = readlink("supervise", buf, sizeof(buf));
  504. if (r != -1) {
  505. if (r == sizeof(buf))
  506. fatal2x_cannot("readlink ./supervise", ": name too long");
  507. buf[r] = 0;
  508. mkdir(buf, 0700);
  509. } else {
  510. if ((errno != ENOENT) && (errno != EINVAL))
  511. fatal_cannot("readlink ./supervise");
  512. }
  513. }
  514. svd[0].fdlock = xopen3("log/supervise/lock"+4,
  515. O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
  516. if (flock(svd[0].fdlock, LOCK_EX | LOCK_NB) == -1)
  517. fatal_cannot("lock supervise/lock");
  518. close_on_exec_on(svd[0].fdlock);
  519. if (haslog) {
  520. if (mkdir("log/supervise", 0700) == -1) {
  521. r = readlink("log/supervise", buf, 256);
  522. if (r != -1) {
  523. if (r == 256)
  524. fatal2x_cannot("readlink ./log/supervise", ": name too long");
  525. buf[r] = 0;
  526. fd = xopen(".", O_RDONLY|O_NDELAY);
  527. xchdir("./log");
  528. mkdir(buf, 0700);
  529. if (fchdir(fd) == -1)
  530. fatal_cannot("change back to service directory");
  531. close(fd);
  532. }
  533. else {
  534. if ((errno != ENOENT) && (errno != EINVAL))
  535. fatal_cannot("readlink ./log/supervise");
  536. }
  537. }
  538. svd[1].fdlock = xopen3("log/supervise/lock",
  539. O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
  540. if (flock(svd[1].fdlock, LOCK_EX) == -1)
  541. fatal_cannot("lock log/supervise/lock");
  542. close_on_exec_on(svd[1].fdlock);
  543. }
  544. open_control("log/supervise/control"+4, &svd[0]);
  545. if (haslog) {
  546. open_control("log/supervise/control", &svd[1]);
  547. }
  548. mkfifo("log/supervise/ok"+4, 0600);
  549. fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY);
  550. close_on_exec_on(fd);
  551. if (haslog) {
  552. mkfifo("log/supervise/ok", 0600);
  553. fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY);
  554. close_on_exec_on(fd);
  555. }
  556. for (;;) {
  557. struct pollfd x[3];
  558. unsigned deadline;
  559. char ch;
  560. if (haslog)
  561. if (!svd[1].pid && svd[1].sd_want == W_UP)
  562. startservice(&svd[1]);
  563. if (!svd[0].pid)
  564. if (svd[0].sd_want == W_UP || svd[0].state == S_FINISH)
  565. startservice(&svd[0]);
  566. x[0].fd = selfpipe.rd;
  567. x[0].events = POLLIN;
  568. x[1].fd = svd[0].fdcontrol;
  569. x[1].events = POLLIN;
  570. /* x[2] is used only if haslog == 1 */
  571. x[2].fd = svd[1].fdcontrol;
  572. x[2].events = POLLIN;
  573. sig_unblock(SIGTERM);
  574. sig_unblock(SIGCHLD);
  575. poll(x, 2 + haslog, 3600*1000);
  576. sig_block(SIGTERM);
  577. sig_block(SIGCHLD);
  578. while (read(selfpipe.rd, &ch, 1) == 1)
  579. continue;
  580. for (;;) {
  581. pid_t child;
  582. int wstat;
  583. child = wait_any_nohang(&wstat);
  584. if (!child)
  585. break;
  586. if ((child == -1) && (errno != EINTR))
  587. break;
  588. if (child == svd[0].pid) {
  589. svd[0].wstat = wstat;
  590. svd[0].pid = 0;
  591. pidchanged = 1;
  592. svd[0].ctrl &= ~C_TERM;
  593. if (svd[0].state != S_FINISH) {
  594. fd = open("finish", O_RDONLY|O_NDELAY);
  595. if (fd != -1) {
  596. close(fd);
  597. svd[0].state = S_FINISH;
  598. update_status(&svd[0]);
  599. continue;
  600. }
  601. }
  602. svd[0].state = S_DOWN;
  603. deadline = svd[0].start.tv_sec + 1;
  604. gettimeofday_ns(&svd[0].start);
  605. update_status(&svd[0]);
  606. if (LESS(svd[0].start.tv_sec, deadline))
  607. sleep(1);
  608. }
  609. if (haslog) {
  610. if (child == svd[1].pid) {
  611. svd[0].wstat = wstat;
  612. svd[1].pid = 0;
  613. pidchanged = 1;
  614. svd[1].state = S_DOWN;
  615. svd[1].ctrl &= ~C_TERM;
  616. deadline = svd[1].start.tv_sec + 1;
  617. gettimeofday_ns(&svd[1].start);
  618. update_status(&svd[1]);
  619. if (LESS(svd[1].start.tv_sec, deadline))
  620. sleep(1);
  621. }
  622. }
  623. } /* for (;;) */
  624. if (read(svd[0].fdcontrol, &ch, 1) == 1)
  625. ctrl(&svd[0], ch);
  626. if (haslog)
  627. if (read(svd[1].fdcontrol, &ch, 1) == 1)
  628. ctrl(&svd[1], ch);
  629. if (sigterm) {
  630. ctrl(&svd[0], 'x');
  631. sigterm = 0;
  632. }
  633. if (svd[0].sd_want == W_EXIT && svd[0].state == S_DOWN) {
  634. if (svd[1].pid == 0)
  635. _exit(EXIT_SUCCESS);
  636. if (svd[1].sd_want != W_EXIT) {
  637. svd[1].sd_want = W_EXIT;
  638. /* stopservice(&svd[1]); */
  639. update_status(&svd[1]);
  640. close(logpipe.wr);
  641. close(logpipe.rd);
  642. }
  643. }
  644. } /* for (;;) */
  645. /* not reached */
  646. return 0;
  647. }