3
0

kill.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. extern 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(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. if (whichApp == KILL) {
  97. /* Looks like they want to do a kill. Do that */
  98. while (--argc >= 0) {
  99. int pid;
  100. if (!isdigit(**argv))
  101. bb_error_msg_and_die( "Bad PID '%s'", *argv);
  102. pid = strtol(*argv, NULL, 0);
  103. if (kill(pid, signo) != 0) {
  104. bb_perror_msg( "Could not kill pid '%d'", pid);
  105. errors++;
  106. }
  107. argv++;
  108. }
  109. }
  110. #ifdef CONFIG_KILLALL
  111. else {
  112. pid_t myPid=getpid();
  113. /* Looks like they want to do a killall. Do that */
  114. while (--argc >= 0) {
  115. long* pidList;
  116. pidList = find_pid_by_name(*argv);
  117. if (!pidList || *pidList<=0) {
  118. errors++;
  119. if (quiet==0)
  120. bb_error_msg( "%s: no process killed", *argv);
  121. } else {
  122. long *pl;
  123. for(pl = pidList; *pl !=0 ; pl++) {
  124. if (*pl==myPid)
  125. continue;
  126. if (kill(*pl, signo) != 0) {
  127. errors++;
  128. if (quiet==0)
  129. bb_perror_msg( "Could not kill pid '%ld'", *pl);
  130. }
  131. }
  132. }
  133. free(pidList);
  134. argv++;
  135. }
  136. }
  137. #endif
  138. return errors;
  139. }