1 /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
2 /* TODO: depends on runit_lib.c - review and reduce/eliminate */
9 static int selfpipe[2];
35 static struct svdir svd[2];
37 static int sigterm = 0;
38 static int haslog = 0;
39 static int pidchanged = 1;
40 static int logpipe[2];
43 #define usage() bb_show_usage()
45 static void fatal2_cannot(char *m1, char *m2)
47 bb_perror_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2);
50 static void fatal_cannot(char *m)
55 static void fatal2x_cannot(char *m1, char *m2)
57 bb_error_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2);
60 static void warn_cannot(char *m)
62 bb_perror_msg("%s: warning: cannot %s", dir, m);
64 static void warnx_cannot(char *m)
66 bb_error_msg("%s: warning: cannot %s", dir, m);
69 static void stopservice(struct svdir *);
71 static void s_child(int sig_no)
73 write(selfpipe[1], "", 1);
76 static void s_term(int sig_no)
79 write(selfpipe[1], "", 1); /* XXX */
82 static char *add_str(char *p, const char *to_add)
84 while ((*p = *to_add) != '\0') {
91 static int open_trunc_or_warn(const char *name)
93 int fd = open_trunc(name);
95 bb_perror_msg("%s: warning: cannot open %s",
100 static int rename_or_warn(const char *old, const char *new)
102 if (rename(old, new) == -1) {
103 bb_perror_msg("%s: warning: cannot rename %s to %s",
110 static void update_status(struct svdir *s)
118 fd = open_trunc_or_warn("supervise/pid.new");
122 char spid[sizeof(s->pid)*3 + 2];
123 int size = sprintf(spid, "%d\n", s->pid);
124 write(fd, spid, size);
128 if (rename_or_warn("supervise/pid.new", "log/supervise/pid"))
130 } else if (rename_or_warn("supervise/pid.new", "supervise/pid")) {
137 fd = open_trunc_or_warn("supervise/stat.new");
142 char stat_buf[sizeof("finish, paused, got TERM, want down\n")];
146 p = add_str(p, "down");
149 p = add_str(p, "run");
152 p = add_str(p, "finish");
155 if (s->ctrl & C_PAUSE) p = add_str(p, ", paused");
156 if (s->ctrl & C_TERM) p = add_str(p, ", got TERM");
157 if (s->state != S_DOWN)
160 p = add_str(p, ", want down");
163 p = add_str(p, ", want exit");
167 write(fd, stat_buf, p - stat_buf);
172 rename_or_warn("supervise/stat.new", "log/supervise/stat");
174 rename_or_warn("supervise/stat.new", "log/supervise/stat"+4);
177 /* supervise compatibility */
178 taia_pack(status, &s->start);
179 l = (unsigned long)s->pid;
180 status[12] = l; l >>=8;
181 status[13] = l; l >>=8;
182 status[14] = l; l >>=8;
184 if (s->ctrl & C_PAUSE)
192 if (s->ctrl & C_TERM)
196 status[19] = s->state;
197 fd = open_trunc_or_warn("supervise/status.new");
200 l = write(fd, status, sizeof status);
202 warn_cannot("write supervise/status.new");
204 unlink("supervise/status.new");
208 if (l < sizeof status) {
209 warnx_cannot("write supervise/status.new: partial write");
213 rename_or_warn("supervise/status.new", "log/supervise/status");
215 rename_or_warn("supervise/status.new", "log/supervise/status"+4);
219 static unsigned custom(struct svdir *s, char c)
227 if (s->islog) return 0;
228 memcpy(a, "control/?", 10);
230 if (stat(a, &st) == 0) {
231 if (st.st_mode & S_IXUSR) {
234 warn_cannot("fork for control/?");
238 if (haslog && fd_copy(1, logpipe[1]) == -1)
239 warn_cannot("setup stdout for control/?");
242 execve(a, prog, environ);
243 fatal_cannot("run control/?");
245 while (wait_pid(&w, pid) == -1) {
246 if (errno == EINTR) continue;
247 warn_cannot("wait for child control/?");
250 return !wait_exitcode(w);
254 if (errno == ENOENT) return 0;
255 warn_cannot("stat control/?");
260 static void stopservice(struct svdir *s)
262 if (s->pid && ! custom(s, 't')) {
263 kill(s->pid, SIGTERM);
267 if (s->want == W_DOWN) {
268 kill(s->pid, SIGCONT);
269 custom(s, 'd'); return;
271 if (s->want == W_EXIT) {
272 kill(s->pid, SIGCONT);
277 static void startservice(struct svdir *s)
282 if (s->state == S_FINISH)
290 if (s->pid != 0) stopservice(s); /* should never happen */
291 while ((p = fork()) == -1) {
292 warn_cannot("fork, sleeping");
299 if (fd_copy(0, logpipe[0]) == -1)
300 fatal_cannot("setup filedescriptor for ./log/run");
302 if (chdir("./log") == -1)
303 fatal_cannot("change directory to ./log");
305 if (fd_copy(1, logpipe[1]) == -1)
306 fatal_cannot("setup filedescriptor for ./run");
310 sig_uncatch(sig_child);
311 sig_unblock(sig_child);
312 sig_uncatch(sig_term);
313 sig_unblock(sig_term);
314 execve(*run, run, environ);
316 fatal2_cannot("start log/", *run);
318 fatal2_cannot("start ", *run);
320 if (s->state != S_FINISH) {
330 static int ctrl(struct svdir *s, char c)
336 if (s->pid && s->state != S_FINISH) stopservice(s);
341 if (s->pid == 0) startservice(s);
347 if (s->pid && s->state != S_FINISH) stopservice(s);
349 case 't': /* sig term */
350 if (s->pid && s->state != S_FINISH) stopservice(s);
352 case 'k': /* sig kill */
353 if (s->pid && ! custom(s, c)) kill(s->pid, SIGKILL);
356 case 'p': /* sig pause */
357 if (s->pid && ! custom(s, c)) kill(s->pid, SIGSTOP);
361 case 'c': /* sig cont */
362 if (s->pid && ! custom(s, c)) kill(s->pid, SIGCONT);
363 if (s->ctrl & C_PAUSE) s->ctrl &=~C_PAUSE;
369 if (!s->pid) startservice(s);
371 case 'a': /* sig alarm */
372 if (s->pid && ! custom(s, c)) kill(s->pid, SIGALRM);
374 case 'h': /* sig hup */
375 if (s->pid && ! custom(s, c)) kill(s->pid, SIGHUP);
377 case 'i': /* sig int */
378 if (s->pid && ! custom(s, c)) kill(s->pid, SIGINT);
380 case 'q': /* sig quit */
381 if (s->pid && ! custom(s, c)) kill(s->pid, SIGQUIT);
383 case '1': /* sig usr1 */
384 if (s->pid && ! custom(s, c)) kill(s->pid, SIGUSR1);
386 case '2': /* sig usr2 */
387 if (s->pid && ! custom(s, c)) kill(s->pid, SIGUSR2);
393 int runsv_main(int argc, char **argv)
400 if (!argv[1] || argv[2]) usage();
403 if (pipe(selfpipe) == -1) fatal_cannot("create selfpipe");
406 ndelay_on(selfpipe[0]);
407 ndelay_on(selfpipe[1]);
409 sig_block(sig_child);
410 sig_catch(sig_child, s_child);
412 sig_catch(sig_term, s_term);
416 svd[0].state = S_DOWN;
417 svd[0].ctrl = C_NOOP;
421 taia_now(&svd[0].start);
422 if (stat("down", &s) != -1) svd[0].want = W_DOWN;
424 if (stat("log", &s) == -1) {
426 warn_cannot("stat ./log");
428 if (!S_ISDIR(s.st_mode))
429 warnx_cannot("stat log/down: log is not a directory");
432 svd[1].state = S_DOWN;
433 svd[1].ctrl = C_NOOP;
436 taia_now(&svd[1].start);
437 if (stat("log/down", &s) != -1)
438 svd[1].want = W_DOWN;
439 if (pipe(logpipe) == -1)
440 fatal_cannot("create log pipe");
446 if (mkdir("supervise", 0700) == -1) {
447 r = readlink("supervise", buf, 256);
450 fatal2x_cannot("readlink ./supervise: ", "name too long");
454 if ((errno != ENOENT) && (errno != EINVAL))
455 fatal_cannot("readlink ./supervise");
458 svd[0].fdlock = xopen3("log/supervise/lock"+4,
459 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
460 if (lock_exnb(svd[0].fdlock) == -1)
461 fatal_cannot("lock supervise/lock");
464 if (mkdir("log/supervise", 0700) == -1) {
465 r = readlink("log/supervise", buf, 256);
468 fatal2x_cannot("readlink ./log/supervise: ", "name too long");
470 fd = xopen(".", O_RDONLY|O_NDELAY);
473 if (fchdir(fd) == -1)
474 fatal_cannot("change back to service directory");
478 if ((errno != ENOENT) && (errno != EINVAL))
479 fatal_cannot("readlink ./log/supervise");
482 svd[1].fdlock = xopen3("log/supervise/lock",
483 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
484 if (lock_ex(svd[1].fdlock) == -1)
485 fatal_cannot("lock log/supervise/lock");
489 fifo_make("log/supervise/control"+4, 0600);
490 svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY);
491 coe(svd[0].fdcontrol);
492 svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY);
493 coe(svd[0].fdcontrolwrite);
494 update_status(&svd[0]);
496 fifo_make("log/supervise/control", 0600);
497 svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY);
498 coe(svd[1].fdcontrol);
499 svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY);
500 coe(svd[1].fdcontrolwrite);
501 update_status(&svd[1]);
503 fifo_make("log/supervise/ok"+4, 0600);
504 fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY);
507 fifo_make("log/supervise/ok", 0600);
508 fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY);
513 struct taia deadline;
518 if (!svd[1].pid && svd[1].want == W_UP)
519 startservice(&svd[1]);
521 if (svd[0].want == W_UP || svd[0].state == S_FINISH)
522 startservice(&svd[0]);
524 x[0].fd = selfpipe[0];
525 x[0].events = IOPAUSE_READ;
526 x[1].fd = svd[0].fdcontrol;
527 x[1].events = IOPAUSE_READ;
529 x[2].fd = svd[1].fdcontrol;
530 x[2].events = IOPAUSE_READ;
533 taia_uint(&deadline, 3600);
534 taia_add(&deadline, &now, &deadline);
536 sig_unblock(sig_term);
537 sig_unblock(sig_child);
538 iopause(x, 2+haslog, &deadline, &now);
540 sig_block(sig_child);
542 while (read(selfpipe[0], &ch, 1) == 1)
548 child = wait_nohang(&wstat);
550 if ((child == -1) && (errno != EINTR)) break;
551 if (child == svd[0].pid) {
554 svd[0].ctrl &=~C_TERM;
555 if (svd[0].state != S_FINISH)
556 fd = open_read("finish");
559 svd[0].state = S_FINISH;
560 update_status(&svd[0]);
563 svd[0].state = S_DOWN;
564 taia_uint(&deadline, 1);
565 taia_add(&deadline, &svd[0].start, &deadline);
566 taia_now(&svd[0].start);
567 update_status(&svd[0]);
568 if (taia_less(&svd[0].start, &deadline)) sleep(1);
571 if (child == svd[1].pid) {
574 svd[1].state = S_DOWN;
575 svd[1].ctrl &=~C_TERM;
576 taia_uint(&deadline, 1);
577 taia_add(&deadline, &svd[1].start, &deadline);
578 taia_now(&svd[1].start);
579 update_status(&svd[1]);
580 if (taia_less(&svd[1].start, &deadline)) sleep(1);
584 if (read(svd[0].fdcontrol, &ch, 1) == 1)
587 if (read(svd[1].fdcontrol, &ch, 1) == 1)
595 if (svd[0].want == W_EXIT && svd[0].state == S_DOWN) {
598 if (svd[1].want != W_EXIT) {
599 svd[1].want = W_EXIT;
600 /* stopservice(&svd[1]); */
601 update_status(&svd[1]);
604 //if (close(logpipe[1]) == -1)
605 // warn_cannot("close logpipe[1]");
606 //if (close(logpipe[0]) == -1)
607 // warn_cannot("close logpipe[0]");