- move #include busybox.h to the very top so we pull in the config
[platform/upstream/busybox.git] / procps / pidof.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * pidof implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under the GPL v2, see the file LICENSE in this tarball.
8  */
9
10 #include "busybox.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include <signal.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 #if ENABLE_FEATURE_PIDOF_SINGLE
22 #define _SINGLE_COMPL(a) a
23 #define SINGLE (1<<0)
24 #else
25 #define _SINGLE_COMPL(a)
26 #define SINGLE (0)
27 #endif
28
29 #if ENABLE_FEATURE_PIDOF_OMIT
30 #define _OMIT_COMPL(a) a
31 #define _OMIT(a) ,a
32 #if ENABLE_FEATURE_PIDOF_SINGLE
33 #define OMIT (1<<1)
34 #else
35 #define OMIT (1<<0)
36 #endif
37 #else
38 #define _OMIT_COMPL(a) ""
39 #define _OMIT(a)
40 #define OMIT (0)
41 #define omitted (0)
42 #endif
43
44 int pidof_main(int argc, char **argv)
45 {
46         unsigned n = 0;
47         unsigned fail = 1;
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::");
52 #endif
53
54         /* do unconditional option parsing */
55         opt = bb_getopt_ulflags(argc, argv,
56                                         _SINGLE_COMPL("s") _OMIT_COMPL("o:")
57                                         _OMIT(&omits));
58
59 #if ENABLE_FEATURE_PIDOF_OMIT
60         /* fill omit list.  */
61         {
62                 char getppid_str[32];
63                 llist_t * omits_p = omits;
64                 while (omits_p) {
65                         /* are we asked to exclude the parent's process ID?  */
66                         if (!strncmp(omits_p->data, "%PPID", 5)) {
67                                 llist_pop(&omits_p);
68                                 snprintf(getppid_str, sizeof(getppid_str), "%d", getppid());
69                                 llist_add_to(&omits_p, getppid_str);
70                         }
71                         omits_p = omits_p->link;
72                 }
73         }
74 #endif
75         /* Looks like everything is set to go.  */
76         while(optind < argc) {
77                 long *pidList;
78                 long *pl;
79
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
84                         unsigned omitted = 0;
85                         if (opt & OMIT) {
86                                 llist_t *omits_p = omits;
87                                 while (omits_p)
88                                         if (strtol(omits_p->data, NULL, 10) == *pl) {
89                                                 omitted = 1; break;
90                                         } else
91                                                 omits_p = omits_p->link;
92                         }
93 #endif
94                         if (!omitted) {
95                                 if (n) {
96                                         putchar(' ');
97                                 } else {
98                                         n = 1;
99                                 }
100                                 printf("%ld", *pl);
101                         }
102                         fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
103
104                         if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
105                                 break;
106                 }
107                 free(pidList);
108                 optind++;
109         }
110         putchar('\n');
111
112 #if ENABLE_FEATURE_PIDOF_OMIT
113         if (ENABLE_FEATURE_CLEAN_UP)
114                 llist_free(omits, NULL);
115 #endif
116         return fail ? EXIT_FAILURE : EXIT_SUCCESS;
117 }