u_signal_names.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Signal name/number conversion routines.
  4. *
  5. * Copyright 2006 Rob Landley <rob@landley.net>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config FEATURE_RTMINMAX
  10. //config: bool "Support RTMIN[+n] and RTMAX[-n] signal names"
  11. //config: default y
  12. //config: help
  13. //config: Support RTMIN[+n] and RTMAX[-n] signal names
  14. //config: in kill, killall etc. This costs ~250 bytes.
  15. #include "libbb.h"
  16. /* Believe it or not, but some arches have more than 32 SIGs!
  17. * HPPA: SIGSTKFLT == 36. */
  18. static const char signals[][7] ALIGN1 = {
  19. // SUSv3 says kill must support these, and specifies the numerical values,
  20. // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
  21. // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
  22. // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
  23. // And Posix adds the following:
  24. // {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
  25. // {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
  26. // {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
  27. // {SIGTTOU, "TTOU"}
  28. [0] = "EXIT",
  29. #ifdef SIGHUP
  30. [SIGHUP ] = "HUP",
  31. #endif
  32. #ifdef SIGINT
  33. [SIGINT ] = "INT",
  34. #endif
  35. #ifdef SIGQUIT
  36. [SIGQUIT ] = "QUIT",
  37. #endif
  38. #ifdef SIGILL
  39. [SIGILL ] = "ILL",
  40. #endif
  41. #ifdef SIGTRAP
  42. [SIGTRAP ] = "TRAP",
  43. #endif
  44. #ifdef SIGABRT
  45. [SIGABRT ] = "ABRT",
  46. #endif
  47. #ifdef SIGBUS
  48. [SIGBUS ] = "BUS",
  49. #endif
  50. #ifdef SIGFPE
  51. [SIGFPE ] = "FPE",
  52. #endif
  53. #ifdef SIGKILL
  54. [SIGKILL ] = "KILL",
  55. #endif
  56. #ifdef SIGUSR1
  57. [SIGUSR1 ] = "USR1",
  58. #endif
  59. #ifdef SIGSEGV
  60. [SIGSEGV ] = "SEGV",
  61. #endif
  62. #ifdef SIGUSR2
  63. [SIGUSR2 ] = "USR2",
  64. #endif
  65. #ifdef SIGPIPE
  66. [SIGPIPE ] = "PIPE",
  67. #endif
  68. #ifdef SIGALRM
  69. [SIGALRM ] = "ALRM",
  70. #endif
  71. #ifdef SIGTERM
  72. [SIGTERM ] = "TERM",
  73. #endif
  74. #ifdef SIGSTKFLT
  75. [SIGSTKFLT] = "STKFLT",
  76. #endif
  77. #ifdef SIGCHLD
  78. [SIGCHLD ] = "CHLD",
  79. #endif
  80. #ifdef SIGCONT
  81. [SIGCONT ] = "CONT",
  82. #endif
  83. #ifdef SIGSTOP
  84. [SIGSTOP ] = "STOP",
  85. #endif
  86. #ifdef SIGTSTP
  87. [SIGTSTP ] = "TSTP",
  88. #endif
  89. #ifdef SIGTTIN
  90. [SIGTTIN ] = "TTIN",
  91. #endif
  92. #ifdef SIGTTOU
  93. [SIGTTOU ] = "TTOU",
  94. #endif
  95. #ifdef SIGURG
  96. [SIGURG ] = "URG",
  97. #endif
  98. #ifdef SIGXCPU
  99. [SIGXCPU ] = "XCPU",
  100. #endif
  101. #ifdef SIGXFSZ
  102. [SIGXFSZ ] = "XFSZ",
  103. #endif
  104. #ifdef SIGVTALRM
  105. [SIGVTALRM] = "VTALRM",
  106. #endif
  107. #ifdef SIGPROF
  108. [SIGPROF ] = "PROF",
  109. #endif
  110. #ifdef SIGWINCH
  111. [SIGWINCH ] = "WINCH",
  112. #endif
  113. #ifdef SIGPOLL
  114. [SIGPOLL ] = "POLL",
  115. #endif
  116. #ifdef SIGPWR
  117. [SIGPWR ] = "PWR",
  118. #endif
  119. #ifdef SIGSYS
  120. [SIGSYS ] = "SYS",
  121. #endif
  122. #if ENABLE_FEATURE_RTMINMAX
  123. # ifdef __SIGRTMIN
  124. [__SIGRTMIN] = "RTMIN",
  125. # endif
  126. // This makes array about x2 bigger.
  127. // More compact approach is to special-case SIGRTMAX in print_signames()
  128. //# ifdef __SIGRTMAX
  129. // [__SIGRTMAX] = "RTMAX",
  130. //# endif
  131. #endif
  132. };
  133. // Convert signal name to number.
  134. int FAST_FUNC get_signum(const char *name)
  135. {
  136. unsigned i;
  137. i = bb_strtou(name, NULL, 10);
  138. if (!errno && i < NSIG) /* for shells, we allow 0 too */
  139. return i;
  140. if (strncasecmp(name, "SIG", 3) == 0)
  141. name += 3;
  142. for (i = 0; i < ARRAY_SIZE(signals); i++)
  143. if (strcasecmp(name, signals[i]) == 0)
  144. return i;
  145. #if ENABLE_DESKTOP
  146. # if defined(SIGIOT) || defined(SIGIO)
  147. /* SIGIO[T] are aliased to other names,
  148. * thus cannot be stored in the signals[] array.
  149. * Need special code to recognize them */
  150. if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
  151. # ifdef SIGIO
  152. if (!name[2])
  153. return SIGIO;
  154. # endif
  155. # ifdef SIGIOT
  156. if ((name[2] | 0x20) == 't' && !name[3])
  157. return SIGIOT;
  158. # endif
  159. }
  160. # endif
  161. #endif
  162. #if ENABLE_FEATURE_RTMINMAX
  163. # if defined(SIGRTMIN) && defined(SIGRTMAX)
  164. /* libc may use some rt sigs for pthreads and therefore "remap" SIGRTMIN/MAX,
  165. * but we want to use "raw" SIGRTMIN/MAX. Underscored names, if exist, provide
  166. * them. If they don't exist, fall back to non-underscored ones: */
  167. # if !defined(__SIGRTMIN)
  168. # define __SIGRTMIN SIGRTMIN
  169. # endif
  170. # if !defined(__SIGRTMAX)
  171. # define __SIGRTMAX SIGRTMAX
  172. # endif
  173. if (strncasecmp(name, "RTMIN", 5) == 0) {
  174. if (!name[5])
  175. return __SIGRTMIN;
  176. if (name[5] == '+') {
  177. i = bb_strtou(name + 6, NULL, 10);
  178. if (!errno && i <= __SIGRTMAX - __SIGRTMIN)
  179. return __SIGRTMIN + i;
  180. }
  181. }
  182. else if (strncasecmp(name, "RTMAX", 5) == 0) {
  183. if (!name[5])
  184. return __SIGRTMAX;
  185. if (name[5] == '-') {
  186. i = bb_strtou(name + 6, NULL, 10);
  187. if (!errno && i <= __SIGRTMAX - __SIGRTMIN)
  188. return __SIGRTMAX - i;
  189. }
  190. }
  191. # endif
  192. #endif
  193. return -1;
  194. }
  195. // Convert signal number to name
  196. const char* FAST_FUNC get_signame(int number)
  197. {
  198. if ((unsigned)number < ARRAY_SIZE(signals)) {
  199. if (signals[number][0]) /* if it's not an empty str */
  200. return signals[number];
  201. }
  202. return itoa(number);
  203. }
  204. // Print the whole signal list
  205. void FAST_FUNC print_signames(void)
  206. {
  207. unsigned signo;
  208. for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
  209. const char *name = signals[signo];
  210. if (name[0])
  211. printf("%2u) %s\n", signo, name);
  212. }
  213. #if ENABLE_FEATURE_RTMINMAX
  214. # ifdef __SIGRTMAX
  215. printf("%2u) %s\n", __SIGRTMAX, "RTMAX");
  216. # endif
  217. #endif
  218. }