runsvdir: fx a recent vda's buglet (was pausing even if not signaled).
[oweals/busybox.git] / runit / runsvdir.c
1 /*
2 Copyright (c) 2001-2006, Gerrit Pape
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8    1. Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10    2. Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13    3. The name of the author may not be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
29 /* TODO: depends on runit_lib.c - review and reduce/eliminate */
30
31 #include <sys/poll.h>
32 #include <sys/file.h>
33 #include "libbb.h"
34 #include "runit_lib.h"
35
36 #define MAXSERVICES 1000
37
38 /* Should be not needed - all dirs are on same FS, right? */
39 #define CHECK_DEVNO_TOO 0
40
41 struct service {
42 #if CHECK_DEVNO_TOO
43         dev_t dev;
44 #endif
45         ino_t ino;
46         pid_t pid;
47         smallint isgone;
48 };
49
50 struct globals {
51         struct service *sv;
52         char *svdir;
53         int svnum;
54 #if ENABLE_FEATURE_RUNSVDIR_LOG
55         char *rplog;
56         int rploglen;
57         struct fd_pair logpipe;
58         struct pollfd pfd[1];
59         unsigned stamplog;
60 #endif
61         smallint set_pgrp;
62 };
63 #define G (*(struct globals*)&bb_common_bufsiz1)
64 #define sv          (G.sv          )
65 #define svdir       (G.svdir       )
66 #define svnum       (G.svnum       )
67 #define rplog       (G.rplog       )
68 #define rploglen    (G.rploglen    )
69 #define logpipe     (G.logpipe     )
70 #define pfd         (G.pfd         )
71 #define stamplog    (G.stamplog    )
72 #define set_pgrp    (G.set_pgrp    )
73 #define INIT_G() do { \
74 } while (0)
75
76 static void fatal2_cannot(const char *m1, const char *m2)
77 {
78         bb_perror_msg_and_die("%s: fatal: cannot %s%s", svdir, m1, m2);
79         /* was exiting 100 */
80 }
81 static void warn3x(const char *m1, const char *m2, const char *m3)
82 {
83         bb_error_msg("%s: warning: %s%s%s", svdir, m1, m2, m3);
84 }
85 static void warn2_cannot(const char *m1, const char *m2)
86 {
87         warn3x("cannot ", m1, m2);
88 }
89 #if ENABLE_FEATURE_RUNSVDIR_LOG
90 static void warnx(const char *m1)
91 {
92         warn3x(m1, "", "");
93 }
94 #endif
95
96 /* inlining + vfork -> bigger code */
97 static NOINLINE pid_t runsv(const char *name)
98 {
99         pid_t pid;
100
101         /* If we got signaled, stop spawning children at once! */
102         if (bb_got_signal)
103                 return 0;
104
105         pid = vfork();
106         if (pid == -1) {
107                 warn2_cannot("vfork", "");
108                 return 0;
109         }
110         if (pid == 0) {
111                 /* child */
112                 if (set_pgrp)
113                         setsid();
114 /* man execv:
115  * "Signals set to be caught by the calling process image
116  *  shall be set to the default action in the new process image."
117  * Therefore, we do not need this: */
118 #if 0
119                 bb_signals(0
120                         | (1 << SIGHUP)
121                         | (1 << SIGTERM)
122                         , SIG_DFL);
123 #endif
124                 execlp("runsv", "runsv", name, NULL);
125                 fatal2_cannot("start runsv ", name);
126         }
127         return pid;
128 }
129
130 /* gcc 4.3.0 does better with NOINLINE */
131 static NOINLINE int do_rescan(void)
132 {
133         DIR *dir;
134         direntry *d;
135         int i;
136         struct stat s;
137         int need_rescan = 0;
138
139         dir = opendir(".");
140         if (!dir) {
141                 warn2_cannot("open directory ", svdir);
142                 return 1; /* need to rescan again soon */
143         }
144         for (i = 0; i < svnum; i++)
145                 sv[i].isgone = 1;
146
147         while (1) {
148                 errno = 0;
149                 d = readdir(dir);
150                 if (!d)
151                         break;
152                 if (d->d_name[0] == '.')
153                         continue;
154                 if (stat(d->d_name, &s) == -1) {
155                         warn2_cannot("stat ", d->d_name);
156                         continue;
157                 }
158                 if (!S_ISDIR(s.st_mode))
159                         continue;
160                 /* Do we have this service listed already? */
161                 for (i = 0; i < svnum; i++) {
162                         if ((sv[i].ino == s.st_ino)
163 #if CHECK_DEVNO_TOO
164                          && (sv[i].dev == s.st_dev)
165 #endif
166                         ) {
167                                 if (sv[i].pid == 0) /* restart if it has died */
168                                         goto run_ith_sv;
169                                 sv[i].isgone = 0; /* "we still see you" */
170                                 goto next_dentry;
171                         }
172                 }
173                 { /* Not found, make new service */
174                         struct service *svnew = realloc(sv, (i+1) * sizeof(*sv));
175                         if (!svnew) {
176                                 warn2_cannot("start runsv ", d->d_name);
177                                 need_rescan = 1;
178                                 continue;
179                         }
180                         sv = svnew;
181                         svnum++;
182 #if CHECK_DEVNO_TOO
183                         sv[i].dev = s.st_dev;
184 #endif
185                         sv[i].ino = s.st_ino;
186  run_ith_sv:
187                         sv[i].pid = runsv(d->d_name);
188                         sv[i].isgone = 0;
189                 }
190  next_dentry: ;
191         }
192         i = errno;
193         closedir(dir);
194         if (i) { /* readdir failed */
195                 warn2_cannot("read directory ", svdir);
196                 return 1; /* need to rescan again soon */
197         }
198
199         /* Send SIGTERM to runsv whose directories
200          * were no longer found (-> must have been removed) */
201         for (i = 0; i < svnum; i++) {
202                 if (!sv[i].isgone)
203                         continue;
204                 if (sv[i].pid)
205                         kill(sv[i].pid, SIGTERM);
206                 svnum--;
207                 sv[i] = sv[svnum];
208                 i--; /* so that we don't skip new sv[i] (bug was here!) */
209         }
210         return need_rescan;
211 }
212
213 int runsvdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
214 int runsvdir_main(int argc UNUSED_PARAM, char **argv)
215 {
216         struct stat s;
217         dev_t last_dev = last_dev; /* for gcc */
218         ino_t last_ino = last_ino; /* for gcc */
219         time_t last_mtime = 0;
220         int wstat;
221         int curdir;
222         int pid;
223         unsigned deadline;
224         unsigned now;
225         unsigned stampcheck;
226         int i;
227         int need_rescan = 1;
228
229         INIT_G();
230
231         opt_complementary = "-1";
232         set_pgrp = getopt32(argv, "P");
233         argv += optind;
234
235         bb_signals(0
236                 | (1 << SIGTERM)
237                 | (1 << SIGHUP)
238                 /* For busybox's init, SIGTERM == reboot,
239                  * SIGUSR1 == halt
240                  * SIGUSR2 == poweroff
241                  * so we need to intercept SIGUSRn too.
242                  * Note that we do not implement actual reboot
243                  * (killall(TERM) + umount, etc), we just pause
244                  * respawing and avoid exiting (-> making kernel oops).
245                  * The user is responsible for the rest. */
246                 | (getpid() == 1 ? ((1 << SIGUSR1) | (1 << SIGUSR2)) : 0)
247                 , record_signo);
248         svdir = *argv++;
249
250 #if ENABLE_FEATURE_RUNSVDIR_LOG
251         /* setup log */
252         if (*argv) {
253                 rplog = *argv;
254                 rploglen = strlen(rplog);
255                 if (rploglen < 7) {
256                         warnx("log must have at least seven characters");
257                 } else if (piped_pair(logpipe)) {
258                         warnx("cannot create pipe for log");
259                 } else {
260                         close_on_exec_on(logpipe.rd);
261                         close_on_exec_on(logpipe.wr);
262                         ndelay_on(logpipe.rd);
263                         ndelay_on(logpipe.wr);
264                         if (dup2(logpipe.wr, 2) == -1) {
265                                 warnx("cannot set filedescriptor for log");
266                         } else {
267                                 pfd[0].fd = logpipe.rd;
268                                 pfd[0].events = POLLIN;
269                                 stamplog = monotonic_sec();
270                                 goto run;
271                         }
272                 }
273                 rplog = NULL;
274                 warnx("log service disabled");
275         }
276  run:
277 #endif
278         curdir = open_read(".");
279         if (curdir == -1)
280                 fatal2_cannot("open current directory", "");
281         close_on_exec_on(curdir);
282
283         stampcheck = monotonic_sec();
284
285         for (;;) {
286                 /* collect children */
287                 for (;;) {
288                         pid = wait_any_nohang(&wstat);
289                         if (pid <= 0)
290                                 break;
291                         for (i = 0; i < svnum; i++) {
292                                 if (pid == sv[i].pid) {
293                                         /* runsv has died */
294                                         sv[i].pid = 0;
295                                         need_rescan = 1;
296                                 }
297                         }
298                 }
299
300                 now = monotonic_sec();
301                 if ((int)(now - stampcheck) >= 0) {
302                         /* wait at least a second */
303                         stampcheck = now + 1;
304
305                         if (stat(svdir, &s) != -1) {
306                                 if (need_rescan || s.st_mtime != last_mtime
307                                  || s.st_ino != last_ino || s.st_dev != last_dev
308                                 ) {
309                                         /* svdir modified */
310                                         if (chdir(svdir) != -1) {
311                                                 last_mtime = s.st_mtime;
312                                                 last_dev = s.st_dev;
313                                                 last_ino = s.st_ino;
314                                                 //if (now <= mtime)
315                                                 //      sleep(1);
316                                                 need_rescan = do_rescan();
317                                                 while (fchdir(curdir) == -1) {
318                                                         warn2_cannot("change directory, pausing", "");
319                                                         sleep(5);
320                                                 }
321                                         } else {
322                                                 warn2_cannot("change directory to ", svdir);
323                                         }
324                                 }
325                         } else {
326                                 warn2_cannot("stat ", svdir);
327                         }
328                 }
329
330 #if ENABLE_FEATURE_RUNSVDIR_LOG
331                 if (rplog) {
332                         if ((int)(now - stamplog) >= 0) {
333                                 write(logpipe.wr, ".", 1);
334                                 stamplog = now + 900;
335                         }
336                 }
337                 pfd[0].revents = 0;
338 #endif
339                 deadline = (need_rescan ? 1 : 5);
340  do_sleep:
341                 sig_block(SIGCHLD);
342 #if ENABLE_FEATURE_RUNSVDIR_LOG
343                 if (rplog)
344                         poll(pfd, 1, deadline*1000);
345                 else
346 #endif
347                         sleep(deadline);
348                 sig_unblock(SIGCHLD);
349
350 #if ENABLE_FEATURE_RUNSVDIR_LOG
351                 if (pfd[0].revents & POLLIN) {
352                         char ch;
353                         while (read(logpipe.rd, &ch, 1) > 0) {
354                                 if (ch < ' ')
355                                         ch = ' ';
356                                 for (i = 6; i < rploglen; i++)
357                                         rplog[i-1] = rplog[i];
358                                 rplog[rploglen-1] = ch;
359                         }
360                 }
361 #endif
362                 switch (bb_got_signal) {
363                 case 0: /* we are not signaled, business as usual */
364                         break;
365                 case SIGHUP:
366                         for (i = 0; i < svnum; i++)
367                                 if (sv[i].pid)
368                                         kill(sv[i].pid, SIGTERM);
369                         /* fall through */
370                 case SIGTERM:
371                         /* exit, unless we are init */
372                         if (getpid() != 1)
373                                 goto ret;
374                 default:
375                         /* so we are init. do not exit,
376                          * and pause respawning - we may be rebooting... */
377                         bb_got_signal = 0;
378                         deadline = 60;
379                         goto do_sleep;
380                 }
381         }
382  ret:
383         return (SIGHUP == bb_got_signal) ? 111 : EXIT_SUCCESS;
384 }