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.
This commit is contained in:
Lennart Poettering
2024-07-12 08:26:15 +02:00
committed by Daan De Meyer
parent 7eedec7307
commit 0bd7217634
5 changed files with 25 additions and 37 deletions
+11 -5
View File
@@ -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,
};
+5 -8
View 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;
+7 -15
View 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;
}
+1 -4
View 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));
@@ -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);