runsv.c 16 KB

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