runsv.c 16 KB

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