From: Craig Small Date: Sun, 11 Jun 2017 22:57:15 +0000 (+1000) Subject: killall: better parsing of command names X-Git-Tag: v23.0~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=349687afcf31b402edd19b90f58be1e7c287370e;p=psmisc killall: better parsing of command names 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. --- diff --git a/ChangeLog b/ChangeLog index 572c748..9c11b93 100644 --- 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 diff --git a/src/killall.c b/src/killall.c index a2fcabd..db5067b 100644 --- a/src/killall.c +++ b/src/killall.c @@ -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