OSDN Git Service

Adding -s (single shot) and -o (omit pids) options to pidof
authorElie De Brauwer <eliedebrauwer@gmail.com>
Sat, 8 Dec 2012 19:10:05 +0000 (20:10 +0100)
committerElie De Brauwer <eliedebrauwer@gmail.com>
Sat, 8 Dec 2012 19:10:05 +0000 (20:10 +0100)
lib/lib.c
lib/lib.h
toys/lsb/killall.c
toys/lsb/pidof.c

index f452bf3..148a765 100644 (file)
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -975,7 +975,7 @@ int yesno(char *prompt, int def)
 }
 
 // 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;
@@ -998,9 +998,10 @@ void for_each_pid_with_name_in(char **names, void (*callback)(pid_t pid))
     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);
 }
 
index 1c0350e..7cc4d5c 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -144,7 +144,7 @@ void replace_tempfile(int fdin, int fdout, char **tempname);
 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
index debc3df..2294f61 100644 (file)
@@ -25,7 +25,7 @@ GLOBALS(
   int signum;
 )
 
-static void kill_process(pid_t pid)
+static int kill_process(pid_t pid)
 {
   int ret;
 
@@ -33,6 +33,7 @@ static void kill_process(pid_t pid)
   ret = kill(pid, TT.signum);
 
   if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill");
+  return 1;
 }
 
 void killall_main(void)
index 0364301..8aeb33e 100644 (file)
@@ -1,26 +1,54 @@
 /* pidof.c - Print the Process IDs of all processes with the given names.
  *
  * Copyright 2012 Andreas Heck <aheck@gmx.de>
+ * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
  *
  * 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)