aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-11-06 22:39:57 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-11-06 22:39:57 +0000
commit3854c5ddf4eb267e4e53796eaa1051421dcb4e08 (patch)
tree7110eecec665bc64fefcdedaa29eb2e2fdf2df41
parent70e8f49f71b824bbc7c5ec825eb33108134d1ee5 (diff)
downloadbusybox-w32-3854c5ddf4eb267e4e53796eaa1051421dcb4e08.tar.gz
busybox-w32-3854c5ddf4eb267e4e53796eaa1051421dcb4e08.tar.bz2
busybox-w32-3854c5ddf4eb267e4e53796eaa1051421dcb4e08.zip
runsvdir: alternative methon of supporting runsvdir-as-init. +66 bytes.
*: s/int/pid_t where appropriate
-rw-r--r--include/libbb.h8
-rw-r--r--libbb/vfork_daemon_rexec.c8
-rw-r--r--networking/tcpudp.c2
-rw-r--r--runit/runsv.c4
-rw-r--r--runit/runsvdir.c54
-rw-r--r--runit/svlogd.c3
6 files changed, 46 insertions, 33 deletions
diff --git a/include/libbb.h b/include/libbb.h
index a6229a5e1..746db1c0e 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -743,7 +743,7 @@ int bb_execvp(const char *file, char *const argv[]) FAST_FUNC;
743pid_t spawn(char **argv) FAST_FUNC; 743pid_t spawn(char **argv) FAST_FUNC;
744pid_t xspawn(char **argv) FAST_FUNC; 744pid_t xspawn(char **argv) FAST_FUNC;
745 745
746int safe_waitpid(int pid, int *wstat, int options) FAST_FUNC; 746pid_t safe_waitpid(pid_t pid, int *wstat, int options) FAST_FUNC;
747/* Unlike waitpid, waits ONLY for one process. 747/* Unlike waitpid, waits ONLY for one process.
748 * It's safe to pass negative 'pids' from failed [v]fork - 748 * It's safe to pass negative 'pids' from failed [v]fork -
749 * wait4pid will return -1 (and will not clobber [v]fork's errno). 749 * wait4pid will return -1 (and will not clobber [v]fork's errno).
@@ -751,14 +751,14 @@ int safe_waitpid(int pid, int *wstat, int options) FAST_FUNC;
751 * if (rc < 0) bb_perror_msg("%s", argv[0]); 751 * if (rc < 0) bb_perror_msg("%s", argv[0]);
752 * if (rc > 0) bb_error_msg("exit code: %d", rc); 752 * if (rc > 0) bb_error_msg("exit code: %d", rc);
753 */ 753 */
754int wait4pid(int pid) FAST_FUNC; 754int wait4pid(pid_t pid) FAST_FUNC;
755int wait_any_nohang(int *wstat) FAST_FUNC; 755pid_t wait_any_nohang(int *wstat) FAST_FUNC;
756#define wait_crashed(w) ((w) & 127) 756#define wait_crashed(w) ((w) & 127)
757#define wait_exitcode(w) ((w) >> 8) 757#define wait_exitcode(w) ((w) >> 8)
758#define wait_stopsig(w) ((w) >> 8) 758#define wait_stopsig(w) ((w) >> 8)
759#define wait_stopped(w) (((w) & 127) == 127) 759#define wait_stopped(w) (((w) & 127) == 127)
760/* wait4pid(spawn(argv)) + NOFORK/NOEXEC (if configured) */ 760/* wait4pid(spawn(argv)) + NOFORK/NOEXEC (if configured) */
761int spawn_and_wait(char **argv) FAST_FUNC; 761pid_t spawn_and_wait(char **argv) FAST_FUNC;
762struct nofork_save_area { 762struct nofork_save_area {
763 jmp_buf die_jmp; 763 jmp_buf die_jmp;
764 const char *applet_name; 764 const char *applet_name;
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index 17b373cd1..50dc3affe 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -66,9 +66,9 @@ pid_t FAST_FUNC xspawn(char **argv)
66 return pid; 66 return pid;
67} 67}
68 68
69int FAST_FUNC safe_waitpid(int pid, int *wstat, int options) 69pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options)
70{ 70{
71 int r; 71 pid_t r;
72 72
73 do 73 do
74 r = waitpid(pid, wstat, options); 74 r = waitpid(pid, wstat, options);
@@ -76,13 +76,13 @@ int FAST_FUNC safe_waitpid(int pid, int *wstat, int options)
76 return r; 76 return r;
77} 77}
78 78
79int FAST_FUNC wait_any_nohang(int *wstat) 79pid_t FAST_FUNC wait_any_nohang(int *wstat)
80{ 80{
81 return safe_waitpid(-1, wstat, WNOHANG); 81 return safe_waitpid(-1, wstat, WNOHANG);
82} 82}
83 83
84// Wait for the specified child PID to exit, returning child's error return. 84// Wait for the specified child PID to exit, returning child's error return.
85int FAST_FUNC wait4pid(int pid) 85int FAST_FUNC wait4pid(pid_t pid)
86{ 86{
87 int status; 87 int status;
88 88
diff --git a/networking/tcpudp.c b/networking/tcpudp.c
index d6731198c..0d8217e02 100644
--- a/networking/tcpudp.c
+++ b/networking/tcpudp.c
@@ -147,7 +147,7 @@ static void connection_status(void)
147static void sig_child_handler(int sig UNUSED_PARAM) 147static void sig_child_handler(int sig UNUSED_PARAM)
148{ 148{
149 int wstat; 149 int wstat;
150 int pid; 150 pid_t pid;
151 151
152 while ((pid = wait_any_nohang(&wstat)) > 0) { 152 while ((pid = wait_any_nohang(&wstat)) > 0) {
153 if (max_per_host) 153 if (max_per_host)
diff --git a/runit/runsv.c b/runit/runsv.c
index bd0f3dcc2..56244d03f 100644
--- a/runit/runsv.c
+++ b/runit/runsv.c
@@ -247,7 +247,7 @@ static void update_status(struct svdir *s)
247 247
248static unsigned custom(struct svdir *s, char c) 248static unsigned custom(struct svdir *s, char c)
249{ 249{
250 int pid; 250 pid_t pid;
251 int w; 251 int w;
252 char a[10]; 252 char a[10];
253 struct stat st; 253 struct stat st;
@@ -584,7 +584,7 @@ int runsv_main(int argc UNUSED_PARAM, char **argv)
584 continue; 584 continue;
585 585
586 for (;;) { 586 for (;;) {
587 int child; 587 pid_t child;
588 int wstat; 588 int wstat;
589 589
590 child = wait_any_nohang(&wstat); 590 child = wait_any_nohang(&wstat);
diff --git a/runit/runsvdir.c b/runit/runsvdir.c
index 581787f03..9d560e097 100644
--- a/runit/runsvdir.c
+++ b/runit/runsvdir.c
@@ -107,7 +107,7 @@ static NOINLINE pid_t runsv(const char *name)
107 } 107 }
108 if (pid == 0) { 108 if (pid == 0) {
109 /* child */ 109 /* child */
110 if (option_mask32) /* -P option? */ 110 if (option_mask32 & 1) /* -P option? */
111 setsid(); 111 setsid();
112/* man execv: 112/* man execv:
113 * "Signals set to be caught by the calling process image 113 * "Signals set to be caught by the calling process image
@@ -217,17 +217,20 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv)
217 time_t last_mtime = 0; 217 time_t last_mtime = 0;
218 int wstat; 218 int wstat;
219 int curdir; 219 int curdir;
220 int pid; 220 pid_t pid;
221 unsigned deadline; 221 unsigned deadline;
222 unsigned now; 222 unsigned now;
223 unsigned stampcheck; 223 unsigned stampcheck;
224 int i; 224 int i;
225 int need_rescan = 1; 225 int need_rescan = 1;
226 char *opt_s_argv[3];
226 227
227 INIT_G(); 228 INIT_G();
228 229
229 opt_complementary = "-1"; 230 opt_complementary = "-1";
230 getopt32(argv, "P"); 231 opt_s_argv[0] = NULL;
232 opt_s_argv[2] = NULL;
233 getopt32(argv, "Ps:", &opt_s_argv[0]);
231 argv += optind; 234 argv += optind;
232 235
233 bb_signals(0 236 bb_signals(0
@@ -335,7 +338,6 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv)
335 pfd[0].revents = 0; 338 pfd[0].revents = 0;
336#endif 339#endif
337 deadline = (need_rescan ? 1 : 5); 340 deadline = (need_rescan ? 1 : 5);
338 do_sleep:
339 sig_block(SIGCHLD); 341 sig_block(SIGCHLD);
340#if ENABLE_FEATURE_RUNSVDIR_LOG 342#if ENABLE_FEATURE_RUNSVDIR_LOG
341 if (rplog) 343 if (rplog)
@@ -357,27 +359,37 @@ int runsvdir_main(int argc UNUSED_PARAM, char **argv)
357 } 359 }
358 } 360 }
359#endif 361#endif
362 if (!bb_got_signal)
363 continue;
364
365 /* -s SCRIPT: useful if we are init.
366 * In this case typically script never returns,
367 * it halts/powers off/reboots the system. */
368 if (opt_s_argv[0]) {
369 /* Single parameter: signal# */
370 opt_s_argv[1] = utoa(bb_got_signal);
371 pid = spawn(opt_s_argv);
372 if (pid > 0) {
373 /* Remebering to wait for _any_ children,
374 * not just pid */
375 while (wait(NULL) != pid)
376 continue;
377 }
378 }
379
360 switch (bb_got_signal) { 380 switch (bb_got_signal) {
361 case 0: /* we are not signaled, business as usual */
362 break;
363 case SIGHUP: 381 case SIGHUP:
364 for (i = 0; i < svnum; i++) 382 for (i = 0; i < svnum; i++)
365 if (sv[i].pid) 383 if (sv[i].pid)
366 kill(sv[i].pid, SIGTERM); 384 kill(sv[i].pid, SIGTERM);
367 /* fall through */ 385 /* Fall through */
368 case SIGTERM: 386 default: /* SIGTERM (or SIGUSRn if we are init) */
369 /* exit, unless we are init */ 387 /* Exit unless we are init */
370 if (getpid() != 1) 388 if (getpid() == 1)
371 goto ret; 389 break;
372 default: 390 return (SIGHUP == bb_got_signal) ? 111 : EXIT_SUCCESS;
373 /* so we are init. do not exit,
374 * and pause respawning - we may be rebooting
375 * (but SIGHUP is not a reboot, make short pause) */
376 deadline = (SIGHUP == bb_got_signal) ? 5 : 60;
377 bb_got_signal = 0;
378 goto do_sleep;
379 } 391 }
380 } 392
381 ret: 393 bb_got_signal = 0;
382 return (SIGHUP == bb_got_signal) ? 111 : EXIT_SUCCESS; 394 } /* for (;;) */
383} 395}
diff --git a/runit/svlogd.c b/runit/svlogd.c
index 960879513..64191281e 100644
--- a/runit/svlogd.c
+++ b/runit/svlogd.c
@@ -797,7 +797,8 @@ static void sig_term_handler(int sig_no UNUSED_PARAM)
797 797
798static void sig_child_handler(int sig_no UNUSED_PARAM) 798static void sig_child_handler(int sig_no UNUSED_PARAM)
799{ 799{
800 int pid, l; 800 pid_t pid;
801 int l;
801 802
802 if (verbose) 803 if (verbose)
803 bb_error_msg(INFO"sig%s received", "child"); 804 bb_error_msg(INFO"sig%s received", "child");