3
0

runsv.c 16 KB

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