Hello all,

Toybox currently has the pidof command, but it lacks the -s (single shot) and -o (omit pid) options. I've added these in the patch in attach. A short demo:

$ ./toybox pidof bash
383 511 643 771 907 1047 1409 1637 1933 2069 3934 3958 5168 5704 6476 6968 7072 7347 7594 7837 8404 12553 13804 19662 26146 28619 28798 29596 29845 29858 29983 32088 32414 32571 32737

$ ./toybox pidof -s bash
383
$ ./toybox pidof -o 383,511,6,64,43,32737 bash
643 771 907 1047 1409 1637 1933 2069 3934 3958 5168 5704 6476 6968 7072 7347 7594 7837 8404 12553 13804 19662 26146 28619 28798 29596 29845 29858 29983 32088 32414 32571

(Observe first, second and last entry are gone but 643 is still present).

Both together:
$ ./toybox pidof -s -o 383,511,6,64,43,32737 bash
643

Both these options are present in the LSB specification ( http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html ) only comment there is that the document specifies -o as "[-o omitpid...]" while man 8 pidof on my box allows "[-o omitpid[,omitpid..]] [-o omitpid[,omit‐pid..]..]" while I implemented it as a single [-o omitpid[,omitpid..]..].

my 2 cents
E.



--
Elie De Brauwer

# HG changeset patch
# User Elie De Brauwer <[email protected]>
# Date 1354993805 -3600
# Node ID 732e9495bab96c2e4eb5afcec4db5399e859f055
# Parent  78dbbc585e5fdc3953bed44b975f74f7f9902986
Adding -s (single shot) and -o (omit pids) options to pidof

diff -r 78dbbc585e5f -r 732e9495bab9 lib/lib.c
--- a/lib/lib.c	Sat Dec 08 14:40:17 2012 +0100
+++ b/lib/lib.c	Sat Dec 08 20:10:05 2012 +0100
@@ -982,7 +982,7 @@
 }
 
 // Execute a callback for each PID that matches a process name from a list.
-void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid))
+void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid))
 {
   DIR *dp;
   struct dirent *entry;
@@ -1005,9 +1005,10 @@
     if (n<1) continue;
 
     for (curname = names; *curname; curname++)
-      if (!strcmp(basename(cmd), *curname)) callback(atol(entry->d_name));
+      if (!strcmp(basename(cmd), *curname)) 
+          if (!callback(atol(entry->d_name))) goto done;
   }
-
+done:
   closedir(dp);
 }
 
diff -r 78dbbc585e5f -r 732e9495bab9 lib/lib.h
--- a/lib/lib.h	Sat Dec 08 14:40:17 2012 +0100
+++ b/lib/lib.h	Sat Dec 08 20:10:05 2012 +0100
@@ -144,7 +144,7 @@
 void crc_init(unsigned int *crc_table, int little_endian);
 void terminal_size(unsigned *x, unsigned *y);
 int yesno(char *prompt, int def);
-void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid));
+void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid));
 unsigned long xstrtoul(const char *nptr, char **endptr, int base);
 
 // getmountlist.c
diff -r 78dbbc585e5f -r 732e9495bab9 toys/lsb/killall.c
--- a/toys/lsb/killall.c	Sat Dec 08 14:40:17 2012 +0100
+++ b/toys/lsb/killall.c	Sat Dec 08 20:10:05 2012 +0100
@@ -25,7 +25,7 @@
   int signum;
 )
 
-static void kill_process(pid_t pid)
+static int kill_process(pid_t pid)
 {
   int ret;
 
@@ -33,6 +33,7 @@
   ret = kill(pid, TT.signum);
 
   if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill");
+  return 1;
 }
 
 void killall_main(void)
diff -r 78dbbc585e5f -r 732e9495bab9 toys/lsb/pidof.c
--- a/toys/lsb/pidof.c	Sat Dec 08 14:40:17 2012 +0100
+++ b/toys/lsb/pidof.c	Sat Dec 08 20:10:05 2012 +0100
@@ -1,26 +1,54 @@
 /* pidof.c - Print the Process IDs of all processes with the given names.
  *
  * Copyright 2012 Andreas Heck <[email protected]>
+ * Copyright 2012 Elie De Brauwer <[email protected]>
  *
  * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html
 
-USE_PIDOF(NEWTOY(pidof, "<1", TOYFLAG_USR|TOYFLAG_BIN))
+USE_PIDOF(NEWTOY(pidof, "so:<1", TOYFLAG_USR|TOYFLAG_BIN))
 
 config PIDOF
   bool "pidof"
   default y
   help
-    usage: pidof [NAME]...
+    usage: pidof [-s] [-o omitpid[,omitpid..]] [NAME]...
 
     Print the PIDs of all processes with the given names.
+    -s	single shot, only return one pid.
+    -o	omits processes with specified PID
 */
 
+#define FOR_pidof
 #include "toys.h"
 
-static void print_pid(pid_t pid)
+GLOBALS(
+  char *omit;
+)
+
+static int print_pid(pid_t pid)
 {
+
+  if (toys.optflags & FLAG_o)
+  {
+      char * res;
+      int len;
+      snprintf(toybuf, sizeof(toybuf), "%d", pid);
+      len = strlen(toybuf);
+      res = strstr(TT.omit, toybuf);
+      if (res &&
+          (res == TT.omit || res[-1] == ',') &&
+          (res[len] == ',' || res[len] == 0))
+          // Found in omit string
+          return 1;
+  }
+
   xprintf("%s%ld", toys.exitval ? "" : " ", (long)pid);
   toys.exitval = 0;
+
+  if (toys.optflags & FLAG_s)
+      return 0;
+
+  return 1;
 }
 
 void pidof_main(void)
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to