pidof/killall: fix bug 625 (kernel threads get stale "binary name")

function                                             old     new   delta
procps_scan                                         1622    1642     +20

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2009-09-22 23:06:07 +02:00
parent 200d522fb8
commit ed4ff0e8cb

View File

@ -223,14 +223,16 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
}
#endif
/* After this point we have to break, not continue
* ("continue" would mean that current /proc/NNN
* is not a valid process info) */
/* After this point we can:
* "break": stop parsing, return the data
* "continue": try next /proc/XXX
*/
memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
sp->pid = pid;
if (!(flags & ~PSSCAN_PID)) break;
if (!(flags & ~PSSCAN_PID))
break; /* we needed only pid, we got it */
#if ENABLE_SELINUX
if (flags & PSSCAN_CONTEXT) {
@ -243,7 +245,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
if (flags & PSSCAN_UIDGID) {
if (stat(filename, &sb))
break;
continue; /* process probably exited */
/* Effective UID/GID, not real */
sp->uid = sb.st_uid;
sp->gid = sb.st_gid;
@ -259,10 +261,10 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
strcpy(filename_tail, "stat");
n = read_to_buf(filename, buf);
if (n < 0)
break;
continue; /* process probably exited */
cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
/*if (!cp || cp[1] != ' ')
break;*/
continue;*/
cp[0] = '\0';
if (sizeof(sp->comm) < 16)
BUG_comm_size();
@ -302,7 +304,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
);
if (n < 11)
break;
continue; /* bogus data, get next /proc/XXX */
# if ENABLE_FEATURE_TOP_SMP_PROCESS
if (n < 11+15)
sp->last_seen_on_cpu = 0;
@ -368,8 +370,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
strcpy(filename_tail, "smaps");
file = fopen_for_read(filename);
if (!file)
break;
if (file) {
while (fgets(buf, sizeof(buf), file)) {
unsigned long sz;
char *tp;
@ -411,6 +412,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
}
fclose(file);
}
}
#endif /* TOPMEM */
#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
if (flags & PSSCAN_RUIDGID) {
@ -418,8 +420,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
strcpy(filename_tail, "status");
file = fopen_for_read(filename);
if (!file)
break;
if (file) {
while (fgets(buf, sizeof(buf), file)) {
char *tp;
#define SCAN_TWO(str, name, statement) \
@ -434,7 +435,19 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
}
fclose(file);
}
}
#endif /* PS_ADDITIONAL_COLUMNS */
if (flags & PSSCAN_EXE) {
strcpy(filename_tail, "exe");
free(sp->exe);
sp->exe = xmalloc_readlink(filename);
}
/* Note: if /proc/PID/cmdline is empty,
* code below "breaks". Therefore it must be
* the last code to parse /proc/PID/xxx data
* (we used to have /proc/PID/exe parsing after it
* and were getting stale sp->exe).
*/
#if 0 /* PSSCAN_CMD is not used */
if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
free(sp->argv0);
@ -477,13 +490,9 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
}
}
#endif
if (flags & PSSCAN_EXE) {
strcpy(filename_tail, "exe");
free(sp->exe);
sp->exe = xmalloc_readlink(filename);
}
break;
}
} /* for (;;) */
return sp;
}