[systemd-commits] 2 commits - src/kmsg-syslogd.c src/manager.c src/util.c src/util.h

Michal Schmidt michich at kemper.freedesktop.org
Fri Sep 23 01:32:43 PDT 2011


 src/kmsg-syslogd.c |    2 +-
 src/manager.c      |   11 ++++++-----
 src/util.c         |   41 +++++++++++++++++++++++++++++++++++++++--
 src/util.h         |    8 +++++---
 4 files changed, 51 insertions(+), 11 deletions(-)

New commits:
commit 4e240ab0c93f4d454b065eef7074132969ac6f16
Author: Michal Schmidt <mschmidt at redhat.com>
Date:   Fri Sep 23 09:39:40 2011 +0200

    util: improve signal_to_string, signal_from_string
    
    signal_to_string:
     Produce names for SIGRTMIN+n.
     Never give an "n/a" result. In the worst case give the number itself as
     a string.
    
    signal_from_string:
     Parse "RTMIN+n".
     Parse any valid signal number.

diff --git a/src/kmsg-syslogd.c b/src/kmsg-syslogd.c
index 83c2047..0901a0e 100644
--- a/src/kmsg-syslogd.c
+++ b/src/kmsg-syslogd.c
@@ -379,7 +379,7 @@ static int process_event(Server *s, struct epoll_event *ev) {
                         return -errno;
                 }
 
-                log_debug("Received SIG%s", strna(signal_to_string(sfsi.ssi_signo)));
+                log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
                 return 0;
 
         } else {
diff --git a/src/manager.c b/src/manager.c
index cf308ac..7ce1b3b 100644
--- a/src/manager.c
+++ b/src/manager.c
@@ -2111,11 +2111,11 @@ static int manager_process_signal_fd(Manager *m) {
                         get_process_name(sfsi.ssi_pid, &p);
 
                         log_debug("Received SIG%s from PID %lu (%s).",
-                                  strna(signal_to_string(sfsi.ssi_signo)),
+                                  signal_to_string(sfsi.ssi_signo),
                                   (unsigned long) sfsi.ssi_pid, strna(p));
                         free(p);
                 } else
-                        log_debug("Received SIG%s.", strna(signal_to_string(sfsi.ssi_signo)));
+                        log_debug("Received SIG%s.", signal_to_string(sfsi.ssi_signo));
 
                 switch (sfsi.ssi_signo) {
 
@@ -2283,7 +2283,7 @@ static int manager_process_signal_fd(Manager *m) {
                                 break;
 
                         default:
-                                log_warning("Got unhandled signal <%s>.", strna(signal_to_string(sfsi.ssi_signo)));
+                                log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
                         }
                 }
                 }
diff --git a/src/util.c b/src/util.c
index 2eb6ba7..36c8938 100644
--- a/src/util.c
+++ b/src/util.c
@@ -5814,7 +5814,7 @@ static const char* const ip_tos_table[] = {
 
 DEFINE_STRING_TABLE_LOOKUP(ip_tos, int);
 
-static const char *const signal_table[] = {
+static const char *const __signal_table[] = {
         [SIGHUP] = "HUP",
         [SIGINT] = "INT",
         [SIGQUIT] = "QUIT",
@@ -5850,7 +5850,44 @@ static const char *const signal_table[] = {
         [SIGSYS] = "SYS"
 };
 
-DEFINE_STRING_TABLE_LOOKUP(signal, int);
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP(__signal, int);
+
+const char *signal_to_string(int signo) {
+        static __thread char buf[12];
+        const char *name;
+
+        name = __signal_to_string(signo);
+        if (name)
+                return name;
+
+        if (signo >= SIGRTMIN && signo <= SIGRTMAX)
+                snprintf(buf, sizeof(buf) - 1, "RTMIN+%d", signo - SIGRTMIN);
+        else
+                snprintf(buf, sizeof(buf) - 1, "%d", signo);
+        char_array_0(buf);
+        return buf;
+}
+
+int signal_from_string(const char *s) {
+        int signo;
+        int offset = 0;
+        unsigned u;
+
+        signo =__signal_from_string(s);
+        if (signo > 0)
+                return signo;
+
+        if (startswith(s, "RTMIN+")) {
+                s += 6;
+                offset = SIGRTMIN;
+        }
+        if (safe_atou(s, &u) >= 0) {
+                signo = (int) u + offset;
+                if (signo > 0 && signo < _NSIG)
+                        return signo;
+        }
+        return -1;
+}
 
 bool kexec_loaded(void) {
        bool loaded = false;
diff --git a/src/util.h b/src/util.h
index 3e1f46d..e254bc7 100644
--- a/src/util.h
+++ b/src/util.h
@@ -288,13 +288,13 @@ int make_null_stdio(void);
 
 unsigned long long random_ull(void);
 
-#define DEFINE_STRING_TABLE_LOOKUP(name,type)                           \
-        const char *name##_to_string(type i) {                          \
+#define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope)                   \
+        scope const char *name##_to_string(type i) {                    \
                 if (i < 0 || i >= (type) ELEMENTSOF(name##_table))      \
                         return NULL;                                    \
                 return name##_table[i];                                 \
         }                                                               \
-        type name##_from_string(const char *s) {                        \
+        scope type name##_from_string(const char *s) {                  \
                 type i;                                                 \
                 unsigned u = 0;                                         \
                 assert(s);                                              \
@@ -309,6 +309,8 @@ unsigned long long random_ull(void);
         }                                                               \
         struct __useless_struct_to_allow_trailing_semicolon__
 
+#define DEFINE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,)
+#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
 
 int fd_nonblock(int fd, bool nonblock);
 int fd_cloexec(int fd, bool cloexec);

commit 764e9b5f073e1d5216de150045ec85475835fe3c
Author: Michal Schmidt <mschmidt at redhat.com>
Date:   Fri Sep 23 09:37:45 2011 +0200

    manager: fix job mode for SIGRTMIN+1, +2
    
    The test did not work as intended and always resulted in JOB_REPLACE.

diff --git a/src/manager.c b/src/manager.c
index 6311c10..cf308ac 100644
--- a/src/manager.c
+++ b/src/manager.c
@@ -2233,8 +2233,9 @@ static int manager_process_signal_fd(Manager *m) {
 
                         if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
                             (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
-                                manager_start_target(m, target_table[sfsi.ssi_signo - SIGRTMIN],
-                                                     (sfsi.ssi_signo == 1 || sfsi.ssi_signo == 2) ? JOB_ISOLATE : JOB_REPLACE);
+                                int idx = (int) sfsi.ssi_signo - SIGRTMIN;
+                                manager_start_target(m, target_table[idx],
+                                                     (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE);
                                 break;
                         }
 



More information about the systemd-commits mailing list