2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2015-05-29 20:14:11 +02:00
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2015-12-01 23:22:03 +01:00
|
|
|
#include "macro.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
|
|
|
#include "signal-util.h"
|
2016-01-12 15:34:20 +01:00
|
|
|
#include "stdio-util.h"
|
2015-10-26 22:31:05 +01:00
|
|
|
#include "string-table.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
|
|
|
|
2015-05-29 20:14:11 +02:00
|
|
|
int reset_all_signal_handlers(void) {
|
2015-05-31 23:21:48 +02:00
|
|
|
static const struct sigaction sa = {
|
|
|
|
.sa_handler = SIG_DFL,
|
|
|
|
.sa_flags = SA_RESTART,
|
|
|
|
};
|
2015-05-29 20:14:11 +02:00
|
|
|
int sig, r = 0;
|
|
|
|
|
|
|
|
for (sig = 1; sig < _NSIG; sig++) {
|
|
|
|
|
|
|
|
/* These two cannot be caught... */
|
2017-09-29 00:37:23 +02:00
|
|
|
if (IN_SET(sig, SIGKILL, SIGSTOP))
|
2015-05-29 20:14:11 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* On Linux the first two RT signals are reserved by
|
|
|
|
* glibc, and sigaction() will return EINVAL for them. */
|
2019-10-30 14:36:05 +01:00
|
|
|
if (sigaction(sig, &sa, NULL) < 0)
|
2015-05-31 23:21:48 +02:00
|
|
|
if (errno != EINVAL && r >= 0)
|
2015-05-29 20:14:11 +02:00
|
|
|
r = -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int reset_signal_mask(void) {
|
|
|
|
sigset_t ss;
|
|
|
|
|
|
|
|
if (sigemptyset(&ss) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (sigprocmask(SIG_SETMASK, &ss, NULL) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:21:48 +02:00
|
|
|
static int sigaction_many_ap(const struct sigaction *sa, int sig, va_list ap) {
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
/* negative signal ends the list. 0 signal is skipped. */
|
2020-10-30 10:27:55 +01:00
|
|
|
for (; sig >= 0; sig = va_arg(ap, int)) {
|
2015-05-31 23:21:48 +02:00
|
|
|
|
|
|
|
if (sig == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sigaction(sig, sa, NULL) < 0) {
|
|
|
|
if (r >= 0)
|
|
|
|
r = -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-05-29 20:14:11 +02:00
|
|
|
int sigaction_many(const struct sigaction *sa, ...) {
|
|
|
|
va_list ap;
|
2015-05-31 23:21:48 +02:00
|
|
|
int r;
|
2015-05-29 20:14:11 +02:00
|
|
|
|
|
|
|
va_start(ap, sa);
|
2015-05-31 23:21:48 +02:00
|
|
|
r = sigaction_many_ap(sa, 0, ap);
|
2015-05-29 20:14:11 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ignore_signals(int sig, ...) {
|
2015-05-31 23:21:48 +02:00
|
|
|
|
2015-05-29 20:14:11 +02:00
|
|
|
static const struct sigaction sa = {
|
|
|
|
.sa_handler = SIG_IGN,
|
|
|
|
.sa_flags = SA_RESTART,
|
|
|
|
};
|
|
|
|
|
2015-05-31 23:21:48 +02:00
|
|
|
va_list ap;
|
|
|
|
int r;
|
2015-05-29 20:14:11 +02:00
|
|
|
|
|
|
|
va_start(ap, sig);
|
2015-05-31 23:21:48 +02:00
|
|
|
r = sigaction_many_ap(&sa, sig, ap);
|
2015-05-29 20:14:11 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int default_signals(int sig, ...) {
|
2015-05-31 23:21:48 +02:00
|
|
|
|
2015-05-29 20:14:11 +02:00
|
|
|
static const struct sigaction sa = {
|
|
|
|
.sa_handler = SIG_DFL,
|
|
|
|
.sa_flags = SA_RESTART,
|
|
|
|
};
|
|
|
|
|
2015-05-31 23:21:48 +02:00
|
|
|
va_list ap;
|
|
|
|
int r;
|
2015-05-29 20:14:11 +02:00
|
|
|
|
|
|
|
va_start(ap, sig);
|
2015-05-31 23:21:48 +02:00
|
|
|
r = sigaction_many_ap(&sa, sig, ap);
|
2015-05-29 20:14:11 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:21:48 +02:00
|
|
|
static int sigset_add_many_ap(sigset_t *ss, va_list ap) {
|
|
|
|
int sig, r = 0;
|
2015-05-29 20:14:11 +02:00
|
|
|
|
|
|
|
assert(ss);
|
|
|
|
|
2015-05-31 23:21:48 +02:00
|
|
|
while ((sig = va_arg(ap, int)) >= 0) {
|
|
|
|
|
|
|
|
if (sig == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sigaddset(ss, sig) < 0) {
|
|
|
|
if (r >= 0)
|
|
|
|
r = -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sigset_add_many(sigset_t *ss, ...) {
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
|
|
|
|
2015-05-29 20:14:11 +02:00
|
|
|
va_start(ap, ss);
|
2015-05-31 23:21:48 +02:00
|
|
|
r = sigset_add_many_ap(ss, ap);
|
2015-05-29 20:14:11 +02:00
|
|
|
va_end(ap);
|
2015-05-31 23:21:48 +02:00
|
|
|
|
|
|
|
return r;
|
2015-05-29 20:14:11 +02:00
|
|
|
}
|
|
|
|
|
2015-06-15 20:13:23 +02:00
|
|
|
int sigprocmask_many(int how, sigset_t *old, ...) {
|
2015-05-29 20:14:11 +02:00
|
|
|
va_list ap;
|
|
|
|
sigset_t ss;
|
2015-05-31 23:21:48 +02:00
|
|
|
int r;
|
2015-05-29 20:14:11 +02:00
|
|
|
|
2015-05-31 23:21:48 +02:00
|
|
|
if (sigemptyset(&ss) < 0)
|
|
|
|
return -errno;
|
2015-05-29 20:14:11 +02:00
|
|
|
|
2015-06-16 11:38:06 +02:00
|
|
|
va_start(ap, old);
|
2015-05-31 23:21:48 +02:00
|
|
|
r = sigset_add_many_ap(&ss, ap);
|
2015-05-29 20:14:11 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
2015-05-31 23:21:48 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2015-06-15 20:13:23 +02:00
|
|
|
if (sigprocmask(how, &ss, old) < 0)
|
2015-05-29 20:14:11 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const __signal_table[] = {
|
|
|
|
[SIGHUP] = "HUP",
|
|
|
|
[SIGINT] = "INT",
|
|
|
|
[SIGQUIT] = "QUIT",
|
|
|
|
[SIGILL] = "ILL",
|
|
|
|
[SIGTRAP] = "TRAP",
|
|
|
|
[SIGABRT] = "ABRT",
|
|
|
|
[SIGBUS] = "BUS",
|
|
|
|
[SIGFPE] = "FPE",
|
|
|
|
[SIGKILL] = "KILL",
|
|
|
|
[SIGUSR1] = "USR1",
|
|
|
|
[SIGSEGV] = "SEGV",
|
|
|
|
[SIGUSR2] = "USR2",
|
|
|
|
[SIGPIPE] = "PIPE",
|
|
|
|
[SIGALRM] = "ALRM",
|
|
|
|
[SIGTERM] = "TERM",
|
|
|
|
#ifdef SIGSTKFLT
|
|
|
|
[SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */
|
|
|
|
#endif
|
|
|
|
[SIGCHLD] = "CHLD",
|
|
|
|
[SIGCONT] = "CONT",
|
|
|
|
[SIGSTOP] = "STOP",
|
|
|
|
[SIGTSTP] = "TSTP",
|
|
|
|
[SIGTTIN] = "TTIN",
|
|
|
|
[SIGTTOU] = "TTOU",
|
|
|
|
[SIGURG] = "URG",
|
|
|
|
[SIGXCPU] = "XCPU",
|
|
|
|
[SIGXFSZ] = "XFSZ",
|
|
|
|
[SIGVTALRM] = "VTALRM",
|
|
|
|
[SIGPROF] = "PROF",
|
|
|
|
[SIGWINCH] = "WINCH",
|
|
|
|
[SIGIO] = "IO",
|
|
|
|
[SIGPWR] = "PWR",
|
|
|
|
[SIGSYS] = "SYS"
|
|
|
|
};
|
|
|
|
|
|
|
|
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
|
|
|
|
|
|
|
|
const char *signal_to_string(int signo) {
|
2017-12-14 19:02:29 +01:00
|
|
|
static thread_local char buf[STRLEN("RTMIN+") + DECIMAL_STR_MAX(int) + 1];
|
2015-05-29 20:14:11 +02:00
|
|
|
const char *name;
|
|
|
|
|
|
|
|
name = __signal_to_string(signo);
|
|
|
|
if (name)
|
|
|
|
return name;
|
|
|
|
|
|
|
|
if (signo >= SIGRTMIN && signo <= SIGRTMAX)
|
2016-01-12 15:34:20 +01:00
|
|
|
xsprintf(buf, "RTMIN+%d", signo - SIGRTMIN);
|
2015-05-29 20:14:11 +02:00
|
|
|
else
|
2016-01-12 15:34:20 +01:00
|
|
|
xsprintf(buf, "%d", signo);
|
2015-05-29 20:14:11 +02:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int signal_from_string(const char *s) {
|
2018-05-03 09:40:02 +02:00
|
|
|
const char *p;
|
|
|
|
int signo, r;
|
2015-05-29 20:14:11 +02:00
|
|
|
|
2018-05-03 09:38:57 +02:00
|
|
|
/* Check that the input is a signal number. */
|
2018-05-03 09:40:02 +02:00
|
|
|
if (safe_atoi(s, &signo) >= 0) {
|
2016-04-08 11:27:28 +02:00
|
|
|
if (SIGNAL_VALID(signo))
|
2015-05-29 20:14:11 +02:00
|
|
|
return signo;
|
2018-05-03 09:40:02 +02:00
|
|
|
else
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
2018-05-03 09:38:57 +02:00
|
|
|
/* Drop "SIG" prefix. */
|
|
|
|
if (startswith(s, "SIG"))
|
|
|
|
s += 3;
|
|
|
|
|
|
|
|
/* Check that the input is a signal name. */
|
|
|
|
signo = __signal_from_string(s);
|
|
|
|
if (signo > 0)
|
|
|
|
return signo;
|
|
|
|
|
2018-05-03 09:40:02 +02:00
|
|
|
/* Check that the input is RTMIN or
|
|
|
|
* RTMIN+n (0 <= n <= SIGRTMAX-SIGRTMIN). */
|
|
|
|
p = startswith(s, "RTMIN");
|
|
|
|
if (p) {
|
|
|
|
if (*p == '\0')
|
|
|
|
return SIGRTMIN;
|
|
|
|
if (*p != '+')
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
r = safe_atoi(p, &signo);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (signo < 0 || signo > SIGRTMAX - SIGRTMIN)
|
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
return signo + SIGRTMIN;
|
2015-05-29 20:14:11 +02:00
|
|
|
}
|
2018-05-03 09:40:02 +02:00
|
|
|
|
|
|
|
/* Check that the input is RTMAX or
|
|
|
|
* RTMAX-n (0 <= n <= SIGRTMAX-SIGRTMIN). */
|
|
|
|
p = startswith(s, "RTMAX");
|
|
|
|
if (p) {
|
|
|
|
if (*p == '\0')
|
|
|
|
return SIGRTMAX;
|
|
|
|
if (*p != '-')
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
r = safe_atoi(p, &signo);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (signo > 0 || signo < SIGRTMIN - SIGRTMAX)
|
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
return signo + SIGRTMAX;
|
|
|
|
}
|
|
|
|
|
2015-05-29 20:14:11 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-10-26 16:18:16 +01:00
|
|
|
void nop_signal_handler(int sig) {
|
|
|
|
/* nothing here */
|
|
|
|
}
|
2019-10-30 17:37:00 +01:00
|
|
|
|
|
|
|
int signal_is_blocked(int sig) {
|
|
|
|
sigset_t ss;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
|
|
|
|
if (r != 0)
|
|
|
|
return -r;
|
|
|
|
|
|
|
|
r = sigismember(&ss, sig);
|
|
|
|
if (r < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|