summaryrefslogtreecommitdiff
path: root/sysutils/psmisc/files/patch-src-killall.c
blob: 31cf7da73519b5d8e998db74da8959d13859ce2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--- src/killall.c.orig	Fri Dec 10 13:31:29 2004
+++ src/killall.c	Wed Oct  5 09:16:10 2005
@@ -39,35 +39,31 @@
            quiet = 0, wait_until_dead = 0, process_group = 0,
            ignore_case = 0, pidof;
 
+/*
+ * This is the implementation from 21.5, as the one in 21.6 uses
+ * Linux specific functions getline() and rpmatch()
+ */
 static int
 ask (char *name, pid_t pid)
 {
-  int res;
-  size_t len;
-  char *line;
+  int ch, c;
 
-  line = NULL;
-  len = 0;
-
-  do {
-    printf (_("Kill %s(%s%d) ? (y/N) "), name, process_group ? "pgid " : "",
-	    pid);
-    fflush (stdout);
-
-    if (getline (&line, &len, stdin) < 0)
-      return 0;
-    /* Check for default */
-    if (line[0] == '\n') {
-      free(line);
-      return 0;
-    }
-    res = rpmatch(line);
-    if (res >= 0) {
-      free(line);
-      return res;
+  do
+    {
+      printf (_("Kill %s(%s%d) ? (y/n) "), name, process_group ? "pgid " : "",
+	      pid);
+      fflush (stdout);
+      do
+	if ((ch = getchar ()) == EOF)
+	  exit (0);
+      while (ch == '\n' || ch == '\t' || ch == ' ');
+      do
+	if ((c = getchar ()) == EOF)
+	  exit (0);
+      while (c != '\n');
     }
-  } while(1);
-  /* Never should get here */
+  while (ch != 'y' && ch != 'n' && ch != 'Y' && ch != 'N');
+  return ch == 'y' || ch == 'Y';
 }
 
 #ifdef FLASK_LINUX
@@ -82,7 +78,7 @@
   struct dirent *de;
   FILE *file;
   struct stat st, sts[MAX_NAMES];
-  int *name_len;
+  int *name_len = 0;
   char *path, comm[COMM_LEN];
   char *command_buf;
   char *command;
@@ -173,7 +169,7 @@
     }
   for (i = 0; i < pids; i++)
     {
-      if (asprintf (&path, PROC_BASE "/%d/stat", pid_table[i]) < 0)
+      if (asprintf (&path, PROC_BASE "/%d/status", pid_table[i]) < 0)
 	continue;
       if (!(file = fopen (path, "r"))) 
 	{
@@ -182,72 +178,13 @@
 	}
       free (path);
       empty = 0;
-      okay = fscanf (file, "%*d (%[^)]", comm) == 1;
+      okay = fscanf (file, "%s", comm) == 1;
       (void) fclose (file);
       if (!okay)
 	continue;
       got_long = 0;
       command = NULL;		/* make gcc happy */
       length = strlen (comm);
-      if (length == COMM_LEN - 1)
-	{
-	  if (asprintf (&path, PROC_BASE "/%d/cmdline", pid_table[i]) < 0)
-	    continue;
-	  if (!(file = fopen (path, "r"))) {
-	    free (path);
-	    continue;
-	  }
-	  free (path);
-          while (1) {
-            /* look for actual command so we skip over initial "sh" if any */
-            char *p;
-	    int cmd_size = 128;
-	    command_buf = (char *)malloc (cmd_size);
-	    if (!command_buf)
-	      exit (1);
-
-            /* 'cmdline' has arguments separated by nulls */
-            for (p=command_buf; ; p++) {
-              int c;
-	      if (p == (command_buf + cmd_size)) 
-		{
-		  int cur_size = cmd_size;
-		  cmd_size *= 2;
-		  command_buf = (char *)realloc(command_buf, cmd_size);
-		  if (!command_buf)
-		    exit (1);
-		  p = command_buf + cur_size;
-		}
-              c = fgetc(file);
-              if (c == EOF || c == '\0') {
-                *p = '\0';
-                break;
-              } else {
-                *p = c;
-              }
-            }
-            if (strlen(command_buf) == 0) {
-              okay = 0;
-              break;
-            }
-            p = strrchr(command_buf,'/');
-            p = p ? p+1 : command_buf;
-            if (strncmp(p, comm, COMM_LEN-1) == 0) {
-              okay = 1;
-              command = p;
-              break;
-            }
-          }
-          (void) fclose(file);
-	  if (exact && !okay)
-	    {
-	      if (verbose)
-		fprintf (stderr, _("skipping partial match %s(%d)\n"), comm,
-			 pid_table[i]);
-	      continue;
-	    }
-	  got_long = okay;
-	}
       for (j = 0; j < names; j++)
 	{
 	  pid_t id;
@@ -281,7 +218,7 @@
 	    }
 	  else
 	    {
-	      if (asprintf (&path, PROC_BASE "/%d/exe", pid_table[i]) < 0)
+	      if (asprintf (&path, PROC_BASE "/%d/file", pid_table[i]) < 0)
 		continue;
 #ifdef FLASK_LINUX
           if (stat_secure(path,&st,&lsid) < 0) {
@@ -357,6 +294,7 @@
 	fprintf (stderr, _("%s: no process killed\n"), namelist[i]);
   if (pidof)
     putchar ('\n');
+    return 0;
   error = found == ((1 << (names - 1)) | ((1 << (names - 1)) - 1)) ? 0 : 1;
   /*
    * We scan all (supposedly) killed processes every second to detect dead