3
0

runsv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
  2. /* TODO: depends on runit_lib.c - review and reduce/eliminate */
  3. #include <sys/poll.h>
  4. #include <sys/file.h>
  5. #include "busybox.h"
  6. #include "runit_lib.h"
  7. static int selfpipe[2];
  8. /* state */
  9. #define S_DOWN 0
  10. #define S_RUN 1
  11. #define S_FINISH 2
  12. /* ctrl */
  13. #define C_NOOP 0
  14. #define C_TERM 1
  15. #define C_PAUSE 2
  16. /* want */
  17. #define W_UP 0
  18. #define W_DOWN 1
  19. #define W_EXIT 2
  20. struct svdir {
  21. int pid;
  22. int state;
  23. int ctrl;
  24. int want;
  25. struct taia start;
  26. int fdlock;
  27. int fdcontrol;
  28. int fdcontrolwrite;
  29. int islog;
  30. };
  31. static struct svdir svd[2];
  32. static int sigterm = 0;
  33. static int haslog = 0;
  34. static int pidchanged = 1;
  35. static int logpipe[2];
  36. static char *dir;
  37. #define usage() bb_show_usage()
  38. static void fatal2_cannot(char *m1, char *m2)
  39. {
  40. bb_perror_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2);
  41. /* was exiting 111 */
  42. }
  43. static void fatal_cannot(char *m)
  44. {
  45. fatal2_cannot(m, "");
  46. /* was exiting 111 */
  47. }
  48. static void fatal2x_cannot(char *m1, char *m2)
  49. {
  50. bb_error_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2);
  51. /* was exiting 111 */
  52. }
  53. static void warn_cannot(char *m)
  54. {
  55. bb_perror_msg("%s: warning: cannot %s", dir, m);
  56. }
  57. static void warnx_cannot(char *m)
  58. {
  59. bb_error_msg("%s: warning: cannot %s", dir, m);
  60. }
  61. static void stopservice(struct svdir *);
  62. static void s_child(int sig_no)
  63. {
  64. write(selfpipe[1], "", 1);
  65. }
  66. static void s_term(int sig_no)
  67. {
  68. sigterm = 1;
  69. write(selfpipe[1], "", 1); /* XXX */
  70. }
  71. static char *add_str(char *p, const char *to_add)
  72. {
  73. while ((*p = *to_add) != '\0') {
  74. p++;
  75. to_add++;
  76. }
  77. return p;
  78. }
  79. static int open_trunc_or_warn(const char *name)
  80. {
  81. int fd = open_trunc(name);
  82. if (fd < 0)
  83. bb_perror_msg("%s: warning: cannot open %s",
  84. dir, name);
  85. return fd;
  86. }
  87. static int rename_or_warn(const char *old, const char *new)
  88. {
  89. if (rename(old, new) == -1) {
  90. bb_perror_msg("%s: warning: cannot rename %s to %s",
  91. dir, old, new);
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. static void update_status(struct svdir *s)
  97. {
  98. unsigned long l;
  99. int fd;
  100. char status[20];
  101. /* pid */
  102. if (pidchanged) {
  103. fd = open_trunc_or_warn("supervise/pid.new");
  104. if (fd < 0)
  105. return;
  106. if (s->pid) {
  107. char spid[sizeof(s->pid)*3 + 2];
  108. int size = sprintf(spid, "%d\n", s->pid);
  109. write(fd, spid, size);
  110. }
  111. close(fd);
  112. if (s->islog) {
  113. if (rename_or_warn("supervise/pid.new", "log/supervise/pid"))
  114. return;
  115. } else if (rename_or_warn("supervise/pid.new", "supervise/pid")) {
  116. return;
  117. }
  118. pidchanged = 0;
  119. }
  120. /* stat */
  121. fd = open_trunc_or_warn("supervise/stat.new");
  122. if (fd < -1)
  123. return;
  124. {
  125. char stat_buf[sizeof("finish, paused, got TERM, want down\n")];
  126. char *p = stat_buf;
  127. switch (s->state) {
  128. case S_DOWN:
  129. p = add_str(p, "down");
  130. break;
  131. case S_RUN:
  132. p = add_str(p, "run");
  133. break;
  134. case S_FINISH:
  135. p = add_str(p, "finish");
  136. break;
  137. }
  138. if (s->ctrl & C_PAUSE) p = add_str(p, ", paused");
  139. if (s->ctrl & C_TERM) p = add_str(p, ", got TERM");
  140. if (s->state != S_DOWN)
  141. switch(s->want) {
  142. case W_DOWN:
  143. p = add_str(p, ", want down");
  144. break;
  145. case W_EXIT:
  146. p = add_str(p, ", want exit");
  147. break;
  148. }
  149. *p++ = '\n';
  150. write(fd, stat_buf, p - stat_buf);
  151. close(fd);
  152. }
  153. if (s->islog) {
  154. rename_or_warn("supervise/stat.new", "log/supervise/stat");
  155. } else {
  156. rename_or_warn("supervise/stat.new", "log/supervise/stat"+4);
  157. }
  158. /* supervise compatibility */
  159. taia_pack(status, &s->start);
  160. l = (unsigned long)s->pid;
  161. status[12] = l; l >>=8;
  162. status[13] = l; l >>=8;
  163. status[14] = l; l >>=8;
  164. status[15] = l;
  165. if (s->ctrl & C_PAUSE)
  166. status[16] = 1;
  167. else
  168. status[16] = 0;
  169. if (s->want == W_UP)
  170. status[17] = 'u';
  171. else
  172. status[17] = 'd';
  173. if (s->ctrl & C_TERM)
  174. status[18] = 1;
  175. else
  176. status[18] = 0;
  177. status[19] = s->state;
  178. fd = open_trunc_or_warn("supervise/status.new");
  179. if (fd < 0)
  180. return;
  181. l = write(fd, status, sizeof status);
  182. if (l < 0) {
  183. warn_cannot("write supervise/status.new");
  184. close(fd);
  185. unlink("supervise/status.new");
  186. return;
  187. }
  188. close(fd);
  189. if (l < sizeof status) {
  190. warnx_cannot("write supervise/status.new: partial write");
  191. return;
  192. }
  193. if (s->islog) {
  194. rename_or_warn("supervise/status.new", "log/supervise/status");
  195. } else {
  196. rename_or_warn("supervise/status.new", "log/supervise/status"+4);
  197. }
  198. }
  199. static unsigned custom(struct svdir *s, char c)
  200. {
  201. int pid;
  202. int w;
  203. char a[10];
  204. struct stat st;
  205. char *prog[2];
  206. if (s->islog) return 0;
  207. memcpy(a, "control/?", 10);
  208. a[8] = c;
  209. if (stat(a, &st) == 0) {
  210. if (st.st_mode & S_IXUSR) {
  211. pid = fork();
  212. if (pid == -1) {
  213. warn_cannot("fork for control/?");
  214. return 0;
  215. }
  216. if (!pid) {
  217. if (haslog && fd_copy(1, logpipe[1]) == -1)
  218. warn_cannot("setup stdout for control/?");
  219. prog[0] = a;
  220. prog[1] = 0;
  221. execve(a, prog, environ);
  222. fatal_cannot("run control/?");
  223. }
  224. while (wait_pid(&w, pid) == -1) {
  225. if (errno == EINTR) continue;
  226. warn_cannot("wait for child control/?");
  227. return 0;
  228. }
  229. return !wait_exitcode(w);
  230. }
  231. }
  232. else {
  233. if (errno == ENOENT) return 0;
  234. warn_cannot("stat control/?");
  235. }
  236. return 0;
  237. }
  238. static void stopservice(struct svdir *s)
  239. {
  240. if (s->pid && ! custom(s, 't')) {
  241. kill(s->pid, SIGTERM);
  242. s->ctrl |=C_TERM;
  243. update_status(s);
  244. }
  245. if (s->want == W_DOWN) {
  246. kill(s->pid, SIGCONT);
  247. custom(s, 'd'); return;
  248. }
  249. if (s->want == W_EXIT) {
  250. kill(s->pid, SIGCONT);
  251. custom(s, 'x');
  252. }
  253. }
  254. static void startservice(struct svdir *s)
  255. {
  256. int p;
  257. char *run[2];
  258. if (s->state == S_FINISH)
  259. run[0] = "./finish";
  260. else {
  261. run[0] = "./run";
  262. custom(s, 'u');
  263. }
  264. run[1] = 0;
  265. if (s->pid != 0) stopservice(s); /* should never happen */
  266. while ((p = fork()) == -1) {
  267. warn_cannot("fork, sleeping");
  268. sleep(5);
  269. }
  270. if (p == 0) {
  271. /* child */
  272. if (haslog) {
  273. if (s->islog) {
  274. if (fd_copy(0, logpipe[0]) == -1)
  275. fatal_cannot("setup filedescriptor for ./log/run");
  276. close(logpipe[1]);
  277. if (chdir("./log") == -1)
  278. fatal_cannot("change directory to ./log");
  279. } else {
  280. if (fd_copy(1, logpipe[1]) == -1)
  281. fatal_cannot("setup filedescriptor for ./run");
  282. close(logpipe[0]);
  283. }
  284. }
  285. sig_uncatch(sig_child);
  286. sig_unblock(sig_child);
  287. sig_uncatch(sig_term);
  288. sig_unblock(sig_term);
  289. execve(*run, run, environ);
  290. if (s->islog)
  291. fatal2_cannot("start log/", *run);
  292. else
  293. fatal2_cannot("start ", *run);
  294. }
  295. if (s->state != S_FINISH) {
  296. taia_now(&s->start);
  297. s->state = S_RUN;
  298. }
  299. s->pid = p;
  300. pidchanged = 1;
  301. s->ctrl = C_NOOP;
  302. update_status(s);
  303. }
  304. static int ctrl(struct svdir *s, char c)
  305. {
  306. switch(c) {
  307. case 'd': /* down */
  308. s->want = W_DOWN;
  309. update_status(s);
  310. if (s->pid && s->state != S_FINISH) stopservice(s);
  311. break;
  312. case 'u': /* up */
  313. s->want = W_UP;
  314. update_status(s);
  315. if (s->pid == 0) startservice(s);
  316. break;
  317. case 'x': /* exit */
  318. if (s->islog) break;
  319. s->want = W_EXIT;
  320. update_status(s);
  321. if (s->pid && s->state != S_FINISH) stopservice(s);
  322. break;
  323. case 't': /* sig term */
  324. if (s->pid && s->state != S_FINISH) stopservice(s);
  325. break;
  326. case 'k': /* sig kill */
  327. if (s->pid && ! custom(s, c)) kill(s->pid, SIGKILL);
  328. s->state = S_DOWN;
  329. break;
  330. case 'p': /* sig pause */
  331. if (s->pid && ! custom(s, c)) kill(s->pid, SIGSTOP);
  332. s->ctrl |=C_PAUSE;
  333. update_status(s);
  334. break;
  335. case 'c': /* sig cont */
  336. if (s->pid && ! custom(s, c)) kill(s->pid, SIGCONT);
  337. if (s->ctrl & C_PAUSE) s->ctrl &=~C_PAUSE;
  338. update_status(s);
  339. break;
  340. case 'o': /* once */
  341. s->want = W_DOWN;
  342. update_status(s);
  343. if (!s->pid) startservice(s);
  344. break;
  345. case 'a': /* sig alarm */
  346. if (s->pid && ! custom(s, c)) kill(s->pid, SIGALRM);
  347. break;
  348. case 'h': /* sig hup */
  349. if (s->pid && ! custom(s, c)) kill(s->pid, SIGHUP);
  350. break;
  351. case 'i': /* sig int */
  352. if (s->pid && ! custom(s, c)) kill(s->pid, SIGINT);
  353. break;
  354. case 'q': /* sig quit */
  355. if (s->pid && ! custom(s, c)) kill(s->pid, SIGQUIT);
  356. break;
  357. case '1': /* sig usr1 */
  358. if (s->pid && ! custom(s, c)) kill(s->pid, SIGUSR1);
  359. break;
  360. case '2': /* sig usr2 */
  361. if (s->pid && ! custom(s, c)) kill(s->pid, SIGUSR2);
  362. break;
  363. }
  364. return 1;
  365. }
  366. int runsv_main(int argc, char **argv)
  367. {
  368. struct stat s;
  369. int fd;
  370. int r;
  371. char buf[256];
  372. if (!argv[1] || argv[2]) usage();
  373. dir = argv[1];
  374. if (pipe(selfpipe) == -1) fatal_cannot("create selfpipe");
  375. coe(selfpipe[0]);
  376. coe(selfpipe[1]);
  377. ndelay_on(selfpipe[0]);
  378. ndelay_on(selfpipe[1]);
  379. sig_block(sig_child);
  380. sig_catch(sig_child, s_child);
  381. sig_block(sig_term);
  382. sig_catch(sig_term, s_term);
  383. xchdir(dir);
  384. svd[0].pid = 0;
  385. svd[0].state = S_DOWN;
  386. svd[0].ctrl = C_NOOP;
  387. svd[0].want = W_UP;
  388. svd[0].islog = 0;
  389. svd[1].pid = 0;
  390. taia_now(&svd[0].start);
  391. if (stat("down", &s) != -1) svd[0].want = W_DOWN;
  392. if (stat("log", &s) == -1) {
  393. if (errno != ENOENT)
  394. warn_cannot("stat ./log");
  395. } else {
  396. if (!S_ISDIR(s.st_mode))
  397. warnx_cannot("stat log/down: log is not a directory");
  398. else {
  399. haslog = 1;
  400. svd[1].state = S_DOWN;
  401. svd[1].ctrl = C_NOOP;
  402. svd[1].want = W_UP;
  403. svd[1].islog = 1;
  404. taia_now(&svd[1].start);
  405. if (stat("log/down", &s) != -1)
  406. svd[1].want = W_DOWN;
  407. if (pipe(logpipe) == -1)
  408. fatal_cannot("create log pipe");
  409. coe(logpipe[0]);
  410. coe(logpipe[1]);
  411. }
  412. }
  413. if (mkdir("supervise", 0700) == -1) {
  414. r = readlink("supervise", buf, 256);
  415. if (r != -1) {
  416. if (r == 256)
  417. fatal2x_cannot("readlink ./supervise: ", "name too long");
  418. buf[r] = 0;
  419. mkdir(buf, 0700);
  420. } else {
  421. if ((errno != ENOENT) && (errno != EINVAL))
  422. fatal_cannot("readlink ./supervise");
  423. }
  424. }
  425. svd[0].fdlock = xopen3("log/supervise/lock"+4,
  426. O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
  427. if (lock_exnb(svd[0].fdlock) == -1)
  428. fatal_cannot("lock supervise/lock");
  429. coe(svd[0].fdlock);
  430. if (haslog) {
  431. if (mkdir("log/supervise", 0700) == -1) {
  432. r = readlink("log/supervise", buf, 256);
  433. if (r != -1) {
  434. if (r == 256)
  435. fatal2x_cannot("readlink ./log/supervise: ", "name too long");
  436. buf[r] = 0;
  437. fd = xopen(".", O_RDONLY|O_NDELAY);
  438. xchdir("./log");
  439. mkdir(buf, 0700);
  440. if (fchdir(fd) == -1)
  441. fatal_cannot("change back to service directory");
  442. close(fd);
  443. }
  444. else {
  445. if ((errno != ENOENT) && (errno != EINVAL))
  446. fatal_cannot("readlink ./log/supervise");
  447. }
  448. }
  449. svd[1].fdlock = xopen3("log/supervise/lock",
  450. O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
  451. if (lock_ex(svd[1].fdlock) == -1)
  452. fatal_cannot("lock log/supervise/lock");
  453. coe(svd[1].fdlock);
  454. }
  455. fifo_make("log/supervise/control"+4, 0600);
  456. svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY);
  457. coe(svd[0].fdcontrol);
  458. svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY);
  459. coe(svd[0].fdcontrolwrite);
  460. update_status(&svd[0]);
  461. if (haslog) {
  462. fifo_make("log/supervise/control", 0600);
  463. svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY);
  464. coe(svd[1].fdcontrol);
  465. svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY);
  466. coe(svd[1].fdcontrolwrite);
  467. update_status(&svd[1]);
  468. }
  469. fifo_make("log/supervise/ok"+4, 0600);
  470. fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY);
  471. coe(fd);
  472. if (haslog) {
  473. fifo_make("log/supervise/ok", 0600);
  474. fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY);
  475. coe(fd);
  476. }
  477. for (;;) {
  478. iopause_fd x[3];
  479. struct taia deadline;
  480. struct taia now;
  481. char ch;
  482. if (haslog)
  483. if (!svd[1].pid && svd[1].want == W_UP)
  484. startservice(&svd[1]);
  485. if (!svd[0].pid)
  486. if (svd[0].want == W_UP || svd[0].state == S_FINISH)
  487. startservice(&svd[0]);
  488. x[0].fd = selfpipe[0];
  489. x[0].events = IOPAUSE_READ;
  490. x[1].fd = svd[0].fdcontrol;
  491. x[1].events = IOPAUSE_READ;
  492. if (haslog) {
  493. x[2].fd = svd[1].fdcontrol;
  494. x[2].events = IOPAUSE_READ;
  495. }
  496. taia_now(&now);
  497. taia_uint(&deadline, 3600);
  498. taia_add(&deadline, &now, &deadline);
  499. sig_unblock(sig_term);
  500. sig_unblock(sig_child);
  501. iopause(x, 2+haslog, &deadline, &now);
  502. sig_block(sig_term);
  503. sig_block(sig_child);
  504. while (read(selfpipe[0], &ch, 1) == 1)
  505. ;
  506. for (;;) {
  507. int child;
  508. int wstat;
  509. child = wait_nohang(&wstat);
  510. if (!child) break;
  511. if ((child == -1) && (errno != EINTR)) break;
  512. if (child == svd[0].pid) {
  513. svd[0].pid = 0;
  514. pidchanged = 1;
  515. svd[0].ctrl &=~C_TERM;
  516. if (svd[0].state != S_FINISH)
  517. fd = open_read("finish");
  518. if (fd != -1) {
  519. close(fd);
  520. svd[0].state = S_FINISH;
  521. update_status(&svd[0]);
  522. continue;
  523. }
  524. svd[0].state = S_DOWN;
  525. taia_uint(&deadline, 1);
  526. taia_add(&deadline, &svd[0].start, &deadline);
  527. taia_now(&svd[0].start);
  528. update_status(&svd[0]);
  529. if (taia_less(&svd[0].start, &deadline)) sleep(1);
  530. }
  531. if (haslog) {
  532. if (child == svd[1].pid) {
  533. svd[1].pid = 0;
  534. pidchanged = 1;
  535. svd[1].state = S_DOWN;
  536. svd[1].ctrl &=~C_TERM;
  537. taia_uint(&deadline, 1);
  538. taia_add(&deadline, &svd[1].start, &deadline);
  539. taia_now(&svd[1].start);
  540. update_status(&svd[1]);
  541. if (taia_less(&svd[1].start, &deadline)) sleep(1);
  542. }
  543. }
  544. }
  545. if (read(svd[0].fdcontrol, &ch, 1) == 1)
  546. ctrl(&svd[0], ch);
  547. if (haslog)
  548. if (read(svd[1].fdcontrol, &ch, 1) == 1)
  549. ctrl(&svd[1], ch);
  550. if (sigterm) {
  551. ctrl(&svd[0], 'x');
  552. sigterm = 0;
  553. }
  554. if (svd[0].want == W_EXIT && svd[0].state == S_DOWN) {
  555. if (svd[1].pid == 0)
  556. _exit(0);
  557. if (svd[1].want != W_EXIT) {
  558. svd[1].want = W_EXIT;
  559. /* stopservice(&svd[1]); */
  560. update_status(&svd[1]);
  561. close(logpipe[1]);
  562. close(logpipe[0]);
  563. //if (close(logpipe[1]) == -1)
  564. // warn_cannot("close logpipe[1]");
  565. //if (close(logpipe[0]) == -1)
  566. // warn_cannot("close logpipe[0]");
  567. }
  568. }
  569. }
  570. /* not reached */
  571. return 0;
  572. }