1 /* vi: set sw=4 ts=4: */
3 * pidof implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 //config: bool "pidof (6.6 kb)"
13 //config: Pidof finds the process id's (pids) of the named programs. It prints
14 //config: those id's on the standard output.
16 //config:config FEATURE_PIDOF_SINGLE
17 //config: bool "Enable single shot (-s)"
19 //config: depends on PIDOF
21 //config: Support '-s' for returning only the first pid found.
23 //config:config FEATURE_PIDOF_OMIT
24 //config: bool "Enable omitting pids (-o PID)"
26 //config: depends on PIDOF
28 //config: Support '-o PID' for omitting the given pid(s) in output.
29 //config: The special pid %PPID can be used to name the parent process
30 //config: of the pidof, in other words the calling shell or shell script.
32 //applet:IF_PIDOF(APPLET(pidof, BB_DIR_BIN, BB_SUID_DROP))
33 /* can't be noexec: can find _itself_ under wrong name, since after fork only,
34 * /proc/PID/cmdline and comm are wrong! Can fix comm (prctl(PR_SET_NAME)),
38 //kbuild:lib-$(CONFIG_PIDOF) += pidof.o
40 //usage:#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
41 //usage:#define pidof_trivial_usage
42 //usage: "[OPTIONS] [NAME]..."
43 //usage:#define USAGE_PIDOF "\n"
45 //usage:#define pidof_trivial_usage
47 //usage:#define USAGE_PIDOF /* none */
49 //usage:#define pidof_full_usage "\n\n"
50 //usage: "List PIDs of all processes with names that match NAMEs"
52 //usage: IF_FEATURE_PIDOF_SINGLE(
53 //usage: "\n -s Show only one PID"
55 //usage: IF_FEATURE_PIDOF_OMIT(
56 //usage: "\n -o PID Omit given pid"
57 //usage: "\n Use %PPID to omit pid of pidof's parent"
60 //usage:#define pidof_example_usage
61 //usage: "$ pidof init\n"
63 //usage: IF_FEATURE_PIDOF_OMIT(
64 //usage: "$ pidof /bin/sh\n20351 5973 5950\n")
65 //usage: IF_FEATURE_PIDOF_OMIT(
66 //usage: "$ pidof /bin/sh -o %PPID\n20351 5950")
71 IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
72 IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
73 OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
74 OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
77 int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78 int pidof_main(int argc UNUSED_PARAM, char **argv)
82 #if ENABLE_FEATURE_PIDOF_OMIT
83 llist_t *omits = NULL; /* list of pids to omit */
86 /* do unconditional option parsing */
87 opt = getopt32(argv, ""
88 IF_FEATURE_PIDOF_SINGLE ("s")
89 IF_FEATURE_PIDOF_OMIT("o:*", &omits));
91 #if ENABLE_FEATURE_PIDOF_OMIT
94 llist_t *omits_p = omits;
96 omits_p = llist_find_str(omits_p, "%PPID");
99 /* are we asked to exclude the parent's process ID? */
100 omits_p->data = utoa((unsigned)getppid());
104 /* Looks like everything is set to go. */
110 /* reverse the pidlist like GNU pidof does. */
111 pidList = pidlist_reverse(find_pid_by_name(*argv));
112 for (pl = pidList; *pl; pl++) {
113 #if ENABLE_FEATURE_PIDOF_OMIT
114 if (opt & OPT_OMIT) {
115 llist_t *omits_p = omits;
117 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
120 omits_p = omits_p->link;
124 printf(" %u" + first, (unsigned)*pl);
126 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
128 #if ENABLE_FEATURE_PIDOF_OMIT
138 #if ENABLE_FEATURE_PIDOF_OMIT
139 if (ENABLE_FEATURE_CLEAN_UP)
140 llist_free(omits, NULL);
142 return first; /* 1 (failure) - no processes found */