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 the GPL version 2, see the file LICENSE in this tarball.
13 USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
14 USE_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
15 OPT_SINGLE = USE_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
16 OPT_OMIT = USE_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
19 int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
20 int pidof_main(int argc ATTRIBUTE_UNUSED, char **argv)
24 #if ENABLE_FEATURE_PIDOF_OMIT
25 char ppid_str[sizeof(int)*3 + 1];
26 llist_t *omits = NULL; /* list of pids to omit */
27 opt_complementary = "o::";
30 /* do unconditional option parsing */
31 opt = getopt32(argv, ""
32 USE_FEATURE_PIDOF_SINGLE ("s")
33 USE_FEATURE_PIDOF_OMIT("o:", &omits));
35 #if ENABLE_FEATURE_PIDOF_OMIT
38 llist_t *omits_p = omits;
40 /* are we asked to exclude the parent's process ID? */
41 if (strcmp(omits_p->data, "%PPID") == 0) {
42 sprintf(ppid_str, "%u", (unsigned)getppid());
43 omits_p->data = ppid_str;
45 omits_p = omits_p->link;
49 /* Looks like everything is set to go. */
55 /* reverse the pidlist like GNU pidof does. */
56 pidList = pidlist_reverse(find_pid_by_name(*argv));
57 for (pl = pidList; *pl; pl++) {
58 #if ENABLE_FEATURE_PIDOF_OMIT
60 llist_t *omits_p = omits;
62 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
65 omits_p = omits_p->link;
69 printf(" %u" + first, (unsigned)*pl);
71 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
73 #if ENABLE_FEATURE_PIDOF_OMIT
83 #if ENABLE_FEATURE_PIDOF_OMIT
84 if (ENABLE_FEATURE_CLEAN_UP)
85 llist_free(omits, NULL);
87 return first; /* 1 (failure) - no processes found */