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 //usage:#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
11 //usage:#define pidof_trivial_usage
12 //usage: "[OPTIONS] [NAME]..."
13 //usage:#define USAGE_PIDOF "\n"
15 //usage:#define pidof_trivial_usage
17 //usage:#define USAGE_PIDOF /* none */
19 //usage:#define pidof_full_usage "\n\n"
20 //usage: "List PIDs of all processes with names that match NAMEs"
22 //usage: IF_FEATURE_PIDOF_SINGLE(
23 //usage: "\n -s Show only one PID"
25 //usage: IF_FEATURE_PIDOF_OMIT(
26 //usage: "\n -o PID Omit given pid"
27 //usage: "\n Use %PPID to omit pid of pidof's parent"
30 //usage:#define pidof_example_usage
31 //usage: "$ pidof init\n"
33 //usage: IF_FEATURE_PIDOF_OMIT(
34 //usage: "$ pidof /bin/sh\n20351 5973 5950\n")
35 //usage: IF_FEATURE_PIDOF_OMIT(
36 //usage: "$ pidof /bin/sh -o %PPID\n20351 5950")
41 IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
42 IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
43 OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
44 OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
47 int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
48 int pidof_main(int argc UNUSED_PARAM, char **argv)
52 #if ENABLE_FEATURE_PIDOF_OMIT
53 llist_t *omits = NULL; /* list of pids to omit */
54 opt_complementary = "o::";
57 /* do unconditional option parsing */
58 opt = getopt32(argv, ""
59 IF_FEATURE_PIDOF_SINGLE ("s")
60 IF_FEATURE_PIDOF_OMIT("o:", &omits));
62 #if ENABLE_FEATURE_PIDOF_OMIT
65 llist_t *omits_p = omits;
67 omits_p = llist_find_str(omits_p, "%PPID");
70 /* are we asked to exclude the parent's process ID? */
71 omits_p->data = utoa((unsigned)getppid());
75 /* Looks like everything is set to go. */
81 /* reverse the pidlist like GNU pidof does. */
82 pidList = pidlist_reverse(find_pid_by_name(*argv));
83 for (pl = pidList; *pl; pl++) {
84 #if ENABLE_FEATURE_PIDOF_OMIT
86 llist_t *omits_p = omits;
88 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
91 omits_p = omits_p->link;
95 printf(" %u" + first, (unsigned)*pl);
97 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
99 #if ENABLE_FEATURE_PIDOF_OMIT
109 #if ENABLE_FEATURE_PIDOF_OMIT
110 if (ENABLE_FEATURE_CLEAN_UP)
111 llist_free(omits, NULL);
113 return first; /* 1 (failure) - no processes found */