patch-src-killall.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. --- src/killall.c.orig 2012-02-19 23:08:52 UTC
  2. +++ src/killall.c
  3. @@ -36,6 +36,8 @@
  4. #include <dirent.h>
  5. #include <signal.h>
  6. #include <errno.h>
  7. +#include <limits.h>
  8. +#include <locale.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <getopt.h>
  12. @@ -87,40 +89,35 @@ static int verbose = 0, exact = 0, inter
  13. ignore_case = 0, pidof;
  14. static long younger_than = 0, older_than = 0;
  15. + /*
  16. + * This is based on the implementation from 21.5, as the one in 21.6
  17. + * and newer uses Linux specific functions getline() and rpmatch()
  18. + */
  19. static int
  20. ask (char *name, pid_t pid, const int signal)
  21. {
  22. - int res;
  23. - size_t len;
  24. - char *line;
  25. -
  26. - line = NULL;
  27. - len = 0;
  28. -
  29. - do {
  30. - if (signal == SIGTERM)
  31. - printf (_("Kill %s(%s%d) ? (y/N) "), name, process_group ? "pgid " : "",
  32. - pid);
  33. - else
  34. - printf (_("Signal %s(%s%d) ? (y/N) "), name, process_group ? "pgid " : "",
  35. - pid);
  36. -
  37. - fflush (stdout);
  38. + int ch, c;
  39. - if (getline (&line, &len, stdin) < 0)
  40. - return 0;
  41. - /* Check for default */
  42. - if (line[0] == '\n') {
  43. - free(line);
  44. - return 0;
  45. - }
  46. - res = rpmatch(line);
  47. - if (res >= 0) {
  48. - free(line);
  49. - return res;
  50. + do
  51. + {
  52. + if (signal == SIGTERM)
  53. + printf (_("Kill %s(%s%d) ? (y/N) "), name, process_group ? "pgid " : "",
  54. + pid);
  55. + else
  56. + printf (_("Signal %s(%s%d) ? (y/N) "), name, process_group ? "pgid " : "",
  57. + pid);
  58. + fflush (stdout);
  59. + do
  60. + if ((ch = getchar ()) == EOF)
  61. + exit (0);
  62. + while (ch == '\n' || ch == '\t' || ch == ' ');
  63. + do
  64. + if ((c = getchar ()) == EOF)
  65. + exit (0);
  66. + while (c != '\n');
  67. }
  68. - } while(1);
  69. - /* Never should get here */
  70. + while (ch != 'y' && ch != 'n' && ch != 'Y' && ch != 'N');
  71. + return ch == 'y' || ch == 'Y';
  72. }
  73. static double
  74. @@ -197,7 +194,7 @@ match_process_uid(pid_t pid, uid_t uid)
  75. while (fgets(buf, sizeof buf, f))
  76. {
  77. - if (sscanf (buf, "Uid:\t%d", &puid))
  78. + if (sscanf (buf, "%*s %*d %*d %*d %*d %*s %*s %*s %*s %*s %*s %*s %d", &puid))
  79. {
  80. re = uid==puid;
  81. break;
  82. @@ -360,7 +357,7 @@ kill_all (int signal, int names, char **
  83. }
  84. #endif /*WITH_SELINUX*/
  85. /* load process name */
  86. - if (asprintf (&path, PROC_BASE "/%d/stat", pid_table[i]) < 0)
  87. + if (asprintf (&path, PROC_BASE "/%d/status", pid_table[i]) < 0)
  88. continue;
  89. if (!(file = fopen (path, "r")))
  90. {
  91. @@ -368,7 +365,7 @@ kill_all (int signal, int names, char **
  92. continue;
  93. }
  94. free (path);
  95. - okay = fscanf (file, "%*d (%15[^)]", comm) == 1;
  96. + okay = fscanf (file, "%s", comm) == 1;
  97. if (!okay) {
  98. fclose(file);
  99. continue;
  100. @@ -390,65 +387,6 @@ kill_all (int signal, int names, char **
  101. got_long = 0;
  102. command = NULL; /* make gcc happy */
  103. length = strlen (comm);
  104. - if (length == COMM_LEN - 1)
  105. - {
  106. - if (asprintf (&path, PROC_BASE "/%d/cmdline", pid_table[i]) < 0)
  107. - continue;
  108. - if (!(file = fopen (path, "r"))) {
  109. - free (path);
  110. - continue;
  111. - }
  112. - free (path);
  113. - while (1) {
  114. - /* look for actual command so we skip over initial "sh" if any */
  115. - char *p;
  116. - int cmd_size = 128;
  117. - command_buf = (char *)malloc (cmd_size);
  118. - if (!command_buf)
  119. - exit (1);
  120. -
  121. - /* 'cmdline' has arguments separated by nulls */
  122. - for (p=command_buf; ; p++) {
  123. - int c;
  124. - if (p == (command_buf + cmd_size))
  125. - {
  126. - int cur_size = cmd_size;
  127. - cmd_size *= 2;
  128. - command_buf = (char *)realloc(command_buf, cmd_size);
  129. - if (!command_buf)
  130. - exit (1);
  131. - p = command_buf + cur_size;
  132. - }
  133. - c = fgetc(file);
  134. - if (c == EOF || c == '\0') {
  135. - *p = '\0';
  136. - break;
  137. - } else {
  138. - *p = c;
  139. - }
  140. - }
  141. - if (strlen(command_buf) == 0) {
  142. - okay = 0;
  143. - break;
  144. - }
  145. - p = strrchr(command_buf,'/');
  146. - p = p ? p+1 : command_buf;
  147. - if (strncmp(p, comm, COMM_LEN-1) == 0) {
  148. - okay = 1;
  149. - command = p;
  150. - break;
  151. - }
  152. - }
  153. - (void) fclose(file);
  154. - if (exact && !okay)
  155. - {
  156. - if (verbose)
  157. - fprintf (stderr, _("skipping partial match %s(%d)\n"), comm,
  158. - pid_table[i]);
  159. - continue;
  160. - }
  161. - got_long = okay;
  162. - }
  163. /* mach by process name */
  164. for (j = 0; j < names; j++)
  165. {
  166. @@ -499,7 +437,7 @@ kill_all (int signal, int names, char **
  167. {
  168. int ok = 1;
  169. - if (asprintf (&path, PROC_BASE "/%d/exe", pid_table[i]) < 0)
  170. + if (asprintf (&path, PROC_BASE "/%d/file", pid_table[i]) < 0)
  171. continue;
  172. if (stat (path, &st) < 0)
  173. @@ -693,7 +631,7 @@ have_proc_self_stat (void)
  174. struct stat isproc;
  175. pid_t pid = getpid();
  176. - snprintf(filename, sizeof(filename), PROC_BASE"/%d/stat", (int) pid);
  177. + snprintf(filename, sizeof(filename), PROC_BASE"/%d/status", (int) pid);
  178. return stat(filename, &isproc) == 0;
  179. }