diff options
author | Ron Yorston <rmy@pobox.com> | 2015-03-14 20:33:00 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2015-03-14 20:33:00 +0000 |
commit | a4f58436b78fe59e57620c6e0301f213ee25f273 (patch) | |
tree | 8355f724926e605280af2d6f2b1ccc6b1bd02dee /runit | |
parent | ba0c36cfcf84efbac6f89e27238e04bb57e9cd45 (diff) | |
parent | 49acc1a7618a28d34381cbb7661d7c981fcb238f (diff) | |
download | busybox-w32-a4f58436b78fe59e57620c6e0301f213ee25f273.tar.gz busybox-w32-a4f58436b78fe59e57620c6e0301f213ee25f273.tar.bz2 busybox-w32-a4f58436b78fe59e57620c6e0301f213ee25f273.zip |
Merge branch 'busybox' into merge
Conflicts:
coreutils/od_bloaty.c
libbb/lineedit.c
Diffstat (limited to 'runit')
-rw-r--r-- | runit/runsvdir.c | 67 |
1 files changed, 36 insertions, 31 deletions
diff --git a/runit/runsvdir.c b/runit/runsvdir.c index af7e75ba7..b4c0b2ef0 100644 --- a/runit/runsvdir.c +++ b/runit/runsvdir.c | |||
@@ -59,7 +59,6 @@ struct globals { | |||
59 | int svnum; | 59 | int svnum; |
60 | #if ENABLE_FEATURE_RUNSVDIR_LOG | 60 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
61 | char *rplog; | 61 | char *rplog; |
62 | int rploglen; | ||
63 | struct fd_pair logpipe; | 62 | struct fd_pair logpipe; |
64 | struct pollfd pfd[1]; | 63 | struct pollfd pfd[1]; |
65 | unsigned stamplog; | 64 | unsigned stamplog; |
@@ -70,7 +69,6 @@ struct globals { | |||
70 | #define svdir (G.svdir ) | 69 | #define svdir (G.svdir ) |
71 | #define svnum (G.svnum ) | 70 | #define svnum (G.svnum ) |
72 | #define rplog (G.rplog ) | 71 | #define rplog (G.rplog ) |
73 | #define rploglen (G.rploglen ) | ||
74 | #define logpipe (G.logpipe ) | 72 | #define logpipe (G.logpipe ) |
75 | #define pfd (G.pfd ) | 73 | #define pfd (G.pfd ) |
76 | #define stamplog (G.stamplog ) | 74 | #define stamplog (G.stamplog ) |
@@ -219,15 +217,12 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv) | |||
219 | struct stat s; | 217 | struct stat s; |
220 | dev_t last_dev = last_dev; /* for gcc */ | 218 | dev_t last_dev = last_dev; /* for gcc */ |
221 | ino_t last_ino = last_ino; /* for gcc */ | 219 | ino_t last_ino = last_ino; /* for gcc */ |
222 | time_t last_mtime = 0; | 220 | time_t last_mtime; |
223 | int wstat; | ||
224 | int curdir; | 221 | int curdir; |
225 | pid_t pid; | ||
226 | unsigned deadline; | ||
227 | unsigned now; | ||
228 | unsigned stampcheck; | 222 | unsigned stampcheck; |
229 | int i; | 223 | int i; |
230 | int need_rescan = 1; | 224 | int need_rescan; |
225 | bool i_am_init; | ||
231 | char *opt_s_argv[3]; | 226 | char *opt_s_argv[3]; |
232 | 227 | ||
233 | INIT_G(); | 228 | INIT_G(); |
@@ -238,18 +233,21 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv) | |||
238 | getopt32(argv, "Ps:", &opt_s_argv[0]); | 233 | getopt32(argv, "Ps:", &opt_s_argv[0]); |
239 | argv += optind; | 234 | argv += optind; |
240 | 235 | ||
236 | i_am_init = (getpid() == 1); | ||
241 | bb_signals(0 | 237 | bb_signals(0 |
242 | | (1 << SIGTERM) | 238 | | (1 << SIGTERM) |
243 | | (1 << SIGHUP) | 239 | | (1 << SIGHUP) |
244 | /* For busybox's init, SIGTERM == reboot, | 240 | /* For busybox's init, SIGTERM == reboot, |
245 | * SIGUSR1 == halt | 241 | * SIGUSR1 == halt, |
246 | * SIGUSR2 == poweroff | 242 | * SIGUSR2 == poweroff, |
247 | * so we need to intercept SIGUSRn too. | 243 | * Ctlr-ALt-Del sends SIGINT to init, |
244 | * so we need to intercept SIGUSRn and SIGINT too. | ||
248 | * Note that we do not implement actual reboot | 245 | * Note that we do not implement actual reboot |
249 | * (killall(TERM) + umount, etc), we just pause | 246 | * (killall(TERM) + umount, etc), we just pause |
250 | * respawing and avoid exiting (-> making kernel oops). | 247 | * respawing and avoid exiting (-> making kernel oops). |
251 | * The user is responsible for the rest. */ | 248 | * The user is responsible for the rest. |
252 | | (getpid() == 1 ? ((1 << SIGUSR1) | (1 << SIGUSR2)) : 0) | 249 | */ |
250 | | (i_am_init ? ((1 << SIGUSR1) | (1 << SIGUSR2) | (1 << SIGINT)) : 0) | ||
253 | , record_signo); | 251 | , record_signo); |
254 | svdir = *argv++; | 252 | svdir = *argv++; |
255 | 253 | ||
@@ -257,8 +255,7 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv) | |||
257 | /* setup log */ | 255 | /* setup log */ |
258 | if (*argv) { | 256 | if (*argv) { |
259 | rplog = *argv; | 257 | rplog = *argv; |
260 | rploglen = strlen(rplog); | 258 | if (strlen(rplog) < 7) { |
261 | if (rploglen < 7) { | ||
262 | warnx("log must have at least seven characters"); | 259 | warnx("log must have at least seven characters"); |
263 | } else if (piped_pair(logpipe)) { | 260 | } else if (piped_pair(logpipe)) { |
264 | warnx("can't create pipe for log"); | 261 | warnx("can't create pipe for log"); |
@@ -287,11 +284,16 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv) | |||
287 | close_on_exec_on(curdir); | 284 | close_on_exec_on(curdir); |
288 | 285 | ||
289 | stampcheck = monotonic_sec(); | 286 | stampcheck = monotonic_sec(); |
287 | need_rescan = 1; | ||
288 | last_mtime = 0; | ||
290 | 289 | ||
291 | for (;;) { | 290 | for (;;) { |
291 | unsigned now; | ||
292 | unsigned sig; | ||
293 | |||
292 | /* collect children */ | 294 | /* collect children */ |
293 | for (;;) { | 295 | for (;;) { |
294 | pid = wait_any_nohang(&wstat); | 296 | pid_t pid = wait_any_nohang(NULL); |
295 | if (pid <= 0) | 297 | if (pid <= 0) |
296 | break; | 298 | break; |
297 | for (i = 0; i < svnum; i++) { | 299 | for (i = 0; i < svnum; i++) { |
@@ -345,15 +347,15 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv) | |||
345 | } | 347 | } |
346 | pfd[0].revents = 0; | 348 | pfd[0].revents = 0; |
347 | #endif | 349 | #endif |
348 | deadline = (need_rescan ? 1 : 5); | 350 | { |
349 | sig_block(SIGCHLD); | 351 | unsigned deadline = (need_rescan ? 1 : 5); |
350 | #if ENABLE_FEATURE_RUNSVDIR_LOG | 352 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
351 | if (rplog) | 353 | if (rplog) |
352 | poll(pfd, 1, deadline*1000); | 354 | poll(pfd, 1, deadline*1000); |
353 | else | 355 | else |
354 | #endif | 356 | #endif |
355 | sleep(deadline); | 357 | sleep(deadline); |
356 | sig_unblock(SIGCHLD); | 358 | } |
357 | 359 | ||
358 | #if ENABLE_FEATURE_RUNSVDIR_LOG | 360 | #if ENABLE_FEATURE_RUNSVDIR_LOG |
359 | if (pfd[0].revents & POLLIN) { | 361 | if (pfd[0].revents & POLLIN) { |
@@ -361,21 +363,25 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv) | |||
361 | while (read(logpipe.rd, &ch, 1) > 0) { | 363 | while (read(logpipe.rd, &ch, 1) > 0) { |
362 | if (ch < ' ') | 364 | if (ch < ' ') |
363 | ch = ' '; | 365 | ch = ' '; |
364 | for (i = 6; i < rploglen; i++) | 366 | for (i = 6; rplog[i] != '\0'; i++) |
365 | rplog[i-1] = rplog[i]; | 367 | rplog[i-1] = rplog[i]; |
366 | rplog[rploglen-1] = ch; | 368 | rplog[i-1] = ch; |
367 | } | 369 | } |
368 | } | 370 | } |
369 | #endif | 371 | #endif |
370 | if (!bb_got_signal) | 372 | sig = bb_got_signal; |
373 | if (!sig) | ||
371 | continue; | 374 | continue; |
375 | bb_got_signal = 0; | ||
372 | 376 | ||
373 | /* -s SCRIPT: useful if we are init. | 377 | /* -s SCRIPT: useful if we are init. |
374 | * In this case typically script never returns, | 378 | * In this case typically script never returns, |
375 | * it halts/powers off/reboots the system. */ | 379 | * it halts/powers off/reboots the system. */ |
376 | if (opt_s_argv[0]) { | 380 | if (opt_s_argv[0]) { |
381 | pid_t pid; | ||
382 | |||
377 | /* Single parameter: signal# */ | 383 | /* Single parameter: signal# */ |
378 | opt_s_argv[1] = utoa(bb_got_signal); | 384 | opt_s_argv[1] = utoa(sig); |
379 | pid = spawn(opt_s_argv); | 385 | pid = spawn(opt_s_argv); |
380 | if (pid > 0) { | 386 | if (pid > 0) { |
381 | /* Remembering to wait for _any_ children, | 387 | /* Remembering to wait for _any_ children, |
@@ -385,17 +391,16 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv) | |||
385 | } | 391 | } |
386 | } | 392 | } |
387 | 393 | ||
388 | if (bb_got_signal == SIGHUP) { | 394 | if (sig == SIGHUP) { |
389 | for (i = 0; i < svnum; i++) | 395 | for (i = 0; i < svnum; i++) |
390 | if (sv[i].pid) | 396 | if (sv[i].pid) |
391 | kill(sv[i].pid, SIGTERM); | 397 | kill(sv[i].pid, SIGTERM); |
392 | } | 398 | } |
393 | /* SIGHUP or SIGTERM (or SIGUSRn if we are init) */ | 399 | /* SIGHUP or SIGTERM (or SIGUSRn if we are init) */ |
394 | /* Exit unless we are init */ | 400 | /* Exit unless we are init */ |
395 | if (getpid() != 1) | 401 | if (!i_am_init) |
396 | return (SIGHUP == bb_got_signal) ? 111 : EXIT_SUCCESS; | 402 | return (SIGHUP == sig) ? 111 : EXIT_SUCCESS; |
397 | 403 | ||
398 | /* init continues to monitor services forever */ | 404 | /* init continues to monitor services forever */ |
399 | bb_got_signal = 0; | ||
400 | } /* for (;;) */ | 405 | } /* for (;;) */ |
401 | } | 406 | } |