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.
12 #if ENABLE_FEATURE_PIDOF_SINGLE
13 #define _SINGLE_COMPL(a) a
16 #define _SINGLE_COMPL(a)
20 #if ENABLE_FEATURE_PIDOF_OMIT
21 #define _OMIT_COMPL(a) a
23 #if ENABLE_FEATURE_PIDOF_SINGLE
29 #define _OMIT_COMPL(a) ""
35 int pidof_main(int argc, char **argv)
39 unsigned long int opt;
40 #if ENABLE_FEATURE_PIDOF_OMIT
41 llist_t *omits = NULL; /* list of pids to omit */
42 opt_complementary = _OMIT_COMPL("o::");
45 /* do unconditional option parsing */
46 opt = getopt32(argc, argv,
47 _SINGLE_COMPL("s") _OMIT_COMPL("o:")
50 #if ENABLE_FEATURE_PIDOF_OMIT
54 llist_t * omits_p = omits;
56 /* are we asked to exclude the parent's process ID? */
57 if (!strncmp(omits_p->data, "%PPID", 5)) {
59 snprintf(getppid_str, sizeof(getppid_str), "%ld", (long)getppid());
60 llist_add_to(&omits_p, getppid_str);
62 omits_p = omits_p->link;
66 /* Looks like everything is set to go. */
67 while (optind < argc) {
71 /* reverse the pidlist like GNU pidof does. */
72 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
73 for (pl = pidList; *pl > 0; pl++) {
74 #if ENABLE_FEATURE_PIDOF_OMIT
77 llist_t *omits_p = omits;
79 if (xatoul(omits_p->data) == *pl) {
82 omits_p = omits_p->link;
93 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
95 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
103 #if ENABLE_FEATURE_PIDOF_OMIT
104 if (ENABLE_FEATURE_CLEAN_UP)
105 llist_free(omits, NULL);
107 return fail ? EXIT_FAILURE : EXIT_SUCCESS;