aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-03-17 08:29:08 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-03-17 08:29:08 +0000
commit3f165fa5b3b38fa4b321be94a97f06927f636fb1 (patch)
treed5e2d1045e714823c2e9a3ef912dcafeffcc9ebd
parent8e2cfec1cdf34068f2378aae9fe8f5ec0f821e21 (diff)
downloadbusybox-w32-3f165fa5b3b38fa4b321be94a97f06927f636fb1.tar.gz
busybox-w32-3f165fa5b3b38fa4b321be94a97f06927f636fb1.tar.bz2
busybox-w32-3f165fa5b3b38fa4b321be94a97f06927f636fb1.zip
libbb: introduce and use sigprocmask_allsigs and sigaction_set.
libbb: rename sig_pause to wait_for_any_sig.
-rw-r--r--include/libbb.h13
-rw-r--r--init/init.c31
-rw-r--r--ipsvd/tcpudp.c2
-rw-r--r--libbb/signals.c18
-rw-r--r--networking/inetd.c22
-rw-r--r--shell/ash.c4
-rw-r--r--shell/hush.c5
7 files changed, 31 insertions, 64 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 505396b86..73638a145 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -311,13 +311,14 @@ void bb_signals_recursive(int sigs, void (*f)(int));
311void signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int)); 311void signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int));
312/* syscalls like read() won't be interrupted (though select/poll will be): */ 312/* syscalls like read() won't be interrupted (though select/poll will be): */
313void signal_SA_RESTART_empty_mask(int sig, void (*handler)(int)); 313void signal_SA_RESTART_empty_mask(int sig, void (*handler)(int));
314/* Will do sigaction(signum, act, NULL): */ 314void wait_for_any_sig(void);
315int sigaction_set(int signum, const struct sigaction *act);
316void sig_block(int);
317void sig_unblock(int);
318/* UNUSED: void sig_blocknone(void); */
319void sig_pause(void);
320void kill_myself_with_sig(int sig) ATTRIBUTE_NORETURN; 315void kill_myself_with_sig(int sig) ATTRIBUTE_NORETURN;
316void sig_block(int sig);
317void sig_unblock(int sig);
318/* Will do sigaction(signum, act, NULL): */
319int sigaction_set(int sig, const struct sigaction *act);
320/* SIG_BLOCK/SIG_UNBLOCK all signals: */
321int sigprocmask_allsigs(int how);
321 322
322 323
323void xsetgid(gid_t gid); 324void xsetgid(gid_t gid);
diff --git a/init/init.c b/init/init.c
index 13410ac39..1145c0490 100644
--- a/init/init.c
+++ b/init/init.c
@@ -526,27 +526,13 @@ static void init_reboot(unsigned long magic)
526 526
527static void kill_all_processes(void) 527static void kill_all_processes(void)
528{ 528{
529 sigset_t block_signals;
530
531 /* run everything to be run at "shutdown". This is done _prior_ 529 /* run everything to be run at "shutdown". This is done _prior_
532 * to killing everything, in case people wish to use scripts to 530 * to killing everything, in case people wish to use scripts to
533 * shut things down gracefully... */ 531 * shut things down gracefully... */
534 run_actions(SHUTDOWN); 532 run_actions(SHUTDOWN);
535 533
536 /* first disable all our signals */ 534 /* first disable all our signals */
537 sigfillset(&block_signals); 535 sigprocmask_allsigs(SIG_BLOCK);
538 /*sigemptyset(&block_signals);
539 sigaddset(&block_signals, SIGHUP);
540 sigaddset(&block_signals, SIGQUIT);
541 sigaddset(&block_signals, SIGCHLD);
542 sigaddset(&block_signals, SIGUSR1);
543 sigaddset(&block_signals, SIGUSR2);
544 sigaddset(&block_signals, SIGINT);
545 sigaddset(&block_signals, SIGTERM);
546 sigaddset(&block_signals, SIGCONT);
547 sigaddset(&block_signals, SIGSTOP);
548 sigaddset(&block_signals, SIGTSTP);*/
549 sigprocmask(SIG_BLOCK, &block_signals, NULL);
550 536
551 message(L_CONSOLE | L_LOG, "The system is going down NOW!"); 537 message(L_CONSOLE | L_LOG, "The system is going down NOW!");
552 538
@@ -593,26 +579,13 @@ static void halt_reboot_pwoff(int sig)
593static void exec_restart_action(int sig ATTRIBUTE_UNUSED) 579static void exec_restart_action(int sig ATTRIBUTE_UNUSED)
594{ 580{
595 struct init_action *a; 581 struct init_action *a;
596 sigset_t unblock_signals;
597 582
598 for (a = init_action_list; a; a = a->next) { 583 for (a = init_action_list; a; a = a->next) {
599 if (a->action_type & RESTART) { 584 if (a->action_type & RESTART) {
600 kill_all_processes(); 585 kill_all_processes();
601 586
602 /* unblock all signals (blocked in kill_all_processes()) */ 587 /* unblock all signals (blocked in kill_all_processes()) */
603 sigfillset(&unblock_signals); 588 sigprocmask_allsigs(SIG_UNBLOCK);
604 /*sigemptyset(&unblock_signals);
605 sigaddset(&unblock_signals, SIGHUP);
606 sigaddset(&unblock_signals, SIGQUIT);
607 sigaddset(&unblock_signals, SIGCHLD);
608 sigaddset(&unblock_signals, SIGUSR1);
609 sigaddset(&unblock_signals, SIGUSR2);
610 sigaddset(&unblock_signals, SIGINT);
611 sigaddset(&unblock_signals, SIGTERM);
612 sigaddset(&unblock_signals, SIGCONT);
613 sigaddset(&unblock_signals, SIGSTOP);
614 sigaddset(&unblock_signals, SIGTSTP);*/
615 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
616 589
617 /* Open the new terminal device */ 590 /* Open the new terminal device */
618 open_stdio_to_tty(a->terminal, 0 /* - halt if open fails */); 591 open_stdio_to_tty(a->terminal, 0 /* - halt if open fails */);
diff --git a/ipsvd/tcpudp.c b/ipsvd/tcpudp.c
index 8b4ae88f4..5362af5fd 100644
--- a/ipsvd/tcpudp.c
+++ b/ipsvd/tcpudp.c
@@ -290,7 +290,7 @@ int tcpudpsvd_main(int argc, char **argv)
290 hccp = NULL; 290 hccp = NULL;
291 291
292 while (cnum >= cmax) 292 while (cnum >= cmax)
293 sig_pause(); /* wait for any signal (expecting SIGCHLD) */ 293 wait_for_any_sig(); /* expecting SIGCHLD */
294 294
295 /* Accept a connection to fd #0 */ 295 /* Accept a connection to fd #0 */
296 again1: 296 again1:
diff --git a/libbb/signals.c b/libbb/signals.c
index 719beab63..685c55230 100644
--- a/libbb/signals.c
+++ b/libbb/signals.c
@@ -17,6 +17,13 @@ int sigaction_set(int signum, const struct sigaction *act)
17 return sigaction(signum, act, NULL); 17 return sigaction(signum, act, NULL);
18} 18}
19 19
20int sigprocmask_allsigs(int how)
21{
22 sigset_t set;
23 sigfillset(&set);
24 return sigprocmask(how, &set, NULL);
25}
26
20void bb_signals(int sigs, void (*f)(int)) 27void bb_signals(int sigs, void (*f)(int))
21{ 28{
22 int sig_no = 0; 29 int sig_no = 0;
@@ -69,16 +76,7 @@ void sig_unblock(int sig)
69 sigprocmask(SIG_UNBLOCK, &ss, NULL); 76 sigprocmask(SIG_UNBLOCK, &ss, NULL);
70} 77}
71 78
72#if 0 79void wait_for_any_sig(void)
73void sig_blocknone(void)
74{
75 sigset_t ss;
76 sigemptyset(&ss);
77 sigprocmask(SIG_SETMASK, &ss, NULL);
78}
79#endif
80
81void sig_pause(void)
82{ 80{
83 sigset_t ss; 81 sigset_t ss;
84 sigemptyset(&ss); 82 sigemptyset(&ss);
diff --git a/networking/inetd.c b/networking/inetd.c
index 196f6d29d..0620188d6 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -378,12 +378,12 @@ static void block_CHLD_HUP_ALRM(sigset_t *m)
378 sigaddset(m, SIGCHLD); 378 sigaddset(m, SIGCHLD);
379 sigaddset(m, SIGHUP); 379 sigaddset(m, SIGHUP);
380 sigaddset(m, SIGALRM); 380 sigaddset(m, SIGALRM);
381 sigprocmask(SIG_BLOCK, m, NULL); 381 sigprocmask(SIG_BLOCK, m, m); /* old sigmask is stored in m */
382} 382}
383 383
384static void unblock_sigs(sigset_t *m) 384static void restore_sigmask(sigset_t *m)
385{ 385{
386 sigprocmask(SIG_UNBLOCK, m, NULL); 386 sigprocmask(SIG_SETMASK, m, NULL);
387} 387}
388 388
389#if ENABLE_FEATURE_INETD_RPC 389#if ENABLE_FEATURE_INETD_RPC
@@ -870,7 +870,7 @@ static servtab_t *insert_in_servlist(servtab_t *cp)
870 block_CHLD_HUP_ALRM(&omask); 870 block_CHLD_HUP_ALRM(&omask);
871 sep->se_next = serv_list; 871 sep->se_next = serv_list;
872 serv_list = sep; 872 serv_list = sep;
873 unblock_sigs(&omask); 873 restore_sigmask(&omask);
874 return sep; 874 return sep;
875} 875}
876 876
@@ -941,7 +941,7 @@ static void reread_config_file(int sig ATTRIBUTE_UNUSED)
941 for (i = 0; i < MAXARGV; i++) 941 for (i = 0; i < MAXARGV; i++)
942 SWAP(char*, sep->se_argv[i], cp->se_argv[i]); 942 SWAP(char*, sep->se_argv[i], cp->se_argv[i]);
943#undef SWAP 943#undef SWAP
944 unblock_sigs(&omask); 944 restore_sigmask(&omask);
945 free_servtab_strings(cp); 945 free_servtab_strings(cp);
946 } 946 }
947 after_check: 947 after_check:
@@ -1052,7 +1052,7 @@ static void reread_config_file(int sig ATTRIBUTE_UNUSED)
1052 free_servtab_strings(sep); 1052 free_servtab_strings(sep);
1053 free(sep); 1053 free(sep);
1054 } 1054 }
1055 unblock_sigs(&omask); 1055 restore_sigmask(&omask);
1056} 1056}
1057 1057
1058static void reap_child(int sig ATTRIBUTE_UNUSED) 1058static void reap_child(int sig ATTRIBUTE_UNUSED)
@@ -1252,7 +1252,7 @@ int inetd_main(int argc, char **argv)
1252 sep->se_fd = -1; 1252 sep->se_fd = -1;
1253 sep->se_count = 0; 1253 sep->se_count = 0;
1254 rearm_alarm(); /* will revive it in RETRYTIME sec */ 1254 rearm_alarm(); /* will revive it in RETRYTIME sec */
1255 unblock_sigs(&omask); 1255 restore_sigmask(&omask);
1256 maybe_close(accepted_fd); 1256 maybe_close(accepted_fd);
1257 continue; /* -> check next fd in fd set */ 1257 continue; /* -> check next fd in fd set */
1258 } 1258 }
@@ -1272,7 +1272,7 @@ int inetd_main(int argc, char **argv)
1272 if (pid < 0) { /* fork error */ 1272 if (pid < 0) { /* fork error */
1273 bb_perror_msg("fork"); 1273 bb_perror_msg("fork");
1274 sleep(1); 1274 sleep(1);
1275 unblock_sigs(&omask); 1275 restore_sigmask(&omask);
1276 maybe_close(accepted_fd); 1276 maybe_close(accepted_fd);
1277 continue; /* -> check next fd in fd set */ 1277 continue; /* -> check next fd in fd set */
1278 } 1278 }
@@ -1288,7 +1288,7 @@ int inetd_main(int argc, char **argv)
1288 /* we passed listening socket to child, 1288 /* we passed listening socket to child,
1289 * will wait for child to terminate */ 1289 * will wait for child to terminate */
1290 } 1290 }
1291 unblock_sigs(&omask); 1291 restore_sigmask(&omask);
1292 maybe_close(accepted_fd); 1292 maybe_close(accepted_fd);
1293 continue; /* -> check next fd in fd set */ 1293 continue; /* -> check next fd in fd set */
1294 } 1294 }
@@ -1300,7 +1300,7 @@ int inetd_main(int argc, char **argv)
1300 close(sep->se_fd); /* listening socket */ 1300 close(sep->se_fd); /* listening socket */
1301 logmode = 0; /* make xwrite etc silent */ 1301 logmode = 0; /* make xwrite etc silent */
1302 } 1302 }
1303 unblock_sigs(&omask); 1303 restore_sigmask(&omask);
1304 if (sep->se_socktype == SOCK_STREAM) 1304 if (sep->se_socktype == SOCK_STREAM)
1305 sep->se_builtin->bi_stream_fn(ctrl, sep); 1305 sep->se_builtin->bi_stream_fn(ctrl, sep);
1306 else 1306 else
@@ -1383,7 +1383,7 @@ int inetd_main(int argc, char **argv)
1383 for (sep2 = serv_list; sep2; sep2 = sep2->se_next) 1383 for (sep2 = serv_list; sep2; sep2 = sep2->se_next)
1384 maybe_close(sep2->se_fd); 1384 maybe_close(sep2->se_fd);
1385 sigaction_set(SIGPIPE, &saved_pipe_handler); 1385 sigaction_set(SIGPIPE, &saved_pipe_handler);
1386 unblock_sigs(&omask); 1386 restore_sigmask(&omask);
1387 BB_EXECVP(sep->se_program, sep->se_argv); 1387 BB_EXECVP(sep->se_program, sep->se_argv);
1388 bb_perror_msg("exec %s", sep->se_program); 1388 bb_perror_msg("exec %s", sep->se_program);
1389 do_exit1: 1389 do_exit1:
diff --git a/shell/ash.c b/shell/ash.c
index 6f675ce69..069909118 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -273,13 +273,11 @@ static void
273raise_interrupt(void) 273raise_interrupt(void)
274{ 274{
275 int i; 275 int i;
276 sigset_t mask;
277 276
278 intpending = 0; 277 intpending = 0;
279 /* Signal is not automatically unmasked after it is raised, 278 /* Signal is not automatically unmasked after it is raised,
280 * do it ourself - unmask all signals */ 279 * do it ourself - unmask all signals */
281 sigemptyset(&mask); 280 sigprocmask_allsigs(SIG_UNBLOCK);
282 sigprocmask(SIG_SETMASK, &mask, NULL);
283 /* pendingsig = 0; - now done in onsig() */ 281 /* pendingsig = 0; - now done in onsig() */
284 282
285 i = EXSIG; 283 i = EXSIG;
diff --git a/shell/hush.c b/shell/hush.c
index baa2db780..c61607dd3 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -788,11 +788,8 @@ static void handler_ctrl_z(int sig)
788static void sigexit(int sig) ATTRIBUTE_NORETURN; 788static void sigexit(int sig) ATTRIBUTE_NORETURN;
789static void sigexit(int sig) 789static void sigexit(int sig)
790{ 790{
791 sigset_t block_all;
792
793 /* Disable all signals: job control, SIGPIPE, etc. */ 791 /* Disable all signals: job control, SIGPIPE, etc. */
794 sigfillset(&block_all); 792 sigprocmask_allsigs(SIG_BLOCK);
795 sigprocmask(SIG_SETMASK, &block_all, NULL);
796 793
797 if (interactive_fd) 794 if (interactive_fd)
798 tcsetpgrp(interactive_fd, saved_tty_pgrp); 795 tcsetpgrp(interactive_fd, saved_tty_pgrp);