Fiddling with llist to make memory management easier. Specifically, the
[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
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 #include "busybox.h"
21
22 #if ENABLE_FEATURE_PIDOF_SINGLE
23 #define _SINGLE_COMPL(a) a
24 #define SINGLE (1<<0)
25 #else
26 #define _SINGLE_COMPL(a)
27 #define SINGLE (0)
28 #endif
29
30 #if ENABLE_FEATURE_PIDOF_OMIT
31 #define _OMIT_COMPL(a) a
32 #define _OMIT(a) ,a
33 #if ENABLE_FEATURE_PIDOF_SINGLE
34 #define OMIT (1<<1)
35 #else
36 #define OMIT (1<<0)
37 #endif
38 #else
39 #define _OMIT_COMPL(a) ""
40 #define _OMIT(a)
41 #define OMIT (0)
42 #define omitted (0)
43 #endif
44
45 int pidof_main(int argc, char **argv)
46 {
47         unsigned n = 0;
48         unsigned fail = 1;
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::");
53 #endif
54
55         /* do unconditional option parsing */
56         opt = bb_getopt_ulflags(argc, argv,
57                                         _SINGLE_COMPL("s") _OMIT_COMPL("o:")
58                                         _OMIT(&omits));
59
60 #if ENABLE_FEATURE_PIDOF_OMIT
61         /* fill omit list.  */
62         {
63                 char getppid_str[32];
64                 llist_t * omits_p = omits;
65                 while (omits_p) {
66                         /* are we asked to exclude the parent's process ID?  */
67                         if (!strncmp(omits_p->data, "%PPID", 5)) {
68                                 llist_pop(&omits_p);
69                                 snprintf(getppid_str, sizeof(getppid_str), "%d", getppid());
70                                 omits_p = llist_add_to(omits_p, getppid_str);
71 #if 0
72                         } else {
73                                 bb_error_msg_and_die("illegal omit pid value (%s)!\n",
74                                                 omits_p->data);
75 #endif
76                         }
77                         omits_p = omits_p->link;
78                 }
79         }
80 #endif
81         /* Looks like everything is set to go.  */
82         while(optind < argc) {
83                 long *pidList;
84                 long *pl;
85
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
90                         unsigned omitted = 0;
91                         if (opt & OMIT) {
92                                 llist_t *omits_p = omits;
93                                 while (omits_p)
94                                         if (strtol(omits_p->data, NULL, 10) == *pl) {
95                                                 omitted = 1; break;
96                                         } else
97                                                 omits_p = omits_p->link;
98                         }
99 #endif
100                         if (!omitted) {
101                                 if (n) {
102                                         putchar(' ');
103                                 } else {
104                                         n = 1;
105                                 }
106                                 printf("%ld", *pl);
107                         }
108                         fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
109
110                         if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
111                                 break;
112                 }
113                 free(pidList);
114                 optind++;
115         }
116         putchar('\n');
117
118 #if ENABLE_FEATURE_PIDOF_OMIT
119         if (ENABLE_FEATURE_CLEAN_UP)
120                 llist_free(omits, NULL);
121 #endif
122         return fail ? EXIT_FAILURE : EXIT_SUCCESS;
123 }