kill.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. //usage:#define kill_trivial_usage
  11. //usage: "[-l] [-SIG] PID..."
  12. //usage:#define kill_full_usage "\n\n"
  13. //usage: "Send a signal (default: TERM) to given PIDs\n"
  14. //usage: "\n -l List all signal names and numbers"
  15. /* //usage: "\n -s SIG Yet another way of specifying SIG" */
  16. //usage:
  17. //usage:#define kill_example_usage
  18. //usage: "$ ps | grep apache\n"
  19. //usage: "252 root root S [apache]\n"
  20. //usage: "263 www-data www-data S [apache]\n"
  21. //usage: "264 www-data www-data S [apache]\n"
  22. //usage: "265 www-data www-data S [apache]\n"
  23. //usage: "266 www-data www-data S [apache]\n"
  24. //usage: "267 www-data www-data S [apache]\n"
  25. //usage: "$ kill 252\n"
  26. //usage:
  27. //usage:#define killall_trivial_usage
  28. //usage: "[-l] [-q] [-SIG] PROCESS_NAME..."
  29. //usage:#define killall_full_usage "\n\n"
  30. //usage: "Send a signal (default: TERM) to given processes\n"
  31. //usage: "\n -l List all signal names and numbers"
  32. /* //usage: "\n -s SIG Yet another way of specifying SIG" */
  33. //usage: "\n -q Don't complain if no processes were killed"
  34. //usage:
  35. //usage:#define killall_example_usage
  36. //usage: "$ killall apache\n"
  37. //usage:
  38. //usage:#define killall5_trivial_usage
  39. //usage: "[-l] [-SIG] [-o PID]..."
  40. //usage:#define killall5_full_usage "\n\n"
  41. //usage: "Send a signal (default: TERM) to all processes outside current session\n"
  42. //usage: "\n -l List all signal names and numbers"
  43. //usage: "\n -o PID Don't signal this PID"
  44. /* //usage: "\n -s SIG Yet another way of specifying SIG" */
  45. #include "libbb.h"
  46. /* Note: kill_main is directly called from shell in order to implement
  47. * kill built-in. Shell substitutes job ids with process groups first.
  48. *
  49. * This brings some complications:
  50. *
  51. * + we can't use xfunc here
  52. * + we can't use applet_name
  53. * + we can't use bb_show_usage
  54. * (Above doesn't apply for killall[5] cases)
  55. *
  56. * kill %n gets translated into kill ' -<process group>' by shell (note space!)
  57. * This is needed to avoid collision with kill -9 ... syntax
  58. */
  59. int kill_main(int argc UNUSED_PARAM, char **argv)
  60. {
  61. char *arg;
  62. pid_t pid;
  63. int signo = SIGTERM, errors = 0, quiet = 0;
  64. #if !ENABLE_KILLALL && !ENABLE_KILLALL5
  65. #define killall 0
  66. #define killall5 0
  67. #else
  68. /* How to determine who we are? find 3rd char from the end:
  69. * kill, killall, killall5
  70. * ^i ^a ^l - it's unique
  71. * (checking from the start is complicated by /bin/kill... case) */
  72. const char char3 = argv[0][strlen(argv[0]) - 3];
  73. #define killall (ENABLE_KILLALL && char3 == 'a')
  74. #define killall5 (ENABLE_KILLALL5 && char3 == 'l')
  75. #endif
  76. /* Parse any options */
  77. arg = *++argv;
  78. if (!arg || arg[0] != '-') {
  79. goto do_it_now;
  80. }
  81. /* The -l option, which prints out signal names.
  82. * Intended usage in shell:
  83. * echo "Died of SIG`kill -l $?`"
  84. * We try to mimic what kill from coreutils-6.8 does */
  85. if (arg[1] == 'l' && arg[2] == '\0') {
  86. arg = *++argv;
  87. if (!arg) {
  88. /* Print the whole signal list */
  89. print_signames();
  90. return 0;
  91. }
  92. /* -l <sig list> */
  93. do {
  94. if (isdigit(arg[0])) {
  95. signo = bb_strtou(arg, NULL, 10);
  96. if (errno) {
  97. bb_error_msg("unknown signal '%s'", arg);
  98. return EXIT_FAILURE;
  99. }
  100. /* Exitcodes >= 0x80 are to be treated
  101. * as "killed by signal (exitcode & 0x7f)" */
  102. puts(get_signame(signo & 0x7f));
  103. /* TODO: 'bad' signal# - coreutils says:
  104. * kill: 127: invalid signal
  105. * we just print "127" instead */
  106. } else {
  107. signo = get_signum(arg);
  108. if (signo < 0) {
  109. bb_error_msg("unknown signal '%s'", arg);
  110. return EXIT_FAILURE;
  111. }
  112. printf("%d\n", signo);
  113. }
  114. arg = *++argv;
  115. } while (arg);
  116. return EXIT_SUCCESS;
  117. }
  118. /* The -q quiet option */
  119. if (killall && arg[1] == 'q' && arg[2] == '\0') {
  120. quiet = 1;
  121. arg = *++argv;
  122. if (!arg)
  123. bb_show_usage();
  124. if (arg[0] != '-')
  125. goto do_it_now;
  126. }
  127. arg++; /* skip '-' */
  128. /* -o PID? (if present, it always is at the end of command line) */
  129. if (killall5 && arg[0] == 'o')
  130. goto do_it_now;
  131. if (argv[1] && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
  132. arg = *++argv;
  133. } /* else it must be -SIG */
  134. signo = get_signum(arg);
  135. if (signo < 0) { /* || signo > MAX_SIGNUM ? */
  136. bb_error_msg("bad signal name '%s'", arg);
  137. return EXIT_FAILURE;
  138. }
  139. arg = *++argv;
  140. do_it_now:
  141. pid = getpid();
  142. if (killall5) {
  143. pid_t sid;
  144. procps_status_t* p = NULL;
  145. /* compat: exitcode 2 is "no one was signaled" */
  146. int ret = 2;
  147. /* Find out our session id */
  148. sid = getsid(pid);
  149. /* Stop all processes */
  150. if (signo != SIGSTOP && signo != SIGCONT)
  151. kill(-1, SIGSTOP);
  152. /* Signal all processes except those in our session */
  153. while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID)) != NULL) {
  154. char **args;
  155. if (p->sid == (unsigned)sid
  156. || p->sid == 0 /* compat: kernel thread, don't signal it */
  157. || p->pid == (unsigned)pid
  158. || p->pid == 1
  159. ) {
  160. continue;
  161. }
  162. /* All remaining args must be -o PID options.
  163. * Check p->pid against them. */
  164. args = argv;
  165. while (*args) {
  166. pid_t omit;
  167. arg = *args++;
  168. if (arg[0] != '-' || arg[1] != 'o') {
  169. bb_error_msg("bad option '%s'", arg);
  170. ret = 1;
  171. goto resume;
  172. }
  173. arg += 2;
  174. if (!arg[0] && *args)
  175. arg = *args++;
  176. omit = bb_strtoi(arg, NULL, 10);
  177. if (errno) {
  178. bb_error_msg("invalid number '%s'", arg);
  179. ret = 1;
  180. goto resume;
  181. }
  182. if (p->pid == omit)
  183. goto dont_kill;
  184. }
  185. kill(p->pid, signo);
  186. ret = 0;
  187. dont_kill: ;
  188. }
  189. resume:
  190. /* And let them continue */
  191. if (signo != SIGSTOP && signo != SIGCONT)
  192. kill(-1, SIGCONT);
  193. return ret;
  194. }
  195. /* Pid or name is required for kill/killall */
  196. if (!arg) {
  197. bb_error_msg("you need to specify whom to kill");
  198. return EXIT_FAILURE;
  199. }
  200. if (killall) {
  201. /* Looks like they want to do a killall. Do that */
  202. do {
  203. pid_t* pidList;
  204. pidList = find_pid_by_name(arg);
  205. if (*pidList == 0) {
  206. errors++;
  207. if (!quiet)
  208. bb_error_msg("%s: no process killed", arg);
  209. } else {
  210. pid_t *pl;
  211. for (pl = pidList; *pl; pl++) {
  212. if (*pl == pid)
  213. continue;
  214. if (kill(*pl, signo) == 0)
  215. continue;
  216. errors++;
  217. if (!quiet)
  218. bb_perror_msg("can't kill pid %d", (int)*pl);
  219. }
  220. }
  221. free(pidList);
  222. arg = *++argv;
  223. } while (arg);
  224. return errors;
  225. }
  226. /* Looks like they want to do a kill. Do that */
  227. while (arg) {
  228. #if ENABLE_ASH || ENABLE_HUSH
  229. /*
  230. * We need to support shell's "hack formats" of
  231. * " -PRGP_ID" (yes, with a leading space)
  232. * and " PID1 PID2 PID3" (with degenerate case "")
  233. */
  234. while (*arg != '\0') {
  235. char *end;
  236. if (*arg == ' ')
  237. arg++;
  238. pid = bb_strtoi(arg, &end, 10);
  239. if (errno && (errno != EINVAL || *end != ' ')) {
  240. bb_error_msg("invalid number '%s'", arg);
  241. errors++;
  242. break;
  243. }
  244. if (kill(pid, signo) != 0) {
  245. bb_perror_msg("can't kill pid %d", (int)pid);
  246. errors++;
  247. }
  248. arg = end; /* can only point to ' ' or '\0' now */
  249. }
  250. #else
  251. pid = bb_strtoi(arg, NULL, 10);
  252. if (errno) {
  253. bb_error_msg("invalid number '%s'", arg);
  254. errors++;
  255. } else if (kill(pid, signo) != 0) {
  256. bb_perror_msg("can't kill pid %d", (int)pid);
  257. errors++;
  258. }
  259. #endif
  260. arg = *++argv;
  261. }
  262. return errors;
  263. }