Found running LTP file system tests on Android.

Bug: http://b/70627145
---
 lib/lib.c          | 28 +++++++++++++++++++---------
 tests/killall.test | 14 ++++++++++++++
 2 files changed, 33 insertions(+), 9 deletions(-)
 create mode 100644 tests/killall.test
From 8ed2c141c3ef516ba0698d2a771e0869df09e276 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Wed, 13 Dec 2017 11:47:08 -0800
Subject: [PATCH] killall should kill scripts too.

Found running LTP file system tests on Android.

Bug: http://b/70627145
---
 lib/lib.c          | 28 +++++++++++++++++++---------
 tests/killall.test | 14 ++++++++++++++
 2 files changed, 33 insertions(+), 9 deletions(-)
 create mode 100644 tests/killall.test

diff --git a/lib/lib.c b/lib/lib.c
index 651593e..44a7cc7 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1010,6 +1010,12 @@ char *getbasename(char *name)
   return name;
 }
 
+static int argv0_match(char *cmd, char *name)
+{
+  return (*name == '/' ? !strcmp(cmd, name)
+      : !strcmp(getbasename(cmd), getbasename(name)));
+}
+
 // Execute a callback for each PID that matches a process name from a list.
 void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))
 {
@@ -1020,17 +1026,21 @@ void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))
 
   while ((entry = readdir(dp))) {
     unsigned u;
-    char *cmd, **curname;
+    char *cmd, *comm, **cur;
 
     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 (**curname == '/' ? !strcmp(cmd, *curname)
-          : !strcmp(getbasename(cmd), getbasename(*curname)))
-        if (callback(u, *curname)) break;
-    if (*curname) break;
+
+    // For a script, comm and argv[1] will match (argv[0] will be the interp).
+    sprintf(libbuf, "/proc/%u/comm", u);
+    if (!(comm = readfile(libbuf, libbuf, sizeof(libbuf)))) continue;
+    sprintf(libbuf+16, "/proc/%u/cmdline", u);
+    if (!(cmd = readfile(libbuf+16, libbuf+16, sizeof(libbuf)-16))) continue;
+
+    for (cur = names; *cur; cur++)
+      if (argv0_match(cmd, *cur) ||
+          (!strncmp(comm, *cur, 15) && argv0_match(cmd+strlen(cmd)+1, *cur)))
+        if (callback(u, *cur)) break;
+    if (*cur) break;
   }
   closedir(dp);
 }
diff --git a/tests/killall.test b/tests/killall.test
new file mode 100644
index 0000000..100ac7c
--- /dev/null
+++ b/tests/killall.test
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+echo "#!/bin/sh
+yes > /dev/null" > toybox.killall.test.script
+chmod a+x toybox.killall.test.script
+
+./toybox.killall.test.script &
+testing "script" "killall toybox.killall.test.script && echo killed ; pgrep -l toybox.killall.test.script || echo really" "killed\nreally\n" "" ""
+
+rm -f toybox.killall.test.script
-- 
2.15.1.504.g5279b80103-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to