]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
signal-util: use common definitions for ignore + default "struct sigaction"
authorLennart Poettering <lennart@poettering.net>
Wed, 10 Jul 2024 15:14:17 +0000 (17:14 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 12 Jul 2024 06:26:15 +0000 (08:26 +0200)
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.

src/basic/signal-util.c
src/basic/signal-util.h
src/basic/terminal-util.c
src/core/crash-handler.c
src/tty-ask-password-agent/tty-ask-password-agent.c

index bb04351d4bab8bdf095123bebab296003883077f..670040f1ad5d0596def22aaa3f3d92412e58fc2c 100644 (file)
 #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,
+};
index 80579a59f334e1ca5c86fb3c6b3a6b54179de348..814aa1a380eb0716e7939c3336c451c29497c16d 100644 (file)
@@ -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;
index cf27134984e96c79f6583be752c0ff523732b0ea..e50b54006a3d78f6844fb43f9f65710f87c69f96 100644 (file)
@@ -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;
 }
index 4a3fc017fabd267dafd5bc04924a4d360da86eb3..056cb103fe4f8257c791da151d1a035c2b11b1f9 100644 (file)
@@ -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));
index 31b284b5025b27f4ba12da6e3d5cf50d7a652be5..84ce5e1940e0144f98215121cf5a084c1872a01d 100644 (file)
@@ -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);