diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-17 01:58:56 +0300 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-17 01:58:56 +0300 |
commit | 25591c322c9305bd54d3ab80cfaf01ef87640d77 (patch) | |
tree | 66ce77758e35f4faa2d5f611d0535365f2cba00a /libbb | |
parent | 7fc294cdfe1e7f4a12c44f984a698b0c0f609075 (diff) |
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
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Kbuild | 1 | ||||
-rw-r--r-- | libbb/signals.c | 80 | ||||
-rw-r--r-- | libbb/xfuncs.c | 42 |
3 files changed, 81 insertions, 42 deletions
diff --git a/libbb/Kbuild b/libbb/Kbuild index 2fb1b2420..515368de7 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild @@ -78,6 +78,7 @@ lib-y += safe_strncpy.o lib-y += safe_write.o lib-y += setup_environment.o lib-y += sha1.o +lib-y += signals.o lib-y += simplify_path.o lib-y += skip_whitespace.o lib-y += speed_table.o diff --git a/libbb/signals.c b/libbb/signals.c new file mode 100644 index 000000000..f7e4908e7 --- /dev/null +++ b/libbb/signals.c @@ -0,0 +1,80 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> + * Copyright (C) 2006 Rob Landley + * Copyright (C) 2006 Denis Vlasenko + * + * Licensed under GPL version 2, see file LICENSE in this tarball for details. + */ + +#include "libbb.h" + +void bb_signals(int sigs, void (*f)(int)) +{ + int sig_no = 0; + int bit = 1; + + while (sigs) { + if (sigs & bit) { + sigs &= ~bit; + signal(sig_no, f); + } + sig_no++; + bit <<= 1; + } +} + +void bb_signals_recursive(int sigs, void (*f)(int)) +{ + int sig_no = 0; + int bit = 1; + struct sigaction sa; + + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = f; + /*sa.sa_flags = 0;*/ + /*sigemptyset(&sa.sa_mask); - hope memset did it*/ + + while (sigs) { + if (sigs & bit) { + sigs &= ~bit; + sigaction(sig_no, &sa, NULL); + } + sig_no++; + bit <<= 1; + } +} + +void sig_block(int sig) +{ + sigset_t ss; + sigemptyset(&ss); + sigaddset(&ss, sig); + sigprocmask(SIG_BLOCK, &ss, NULL); +} + +void sig_unblock(int sig) +{ + sigset_t ss; + sigemptyset(&ss); + sigaddset(&ss, sig); + sigprocmask(SIG_UNBLOCK, &ss, NULL); +} + +#if 0 +void sig_blocknone(void) +{ + sigset_t ss; + sigemptyset(&ss); + sigprocmask(SIG_SETMASK, &ss, NULL); +} +#endif + +void sig_pause(void) +{ + sigset_t ss; + sigemptyset(&ss); + sigsuspend(&ss); +} diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 445e07717..8dd414d6a 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -234,48 +234,6 @@ void xfflush_stdout(void) } } -void sig_block(int sig) -{ - sigset_t ss; - sigemptyset(&ss); - sigaddset(&ss, sig); - sigprocmask(SIG_BLOCK, &ss, NULL); -} - -void sig_unblock(int sig) -{ - sigset_t ss; - sigemptyset(&ss); - sigaddset(&ss, sig); - sigprocmask(SIG_UNBLOCK, &ss, NULL); -} - -#if 0 -void sig_blocknone(void) -{ - sigset_t ss; - sigemptyset(&ss); - sigprocmask(SIG_SETMASK, &ss, NULL); -} -#endif - -void sig_catch(int sig, void (*f)(int)) -{ - struct sigaction sa; - sa.sa_handler = f; - sa.sa_flags = 0; - sigemptyset(&sa.sa_mask); - sigaction(sig, &sa, NULL); -} - -void sig_pause(void) -{ - sigset_t ss; - sigemptyset(&ss); - sigsuspend(&ss); -} - - void xsetenv(const char *key, const char *value) { if (setenv(key, value, 1)) |