1 /* pidof.c - Print the Process IDs of all processes with the given names.
2 *
3 * Copyright 2012 Andreas Heck <aheck@gmx.de>
4 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
5 *
6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html
7
8 USE_PIDOF(NEWTOY(pidof, "<1so:", TOYFLAG_BIN))
9
10 config PIDOF
11 bool "pidof"
12 default y
13 help
14 usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...
15
16 Print the PIDs of all processes with the given names.
17
18 -s single shot, only return one pid.
19 -o omit PID(s)
20 */
21
22 #define FOR_pidof
23 #include "toys.h"
24
GLOBALS(char * omit;)25 GLOBALS(
26 char *omit;
27 )
28
29 static int print_pid(pid_t pid, char *name)
30 {
31 char * res;
32 int len;
33
34 sprintf(toybuf, "%d", (int)pid);
35 len = strlen(toybuf);
36
37 // Check omit string
38 if (TT.omit && (res = strstr(TT.omit, toybuf)))
39 if ((res == TT.omit || res[-1] == ',') &&
40 (res[len] == ',' || !res[len])) return 0;
41
42 xprintf("%*s", len+(!toys.exitval), toybuf);
43 toys.exitval = 0;
44
45 return toys.optflags & FLAG_s;
46 }
47
pidof_main(void)48 void pidof_main(void)
49 {
50 toys.exitval = 1;
51 names_to_pid(toys.optargs, print_pid);
52 if (!toys.exitval) xputc('\n');
53 }
54