Skip to content

Commit

Permalink
killall: better handling of long names.
Browse files Browse the repository at this point in the history
Change names_to_pid() so that we can actually match shell scripts with
long names (the code to get the shell script's name was correct, but
there was an extra test preventing us from actually comparing it to the
sought name).

In kill.c itself, remove a dead test for -l and switch to the FLAG()
macro.

Also extend the tests to explicitly cover long and short names.
  • Loading branch information
enh-google authored and landley committed Jul 6, 2019
1 parent 3d8bbdc commit 82a33b3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))
cmd[len] = 0;
}
if (!strcmp(bb, getbasename(cmd))) goto match;
if (bb!=*cur && !strcmp(bb, getbasename(cmd+strlen(cmd)+1))) goto match;
if (!strcmp(bb, getbasename(cmd+strlen(cmd)+1))) goto match;
continue;
match:
if (callback(u, *cur)) break;
Expand Down
12 changes: 9 additions & 3 deletions tests/killall.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
#testing "name" "command" "result" "infile" "stdin"

echo "#!$(which sh)
yes > /dev/null" > toybox.killall.test.script
while true; do
sleep 0.1
done" > toybox.killall.test.script
chmod a+x toybox.killall.test.script
cp toybox.killall.test.script toybox.test

./toybox.test &
testing "short name" "killall toybox.test && echo killed ; pgrep -l toybox.test || echo really" "killed\nreally\n" "" ""

./toybox.killall.test.script &
testing "script" "killall toybox.killall.test.script && echo killed ; pgrep -l toybox.killall.test.script || echo really" "killed\nreally\n" "" ""
testing "long name" "killall toybox.killall.test.script && echo killed ; pgrep -l toybox.killall.test.script || echo really" "killed\nreally\n" "" ""

rm -f toybox.killall.test.script
rm -f toybox.killall.test.script toybox.test
12 changes: 6 additions & 6 deletions toys/lsb/killall.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static int kill_process(pid_t pid, char *name)

if (pid == TT.cur_pid) return 0;

if (toys.optflags & FLAG_i) {
if (FLAG(i)) {
fprintf(stderr, "Signal %s(%d)", name, (int)pid);
if (!yesno(0)) return 0;
}
Expand All @@ -53,8 +53,8 @@ static int kill_process(pid_t pid, char *name)
} else offset++;
}
if (errno) {
if (!(toys.optflags & FLAG_q)) perror_msg("pid %d", (int)pid);
} else if (toys.optflags & FLAG_v)
if (!FLAG(q)) perror_msg("pid %d", (int)pid);
} else if (FLAG(v))
printf("Killed %s(%d) with signal %d\n", name, pid, TT.signum);

return 0;
Expand All @@ -67,14 +67,14 @@ void killall_main(void)
TT.names = toys.optargs;
TT.signum = SIGTERM;

if (toys.optflags & FLAG_l) {
if (FLAG(l)) {
list_signals();
return;
}

if (TT.s || (*TT.names && **TT.names == '-')) {
if (0 > (TT.signum = sig_to_num(TT.s ? TT.s : (*TT.names)+1))) {
if (toys.optflags & FLAG_q) exit(1);
if (FLAG(q)) exit(1);
error_exit("Invalid signal");
}
if (!TT.s) {
Expand All @@ -83,7 +83,7 @@ void killall_main(void)
}
}

if (!(toys.optflags & FLAG_l) && !toys.optc) help_exit("no name");
if (!toys.optc) help_exit("no name");

TT.cur_pid = getpid();

Expand Down

0 comments on commit 82a33b3

Please sign in to comment.