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