From: Lennart Poettering Date: Wed, 10 Jul 2024 15:14:17 +0000 (+0200) Subject: signal-util: use common definitions for ignore + default "struct sigaction" X-Git-Tag: v257-rc1~919 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0bd721763436e6b156eebcf94492525631801668;p=thirdparty%2Fsystemd.git signal-util: use common definitions for ignore + default "struct sigaction" We use this at various places, let's unify this in one global constant. This changes flags in crash-handler.c in a tiny irrelevant way: we ask syscalls to be continued on signal arrival, which we previously didn't. But that shouldn't change anything, the only thing we'll do in the relevant process is call raise(), and that's it, hence there definitely are no syscalls to restart or not to restart. --- diff --git a/src/basic/signal-util.c b/src/basic/signal-util.c index bb04351d4ba..670040f1ad5 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 80579a59f33..814aa1a380e 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 cf27134984e..e50b54006a3 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 4a3fc017fab..056cb103fe4 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 31b284b5025..84ce5e1940e 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);