diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-03-17 03:05:42 +0300 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-03-17 03:05:42 +0300 |
commit | 8a6dbf6709e032bd7e91fc2e12a8babab96c59c2 (patch) | |
tree | c6c558f245b95d9b080d9df6f33d85c3438b968f /libbb/find_pid_by_name.c | |
parent | e2a3cd15a859cd419a9920450b60453402f945ee (diff) |
Patch from Pierre PEIFFER <pierre.peiffer@sxb.bsf.alcatel.fr>
that copes with the fact that processes may have been swapped
out.
Diffstat (limited to 'libbb/find_pid_by_name.c')
-rw-r--r-- | libbb/find_pid_by_name.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c index a22ee1ff8..ea1cba65b 100644 --- a/libbb/find_pid_by_name.c +++ b/libbb/find_pid_by_name.c @@ -32,6 +32,8 @@ #include <stdlib.h> #include "libbb.h" +#define READ_BUF_SIZE 50 + /* For Erik's nifty devps device driver */ #ifdef BB_FEATURE_USE_DEVPS_PATCH @@ -131,22 +133,27 @@ extern pid_t* find_pid_by_name( char* pidName) while ((next = readdir(dir)) != NULL) { FILE *status; - char filename[256]; - char buffer[256]; + char filename[READ_BUF_SIZE]; + char buffer[READ_BUF_SIZE]; + char name[READ_BUF_SIZE]; /* If it isn't a number, we don't want it */ if (!isdigit(*next->d_name)) continue; - sprintf(filename, "/proc/%s/cmdline", next->d_name); - status = fopen(filename, "r"); - if (!status) { + sprintf(filename, "/proc/%s/status", next->d_name); + if (! (status = fopen(filename, "r")) ) { + continue; + } + if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) { + fclose(status); continue; } - fgets(buffer, 256, status); fclose(status); - if (strstr(get_last_path_component(buffer), pidName) != NULL) { + /* Buffer should contain a string like "Name: binary_name" */ + sscanf(buffer, "%*s %s", name); + if (strcmp(name, pidName) == 0) { pidList=xrealloc( pidList, sizeof(pid_t) * (i+2)); pidList[i++]=strtol(next->d_name, NULL, 0); } |