runsv.c 17 KB

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