kill.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 (3.1 kb)"
  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 (5.6 kb)"
  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 (5.3 kb)"
  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_NOFORK(kill, kill, BB_DIR_BIN, BB_SUID_DROP, kill))
  35. // APPLET_NOFORK:name main location suid_type help
  36. //applet:IF_KILLALL( APPLET_NOFORK(killall, kill, BB_DIR_USR_BIN, BB_SUID_DROP, killall))
  37. //applet:IF_KILLALL5(APPLET_NOFORK(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. * (doesn't apply for killall[5], still should be careful b/c NOFORK)
  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;
  104. #if ENABLE_KILL || ENABLE_KILLALL
  105. int quiet = 0;
  106. #endif
  107. #if KILL_APPLET_CNT == 1
  108. # define is_killall ENABLE_KILLALL
  109. # define is_killall5 ENABLE_KILLALL5
  110. #else
  111. /* How to determine who we are? find 3rd char from the end:
  112. * kill, killall, killall5
  113. * ^i ^a ^l - it's unique
  114. * (checking from the start is complicated by /bin/kill... case) */
  115. const char char3 = argv[0][strlen(argv[0]) - 3];
  116. # define is_killall (ENABLE_KILLALL && char3 == 'a')
  117. # define is_killall5 (ENABLE_KILLALL5 && char3 == 'l')
  118. #endif
  119. /* Parse any options */
  120. arg = *++argv;
  121. if (!arg || arg[0] != '-') {
  122. goto do_it_now;
  123. }
  124. /* The -l option, which prints out signal names.
  125. * Intended usage in shell:
  126. * echo "Died of SIG`kill -l $?`"
  127. * We try to mimic what kill from coreutils-6.8 does */
  128. if (arg[1] == 'l' && arg[2] == '\0') {
  129. arg = *++argv;
  130. if (!arg) {
  131. /* Print the whole signal list */
  132. print_signames();
  133. return 0;
  134. }
  135. /* -l <sig list> */
  136. do {
  137. if (isdigit(arg[0])) {
  138. signo = bb_strtou(arg, NULL, 10);
  139. if (errno) {
  140. bb_error_msg("unknown signal '%s'", arg);
  141. return EXIT_FAILURE;
  142. }
  143. /* Exitcodes >= 0x80 are to be treated
  144. * as "killed by signal (exitcode & 0x7f)" */
  145. puts(get_signame(signo & 0x7f));
  146. /* TODO: 'bad' signal# - coreutils says:
  147. * kill: 127: invalid signal
  148. * we just print "127" instead */
  149. } else {
  150. signo = get_signum(arg);
  151. if (signo < 0) {
  152. bb_error_msg("unknown signal '%s'", arg);
  153. return EXIT_FAILURE;
  154. }
  155. printf("%d\n", signo);
  156. }
  157. arg = *++argv;
  158. } while (arg);
  159. return EXIT_SUCCESS;
  160. }
  161. /* The -q quiet option */
  162. if (is_killall && arg[1] == 'q' && arg[2] == '\0') {
  163. #if ENABLE_KILL || ENABLE_KILLALL
  164. quiet = 1;
  165. #endif
  166. arg = *++argv;
  167. if (!arg)
  168. bb_show_usage();
  169. if (arg[0] != '-')
  170. goto do_it_now;
  171. }
  172. arg++; /* skip '-' */
  173. /* -o PID? (if present, it always is at the end of command line) */
  174. if (is_killall5 && arg[0] == 'o')
  175. goto do_it_now;
  176. /* "--" separates options from args. Testcase: "kill -- -123" */
  177. if (!is_killall5 && arg[0] == '-' && arg[1] == '\0')
  178. goto do_it_sooner;
  179. if (argv[1] && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
  180. arg = *++argv;
  181. } /* else it must be -SIG */
  182. signo = get_signum(arg);
  183. if (signo < 0) {
  184. bb_error_msg("bad signal name '%s'", arg);
  185. return EXIT_FAILURE;
  186. }
  187. do_it_sooner:
  188. arg = *++argv;
  189. do_it_now:
  190. pid = getpid();
  191. if (is_killall5) {
  192. pid_t sid;
  193. procps_status_t* p = NULL;
  194. /* compat: exitcode 2 is "no one was signaled" */
  195. errors = 2;
  196. /* Find out our session id */
  197. sid = getsid(pid);
  198. /* Stop all processes */
  199. if (signo != SIGSTOP && signo != SIGCONT)
  200. kill(-1, SIGSTOP);
  201. /* Signal all processes except those in our session */
  202. while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID)) != NULL) {
  203. char **args;
  204. if (p->sid == (unsigned)sid
  205. || p->sid == 0 /* compat: kernel thread, don't signal it */
  206. || p->pid == (unsigned)pid
  207. || p->pid == 1
  208. ) {
  209. continue;
  210. }
  211. /* All remaining args must be -o PID options.
  212. * Check p->pid against them. */
  213. args = argv;
  214. while (*args) {
  215. pid_t omit;
  216. arg = *args++;
  217. if (arg[0] != '-' || arg[1] != 'o') {
  218. bb_error_msg("bad option '%s'", arg);
  219. errors = 1;
  220. goto resume;
  221. }
  222. arg += 2;
  223. if (!arg[0] && *args)
  224. arg = *args++;
  225. omit = bb_strtoi(arg, NULL, 10);
  226. if (errno) {
  227. bb_error_msg("invalid number '%s'", arg);
  228. errors = 1;
  229. goto resume;
  230. }
  231. if (p->pid == omit)
  232. goto dont_kill;
  233. }
  234. kill(p->pid, signo);
  235. errors = 0;
  236. dont_kill: ;
  237. }
  238. resume:
  239. /* And let them continue */
  240. if (signo != SIGSTOP && signo != SIGCONT)
  241. kill(-1, SIGCONT);
  242. return errors;
  243. }
  244. #if ENABLE_KILL || ENABLE_KILLALL
  245. /* Pid or name is required for kill/killall */
  246. if (!arg) {
  247. bb_simple_error_msg("you need to specify whom to kill");
  248. return EXIT_FAILURE;
  249. }
  250. if (!ENABLE_KILL || is_killall) {
  251. /* Looks like they want to do a killall. Do that */
  252. do {
  253. pid_t* pidList;
  254. pidList = find_pid_by_name(arg);
  255. if (*pidList == 0) {
  256. errors++;
  257. if (!quiet)
  258. bb_error_msg("%s: no process killed", arg);
  259. } else {
  260. pid_t *pl;
  261. for (pl = pidList; *pl; pl++) {
  262. if (*pl == pid)
  263. continue;
  264. if (kill(*pl, signo) == 0)
  265. continue;
  266. errors++;
  267. if (!quiet)
  268. bb_perror_msg("can't kill pid %d", (int)*pl);
  269. }
  270. }
  271. free(pidList);
  272. arg = *++argv;
  273. } while (arg);
  274. return errors;
  275. }
  276. #endif
  277. #if ENABLE_KILL
  278. /* Looks like they want to do a kill. Do that */
  279. while (arg) {
  280. # if SH_KILL
  281. /*
  282. * We need to support shell's "hack formats" of
  283. * " -PRGP_ID" (yes, with a leading space)
  284. * and " PID1 PID2 PID3" (with degenerate case "")
  285. */
  286. while (*arg != '\0') {
  287. char *end;
  288. if (*arg == ' ')
  289. arg++;
  290. pid = bb_strtoi(arg, &end, 10);
  291. if (errno && (errno != EINVAL || *end != ' ')) {
  292. bb_error_msg("invalid number '%s'", arg);
  293. errors++;
  294. break;
  295. }
  296. if (kill(pid, signo) != 0) {
  297. bb_perror_msg("can't kill pid %d", (int)pid);
  298. errors++;
  299. }
  300. arg = end; /* can only point to ' ' or '\0' now */
  301. }
  302. # else /* ENABLE_KILL but !SH_KILL */
  303. pid = bb_strtoi(arg, NULL, 10);
  304. if (errno) {
  305. bb_error_msg("invalid number '%s'", arg);
  306. errors++;
  307. } else if (kill(pid, signo) != 0) {
  308. bb_perror_msg("can't kill pid %d", (int)pid);
  309. errors++;
  310. }
  311. # endif
  312. arg = *++argv;
  313. }
  314. return errors;
  315. #endif
  316. }