1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "signal-util.h"
25 int reset_all_signal_handlers(void) {
28 for (sig = 1; sig < _NSIG; sig++) {
29 static const struct sigaction sa = {
30 .sa_handler = SIG_DFL,
31 .sa_flags = SA_RESTART,
34 /* These two cannot be caught... */
35 if (sig == SIGKILL || sig == SIGSTOP)
38 /* On Linux the first two RT signals are reserved by
39 * glibc, and sigaction() will return EINVAL for them. */
40 if ((sigaction(sig, &sa, NULL) < 0))
41 if (errno != EINVAL && r == 0)
48 int reset_signal_mask(void) {
51 if (sigemptyset(&ss) < 0)
54 if (sigprocmask(SIG_SETMASK, &ss, NULL) < 0)
60 int sigaction_many(const struct sigaction *sa, ...) {
65 while ((sig = va_arg(ap, int)) > 0)
66 if (sigaction(sig, sa, NULL) < 0)
73 int ignore_signals(int sig, ...) {
74 static const struct sigaction sa = {
75 .sa_handler = SIG_IGN,
76 .sa_flags = SA_RESTART,
81 if (sigaction(sig, &sa, NULL) < 0)
85 while ((sig = va_arg(ap, int)) > 0)
86 if (sigaction(sig, &sa, NULL) < 0)
93 int default_signals(int sig, ...) {
94 static const struct sigaction sa = {
95 .sa_handler = SIG_DFL,
96 .sa_flags = SA_RESTART,
101 if (sigaction(sig, &sa, NULL) < 0)
105 while ((sig = va_arg(ap, int)) > 0)
106 if (sigaction(sig, &sa, NULL) < 0)
113 void sigset_add_many(sigset_t *ss, ...) {
120 while ((sig = va_arg(ap, int)) > 0)
121 assert_se(sigaddset(ss, sig) == 0);
125 int sigprocmask_many(int how, ...) {
130 assert_se(sigemptyset(&ss) == 0);
133 while ((sig = va_arg(ap, int)) > 0)
134 assert_se(sigaddset(&ss, sig) == 0);
137 if (sigprocmask(how, &ss, NULL) < 0)
143 static const char *const __signal_table[] = {
160 [SIGSTKFLT] = "STKFLT", /* Linux on SPARC doesn't know SIGSTKFLT */
171 [SIGVTALRM] = "VTALRM",
173 [SIGWINCH] = "WINCH",
179 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
181 const char *signal_to_string(int signo) {
182 static thread_local char buf[sizeof("RTMIN+")-1 + DECIMAL_STR_MAX(int) + 1];
185 name = __signal_to_string(signo);
189 if (signo >= SIGRTMIN && signo <= SIGRTMAX)
190 snprintf(buf, sizeof(buf), "RTMIN+%d", signo - SIGRTMIN);
192 snprintf(buf, sizeof(buf), "%d", signo);
197 int signal_from_string(const char *s) {
202 signo = __signal_from_string(s);
206 if (startswith(s, "RTMIN+")) {
210 if (safe_atou(s, &u) >= 0) {
211 signo = (int) u + offset;
212 if (signo > 0 && signo < _NSIG)
218 int signal_from_string_try_harder(const char *s) {
222 signo = signal_from_string(s);
224 if (startswith(s, "SIG"))
225 return signal_from_string(s+3);