runsv.c 16 KB

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