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 v2, see the file LICENSE in this tarball.
18 #include <sys/types.h>
21 #if ENABLE_FEATURE_PIDOF_SINGLE
22 #define _SINGLE_COMPL(a) a
25 #define _SINGLE_COMPL(a)
29 #if ENABLE_FEATURE_PIDOF_OMIT
30 #define _OMIT_COMPL(a) a
32 #if ENABLE_FEATURE_PIDOF_SINGLE
38 #define _OMIT_COMPL(a) ""
44 int pidof_main(int argc, char **argv)
48 unsigned long int opt;
49 #if ENABLE_FEATURE_PIDOF_OMIT
50 llist_t *omits = NULL; /* list of pids to omit */
51 bb_opt_complementally = _OMIT_COMPL("o::");
54 /* do unconditional option parsing */
55 opt = bb_getopt_ulflags(argc, argv,
56 _SINGLE_COMPL("s") _OMIT_COMPL("o:")
59 #if ENABLE_FEATURE_PIDOF_OMIT
63 llist_t * omits_p = omits;
65 /* are we asked to exclude the parent's process ID? */
66 if (!strncmp(omits_p->data, "%PPID", 5)) {
68 snprintf(getppid_str, sizeof(getppid_str), "%d", getppid());
69 llist_add_to(&omits_p, getppid_str);
71 omits_p = omits_p->link;
75 /* Looks like everything is set to go. */
76 while(optind < argc) {
80 /* reverse the pidlist like GNU pidof does. */
81 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
82 for(pl = pidList; *pl > 0; pl++) {
83 #if ENABLE_FEATURE_PIDOF_OMIT
86 llist_t *omits_p = omits;
88 if (strtol(omits_p->data, NULL, 10) == *pl) {
91 omits_p = omits_p->link;
102 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
104 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
112 #if ENABLE_FEATURE_PIDOF_OMIT
113 if (ENABLE_FEATURE_CLEAN_UP)
114 llist_free(omits, NULL);
116 return fail ? EXIT_FAILURE : EXIT_SUCCESS;