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"
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))
34 //kbuild:lib-$(CONFIG_PIDOF) += pidof.o
36 //usage:#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
37 //usage:#define pidof_trivial_usage
38 //usage: "[OPTIONS] [NAME]..."
39 //usage:#define USAGE_PIDOF "\n"
41 //usage:#define pidof_trivial_usage
43 //usage:#define USAGE_PIDOF /* none */
45 //usage:#define pidof_full_usage "\n\n"
46 //usage: "List PIDs of all processes with names that match NAMEs"
48 //usage: IF_FEATURE_PIDOF_SINGLE(
49 //usage: "\n -s Show only one PID"
51 //usage: IF_FEATURE_PIDOF_OMIT(
52 //usage: "\n -o PID Omit given pid"
53 //usage: "\n Use %PPID to omit pid of pidof's parent"
56 //usage:#define pidof_example_usage
57 //usage: "$ pidof init\n"
59 //usage: IF_FEATURE_PIDOF_OMIT(
60 //usage: "$ pidof /bin/sh\n20351 5973 5950\n")
61 //usage: IF_FEATURE_PIDOF_OMIT(
62 //usage: "$ pidof /bin/sh -o %PPID\n20351 5950")
67 IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
68 IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
69 OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
70 OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
73 int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
74 int pidof_main(int argc UNUSED_PARAM, char **argv)
78 #if ENABLE_FEATURE_PIDOF_OMIT
79 llist_t *omits = NULL; /* list of pids to omit */
82 /* do unconditional option parsing */
83 opt = getopt32(argv, ""
84 IF_FEATURE_PIDOF_SINGLE ("s")
85 IF_FEATURE_PIDOF_OMIT("o:*", &omits));
87 #if ENABLE_FEATURE_PIDOF_OMIT
90 llist_t *omits_p = omits;
92 omits_p = llist_find_str(omits_p, "%PPID");
95 /* are we asked to exclude the parent's process ID? */
96 omits_p->data = utoa((unsigned)getppid());
100 /* Looks like everything is set to go. */
106 /* reverse the pidlist like GNU pidof does. */
107 pidList = pidlist_reverse(find_pid_by_name(*argv));
108 for (pl = pidList; *pl; pl++) {
109 #if ENABLE_FEATURE_PIDOF_OMIT
110 if (opt & OPT_OMIT) {
111 llist_t *omits_p = omits;
113 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
116 omits_p = omits_p->link;
120 printf(" %u" + first, (unsigned)*pl);
122 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
124 #if ENABLE_FEATURE_PIDOF_OMIT
134 #if ENABLE_FEATURE_PIDOF_OMIT
135 if (ENABLE_FEATURE_CLEAN_UP)
136 llist_free(omits, NULL);
138 return first; /* 1 (failure) - no processes found */