2 Copyright (c) 2001-2006, Gerrit Pape
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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.
28 /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */
30 //config:config RUNSVDIR
31 //config: bool "runsvdir (6.6 kb)"
34 //config: runsvdir starts a runsv process for each subdirectory, or symlink to
35 //config: a directory, in the services directory dir, up to a limit of 1000
36 //config: subdirectories, and restarts a runsv process if it terminates.
38 //config:config FEATURE_RUNSVDIR_LOG
39 //config: bool "Enable scrolling argument log"
40 //config: depends on RUNSVDIR
43 //config: Enable feature where second parameter of runsvdir holds last error
44 //config: message (viewable via top/ps). Otherwise (feature is off
45 //config: or no parameter), error messages go to stderr only.
47 //applet:IF_RUNSVDIR(APPLET(runsvdir, BB_DIR_USR_BIN, BB_SUID_DROP))
49 //kbuild:lib-$(CONFIG_RUNSVDIR) += runsvdir.o
51 //usage:#define runsvdir_trivial_usage
52 //usage: "[-P] [-s SCRIPT] DIR"
53 //usage:#define runsvdir_full_usage "\n\n"
54 //usage: "Start a runsv process for each subdirectory. If it exits, restart it.\n"
55 //usage: "\n -P Put each runsv in a new session"
56 //usage: "\n -s SCRIPT Run SCRIPT <signo> after signal is processed"
60 #include "common_bufsiz.h"
61 #include "runit_lib.h"
63 #define MAXSERVICES 1000
65 /* Should be not needed - all dirs are on same FS, right? */
66 #define CHECK_DEVNO_TOO 0
81 #if ENABLE_FEATURE_RUNSVDIR_LOG
83 struct fd_pair logpipe
;
88 #define G (*(struct globals*)bb_common_bufsiz1)
90 #define svdir (G.svdir )
91 #define svnum (G.svnum )
92 #define rplog (G.rplog )
93 #define logpipe (G.logpipe )
95 #define stamplog (G.stamplog )
96 #define INIT_G() do { setup_common_bufsiz(); } while (0)
98 static void fatal2_cannot(const char *m1
, const char *m2
)
100 bb_perror_msg_and_die("%s: fatal: can't %s%s", svdir
, m1
, m2
);
101 /* was exiting 100 */
103 static void warn3x(const char *m1
, const char *m2
, const char *m3
)
105 bb_error_msg("%s: warning: %s%s%s", svdir
, m1
, m2
, m3
);
107 static void warn2_cannot(const char *m1
, const char *m2
)
109 warn3x("can't ", m1
, m2
);
111 #if ENABLE_FEATURE_RUNSVDIR_LOG
112 static void warnx(const char *m1
)
118 /* inlining + vfork -> bigger code */
119 static NOINLINE pid_t
runsv(const char *name
)
123 /* If we got signaled, stop spawning children at once! */
129 warn2_cannot("vfork", "");
134 if (option_mask32
& 1) /* -P option? */
137 * "Signals set to be caught by the calling process image
138 * shall be set to the default action in the new process image."
139 * Therefore, we do not need this: */
146 execlp("runsv", "runsv", name
, (char *) NULL
);
147 fatal2_cannot("start runsv ", name
);
152 /* gcc 4.3.0 does better with NOINLINE */
153 static NOINLINE
int do_rescan(void)
163 warn2_cannot("open directory ", svdir
);
164 return 1; /* need to rescan again soon */
166 for (i
= 0; i
< svnum
; i
++)
174 if (d
->d_name
[0] == '.')
176 if (stat(d
->d_name
, &s
) == -1) {
177 warn2_cannot("stat ", d
->d_name
);
180 if (!S_ISDIR(s
.st_mode
))
182 /* Do we have this service listed already? */
183 for (i
= 0; i
< svnum
; i
++) {
184 if (sv
[i
].ino
== s
.st_ino
186 && sv
[i
].dev
== s
.st_dev
189 if (sv
[i
].pid
== 0) /* restart if it has died */
191 sv
[i
].isgone
= 0; /* "we still see you" */
195 { /* Not found, make new service */
196 struct service
*svnew
= realloc(sv
, (i
+1) * sizeof(*sv
));
198 warn2_cannot("start runsv ", d
->d_name
);
205 sv
[i
].dev
= s
.st_dev
;
207 sv
[i
].ino
= s
.st_ino
;
209 sv
[i
].pid
= runsv(d
->d_name
);
216 if (i
) { /* readdir failed */
217 warn2_cannot("read directory ", svdir
);
218 return 1; /* need to rescan again soon */
221 /* Send SIGTERM to runsv whose directories
222 * were no longer found (-> must have been removed) */
223 for (i
= 0; i
< svnum
; i
++) {
227 kill(sv
[i
].pid
, SIGTERM
);
230 i
--; /* so that we don't skip new sv[i] (bug was here!) */
235 int runsvdir_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
236 int runsvdir_main(int argc UNUSED_PARAM
, char **argv
)
239 dev_t last_dev
= last_dev
; /* for gcc */
240 ino_t last_ino
= last_ino
; /* for gcc */
251 opt_s_argv
[0] = NULL
;
252 opt_s_argv
[2] = NULL
;
253 getopt32(argv
, "^" "Ps:" "\0" "-1", &opt_s_argv
[0]);
256 i_am_init
= (getpid() == 1);
260 /* For busybox's init, SIGTERM == reboot,
262 * SIGUSR2 == poweroff,
263 * Ctlr-ALt-Del sends SIGINT to init,
264 * so we need to intercept SIGUSRn and SIGINT too.
265 * Note that we do not implement actual reboot
266 * (killall(TERM) + umount, etc), we just pause
267 * respawing and avoid exiting (-> making kernel oops).
268 * The user is responsible for the rest.
270 | (i_am_init
? ((1 << SIGUSR1
) | (1 << SIGUSR2
) | (1 << SIGINT
)) : 0)
274 #if ENABLE_FEATURE_RUNSVDIR_LOG
278 if (strlen(rplog
) < 7) {
279 warnx("log must have at least seven characters");
280 } else if (piped_pair(logpipe
)) {
281 warnx("can't create pipe for log");
283 close_on_exec_on(logpipe
.rd
);
284 close_on_exec_on(logpipe
.wr
);
285 ndelay_on(logpipe
.rd
);
286 ndelay_on(logpipe
.wr
);
287 if (dup2(logpipe
.wr
, 2) == -1) {
288 warnx("can't set filedescriptor for log");
290 pfd
[0].fd
= logpipe
.rd
;
291 pfd
[0].events
= POLLIN
;
292 stamplog
= monotonic_sec();
297 warnx("log service disabled");
301 curdir
= open(".", O_RDONLY
|O_NDELAY
);
303 fatal2_cannot("open current directory", "");
304 close_on_exec_on(curdir
);
306 stampcheck
= monotonic_sec();
314 /* collect children */
316 pid_t pid
= wait_any_nohang(NULL
);
319 for (i
= 0; i
< svnum
; i
++) {
320 if (pid
== sv
[i
].pid
) {
328 now
= monotonic_sec();
329 if ((int)(now
- stampcheck
) >= 0) {
330 /* wait at least a second */
331 stampcheck
= now
+ 1;
333 if (stat(svdir
, &s
) != -1) {
334 if (need_rescan
|| s
.st_mtime
!= last_mtime
335 || s
.st_ino
!= last_ino
|| s
.st_dev
!= last_dev
338 if (chdir(svdir
) != -1) {
339 last_mtime
= s
.st_mtime
;
342 /* if the svdir changed this very second, wait until the
343 * next second, because we won't be able to detect more
344 * changes within this second */
345 while (time(NULL
) == last_mtime
)
347 need_rescan
= do_rescan();
348 while (fchdir(curdir
) == -1) {
349 warn2_cannot("change directory, pausing", "");
353 warn2_cannot("change directory to ", svdir
);
357 warn2_cannot("stat ", svdir
);
361 #if ENABLE_FEATURE_RUNSVDIR_LOG
363 if ((int)(now
- stamplog
) >= 0) {
364 write(logpipe
.wr
, ".", 1);
365 stamplog
= now
+ 900;
371 unsigned deadline
= (need_rescan
? 1 : 5);
372 #if ENABLE_FEATURE_RUNSVDIR_LOG
374 poll(pfd
, 1, deadline
*1000);
380 #if ENABLE_FEATURE_RUNSVDIR_LOG
381 if (pfd
[0].revents
& POLLIN
) {
383 while (read(logpipe
.rd
, &ch
, 1) > 0) {
386 for (i
= 6; rplog
[i
] != '\0'; i
++)
387 rplog
[i
-1] = rplog
[i
];
397 /* -s SCRIPT: useful if we are init.
398 * In this case typically script never returns,
399 * it halts/powers off/reboots the system. */
403 /* Single parameter: signal# */
404 opt_s_argv
[1] = utoa(sig
);
405 pid
= spawn(opt_s_argv
);
407 /* Remembering to wait for _any_ children,
409 while (wait(NULL
) != pid
)
415 for (i
= 0; i
< svnum
; i
++)
417 kill(sv
[i
].pid
, SIGTERM
);
419 /* SIGHUP or SIGTERM (or SIGUSRn if we are init) */
420 /* Exit unless we are init */
422 return (SIGHUP
== sig
) ? 111 : EXIT_SUCCESS
;
424 /* init continues to monitor services forever */