]> granicus.if.org Git - psmisc/commitdiff
killall: better parsing of command names
authorCraig Small <csmall@enc.com.au>
Sun, 11 Jun 2017 22:57:15 +0000 (08:57 +1000)
committerCraig Small <csmall@enc.com.au>
Sun, 11 Jun 2017 22:57:15 +0000 (08:57 +1000)
killall had a simple parser for command names and if you crafted a
process that make its command name strange, killall could bypass it.

The parser now uses the same method as procps.

ChangeLog
src/killall.c

index 572c7482aeacc1c1d73dc6282ebd523dfac7c01e..9c11b93b74e96dc28bc3d835ab4238451372fbd5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
 Changes in 23.0
 ===============
+       * killall: better parsing of command names
        * pstree: add cgroup namespaces !10
        * peekfd: Help give long options too !5
        * killall: correctly report when 32+ procs match !8
index a2fcabdb6df4873cbcdee3988be525e1014b946f..db5067b699c044ab5fca8043db2db974ae9b03fb 100644 (file)
@@ -288,36 +288,45 @@ load_process_name_and_age(char *comm, double *process_age_sec,
 {
     FILE *file;
     char *path;
+    char buf[1024];
+    char *startcomm, *endcomm;
+    unsigned lencomm;
     *process_age_sec = 0;
 
     if (asprintf (&path, PROC_BASE "/%d/stat", pid) < 0)
        return -1;
     if (!(file = fopen (path, "r")))
     {
-       free(path);
-       return -1;
+    free(path);
+    return -1;
     }
     free (path);
-    if (fscanf (file, "%*d (%15[^)]", comm) != 1)
+    if (fgets(buf, 1024, file) == NULL)
     {
-       fclose(file);
-       return -1;
+        fclose(file);
+        return -1;
     }
-
+    fclose(file);
+    startcomm = strchr(buf, '(') + 1;
+    endcomm = strrchr(startcomm, ')');
+    lencomm = endcomm - startcomm;
+    if (lencomm > 15)
+        lencomm = 15;
+    strncpy(comm, startcomm, lencomm);
+    comm[lencomm] = '\0';
+
+    endcomm += 2; // skip ") "
     if (load_age)
     {
-       rewind(file);
-       unsigned long long proc_stt_jf = 0;
-       if (fscanf(file, "%*d %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %Lu",
-                   &proc_stt_jf) != 1)
-       {
-           fclose(file);
-           return -1;
-       }
-       *process_age_sec = process_age(proc_stt_jf);
+        unsigned long long proc_stt_jf = 0;
+        if (sscanf(endcomm, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %Lu",
+                   &proc_stt_jf) != 1)
+        {
+            return -1;
+        }
+        *process_age_sec = process_age(proc_stt_jf);
     }
-    (void) fclose (file);
-    return strlen(comm);
+    return lencomm;
 }
 
 static int