diff --git a/src/basic/signal-util.c b/src/basic/signal-util.c index bb04351d4b..670040f1ad 100644 --- a/src/basic/signal-util.c +++ b/src/basic/signal-util.c @@ -14,10 +14,6 @@ #include "string-util.h" int reset_all_signal_handlers(void) { - static const struct sigaction sa = { - .sa_handler = SIG_DFL, - .sa_flags = SA_RESTART, - }; int ret = 0, r; for (int sig = 1; sig < _NSIG; sig++) { @@ -28,7 +24,7 @@ int reset_all_signal_handlers(void) { /* On Linux the first two RT signals are reserved by glibc, and sigaction() will return * EINVAL for them. */ - r = RET_NERRNO(sigaction(sig, &sa, NULL)); + r = RET_NERRNO(sigaction(sig, &sigaction_default, NULL)); if (r != -EINVAL) RET_GATHER(ret, r); } @@ -295,3 +291,13 @@ void propagate_signal(int sig, siginfo_t *siginfo) { if (rt_tgsigqueueinfo(p, gettid(), sig, siginfo) < 0) assert_se(kill(p, sig) >= 0); } + +const struct sigaction sigaction_ignore = { + .sa_handler = SIG_IGN, + .sa_flags = SA_RESTART, +}; + +const struct sigaction sigaction_default = { + .sa_handler = SIG_DFL, + .sa_flags = SA_RESTART, +}; diff --git a/src/basic/signal-util.h b/src/basic/signal-util.h index 80579a59f3..814aa1a380 100644 --- a/src/basic/signal-util.h +++ b/src/basic/signal-util.h @@ -12,19 +12,13 @@ int sigaction_many_internal(const struct sigaction *sa, ...); #define ignore_signals(...) \ sigaction_many_internal( \ - &(const struct sigaction) { \ - .sa_handler = SIG_IGN, \ - .sa_flags = SA_RESTART \ - }, \ + &sigaction_ignore, \ __VA_ARGS__, \ -1) #define default_signals(...) \ sigaction_many_internal( \ - &(const struct sigaction) { \ - .sa_handler = SIG_DFL, \ - .sa_flags = SA_RESTART \ - }, \ + &sigaction_default, \ __VA_ARGS__, \ -1) @@ -70,3 +64,6 @@ int pop_pending_signal_internal(int sig, ...); #define pop_pending_signal(...) pop_pending_signal_internal(__VA_ARGS__, -1) void propagate_signal(int sig, siginfo_t *siginfo); + +extern const struct sigaction sigaction_ignore; +extern const struct sigaction sigaction_default; diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index cf27134984..e50b54006a 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -37,6 +37,7 @@ #include "path-util.h" #include "proc-cmdline.h" #include "process-util.h" +#include "signal-util.h" #include "socket-util.h" #include "stat-util.h" #include "stdio-util.h" @@ -403,11 +404,6 @@ int acquire_terminal( } for (;;) { - struct sigaction sa_old, sa_new = { - .sa_handler = SIG_IGN, - .sa_flags = SA_RESTART, - }; - if (notify >= 0) { r = flush_fd(notify); if (r < 0) @@ -421,13 +417,14 @@ int acquire_terminal( return fd; /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed if we already own the tty. */ - assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0); + struct sigaction sa_old; + assert_se(sigaction(SIGHUP, &sigaction_ignore, &sa_old) >= 0); /* First, try to get the tty */ r = RET_NERRNO(ioctl(fd, TIOCSCTTY, (flags & ~ACQUIRE_TERMINAL_PERMISSIVE) == ACQUIRE_TERMINAL_FORCE)); /* Reset signal handler to old value */ - assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); + assert_se(sigaction(SIGHUP, &sa_old, NULL) >= 0); /* Success? Exit the loop now! */ if (r >= 0) @@ -496,13 +493,7 @@ int acquire_terminal( } int release_terminal(void) { - static const struct sigaction sa_new = { - .sa_handler = SIG_IGN, - .sa_flags = SA_RESTART, - }; - _cleanup_close_ int fd = -EBADF; - struct sigaction sa_old; int r; fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); @@ -511,11 +502,12 @@ int release_terminal(void) { /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed * by our own TIOCNOTTY */ - assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0); + struct sigaction sa_old; + assert_se(sigaction(SIGHUP, &sigaction_ignore, &sa_old) >= 0); r = RET_NERRNO(ioctl(fd, TIOCNOTTY)); - assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); + assert_se(sigaction(SIGHUP, &sa_old, NULL) >= 0); return r; } diff --git a/src/core/crash-handler.c b/src/core/crash-handler.c index 4a3fc017fa..056cb103fe 100644 --- a/src/core/crash-handler.c +++ b/src/core/crash-handler.c @@ -85,10 +85,7 @@ _noreturn_ static void crash(int sig, siginfo_t *siginfo, void *context) { else if (pid == 0) { /* Enable default signal handler for core dump */ - sa = (struct sigaction) { - .sa_handler = SIG_DFL, - }; - (void) sigaction(sig, &sa, NULL); + (void) sigaction(sig, &sigaction_default, NULL); /* Don't limit the coredump size */ (void) setrlimit(RLIMIT_CORE, &RLIMIT_MAKE_CONST(RLIM_INFINITY)); diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 31b284b502..84ce5e1940 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -548,14 +548,10 @@ static int ask_on_this_console(const char *tty, pid_t *ret_pid, char **arguments .sa_handler = nop_signal_handler, .sa_flags = SA_NOCLDSTOP | SA_RESTART, }; - static const struct sigaction sighup = { - .sa_handler = SIG_DFL, - .sa_flags = SA_RESTART, - }; int r; assert_se(sigaction(SIGCHLD, &sigchld, NULL) >= 0); - assert_se(sigaction(SIGHUP, &sighup, NULL) >= 0); + assert_se(sigaction(SIGHUP, &sigaction_default, NULL) >= 0); assert_se(sigprocmask_many(SIG_UNBLOCK, NULL, SIGHUP, SIGCHLD) >= 0); r = safe_fork("(sd-passwd)", FORK_RESET_SIGNALS|FORK_LOG, ret_pid);