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
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;
81 /* Looks like everything is set to go. */
82 while(optind < argc) {
86 /* reverse the pidlist like GNU pidof does. */
87 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
88 for(pl = pidList; *pl > 0; pl++) {
89 #if ENABLE_FEATURE_PIDOF_OMIT
92 llist_t *omits_p = omits;
94 if (strtol(omits_p->data, NULL, 10) == *pl) {
97 omits_p = omits_p->link;
108 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
110 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
118 #if ENABLE_FEATURE_PIDOF_OMIT
119 if (ENABLE_FEATURE_CLEAN_UP)
122 return fail ? EXIT_FAILURE : EXIT_SUCCESS;