kill.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini kill/killall 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. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <unistd.h>
  27. #include <signal.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include "busybox.h"
  32. #define KILL 0
  33. #define KILLALL 1
  34. int kill_main(int argc, char **argv)
  35. {
  36. int whichApp, signo = SIGTERM;
  37. const char *name;
  38. int errors = 0;
  39. #ifdef CONFIG_KILLALL
  40. int quiet=0;
  41. /* Figure out what we are trying to do here */
  42. whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL;
  43. #else
  44. whichApp = KILL;
  45. #endif
  46. /* Parse any options */
  47. if (argc < 2)
  48. bb_show_usage();
  49. if(argv[1][0] != '-'){
  50. argv++;
  51. argc--;
  52. goto do_it_now;
  53. }
  54. /* The -l option, which prints out signal names. */
  55. if(argv[1][1]=='l' && argv[1][2]=='\0'){
  56. if(argc==2) {
  57. /* Print the whole signal list */
  58. int col = 0;
  59. for(signo=1; signo < NSIG; signo++) {
  60. name = u_signal_names(0, &signo, 1);
  61. if(name==NULL) /* unnamed */
  62. continue;
  63. col += printf("%2d) %-16s", signo, name);
  64. if (col > 60) {
  65. printf("\n");
  66. col = 0;
  67. }
  68. }
  69. printf("\n");
  70. } else {
  71. for(argv++; *argv; argv++) {
  72. name = u_signal_names(*argv, &signo, -1);
  73. if(name!=NULL)
  74. printf("%s\n", name);
  75. }
  76. }
  77. /* If they specified -l, were all done */
  78. return EXIT_SUCCESS;
  79. }
  80. #ifdef CONFIG_KILLALL
  81. /* The -q quiet option */
  82. if(whichApp != KILL && argv[1][1]=='q' && argv[1][2]=='\0'){
  83. quiet++;
  84. argv++;
  85. argc--;
  86. if(argc<2 || argv[1][0] != '-'){
  87. goto do_it_now;
  88. }
  89. }
  90. #endif
  91. if(!u_signal_names(argv[1]+1, &signo, 0))
  92. bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1);
  93. argv+=2;
  94. argc-=2;
  95. do_it_now:
  96. /* Pid or name required */
  97. if (argc <= 0)
  98. bb_show_usage();
  99. if (whichApp == KILL) {
  100. /* Looks like they want to do a kill. Do that */
  101. while (--argc >= 0) {
  102. int pid;
  103. if (!isdigit(**argv) && **argv != '-')
  104. bb_error_msg_and_die( "Bad PID '%s'", *argv);
  105. pid = strtol(*argv, NULL, 0);
  106. if (kill(pid, signo) != 0) {
  107. bb_perror_msg( "Could not kill pid '%d'", pid);
  108. errors++;
  109. }
  110. argv++;
  111. }
  112. }
  113. #ifdef CONFIG_KILLALL
  114. else {
  115. pid_t myPid=getpid();
  116. /* Looks like they want to do a killall. Do that */
  117. while (--argc >= 0) {
  118. long* pidList;
  119. pidList = find_pid_by_name(*argv);
  120. if (!pidList || *pidList<=0) {
  121. errors++;
  122. if (quiet==0)
  123. bb_error_msg( "%s: no process killed", *argv);
  124. } else {
  125. long *pl;
  126. for(pl = pidList; *pl !=0 ; pl++) {
  127. if (*pl==myPid)
  128. continue;
  129. if (kill(*pl, signo) != 0) {
  130. errors++;
  131. if (quiet==0)
  132. bb_perror_msg( "Could not kill pid '%ld'", *pl);
  133. }
  134. }
  135. }
  136. free(pidList);
  137. argv++;
  138. }
  139. }
  140. #endif
  141. return errors;
  142. }