diff options
author | Rob Landley <rob@landley.net> | 2013-09-03 18:43:32 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2013-09-03 18:43:32 -0500 |
commit | 7d64dae54bde70744a9154b8ac1cbb09d03881c9 (patch) | |
tree | 1ee92b4e0254f07b71e1d51227ede4116db7b51c /lib | |
parent | b7ca39c9473ecc3f5fbb71e2c9e40ad99b79e03e (diff) | |
download | toybox-7d64dae54bde70744a9154b8ac1cbb09d03881c9.tar.gz |
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pending.c | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/lib/pending.c b/lib/pending.c index 9e250854..a06912ee 100644 --- a/lib/pending.c +++ b/lib/pending.c @@ -6,33 +6,28 @@ #include "toys.h" // Execute a callback for each PID that matches a process name from a list. -void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid, char *name)) +void name_to_pid(char **names, int (*callback)(pid_t pid, char *name)) { DIR *dp; struct dirent *entry; - char cmd[sizeof(toybuf)], path[64]; - char **curname; if (!(dp = opendir("/proc"))) perror_exit("opendir"); while ((entry = readdir(dp))) { int fd, n; + unsigned u; + char *cmd, **curname; - if (!isdigit(*entry->d_name)) continue; - - if (sizeof(path) <= snprintf(path, sizeof(path), "/proc/%s/cmdline", - entry->d_name)) continue; - - if (-1 == (fd=open(path, O_RDONLY))) continue; - n = read(fd, cmd, sizeof(cmd)); - close(fd); - if (n<1) continue; + if (!(u = atoi(entry->d_name))) continue; + sprintf(libbuf, "/proc/%u/cmdline", u); + if (!(cmd = readfile(libbuf, libbuf, sizeof(libbuf)))) continue; for (curname = names; *curname; curname++) - if (!strcmp(basename(cmd), *curname)) - if (!callback(atol(entry->d_name), *curname)) goto done; + if (*curname == '/' ? !strcmp(cmd, *curname) + : !strcmp(basename(cmd), basename(*curname)) + if (!callback(u, *curname)) break; + if (*curname) break; } -done: closedir(dp); } |