Start 1.33.0 development cycle
[oweals/busybox.git] / libbb / u_signal_names.c
index 7a0f75d6f1380c53df5572cb91b1f76dbb7ecbc3..f7d598c7ad1482986d7e898f1e238e5c4d54fa99 100644 (file)
@@ -4,15 +4,33 @@
  *
  * Copyright 2006 Rob Landley <rob@landley.net>
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
+//config:config FEATURE_RTMINMAX
+//config:      bool "Support RTMIN[+n] and RTMAX[-n] signal names"
+//config:      default y
+//config:      help
+//config:      Support RTMIN[+n] and RTMAX[-n] signal names
+//config:      in kill, killall etc. This costs ~250 bytes.
+//config:
+//config:config FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
+//config:      bool "Use the definitions of SIGRTMIN/SIGRTMAX provided by libc"
+//config:      default y
+//config:      depends on FEATURE_RTMINMAX
+//config:      help
+//config:      Some C libraries reserve a few real-time signals for internal
+//config:      use, and adjust the values of SIGRTMIN/SIGRTMAX seen by
+//config:      applications accordingly. Saying yes here means that a signal
+//config:      name RTMIN+n will be interpreted according to the libc definition
+//config:      of SIGRTMIN, and not the raw definition provided by the kernel.
+//config:      This behavior matches "kill -l RTMIN+n" from bash.
 
 #include "libbb.h"
 
 /* Believe it or not, but some arches have more than 32 SIGs!
  * HPPA: SIGSTKFLT == 36. */
 
-static const char signals[][7] = {
+static const char signals[][7] ALIGN1 = {
        // SUSv3 says kill must support these, and specifies the numerical values,
        // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
        // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
@@ -117,36 +135,94 @@ static const char signals[][7] = {
 #ifdef SIGSYS
        [SIGSYS   ] = "SYS",
 #endif
+#if ENABLE_FEATURE_RTMINMAX && !ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
+# ifdef __SIGRTMIN
+       [__SIGRTMIN] = "RTMIN",
+# endif
+// This makes array about x2 bigger.
+// More compact approach is to special-case SIGRTMAX in print_signames()
+//# ifdef __SIGRTMAX
+//     [__SIGRTMAX] = "RTMAX",
+//# endif
+#endif
 };
 
 // Convert signal name to number.
 
-int get_signum(const char *name)
+int FAST_FUNC get_signum(const char *name)
 {
-       int i;
+       unsigned i;
 
+       /* bb_strtou returns UINT_MAX on error. NSIG is smaller
+        * than UINT_MAX on any sane Unix. Hence no need
+        * to check errno after bb_strtou().
+        */
        i = bb_strtou(name, NULL, 10);
-       if (!errno)
+       if (i < NSIG) /* for shells, we allow 0 too */
                return i;
        if (strncasecmp(name, "SIG", 3) == 0)
                name += 3;
-       for (i = 0; (size_t)i < ARRAY_SIZE(signals); i++)
+       for (i = 0; i < ARRAY_SIZE(signals); i++)
                if (strcasecmp(name, signals[i]) == 0)
                        return i;
 
-#if ENABLE_DESKTOP && (defined(SIGIOT) || defined(SIGIO))
+#if ENABLE_DESKTOP
+# if defined(SIGIOT) || defined(SIGIO)
        /* SIGIO[T] are aliased to other names,
         * thus cannot be stored in the signals[] array.
         * Need special code to recognize them */
        if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
-#ifdef SIGIO
+#  ifdef SIGIO
                if (!name[2])
                        return SIGIO;
-#endif
-#ifdef SIGIOT
+#  endif
+#  ifdef SIGIOT
                if ((name[2] | 0x20) == 't' && !name[3])
                        return SIGIOT;
+#  endif
+       }
+# endif
 #endif
+
+#if ENABLE_FEATURE_RTMINMAX && defined(SIGRTMIN) && defined(SIGRTMAX)
+       {
+# if ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
+               /* Use the libc provided values. */
+               unsigned sigrtmin = SIGRTMIN;
+               unsigned sigrtmax = SIGRTMAX;
+# else
+       /* Use the "raw" SIGRTMIN/MAX. Underscored names, if exist, provide
+        * them. If they don't exist, fall back to non-underscored ones: */
+#  if !defined(__SIGRTMIN)
+#   define __SIGRTMIN SIGRTMIN
+#  endif
+#  if !defined(__SIGRTMAX)
+#   define __SIGRTMAX SIGRTMAX
+#  endif
+
+#  define sigrtmin __SIGRTMIN
+#  define sigrtmax __SIGRTMAX
+# endif
+               if (strncasecmp(name, "RTMIN", 5) == 0) {
+                       if (!name[5])
+                               return sigrtmin;
+                       if (name[5] == '+') {
+                               i = bb_strtou(name + 6, NULL, 10);
+                               if (i <= sigrtmax - sigrtmin)
+                                       return sigrtmin + i;
+                       }
+               }
+               else if (strncasecmp(name, "RTMAX", 5) == 0) {
+                       if (!name[5])
+                               return sigrtmax;
+                       if (name[5] == '-') {
+                               i = bb_strtou(name + 6, NULL, 10);
+                               if (i <= sigrtmax - sigrtmin)
+                                       return sigrtmax - i;
+                       }
+               }
+# undef sigrtmin
+# undef sigrtmax
        }
 #endif
 
@@ -155,7 +231,7 @@ int get_signum(const char *name)
 
 // Convert signal number to name
 
-const char *get_signame(int number)
+const char* FAST_FUNC get_signame(int number)
 {
        if ((unsigned)number < ARRAY_SIZE(signals)) {
                if (signals[number][0]) /* if it's not an empty str */
@@ -168,13 +244,26 @@ const char *get_signame(int number)
 
 // Print the whole signal list
 
-void print_signames(void)
+void FAST_FUNC print_signames(void)
 {
-       int signo;
+       unsigned signo;
 
-       for (signo = 1; (size_t)signo < ARRAY_SIZE(signals); signo++) {
+       for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
                const char *name = signals[signo];
                if (name[0])
-                       puts(name);
+                       printf("%2u) %s\n", signo, name);
        }
+#if ENABLE_FEATURE_RTMINMAX
+# if ENABLE_FEATURE_RTMINMAX_USE_LIBC_DEFINITIONS
+#  if defined(SIGRTMIN) && defined(SIGRTMAX)
+       printf("%2u) %s\n", SIGRTMIN, "RTMIN");
+       printf("%2u) %s\n", SIGRTMAX, "RTMAX");
+#  endif
+# else
+// __SIGRTMIN is included in signals[] array.
+#  ifdef __SIGRTMAX
+       printf("%2u) %s\n", __SIGRTMAX, "RTMAX");
+#  endif
+# endif
+#endif
 }