u_signal_names.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 tarball for details.
  8. */
  9. #include "libbb.h"
  10. /* Believe it or not, but some arches have more than 32 SIGs!
  11. * HPPA: SIGSTKFLT == 36. */
  12. static const char signals[][7] = {
  13. // SUSv3 says kill must support these, and specifies the numerical values,
  14. // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
  15. // {0, "EXIT"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"},
  16. // {6, "ABRT"}, {9, "KILL"}, {14, "ALRM"}, {15, "TERM"}
  17. // And Posix adds the following:
  18. // {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
  19. // {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
  20. // {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
  21. // {SIGTTOU, "TTOU"}
  22. [0] = "EXIT",
  23. #ifdef SIGHUP
  24. [SIGHUP ] = "HUP",
  25. #endif
  26. #ifdef SIGINT
  27. [SIGINT ] = "INT",
  28. #endif
  29. #ifdef SIGQUIT
  30. [SIGQUIT ] = "QUIT",
  31. #endif
  32. #ifdef SIGILL
  33. [SIGILL ] = "ILL",
  34. #endif
  35. #ifdef SIGTRAP
  36. [SIGTRAP ] = "TRAP",
  37. #endif
  38. #ifdef SIGABRT
  39. [SIGABRT ] = "ABRT",
  40. #endif
  41. #ifdef SIGBUS
  42. [SIGBUS ] = "BUS",
  43. #endif
  44. #ifdef SIGFPE
  45. [SIGFPE ] = "FPE",
  46. #endif
  47. #ifdef SIGKILL
  48. [SIGKILL ] = "KILL",
  49. #endif
  50. #ifdef SIGUSR1
  51. [SIGUSR1 ] = "USR1",
  52. #endif
  53. #ifdef SIGSEGV
  54. [SIGSEGV ] = "SEGV",
  55. #endif
  56. #ifdef SIGUSR2
  57. [SIGUSR2 ] = "USR2",
  58. #endif
  59. #ifdef SIGPIPE
  60. [SIGPIPE ] = "PIPE",
  61. #endif
  62. #ifdef SIGALRM
  63. [SIGALRM ] = "ALRM",
  64. #endif
  65. #ifdef SIGTERM
  66. [SIGTERM ] = "TERM",
  67. #endif
  68. #ifdef SIGSTKFLT
  69. [SIGSTKFLT] = "STKFLT",
  70. #endif
  71. #ifdef SIGCHLD
  72. [SIGCHLD ] = "CHLD",
  73. #endif
  74. #ifdef SIGCONT
  75. [SIGCONT ] = "CONT",
  76. #endif
  77. #ifdef SIGSTOP
  78. [SIGSTOP ] = "STOP",
  79. #endif
  80. #ifdef SIGTSTP
  81. [SIGTSTP ] = "TSTP",
  82. #endif
  83. #ifdef SIGTTIN
  84. [SIGTTIN ] = "TTIN",
  85. #endif
  86. #ifdef SIGTTOU
  87. [SIGTTOU ] = "TTOU",
  88. #endif
  89. #ifdef SIGURG
  90. [SIGURG ] = "URG",
  91. #endif
  92. #ifdef SIGXCPU
  93. [SIGXCPU ] = "XCPU",
  94. #endif
  95. #ifdef SIGXFSZ
  96. [SIGXFSZ ] = "XFSZ",
  97. #endif
  98. #ifdef SIGVTALRM
  99. [SIGVTALRM] = "VTALRM",
  100. #endif
  101. #ifdef SIGPROF
  102. [SIGPROF ] = "PROF",
  103. #endif
  104. #ifdef SIGWINCH
  105. [SIGWINCH ] = "WINCH",
  106. #endif
  107. #ifdef SIGPOLL
  108. [SIGPOLL ] = "POLL",
  109. #endif
  110. #ifdef SIGPWR
  111. [SIGPWR ] = "PWR",
  112. #endif
  113. #ifdef SIGSYS
  114. [SIGSYS ] = "SYS",
  115. #endif
  116. };
  117. // Convert signal name to number.
  118. int get_signum(const char *name)
  119. {
  120. int i;
  121. i = bb_strtou(name, NULL, 10);
  122. if (!errno)
  123. return i;
  124. if (strncasecmp(name, "SIG", 3) == 0)
  125. name += 3;
  126. for (i = 0; i < ARRAY_SIZE(signals); i++)
  127. if (strcasecmp(name, signals[i]) == 0)
  128. return i;
  129. #if ENABLE_DESKTOP && (defined(SIGIOT) || defined(SIGIO))
  130. /* SIGIO[T] are aliased to other names,
  131. * thus cannot be stored in the signals[] array.
  132. * Need special code to recognize them */
  133. if ((name[0] | 0x20) == 'i' && (name[1] | 0x20) == 'o') {
  134. #ifdef SIGIO
  135. if (!name[2])
  136. return SIGIO;
  137. #endif
  138. #ifdef SIGIOT
  139. if ((name[2] | 0x20) == 't' && !name[3])
  140. return SIGIOT;
  141. #endif
  142. }
  143. #endif
  144. return -1;
  145. }
  146. // Convert signal number to name
  147. const char *get_signame(int number)
  148. {
  149. if ((unsigned)number < ARRAY_SIZE(signals)) {
  150. if (signals[number][0]) /* if it's not an empty str */
  151. return signals[number];
  152. }
  153. return itoa(number);
  154. }
  155. // Print the whole signal list
  156. void print_signames(void)
  157. {
  158. int signo;
  159. for (signo = 1; signo < ARRAY_SIZE(signals); signo++) {
  160. const char *name = signals[signo];
  161. if (name[0])
  162. puts(name);
  163. }
  164. }