u_signal_names.c 3.0 KB

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