3
0

kill.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini kill/killall[5] implementation for busybox
  4. *
  5. * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
  6. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. #include "libbb.h"
  11. /* Note: kill_main is directly called from shell in order to implement
  12. * kill built-in. Shell substitutes job ids with process groups first.
  13. *
  14. * This brings some complications:
  15. *
  16. * + we can't use xfunc here
  17. * + we can't use applet_name
  18. * + we can't use bb_show_usage
  19. * (Above doesn't apply for killall[5] cases)
  20. *
  21. * kill %n gets translated into kill ' -<process group>' by shell (note space!)
  22. * This is needed to avoid collision with kill -9 ... syntax
  23. */
  24. int kill_main(int argc, char **argv)
  25. {
  26. char *arg;
  27. pid_t pid;
  28. int signo = SIGTERM, errors = 0, quiet = 0;
  29. #if !ENABLE_KILLALL && !ENABLE_KILLALL5
  30. #define killall 0
  31. #define killall5 0
  32. #else
  33. /* How to determine who we are? find 3rd char from the end:
  34. * kill, killall, killall5
  35. * ^i ^a ^l - it's unique
  36. * (checking from the start is complicated by /bin/kill... case) */
  37. const char char3 = argv[0][strlen(argv[0]) - 3];
  38. #define killall (ENABLE_KILLALL && char3 == 'a')
  39. #define killall5 (ENABLE_KILLALL5 && char3 == 'l')
  40. #endif
  41. /* Parse any options */
  42. argc--;
  43. arg = *++argv;
  44. if (argc < 1 || arg[0] != '-') {
  45. goto do_it_now;
  46. }
  47. /* The -l option, which prints out signal names.
  48. * Intended usage in shell:
  49. * echo "Died of SIG`kill -l $?`"
  50. * We try to mimic what kill from coreutils-6.8 does */
  51. if (arg[1] == 'l' && arg[2] == '\0') {
  52. if (argc == 1) {
  53. /* Print the whole signal list */
  54. print_signames();
  55. return 0;
  56. }
  57. /* -l <sig list> */
  58. while ((arg = *++argv)) {
  59. if (isdigit(arg[0])) {
  60. signo = bb_strtou(arg, NULL, 10);
  61. if (errno) {
  62. bb_error_msg("unknown signal '%s'", arg);
  63. return EXIT_FAILURE;
  64. }
  65. /* Exitcodes >= 0x80 are to be treated
  66. * as "killed by signal (exitcode & 0x7f)" */
  67. puts(get_signame(signo & 0x7f));
  68. /* TODO: 'bad' signal# - coreutils says:
  69. * kill: 127: invalid signal
  70. * we just print "127" instead */
  71. } else {
  72. signo = get_signum(arg);
  73. if (signo < 0) {
  74. bb_error_msg("unknown signal '%s'", arg);
  75. return EXIT_FAILURE;
  76. }
  77. printf("%d\n", signo);
  78. }
  79. }
  80. /* If they specified -l, we are all done */
  81. return EXIT_SUCCESS;
  82. }
  83. /* The -q quiet option */
  84. if (killall && arg[1] == 'q' && arg[2] == '\0') {
  85. quiet = 1;
  86. arg = *++argv;
  87. argc--;
  88. if (argc < 1)
  89. bb_show_usage();
  90. if (arg[0] != '-')
  91. goto do_it_now;
  92. }
  93. arg++; /* skip '-' */
  94. /* -o PID? (if present, it always is at the end of command line) */
  95. if (killall5 && arg[0] == 'o')
  96. goto do_it_now;
  97. if (argc > 1 && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
  98. argc--;
  99. arg = *++argv;
  100. } /* else it must be -SIG */
  101. signo = get_signum(arg);
  102. if (signo < 0) { /* || signo > MAX_SIGNUM ? */
  103. bb_error_msg("bad signal name '%s'", arg);
  104. return EXIT_FAILURE;
  105. }
  106. arg = *++argv;
  107. argc--;
  108. do_it_now:
  109. pid = getpid();
  110. if (killall5) {
  111. pid_t sid;
  112. procps_status_t* p = NULL;
  113. int ret = 0;
  114. /* Find out our session id */
  115. sid = getsid(pid);
  116. /* Stop all processes */
  117. kill(-1, SIGSTOP);
  118. /* Signal all processes except those in our session */
  119. while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
  120. int i;
  121. if (p->sid == (unsigned)sid
  122. || p->pid == (unsigned)pid
  123. || p->pid == 1)
  124. continue;
  125. /* All remaining args must be -o PID options.
  126. * Check p->pid against them. */
  127. for (i = 0; i < argc; i++) {
  128. pid_t omit;
  129. arg = argv[i];
  130. if (arg[0] != '-' || arg[1] != 'o') {
  131. bb_error_msg("bad option '%s'", arg);
  132. ret = 1;
  133. goto resume;
  134. }
  135. arg += 2;
  136. if (!arg[0] && argv[++i])
  137. arg = argv[i];
  138. omit = bb_strtoi(arg, NULL, 10);
  139. if (errno) {
  140. bb_error_msg("invalid number '%s'", arg);
  141. ret = 1;
  142. goto resume;
  143. }
  144. if (p->pid == omit)
  145. goto dont_kill;
  146. }
  147. kill(p->pid, signo);
  148. dont_kill: ;
  149. }
  150. resume:
  151. /* And let them continue */
  152. kill(-1, SIGCONT);
  153. return ret;
  154. }
  155. /* Pid or name is required for kill/killall */
  156. if (argc < 1) {
  157. bb_error_msg("you need to specify whom to kill");
  158. return EXIT_FAILURE;
  159. }
  160. if (killall) {
  161. /* Looks like they want to do a killall. Do that */
  162. while (arg) {
  163. pid_t* pidList;
  164. pidList = find_pid_by_name(arg);
  165. if (*pidList == 0) {
  166. errors++;
  167. if (!quiet)
  168. bb_error_msg("%s: no process killed", arg);
  169. } else {
  170. pid_t *pl;
  171. for (pl = pidList; *pl; pl++) {
  172. if (*pl == pid)
  173. continue;
  174. if (kill(*pl, signo) == 0)
  175. continue;
  176. errors++;
  177. if (!quiet)
  178. bb_perror_msg("can't kill pid %d", (int)*pl);
  179. }
  180. }
  181. free(pidList);
  182. arg = *++argv;
  183. }
  184. return errors;
  185. }
  186. /* Looks like they want to do a kill. Do that */
  187. while (arg) {
  188. /* Support shell 'space' trick */
  189. if (arg[0] == ' ')
  190. arg++;
  191. pid = bb_strtoi(arg, NULL, 10);
  192. if (errno) {
  193. bb_error_msg("invalid number '%s'", arg);
  194. errors++;
  195. } else if (kill(pid, signo) != 0) {
  196. bb_perror_msg("can't kill pid %d", (int)pid);
  197. errors++;
  198. }
  199. arg = *++argv;
  200. }
  201. return errors;
  202. }