chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.git] / src / basic / signal-util.c
index 4bb2177d67129d2e74b3f220f5186465d6551fea..038b191b493b91b3771cb9a6873bddf811287d94 100644 (file)
@@ -1,26 +1,15 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+/* SPDX-License-Identifier: LGPL-2.1+ */
 
-/***
-  This file is part of systemd.
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
 
-  Copyright 2015 Lennart Poettering
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include "util.h"
+#include "macro.h"
+#include "parse-util.h"
 #include "signal-util.h"
+#include "stdio-util.h"
+#include "string-table.h"
+#include "string-util.h"
 
 int reset_all_signal_handlers(void) {
         static const struct sigaction sa = {
@@ -32,7 +21,7 @@ int reset_all_signal_handlers(void) {
         for (sig = 1; sig < _NSIG; sig++) {
 
                 /* These two cannot be caught... */
-                if (sig == SIGKILL || sig == SIGSTOP)
+                if (IN_SET(sig, SIGKILL, SIGSTOP))
                         continue;
 
                 /* On Linux the first two RT signals are reserved by
@@ -84,6 +73,7 @@ static int sigaction_many_ap(const struct sigaction *sa, int sig, va_list ap) {
         return r;
 }
 
+#if 0 /// UNNEEDED by elogind
 int sigaction_many(const struct sigaction *sa, ...) {
         va_list ap;
         int r;
@@ -94,6 +84,7 @@ int sigaction_many(const struct sigaction *sa, ...) {
 
         return r;
 }
+#endif // 0
 
 int ignore_signals(int sig, ...) {
 
@@ -112,8 +103,6 @@ int ignore_signals(int sig, ...) {
         return r;
 }
 
-/// UNNEEDED by elogind
-#if 0
 int default_signals(int sig, ...) {
 
         static const struct sigaction sa = {
@@ -130,7 +119,6 @@ int default_signals(int sig, ...) {
 
         return r;
 }
-#endif // 0
 
 static int sigset_add_many_ap(sigset_t *ss, va_list ap) {
         int sig, r = 0;
@@ -222,7 +210,7 @@ static const char *const __signal_table[] = {
 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
 
 const char *signal_to_string(int signo) {
-        static thread_local char buf[sizeof("RTMIN+")-1 + DECIMAL_STR_MAX(int) + 1];
+        static thread_local char buf[STRLEN("RTMIN+") + DECIMAL_STR_MAX(int) + 1];
         const char *name;
 
         name = __signal_to_string(signo);
@@ -230,42 +218,78 @@ const char *signal_to_string(int signo) {
                 return name;
 
         if (signo >= SIGRTMIN && signo <= SIGRTMAX)
-                snprintf(buf, sizeof(buf), "RTMIN+%d", signo - SIGRTMIN);
+                xsprintf(buf, "RTMIN+%d", signo - SIGRTMIN);
         else
-                snprintf(buf, sizeof(buf), "%d", signo);
+                xsprintf(buf, "%d", signo);
 
         return buf;
 }
 
 int signal_from_string(const char *s) {
-        int signo;
-        int offset = 0;
-        unsigned u;
+        const char *p;
+        int signo, r;
 
+        /* Check that the input is a signal number. */
+        if (safe_atoi(s, &signo) >= 0) {
+                if (SIGNAL_VALID(signo))
+                        return signo;
+                else
+                        return -ERANGE;
+        }
+
+        /* 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;
 
-        if (startswith(s, "RTMIN+")) {
-                s += 6;
-                offset = SIGRTMIN;
+        /* 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;
         }
-        if (safe_atou(s, &u) >= 0) {
-                signo = (int) u + offset;
-                if (signo > 0 && signo < _NSIG)
-                        return signo;
+
+        /* 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;
         }
+
         return -EINVAL;
 }
 
-int signal_from_string_try_harder(const char *s) {
-        int signo;
-        assert(s);
 
-        signo = signal_from_string(s);
-        if (signo <= 0)
-                if (startswith(s, "SIG"))
-                        return signal_from_string(s+3);
-
-        return signo;
+#if 0 /// UNNEEDED by elogind
+void nop_signal_handler(int sig) {
+        /* nothing here */
 }
+#endif // 0