libbb: introduce bb_signals and bb_signals_recursive,

which sets same handler for many signals. sig_catch is nuked
(bb_signals_recursive is more descriptive name).
*: use them as appropriate. 

function                                             old     new   delta
bb_signals_recursive                                   -      95     +95
bb_signals                                             -      52     +52
run_command                                          258     273     +15
svlogd_main                                         1368    1377      +9
runsv_main                                          1746    1752      +6
runsvdir_main                                       1643    1646      +3
UNSPEC_print                                          64      66      +2
time_main                                           1128    1127      -1
...
resize_main                                          246     210     -36
sig_catch                                             63       -     -63
set_fatal_sighandler                                  85      14     -71
------------------------------------------------------------------------------
(add/remove: 2/1 grow/shrink: 5/24 up/down: 182/-548)        Total: -366 bytes
This commit is contained in:
Denis Vlasenko
2008-02-16 22:58:56 +00:00
parent 7fc294cdfe
commit 25591c322c
26 changed files with 249 additions and 154 deletions

View File

@@ -1355,8 +1355,10 @@ int less_main(int argc, char **argv)
empty_line_marker = "";
tcgetattr(kbd_fd, &term_orig);
signal(SIGTERM, sig_catcher);
signal(SIGINT, sig_catcher);
bb_signals(0
+ (1 << SIGTERM)
+ (1 << SIGINT)
, sig_catcher);
term_less = term_orig;
term_less.c_lflag &= ~(ICANON | ECHO);
term_less.c_iflag &= ~(IXON | ICRNL);

View File

@@ -99,10 +99,12 @@ int microcom_main(int argc, char **argv)
}
// setup signals
sig_catch(SIGHUP, signal_handler);
sig_catch(SIGINT, signal_handler);
sig_catch(SIGTERM, signal_handler);
sig_catch(SIGPIPE, signal_handler);
bb_signals_recursive(0
+ (1 << SIGHUP)
+ (1 << SIGINT)
+ (1 << SIGTERM)
+ (1 << SIGPIPE)
, signal_handler);
// error exit code if we fail to open the device
signalled = 1;

View File

@@ -61,7 +61,7 @@ static const char long_format[] ALIGN1 =
Return 0 on error, 1 if ok. */
/* pid_t is short on BSDI, so don't try to promote it. */
static int resuse_end(pid_t pid, resource_t * resp)
static int resuse_end(pid_t pid, resource_t *resp)
{
int status;
pid_t caught;
@@ -69,7 +69,7 @@ static int resuse_end(pid_t pid, resource_t * resp)
/* Ignore signals, but don't ignore the children. When wait3
returns the child process, set the time the command finished. */
while ((caught = wait3(&status, 0, &resp->ru)) != pid) {
if (caught == -1)
if (caught == -1 && errno != EINTR)
return 0;
}
resp->elapsed_ms = (monotonic_us() / 1000) - resp->elapsed_ms;
@@ -373,24 +373,26 @@ static void summarize(const char *fmt, char **command, resource_t * resp)
/* Run command CMD and return statistics on it.
Put the statistics in *RESP. */
static void run_command(char *const *cmd, resource_t * resp)
static void run_command(char *const *cmd, resource_t *resp)
{
pid_t pid; /* Pid of child. */
__sighandler_t interrupt_signal, quit_signal;
void (*interrupt_signal)(int);
void (*quit_signal)(int);
resp->elapsed_ms = monotonic_us() / 1000;
pid = vfork(); /* Run CMD as child process. */
if (pid < 0)
bb_error_msg_and_die("cannot fork");
else if (pid == 0) { /* If child. */
if (pid == 0) { /* If child. */
/* Don't cast execvp arguments; that causes errors on some systems,
versus merely warnings if the cast is left off. */
BB_EXECVP(cmd[0], cmd);
bb_error_msg("cannot run %s", cmd[0]);
_exit(errno == ENOENT ? 127 : 126);
xfunc_error_retval = (errno == ENOENT ? 127 : 126);
bb_error_msg_and_die("cannot run %s", cmd[0]);
}
/* Have signals kill the child but not self (if possible). */
//TODO: just block all sigs? and reenable them in the very end in main?
interrupt_signal = signal(SIGINT, SIG_IGN);
quit_signal = signal(SIGQUIT, SIG_IGN);

View File

@@ -47,8 +47,10 @@ int watchdog_main(int argc, char **argv)
bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
}
signal(SIGHUP, watchdog_shutdown);
signal(SIGINT, watchdog_shutdown);
bb_signals(0
+ (1 << SIGHUP)
+ (1 << SIGINT)
, watchdog_shutdown);
/* Use known fd # - avoid needing global 'int fd' */
xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);