aboutsummaryrefslogtreecommitdiff
path: root/procps
diff options
context:
space:
mode:
Diffstat (limited to 'procps')
-rw-r--r--procps/Config.in16
-rw-r--r--procps/pidof.c126
2 files changed, 112 insertions, 30 deletions
diff --git a/procps/Config.in b/procps/Config.in
index d17bbcde7..b4832db6b 100644
--- a/procps/Config.in
+++ b/procps/Config.in
@@ -37,6 +37,22 @@ config CONFIG_PIDOF
37 Pidof finds the process id's (pids) of the named programs. It prints 37 Pidof finds the process id's (pids) of the named programs. It prints
38 those id's on the standard output. 38 those id's on the standard output.
39 39
40config CONFIG_FEATURE_PIDOF_SINGLE
41 bool " Enable argument for single shot (-s)"
42 default n
43 depends on CONFIG_PIDOF
44 help
45 Support argument '-s' for returning only the first pid found.
46
47config CONFIG_FEATURE_PIDOF_OMIT
48 bool " Enable argument for omitting pids (-o)"
49 default n
50 depends on CONFIG_PIDOF
51 help
52 Support argument '-o' for omitting the given pids in output.
53 The special pid %PPID can be used to name the parent process
54 of the pidof, in other words the calling shell or shell script.
55
40config CONFIG_PS 56config CONFIG_PS
41 bool "ps" 57 bool "ps"
42 default n 58 default n
diff --git a/procps/pidof.c b/procps/pidof.c
index 413864a37..6f959e4ad 100644
--- a/procps/pidof.c
+++ b/procps/pidof.c
@@ -4,20 +4,7 @@
4 * 4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * Licensed under the GPL v2, see the file LICENSE in this tarball.
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */ 8 */
22 9
23 10
@@ -31,41 +18,120 @@
31#include <unistd.h> 18#include <unistd.h>
32#include "busybox.h" 19#include "busybox.h"
33 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
32static llist_t *omits; /* list of pids to omit */
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#endif
34 43
35extern int pidof_main(int argc, char **argv) 44extern int pidof_main(int argc, char **argv)
36{ 45{
37 int opt, n = 0; 46 int n = 0;
38 int single_flag = 0;
39 int fail = 1; 47 int fail = 1;
48 unsigned long int opt;
49#if ENABLE_FEATURE_PIDOF_OMIT
50 bb_opt_complementally = _OMIT_COMPL("o*");
51#endif
40 52
41 /* do normal option parsing */ 53 /* do option parsing */
42 while ((opt = getopt(argc, argv, "s")) > 0) { 54 if ((opt = bb_getopt_ulflags(argc, argv,
43 switch (opt) { 55 _SINGLE_COMPL("s") _OMIT_COMPL("o:")
44 case 's': 56 _OMIT(&omits)))
45 single_flag = 1; 57 > 0) {
46 break; 58#if ENABLE_FEATURE_PIDOF_SINGLE
47 default: 59 if (!(opt & SINGLE))
48 bb_show_usage(); 60#endif
49 } 61#if ENABLE_FEATURE_PIDOF_OMIT
62 if (!(opt & OMIT))
63#endif
64 bb_show_usage();
50 } 65 }
51 66
52 /* Looks like everything is set to go. */ 67#if ENABLE_FEATURE_PIDOF_OMIT
68 /* fill omit list. */
69 {
70 RESERVE_CONFIG_BUFFER(getppid_str, 32);
71 llist_t * omits_p = omits;
72 while (omits_p) {
73 /* are we asked to exclude the parent's process ID? */
74 if (omits_p->data[0] == '%') {
75 if (!strncmp(omits_p->data, "%PPID", 5)) {
76 /* yes, exclude ppid */
77 snprintf(getppid_str, sizeof(getppid_str), "%ld", getppid());
78// omits_p->data = getppid_str;
79#if 0
80 } else {
81 bb_error_msg_and_die("illegal omit pid value (%s)!\n",
82 omits_p->data);
83#endif
84 }
85 } else {
86 /* no, not talking about ppid but deal with usual case (pid). */
87 snprintf(getppid_str, sizeof(getppid_str), "%ld",
88 strtol(omits_p->data, NULL, 10));
89 }
90 omits_p->data = getppid_str;
91 omits_p = omits_p->link;
92 }
93 RELEASE_CONFIG_BUFFER(getppid_str);
94 }
95#endif
96 /* Looks like everything is set to go. */
53 while(optind < argc) { 97 while(optind < argc) {
54 long *pidList; 98 long *pidList;
55 long *pl; 99 long *pl;
56 100
57 pidList = find_pid_by_name(argv[optind]); 101 /* reverse the pidlist like GNU pidof does. */
102 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
58 for(pl = pidList; *pl > 0; pl++) { 103 for(pl = pidList; *pl > 0; pl++) {
104#if ENABLE_FEATURE_PIDOF_OMIT
105 unsigned short omitted = 0;
106 if (opt & OMIT) {
107 llist_t *omits_p = omits;
108 while (omits_p)
109 if (strtol(omits_p->data, NULL, 10) == *pl) {
110 omitted = 1; break;
111 } else
112 omits_p = omits_p->link;
113 }
114 if (!omitted)
115#endif
59 printf("%s%ld", (n++ ? " " : ""), *pl); 116 printf("%s%ld", (n++ ? " " : ""), *pl);
117#if ENABLE_FEATURE_PIDOF_OMIT
118 fail = omitted;
119#else
60 fail = 0; 120 fail = 0;
61 if (single_flag) 121#endif
122#if ENABLE_FEATURE_PIDOF_SINGLE
123 if (opt & SINGLE)
62 break; 124 break;
125#endif
63 } 126 }
64 free(pidList); 127 free(pidList);
65 optind++; 128 optind++;
66
67 } 129 }
68 printf("\n"); 130 putchar('\n');
69 131
132#if ENABLE_FEATURE_PIDOF_OMIT
133 if (ENABLE_FEATURE_CLEAN_UP)
134 llist_free(omits);
135#endif
70 return fail ? EXIT_FAILURE : EXIT_SUCCESS; 136 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
71} 137}