[Toybox] [patch] lib.c - improve names_to_pid().

Hyejin Kim hj8296 at gmail.com
Wed Oct 7 18:25:20 PDT 2015


Hi.

toybox's names_to_pid() func has some limitation because it just checks
/proc/%u/cmdline and its first string only.
so, script executable can't be handled in this function.

pidof, killall, bootchartd 3 commands are using this and all are considered
by my improvement.
Further, an embedded target verified all the operation of killall.

plz, have a look.





 lib/lib.c | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)
diff --git a/lib/lib.c b/lib/lib.c
index d9bea89..8af4116 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -867,16 +867,42 @@ void names_to_pid(char **names, int (*callback)(pid_t
pid, char *name))

   while ((entry = readdir(dp))) {
     unsigned u;
-    char *cmd, **curname;
+    char *cmd, *arg, **curname;

     if (!(u = atoi(entry->d_name))) continue;
-    sprintf(libbuf, "/proc/%u/cmdline", u);
+
+    // check stat
+    snprintf(libbuf, sizeof(libbuf), "/proc/%u/stat", u);
     if (!(cmd = readfile(libbuf, libbuf, sizeof(libbuf)))) continue;

-    for (curname = names; *curname; curname++)
-      if (**curname == '/' ? !strcmp(cmd, *curname)
-          : !strcmp(basename_r(cmd), basename_r(*curname)))
+    cmd = strchr(cmd, '(')+1;
+    *strrchr(cmd, ')') = '\0';
+    for (curname = names; *curname; curname++) {
+      int fd, len;
+      char *fcurname = basename_r(*curname);
+      if (!strcmp(cmd, fcurname)) {
         if (callback(u, *curname)) break;
+  else continue;
+      }
+
+      // check cmdline
+      snprintf(libbuf, sizeof(libbuf), "/proc/%u/cmdline", u);
+   if (-1 == (fd = openat(AT_FDCWD, libbuf, O_RDONLY))) continue;
+      len = readall(fd, libbuf, sizeof(libbuf));
+   close(fd);
+   if (len <= 0) continue;
+
+   libbuf[len] = 0;
+   cmd = libbuf;
+
+      if (!strcmp(basename_r(cmd), fcurname)) {
+        if (callback(u, *curname)) break;
+   } else if (len > strlen(cmd)) {
+        arg = basename_r(cmd + strlen(cmd) + 1);
+        if (arg && !strcmp(arg, fcurname))
+          if (callback(u, *curname)) break;
+   }
+    }
     if (*curname) break;
   }
   closedir(dp);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20151008/4bebfd8f/attachment-0003.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-patch-lib.c-improve-names_to_pid.patch
Type: application/octet-stream
Size: 1940 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20151008/4bebfd8f/attachment-0004.obj>


More information about the Toybox mailing list