3
0

runsv.c 16 KB

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