3
0

kill.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 the GPL v2 or later, see the file LICENSE in this tarball.
  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) MAIN_EXTERNALLY_VISIBLE;
  25. int kill_main(int argc, char **argv)
  26. {
  27. char *arg;
  28. pid_t pid;
  29. int signo = SIGTERM, errors = 0, quiet = 0;
  30. #if !ENABLE_KILLALL && !ENABLE_KILLALL5
  31. #define killall 0
  32. #define killall5 0
  33. #else
  34. /* How to determine who we are? find 3rd char from the end:
  35. * kill, killall, killall5
  36. * ^i ^a ^l - it's unique
  37. * (checking from the start is complicated by /bin/kill... case) */
  38. const char char3 = argv[0][strlen(argv[0]) - 3];
  39. #define killall (ENABLE_KILLALL && char3 == 'a')
  40. #define killall5 (ENABLE_KILLALL5 && char3 == 'l')
  41. #endif
  42. /* Parse any options */
  43. argc--;
  44. arg = *++argv;
  45. if (argc < 1 || arg[0] != '-') {
  46. goto do_it_now;
  47. }
  48. /* The -l option, which prints out signal names.
  49. * Intended usage in shell:
  50. * echo "Died of SIG`kill -l $?`"
  51. * We try to mimic what kill from coreutils-6.8 does */
  52. if (arg[1] == 'l' && arg[2] == '\0') {
  53. if (argc == 1) {
  54. /* Print the whole signal list */
  55. print_signames();
  56. return 0;
  57. }
  58. /* -l <sig list> */
  59. while ((arg = *++argv)) {
  60. if (isdigit(arg[0])) {
  61. signo = bb_strtou(arg, NULL, 10);
  62. if (errno) {
  63. bb_error_msg("unknown signal '%s'", arg);
  64. return EXIT_FAILURE;
  65. }
  66. /* Exitcodes >= 0x80 are to be treated
  67. * as "killed by signal (exitcode & 0x7f)" */
  68. puts(get_signame(signo & 0x7f));
  69. /* TODO: 'bad' signal# - coreutils says:
  70. * kill: 127: invalid signal
  71. * we just print "127" instead */
  72. } else {
  73. signo = get_signum(arg);
  74. if (signo < 0) {
  75. bb_error_msg("unknown signal '%s'", arg);
  76. return EXIT_FAILURE;
  77. }
  78. printf("%d\n", signo);
  79. }
  80. }
  81. /* If they specified -l, we are all done */
  82. return EXIT_SUCCESS;
  83. }
  84. /* The -q quiet option */
  85. if (killall && arg[1] == 'q' && arg[2] == '\0') {
  86. quiet = 1;
  87. arg = *++argv;
  88. argc--;
  89. if (argc < 1) bb_show_usage();
  90. if (arg[0] != '-') goto do_it_now;
  91. }
  92. /* -SIG */
  93. signo = get_signum(&arg[1]);
  94. if (signo < 0) { /* || signo > MAX_SIGNUM ? */
  95. bb_error_msg("bad signal name '%s'", &arg[1]);
  96. return EXIT_FAILURE;
  97. }
  98. arg = *++argv;
  99. argc--;
  100. do_it_now:
  101. if (killall5) {
  102. pid_t sid;
  103. procps_status_t* p = NULL;
  104. /* Now stop all processes */
  105. kill(-1, SIGSTOP);
  106. /* Find out our own session id */
  107. pid = getpid();
  108. sid = getsid(pid);
  109. /* Now kill all processes except our session */
  110. while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
  111. if (p->sid != sid && p->pid != pid && p->pid != 1)
  112. kill(p->pid, signo);
  113. }
  114. /* And let them continue */
  115. kill(-1, SIGCONT);
  116. return 0;
  117. }
  118. /* Pid or name is required for kill/killall */
  119. if (argc < 1) {
  120. puts("You need to specify whom to kill");
  121. return EXIT_FAILURE;
  122. }
  123. if (killall) {
  124. /* Looks like they want to do a killall. Do that */
  125. pid = getpid();
  126. while (arg) {
  127. pid_t* pidList;
  128. pidList = find_pid_by_name(arg);
  129. if (*pidList == 0) {
  130. errors++;
  131. if (!quiet)
  132. bb_error_msg("%s: no process killed", arg);
  133. } else {
  134. pid_t *pl;
  135. for (pl = pidList; *pl; pl++) {
  136. if (*pl == pid)
  137. continue;
  138. if (kill(*pl, signo) == 0)
  139. continue;
  140. errors++;
  141. if (!quiet)
  142. bb_perror_msg("cannot kill pid %u", (unsigned)*pl);
  143. }
  144. }
  145. free(pidList);
  146. arg = *++argv;
  147. }
  148. return errors;
  149. }
  150. /* Looks like they want to do a kill. Do that */
  151. while (arg) {
  152. /* Support shell 'space' trick */
  153. if (arg[0] == ' ')
  154. arg++;
  155. pid = bb_strtoi(arg, NULL, 10);
  156. if (errno) {
  157. bb_error_msg("bad pid '%s'", arg);
  158. errors++;
  159. } else if (kill(pid, signo) != 0) {
  160. bb_perror_msg("cannot kill pid %d", (int)pid);
  161. errors++;
  162. }
  163. arg = *++argv;
  164. }
  165. return errors;
  166. }