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(const char *m1, const char *m2)
47 bb_perror_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2);
50 static void fatal_cannot(const char *m)
55 static void fatal2x_cannot(const char *m1, const char *m2)
57 bb_error_msg_and_die("%s: fatal: cannot %s%s", dir, m1, m2);
60 static void warn_cannot(const char *m)
62 bb_perror_msg("%s: warning: cannot %s", dir, m);
64 static void warnx_cannot(const 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)
283 run[0] = (char*)"./finish";
285 run[0] = (char*)"./run";
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(SIGCHLD);
311 sig_unblock(SIGCHLD);
312 sig_uncatch(SIGTERM);
313 sig_unblock(SIGTERM);
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);
394 int runsv_main(int argc, char **argv)
401 if (!argv[1] || argv[2]) usage();
404 if (pipe(selfpipe) == -1) fatal_cannot("create selfpipe");
407 ndelay_on(selfpipe[0]);
408 ndelay_on(selfpipe[1]);
411 sig_catch(SIGCHLD, s_child);
413 sig_catch(SIGTERM, s_term);
417 svd[0].state = S_DOWN;
418 svd[0].ctrl = C_NOOP;
422 taia_now(&svd[0].start);
423 if (stat("down", &s) != -1) svd[0].want = W_DOWN;
425 if (stat("log", &s) == -1) {
427 warn_cannot("stat ./log");
429 if (!S_ISDIR(s.st_mode))
430 warnx_cannot("stat log/down: log is not a directory");
433 svd[1].state = S_DOWN;
434 svd[1].ctrl = C_NOOP;
437 taia_now(&svd[1].start);
438 if (stat("log/down", &s) != -1)
439 svd[1].want = W_DOWN;
440 if (pipe(logpipe) == -1)
441 fatal_cannot("create log pipe");
447 if (mkdir("supervise", 0700) == -1) {
448 r = readlink("supervise", buf, 256);
451 fatal2x_cannot("readlink ./supervise: ", "name too long");
455 if ((errno != ENOENT) && (errno != EINVAL))
456 fatal_cannot("readlink ./supervise");
459 svd[0].fdlock = xopen3("log/supervise/lock"+4,
460 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
461 if (lock_exnb(svd[0].fdlock) == -1)
462 fatal_cannot("lock supervise/lock");
465 if (mkdir("log/supervise", 0700) == -1) {
466 r = readlink("log/supervise", buf, 256);
469 fatal2x_cannot("readlink ./log/supervise: ", "name too long");
471 fd = xopen(".", O_RDONLY|O_NDELAY);
474 if (fchdir(fd) == -1)
475 fatal_cannot("change back to service directory");
479 if ((errno != ENOENT) && (errno != EINVAL))
480 fatal_cannot("readlink ./log/supervise");
483 svd[1].fdlock = xopen3("log/supervise/lock",
484 O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
485 if (lock_ex(svd[1].fdlock) == -1)
486 fatal_cannot("lock log/supervise/lock");
490 mkfifo("log/supervise/control"+4, 0600);
491 svd[0].fdcontrol = xopen("log/supervise/control"+4, O_RDONLY|O_NDELAY);
492 coe(svd[0].fdcontrol);
493 svd[0].fdcontrolwrite = xopen("log/supervise/control"+4, O_WRONLY|O_NDELAY);
494 coe(svd[0].fdcontrolwrite);
495 update_status(&svd[0]);
497 mkfifo("log/supervise/control", 0600);
498 svd[1].fdcontrol = xopen("log/supervise/control", O_RDONLY|O_NDELAY);
499 coe(svd[1].fdcontrol);
500 svd[1].fdcontrolwrite = xopen("log/supervise/control", O_WRONLY|O_NDELAY);
501 coe(svd[1].fdcontrolwrite);
502 update_status(&svd[1]);
504 mkfifo("log/supervise/ok"+4, 0600);
505 fd = xopen("log/supervise/ok"+4, O_RDONLY|O_NDELAY);
508 mkfifo("log/supervise/ok", 0600);
509 fd = xopen("log/supervise/ok", O_RDONLY|O_NDELAY);
514 struct taia deadline;
519 if (!svd[1].pid && svd[1].want == W_UP)
520 startservice(&svd[1]);
522 if (svd[0].want == W_UP || svd[0].state == S_FINISH)
523 startservice(&svd[0]);
525 x[0].fd = selfpipe[0];
526 x[0].events = IOPAUSE_READ;
527 x[1].fd = svd[0].fdcontrol;
528 x[1].events = IOPAUSE_READ;
530 x[2].fd = svd[1].fdcontrol;
531 x[2].events = IOPAUSE_READ;
534 taia_uint(&deadline, 3600);
535 taia_add(&deadline, &now, &deadline);
537 sig_unblock(SIGTERM);
538 sig_unblock(SIGCHLD);
539 iopause(x, 2+haslog, &deadline, &now);
543 while (read(selfpipe[0], &ch, 1) == 1)
549 child = wait_nohang(&wstat);
551 if ((child == -1) && (errno != EINTR)) break;
552 if (child == svd[0].pid) {
555 svd[0].ctrl &=~C_TERM;
556 if (svd[0].state != S_FINISH)
557 fd = open_read("finish");
560 svd[0].state = S_FINISH;
561 update_status(&svd[0]);
564 svd[0].state = S_DOWN;
565 taia_uint(&deadline, 1);
566 taia_add(&deadline, &svd[0].start, &deadline);
567 taia_now(&svd[0].start);
568 update_status(&svd[0]);
569 if (taia_less(&svd[0].start, &deadline)) sleep(1);
572 if (child == svd[1].pid) {
575 svd[1].state = S_DOWN;
576 svd[1].ctrl &=~C_TERM;
577 taia_uint(&deadline, 1);
578 taia_add(&deadline, &svd[1].start, &deadline);
579 taia_now(&svd[1].start);
580 update_status(&svd[1]);
581 if (taia_less(&svd[1].start, &deadline)) sleep(1);
585 if (read(svd[0].fdcontrol, &ch, 1) == 1)
588 if (read(svd[1].fdcontrol, &ch, 1) == 1)
596 if (svd[0].want == W_EXIT && svd[0].state == S_DOWN) {
599 if (svd[1].want != W_EXIT) {
600 svd[1].want = W_EXIT;
601 /* stopservice(&svd[1]); */
602 update_status(&svd[1]);
605 //if (close(logpipe[1]) == -1)
606 // warn_cannot("close logpipe[1]");
607 //if (close(logpipe[0]) == -1)
608 // warn_cannot("close logpipe[0]");