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>
22 #if ENABLE_FEATURE_PIDOF_SINGLE
23 #define _SINGLE_COMPL(a) a
26 #define _SINGLE_COMPL(a)
30 #if ENABLE_FEATURE_PIDOF_OMIT
31 #define _OMIT_COMPL(a) a
33 #if ENABLE_FEATURE_PIDOF_SINGLE
39 #define _OMIT_COMPL(a) ""
45 extern int pidof_main(int argc, char **argv)
49 unsigned long int opt;
50 #if ENABLE_FEATURE_PIDOF_OMIT
51 llist_t *omits = NULL; /* list of pids to omit */
52 bb_opt_complementally = _OMIT_COMPL("o*");
55 /* do unconditional option parsing */
56 opt = bb_getopt_ulflags(argc, argv,
57 _SINGLE_COMPL("s") _OMIT_COMPL("o:")
60 #if ENABLE_FEATURE_PIDOF_OMIT
63 RESERVE_CONFIG_BUFFER(getppid_str, 32);
64 llist_t * omits_p = omits;
66 /* are we asked to exclude the parent's process ID? */
67 if (!strncmp(omits_p->data, "%PPID", 5)) {
68 omits_p = llist_free_one(omits_p);
69 snprintf(getppid_str, sizeof(getppid_str), "%d", getppid());
70 omits_p = llist_add_to(omits_p, getppid_str);
73 bb_error_msg_and_die("illegal omit pid value (%s)!\n",
77 omits_p = omits_p->link;
79 if (ENABLE_FEATURE_CLEAN_UP)
80 RELEASE_CONFIG_BUFFER(getppid_str);
83 /* Looks like everything is set to go. */
84 while(optind < argc) {
88 /* reverse the pidlist like GNU pidof does. */
89 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
90 for(pl = pidList; *pl > 0; pl++) {
91 #if ENABLE_FEATURE_PIDOF_OMIT
94 llist_t *omits_p = omits;
96 if (strtol(omits_p->data, NULL, 10) == *pl) {
99 omits_p = omits_p->link;
110 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
112 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
120 #if ENABLE_FEATURE_PIDOF_OMIT
121 if (ENABLE_FEATURE_CLEAN_UP)
124 return fail ? EXIT_FAILURE : EXIT_SUCCESS;