3
0

kill.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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)
  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) bb_show_usage();
  89. if (arg[0] != '-') goto do_it_now;
  90. }
  91. /* -SIG */
  92. signo = get_signum(&arg[1]);
  93. if (signo < 0) { /* || signo > MAX_SIGNUM ? */
  94. bb_error_msg("bad signal name '%s'", &arg[1]);
  95. return EXIT_FAILURE;
  96. }
  97. arg = *++argv;
  98. argc--;
  99. do_it_now:
  100. pid = getpid();
  101. if (killall5) {
  102. pid_t sid;
  103. procps_status_t* p = NULL;
  104. /* Find out our own session id */
  105. sid = getsid(pid);
  106. /* Now stop all processes */
  107. kill(-1, SIGSTOP);
  108. /* Now kill all processes except our session */
  109. while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
  110. if (p->sid != (unsigned)sid && p->pid != (unsigned)pid && p->pid != 1)
  111. kill(p->pid, signo);
  112. }
  113. /* And let them continue */
  114. kill(-1, SIGCONT);
  115. return 0;
  116. }
  117. /* Pid or name is required for kill/killall */
  118. if (argc < 1) {
  119. bb_error_msg("you need to specify whom to kill");
  120. return EXIT_FAILURE;
  121. }
  122. if (killall) {
  123. /* Looks like they want to do a killall. Do that */
  124. while (arg) {
  125. pid_t* pidList;
  126. pidList = find_pid_by_name(arg);
  127. if (*pidList == 0) {
  128. errors++;
  129. if (!quiet)
  130. bb_error_msg("%s: no process killed", arg);
  131. } else {
  132. pid_t *pl;
  133. for (pl = pidList; *pl; pl++) {
  134. if (*pl == pid)
  135. continue;
  136. if (kill(*pl, signo) == 0)
  137. continue;
  138. errors++;
  139. if (!quiet)
  140. bb_perror_msg("cannot kill pid %u", (unsigned)*pl);
  141. }
  142. }
  143. free(pidList);
  144. arg = *++argv;
  145. }
  146. return errors;
  147. }
  148. /* Looks like they want to do a kill. Do that */
  149. while (arg) {
  150. /* Support shell 'space' trick */
  151. if (arg[0] == ' ')
  152. arg++;
  153. pid = bb_strtoi(arg, NULL, 10);
  154. if (errno) {
  155. bb_error_msg("bad pid '%s'", arg);
  156. errors++;
  157. } else if (kill(pid, signo) != 0) {
  158. bb_perror_msg("cannot kill pid %d", (int)pid);
  159. errors++;
  160. }
  161. arg = *++argv;
  162. }
  163. return errors;
  164. }