aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2005-10-06 15:37:02 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2005-10-06 15:37:02 +0000
commit81c3a518d3121ac6a9fc991aabb916be95ebdea2 (patch)
treea74704314c1dcffbd472702e265966678a986a7c
parentb31cb87fdce95d4bde2062e44e7a5408e97985c6 (diff)
downloadbusybox-w32-81c3a518d3121ac6a9fc991aabb916be95ebdea2.tar.gz
busybox-w32-81c3a518d3121ac6a9fc991aabb916be95ebdea2.tar.bz2
busybox-w32-81c3a518d3121ac6a9fc991aabb916be95ebdea2.zip
- pidof(8): make -s optional and optional -o; closes #168
first cut.
-rw-r--r--include/usage.h27
-rw-r--r--procps/Config.in16
-rw-r--r--procps/pidof.c126
3 files changed, 136 insertions, 33 deletions
diff --git a/include/usage.h b/include/usage.h
index fad50efdc..bef6a4dd2 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -2160,16 +2160,37 @@
2160 "$ patch -p1 <example.diff\n" \ 2160 "$ patch -p1 <example.diff\n" \
2161 "$ patch -p0 -i example.diff" 2161 "$ patch -p0 -i example.diff"
2162 2162
2163#if ENABLE_FEATURE_PIDOF_SINGLE
2164#define USAGE_FEATURE_PIDOF_SINGLE(a) a
2165#else
2166#define USAGE_FEATURE_PIDOF_SINGLE(a)
2167#endif
2168#if ENABLE_FEATURE_PIDOF_OMIT
2169#define USAGE_FEATURE_PIDOF_OMIT(a) a
2170#else
2171#define USAGE_FEATURE_PIDOF_OMIT(a)
2172#endif
2173#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
2174#define USAGE_PIDOF "Options:"
2175#else
2176#define USAGE_PIDOF "\n\tThis version of pidof accepts no options."
2177#endif
2178
2163#define pidof_trivial_usage \ 2179#define pidof_trivial_usage \
2164 "process-name [OPTION] [process-name ...]" 2180 "process-name [OPTION] [process-name ...]"
2181
2165#define pidof_full_usage \ 2182#define pidof_full_usage \
2166 "Lists the PIDs of all processes with names that match the\n" \ 2183 "Lists the PIDs of all processes with names that match the\n" \
2167 "names on the command line.\n" \ 2184 "names on the command line.\n" \
2168 "Options:\n" \ 2185 USAGE_PIDOF \
2169 "\t-s\t\tdisplay only a single PID" 2186 USAGE_FEATURE_PIDOF_SINGLE("\n\t-s\t\tdisplay only a single PID") \
2187 USAGE_FEATURE_PIDOF_OMIT("\n\t-o\t\tomit given pid.") \
2188 USAGE_FEATURE_PIDOF_OMIT("\n\t\t\tUse %PPID to omit the parent pid of pidof itself")
2170#define pidof_example_usage \ 2189#define pidof_example_usage \
2171 "$ pidof init\n" \ 2190 "$ pidof init\n" \
2172 "1\n" 2191 "1\n" \
2192 USAGE_FEATURE_PIDOF_OMIT("$ pidof /bin/sh\n20351 5973 5950\n") \
2193 USAGE_FEATURE_PIDOF_OMIT("$ pidof /bin/sh -o %PPID\n20351 5950")
2173 2194
2174#ifndef CONFIG_FEATURE_FANCY_PING 2195#ifndef CONFIG_FEATURE_FANCY_PING
2175#define ping_trivial_usage "host" 2196#define ping_trivial_usage "host"
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}