]> mj.ucw.cz Git - libucw.git/commitdiff
Signals: Added a module with a list of signals
authorMartin Mares <mj@ucw.cz>
Wed, 18 Jul 2012 09:12:12 +0000 (11:12 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 18 Jul 2012 09:12:12 +0000 (11:12 +0200)
ucw/Makefile
ucw/signames.c [new file with mode: 0644]
ucw/signames.h [new file with mode: 0644]

index f6cab5f148e0bfb5299df59da1f39cc4ce51f537..75b9c463a5f37d597772a612cadd0b79fff9c746 100644 (file)
@@ -34,7 +34,8 @@ LIBUCW_MODS= \
        getopt \
        strtonum \
        resource trans res-fd res-mem res-subpool res-mempool res-eltpool \
-       daemon daemon-ctrl
+       daemon daemon-ctrl \
+       signames
 
 LIBUCW_MAIN_INCLUDES= \
        lib.h log.h threads.h time.h \
@@ -61,7 +62,8 @@ LIBUCW_MAIN_INCLUDES= \
        partmap.h \
        strtonum.h \
        resource.h trans.h \
-       daemon.h
+       daemon.h \
+       signames.h
 
 ifdef CONFIG_UCW_THREADS
 # Some modules require threading
diff --git a/ucw/signames.c b/ucw/signames.c
new file mode 100644 (file)
index 0000000..84276fd
--- /dev/null
@@ -0,0 +1,170 @@
+/*
+ *     A List of Signal Names
+ *
+ *     (c) 2012 Martin Mares <mj@ucw.cz>
+ */
+
+#include <ucw/lib.h>
+#include <ucw/signames.h>
+
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+
+struct sig_name {
+  const char name[11];
+  byte number;
+};
+
+#define S(sig) { #sig, sig }
+
+static const struct sig_name sig_names[] = {
+#ifdef SIGABRT
+       S(SIGABRT),
+#endif
+#ifdef SIGALRM
+       S(SIGALRM),
+#endif
+#ifdef SIGBUS
+       S(SIGBUS),
+#endif
+#ifdef SIGCHLD
+       S(SIGCHLD),
+#endif
+#ifdef SIGCONT
+       S(SIGCONT),
+#endif
+#ifdef SIGFPE
+       S(SIGFPE),
+#endif
+#ifdef SIGHUP
+       S(SIGHUP),
+#endif
+#ifdef SIGILL
+       S(SIGILL),
+#endif
+#ifdef SIGINT
+       S(SIGINT),
+#endif
+#ifdef SIGIO
+       S(SIGIO),
+#endif
+#ifdef SIGIOT
+       S(SIGIOT),
+#endif
+#ifdef SIGKILL
+       S(SIGKILL),
+#endif
+#ifdef SIGPIPE
+       S(SIGPIPE),
+#endif
+#ifdef SIGPOLL
+       S(SIGPOLL),
+#endif
+#ifdef SIGPROF
+       S(SIGPROF),
+#endif
+#ifdef SIGPWR
+       S(SIGPWR),
+#endif
+#ifdef SIGQUIT
+       S(SIGQUIT),
+#endif
+#ifdef SIGSEGV
+       S(SIGSEGV),
+#endif
+#ifdef SIGSTKFLT
+       S(SIGSTKFLT),
+#endif
+#ifdef SIGSTOP
+       S(SIGSTOP),
+#endif
+#ifdef SIGSYS
+       S(SIGSYS),
+#endif
+#ifdef SIGTERM
+       S(SIGTERM),
+#endif
+#ifdef SIGTRAP
+       S(SIGTRAP),
+#endif
+#ifdef SIGTSTP
+       S(SIGTSTP),
+#endif
+#ifdef SIGTTIN
+       S(SIGTTIN),
+#endif
+#ifdef SIGTTOU
+       S(SIGTTOU),
+#endif
+#ifdef SIGURG
+       S(SIGURG),
+#endif
+#ifdef SIGUSR1
+       S(SIGUSR1),
+#endif
+#ifdef SIGUSR2
+       S(SIGUSR2),
+#endif
+#ifdef SIGVTALRM
+       S(SIGVTALRM),
+#endif
+#ifdef SIGWINCH
+       S(SIGWINCH),
+#endif
+#ifdef SIGXCPU
+       S(SIGXCPU),
+#endif
+#ifdef SIGXFSZ
+       S(SIGXFSZ),
+#endif
+};
+
+int
+sig_name_to_number(const char *name)
+{
+  for (uns i=0; i < ARRAY_SIZE(sig_names); i++)
+    if (!strcmp(sig_names[i].name, name))
+      return sig_names[i].number;
+  return -1;
+}
+
+const char *
+sig_number_to_name(int number)
+{
+  for (uns i=0; i < ARRAY_SIZE(sig_names); i++)
+    if (sig_names[i].number == number)
+      return sig_names[i].name;
+  return NULL;
+}
+
+#ifdef TEST
+
+#include <stdio.h>
+
+int main(void)
+{
+  char c[256];
+  while (fgets(c, sizeof(c), stdin))
+    {
+      char *e = strchr(c, '\n');
+      if (e)
+       *e = 0;
+      if (c[0] == '#')
+       {
+         const char *name = sig_number_to_name(atoi(c+1));
+         puts(name ? : "?");
+       }
+      else
+       {
+         int num = sig_name_to_number(c);
+         if (num < 0)
+           puts("?");
+         else
+           printf("%d\n", num);
+       }
+    }
+  return 0;
+}
+
+#endif
diff --git a/ucw/signames.h b/ucw/signames.h
new file mode 100644 (file)
index 0000000..d7b9cac
--- /dev/null
@@ -0,0 +1,14 @@
+/*
+ *     A List of Signal Names
+ *
+ *     (c) 2012 Martin Mares <mj@ucw.cz>
+ */
+
+#ifndef _UCW_SIGNAMES_H
+#define _UCW_SIGNAMES_H
+
+int sig_name_to_number(const char *name);
+
+const char *sig_number_to_name(int number);
+
+#endif