libbb: introduse and use signal_[no_]SA_RESTART_empty_mask and sigaction_set

signal_SA_RESTART_empty_mask                           -      65     +65
signal_no_SA_RESTART_empty_mask                        -      54     +54
dd_main                                             1325    1369     +44
sigaction_set                                          -      19     +19
dname_enc                                            373     377      +4
UNSPEC_print                                          64      66      +2
setsignal                                            296     294      -2
bb_signals_recursive                                  95      92      -3
bb_askpass                                           367     361      -6
inetd_main                                          1810    1797     -13
rx_main                                              912     883     -29
sigdelset                                             32       -     -32
__sigdelset                                           32       -     -32
__GI_sigdelset                                        32       -     -32
sighup_handler                                        84      37     -47
arping_main                                         1844    1797     -47
fsck_main                                           1869    1807     -62
run_list                                            1917    1844     -73
vlock_main                                           492     409     -83
progressmeter                                        883     798     -85
handle_incoming_and_exit                            2737    2651     -86
------------------------------------------------------------------------------
(add/remove: 3/3 grow/shrink: 3/12 up/down: 188/-632)        Total: -444 bytes
This commit is contained in:
Denis Vlasenko 2008-03-12 23:19:35 +00:00
parent 4cf1d08fc2
commit 8e2cfec1cd
14 changed files with 60 additions and 83 deletions

View File

@ -121,9 +121,6 @@ int dd_main(int argc, char **argv)
off_t count;
off_t seek, skip;
const char *infile, *outfile;
#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
struct sigaction sigact;
#endif
} Z;
#define flags (Z.flags )
#define oc (Z.oc )
@ -132,17 +129,13 @@ int dd_main(int argc, char **argv)
#define skip (Z.skip )
#define infile (Z.infile )
#define outfile (Z.outfile)
#define sigact (Z.sigact )
memset(&Z, 0, sizeof(Z));
INIT_G();
//fflush(NULL); - is this needed because of NOEXEC?
#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
sigact.sa_handler = dd_output_status;
sigact.sa_flags = SA_RESTART;
/*sigemptyset(&sigact.sa_mask); - memset did it */
sigaction(SIGUSR1, &sigact, NULL);
signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
#endif
for (n = 1; n < argc; n++) {

View File

@ -1164,12 +1164,9 @@ int fsck_main(int argc, char **argv)
int interactive;
const char *fstab;
struct fs_info *fs;
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_cancel;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
signal_no_SA_RESTART_empty_mask(SIGINT, signal_cancel);
signal_no_SA_RESTART_empty_mask(SIGTERM, signal_cancel);
setbuf(stdout, NULL);

View File

@ -275,6 +275,7 @@ char *xrealloc_getcwd_or_warn(char *cwd);
char *xmalloc_follow_symlinks(const char *path);
enum {
/* bb_signals(BB_SIGS_FATAL, handler) catches all signals which
* otherwise would kill us, except for those resulting from bugs:
@ -306,6 +307,12 @@ void bb_signals(int sigs, void (*f)(int));
* and in a way that while signal handler is run, no other signals
* will be blocked: */
void bb_signals_recursive(int sigs, void (*f)(int));
/* syscalls like read() will be interrupted with EINTR: */
void signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int));
/* syscalls like read() won't be interrupted (though select/poll will be): */
void signal_SA_RESTART_empty_mask(int sig, void (*handler)(int));
/* Will do sigaction(signum, act, NULL): */
int sigaction_set(int signum, const struct sigaction *act);
void sig_block(int);
void sig_unblock(int);
/* UNUSED: void sig_blocknone(void); */

View File

@ -45,7 +45,7 @@ char *bb_askpass(int timeout, const char *prompt)
sa.sa_handler = askpass_timeout;
sigaction(SIGINT, &sa, &oldsa);
if (timeout) {
sigaction(SIGALRM, &sa, NULL);
sigaction_set(SIGALRM, &sa);
alarm(timeout);
}
@ -68,7 +68,7 @@ char *bb_askpass(int timeout, const char *prompt)
if (timeout) {
alarm(0);
}
sigaction(SIGINT, &oldsa, NULL);
sigaction_set(SIGINT, &oldsa);
tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
bb_putchar('\n');

View File

@ -11,6 +11,12 @@
#include "libbb.h"
/* Saves 2 bytes on x86! Oh my... */
int sigaction_set(int signum, const struct sigaction *act)
{
return sigaction(signum, act, NULL);
}
void bb_signals(int sigs, void (*f)(int))
{
int sig_no = 0;
@ -40,7 +46,7 @@ void bb_signals_recursive(int sigs, void (*f)(int))
while (sigs) {
if (sigs & bit) {
sigs &= ~bit;
sigaction(sig_no, &sa, NULL);
sigaction_set(sig_no, &sa);
}
sig_no++;
bit <<= 1;
@ -87,3 +93,23 @@ void kill_myself_with_sig(int sig)
raise(sig);
_exit(1); /* Should not reach it */
}
void signal_SA_RESTART_empty_mask(int sig, void (*handler)(int))
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
/*sigemptyset(&sa.sa_mask);*/
sa.sa_flags = SA_RESTART;
sa.sa_handler = handler;
sigaction_set(sig, &sa);
}
void signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int))
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
/*sigemptyset(&sa.sa_mask);*/
/*sa.sa_flags = 0;*/
sa.sa_handler = handler;
sigaction_set(sig, &sa);
}

View File

@ -34,7 +34,6 @@ static void acquire_vt(int signo)
int vlock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int vlock_main(int argc, char **argv)
{
struct sigaction sa;
struct vt_mode vtm;
struct termios term;
struct termios oterm;
@ -63,17 +62,11 @@ int vlock_main(int argc, char **argv)
/* We will use SIGUSRx for console switch control: */
/* 1: set handlers */
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sa.sa_handler = release_vt;
sigaction(SIGUSR1, &sa, NULL);
sa.sa_handler = acquire_vt;
sigaction(SIGUSR2, &sa, NULL);
signal_SA_RESTART_empty_mask(SIGUSR1, release_vt);
signal_SA_RESTART_empty_mask(SIGUSR2, acquire_vt);
/* 2: unmask them */
sigprocmask(SIG_SETMASK, NULL, &sa.sa_mask);
sigdelset(&sa.sa_mask, SIGUSR1);
sigdelset(&sa.sa_mask, SIGUSR2);
sigprocmask(SIG_SETMASK, &sa.sa_mask, NULL);
sig_unblock(SIGUSR1);
sig_unblock(SIGUSR2);
/* Revert stdin/out to our controlling tty
* (or die if we have none) */

View File

@ -390,8 +390,8 @@ int devfsd_main(int argc, char **argv)
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
new_action.sa_handler = signal_handler;
sigaction(SIGHUP, &new_action, NULL);
sigaction(SIGUSR1, &new_action, NULL);
sigaction_set(SIGHUP, &new_action);
sigaction_set(SIGUSR1, &new_action);
printf("%s v%s started for %s\n", applet_name, DEVFSD_VERSION, mount_point);

View File

@ -220,7 +220,6 @@ static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int rx_main(int argc, char **argv)
{
struct sigaction act;
struct termios tty, orig_tty;
int termios_err;
int file_fd;
@ -243,9 +242,7 @@ int rx_main(int argc, char **argv)
}
/* No SA_RESTART: we want ALRM to interrupt read() */
memset(&act, 0, sizeof(act));
act.sa_handler = sigalrm_handler;
sigaction(SIGALRM, &act, NULL);
signal_no_SA_RESTART_empty_mask(SIGALRM, sigalrm_handler);
n = receive(file_fd);

View File

@ -377,18 +377,8 @@ int arping_main(int argc, char **argv)
printf(" from %s via %s\n", inet_ntoa(src), device);
}
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_RESTART;
sa.sa_handler = (void (*)(int)) finish;
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = (void (*)(int)) catcher;
sigaction(SIGALRM, &sa, NULL);
}
signal_SA_RESTART_empty_mask(SIGINT, (void (*)(int))finish);
signal_SA_RESTART_empty_mask(SIGALRM, (void (*)(int))catcher);
catcher();

View File

@ -1788,7 +1788,6 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
char *header_ptr = header_ptr;
Htaccess_Proxy *proxy_entry;
#endif
struct sigaction sa;
#if ENABLE_FEATURE_HTTPD_BASIC_AUTH
int credentials = -1; /* if not required this is Ok */
#endif
@ -1819,11 +1818,7 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
}
/* Install timeout handler */
memset(&sa, 0, sizeof(sa));
sa.sa_handler = exit_on_signal;
/* sigemptyset(&sa.sa_mask); - memset should be enough */
/*sa.sa_flags = 0; - no SA_RESTART */
sigaction(SIGALRM, &sa, NULL);
signal_no_SA_RESTART_empty_mask(SIGALRM, exit_on_signal);
alarm(HEADER_READ_TIMEOUT);
if (!get_line()) /* EOF or error or empty line */
@ -2247,15 +2242,9 @@ static void mini_httpd_inetd(void)
#if ENABLE_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
static void sighup_handler(int sig)
{
struct sigaction sa;
parse_conf(default_path_httpd_conf, sig == SIGHUP ? SIGNALED_PARSE : FIRST_PARSE);
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sighup_handler;
/*sigemptyset(&sa.sa_mask); - memset should be enough */
sa.sa_flags = SA_RESTART;
sigaction(SIGHUP, &sa, NULL);
signal_SA_RESTART_empty_mask(SIGHUP, sighup_handler);
}
#endif

View File

@ -1179,15 +1179,15 @@ int inetd_main(int argc, char **argv)
sigaddset(&sa.sa_mask, SIGCHLD);
sigaddset(&sa.sa_mask, SIGHUP);
sa.sa_handler = retry_network_setup;
sigaction(SIGALRM, &sa, NULL);
sigaction_set(SIGALRM, &sa);
sa.sa_handler = reread_config_file;
sigaction(SIGHUP, &sa, NULL);
sigaction_set(SIGHUP, &sa);
sa.sa_handler = reap_child;
sigaction(SIGCHLD, &sa, NULL);
sigaction_set(SIGCHLD, &sa);
sa.sa_handler = clean_up_and_exit;
sigaction(SIGTERM, &sa, NULL);
sigaction_set(SIGTERM, &sa);
sa.sa_handler = clean_up_and_exit;
sigaction(SIGINT, &sa, NULL);
sigaction_set(SIGINT, &sa);
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, &saved_pipe_handler);
@ -1382,7 +1382,7 @@ int inetd_main(int argc, char **argv)
* for nowait stream children */
for (sep2 = serv_list; sep2; sep2 = sep2->se_next)
maybe_close(sep2->se_fd);
sigaction(SIGPIPE, &saved_pipe_handler, NULL);
sigaction_set(SIGPIPE, &saved_pipe_handler);
unblock_sigs(&omask);
BB_EXECVP(sep->se_program, sep->se_argv);
bb_perror_msg("exec %s", sep->se_program);

View File

@ -144,13 +144,8 @@ static void progressmeter(int flag)
transferred = 0;
fputc('\n', stderr);
} else {
if (flag == -1) {
/* first call to progressmeter */
struct sigaction sa;
sa.sa_handler = progressmeter;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, NULL);
if (flag == -1) { /* first call to progressmeter */
signal_SA_RESTART_empty_mask(SIGALRM, progressmeter);
}
alarm(1);
}

View File

@ -3340,7 +3340,7 @@ setsignal(int signo)
*t = action;
act.sa_flags = 0;
sigfillset(&act.sa_mask);
sigaction(signo, &act, NULL);
sigaction_set(signo, &act);
}
/* mode flags for set_curjob */

View File

@ -699,16 +699,6 @@ static const struct built_in_command bltins[] = {
#if ENABLE_HUSH_JOB
/* move to libbb? */
static void signal_SA_RESTART(int sig, void (*handler)(int))
{
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaction(sig, &sa, NULL);
}
/* Signals are grouped, we handle them in batches */
static void set_fatal_sighandler(void (*handler)(int))
{
@ -2115,7 +2105,7 @@ static int run_list(struct pipe *pi)
#if ENABLE_FEATURE_SH_STANDALONE
nofork_save.saved = 0; /* in case we will run a nofork later */
#endif
signal_SA_RESTART(SIGTSTP, handler_ctrl_z);
signal_SA_RESTART_empty_mask(SIGTSTP, handler_ctrl_z);
signal(SIGINT, handler_ctrl_c);
}
#endif /* JOB */