kill.c 9.1 KB

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