# HG changeset patch # User Rob Landley # Date 1378251812 18000 # Node ID 242c5de2bb22395ded8d0d80ff6d686f41c3d01b # Parent 2b35f9c797ad097acec5c980a42068500da18ffa 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. diff -r 2b35f9c797ad -r 242c5de2bb22 lib/pending.c --- a/lib/pending.c Thu Sep 05 23:58:35 2013 -0500 +++ b/lib/pending.c Tue Sep 03 18:43:32 2013 -0500 @@ -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; - - if (!isdigit(*entry->d_name)) continue; + unsigned u; + char *cmd, **curname; - 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); } diff -r 2b35f9c797ad -r 242c5de2bb22 toys/lsb/killall.c --- a/toys/lsb/killall.c Thu Sep 05 23:58:35 2013 -0500 +++ b/toys/lsb/killall.c Tue Sep 03 18:43:32 2013 -0500 @@ -32,21 +32,20 @@ { int ret; - if (pid == TT.cur_pid) return 1; + if (pid == TT.cur_pid) return 0; if (toys.optflags & FLAG_i) { sprintf(toybuf, "Signal %s(%d) ?", name, pid); - if (yesno(toybuf, 0) == 0) return 1; + if (yesno(toybuf, 0) == 0) return 0; } - toys.exitval = 0; - ret = kill(pid, TT.signum); - if (toys.optflags & FLAG_v) + if (ret == -1 && !(toys.optflags & FLAG_q)) + error_msg("bad %u", (unsigned)pid); + else if (toys.optflags & FLAG_v) printf("Killed %s(%d) with signal %d\n", name, pid, TT.signum); - if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill"); - return 1; + return 0; } void killall_main(void) @@ -76,7 +75,7 @@ TT.cur_pid = getpid(); - for_each_pid_with_name_in(names, kill_process); + names_to_pid(names, kill_process); if (toys.exitval && !(toys.optflags & FLAG_q)) error_exit("No such process"); } diff -r 2b35f9c797ad -r 242c5de2bb22 toys/lsb/pidof.c --- a/toys/lsb/pidof.c Thu Sep 05 23:58:35 2013 -0500 +++ b/toys/lsb/pidof.c Tue Sep 03 18:43:32 2013 -0500 @@ -43,12 +43,12 @@ xprintf("%*s", len+(!toys.exitval), toybuf); toys.exitval = 0; - return !(toys.optflags & FLAG_s); + return toys.optflags & FLAG_s; } void pidof_main(void) { toys.exitval = 1; - for_each_pid_with_name_in(toys.optargs, print_pid); + name_to_pid(toys.optargs, print_pid); if (!toys.exitval) xputc('\n'); }