libbb: introduse and use signal_[no_]SA_RESTART_empty_mask and sigaction_set
authorDenis Vlasenko <vda.linux@googlemail.com>
Wed, 12 Mar 2008 23:19:35 +0000 (23:19 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Wed, 12 Mar 2008 23:19:35 +0000 (23:19 -0000)
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

14 files changed:
coreutils/dd.c
e2fsprogs/fsck.c
include/libbb.h
libbb/bb_askpass.c
libbb/signals.c
loginutils/vlock.c
miscutils/devfsd.c
miscutils/rx.c
networking/arping.c
networking/httpd.c
networking/inetd.c
networking/wget.c
shell/ash.c
shell/hush.c

index 961b1fff7cf3e4edf1c13dfd907fae3cec017b07..f3330e624593779d6061d4630d3e790a10fcbe5e 100644 (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++) {
index de2d61eefcdf1bf68282db8c754e4d3c843f9a22..037a563f4aefe283896ef3682e71affabc0c8b8e 100644 (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);
 
index 3c53e5e68dd791248f900123d9f623cb100e97c6..505396b86b0c77eda8468dfdea48b4bd90f9c3dc 100644 (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); */
index fd12f92dc10b3dd3da7149f4787287fab90f62b1..3ad0e97cdd46b79e8f7a5242c109f0028176bb22 100644 (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');
index a327e87c8eaaff99c396a8f00b7b23e71970630d..719beab63484db869ca4ab05fc9b3f74cecfce97 100644 (file)
 
 #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);
+}
index 846733ea8127cb3a7056f2655cc2add5a2b6f7f5..ff60d78ffe5c5fc698bb67020eaf5078928a7110 100644 (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) */
index 50c8203cb693843dad77b5216eae98aaea97f71f..1b88f050e85cc1d0f0d6b0181e345875970503c0 100644 (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);
 
index 9a8fcaa2075d60529999fd2bf1be9881e6c59c0c..48867b83c9f7ca3d4be162a3066d2932e1e1a940 100644 (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);
 
index c89b97567fcfad49a904ad301c52879e726fba90..2277ec55ef4158e43393a03a13a9d569507b632a 100644 (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();
 
index e9137287cdce9022190c93ee38a2912548d9d148..7b7446ed9f4359d9cb8ebcfc04de8f8fe8f44d39 100644 (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
 
index 463c7cfcf80566b912dd2ee8493e55061fafb850..196f6d29dfb8fd72e9abf830ec636abc218eac28 100644 (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);
index 36a83560a92fa8a587a6e5becdea79e9abb7c34d..a77a2add768ba246b0b9ad4439a45c1dddeab4f6 100644 (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);
        }
index 9b1a73024575c0bd3217c572cd820761be611dc4..6f675ce69d571949732a53d3ca5d72f2d08f99fb 100644 (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 */
index b44f35bdd9f63c391e7257c4ccbcd6c5e87389c6..baa2db780819a55022e15c6b48eae67b60bfb070 100644 (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 */