aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-06-26 21:10:47 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-06-26 21:10:47 +0200
commit9f4b4226a4df67b4305f5dcb0df32cba26d8463a (patch)
treeee408159efdac23b4c0e45b6ea4f86171cdf4a51
parent234b82ca19afb20276722334ecf1576f801cb588 (diff)
downloadbusybox-w32-9f4b4226a4df67b4305f5dcb0df32cba26d8463a.tar.gz
busybox-w32-9f4b4226a4df67b4305f5dcb0df32cba26d8463a.tar.bz2
busybox-w32-9f4b4226a4df67b4305f5dcb0df32cba26d8463a.zip
pgrep: implement -a
function old new delta pgrep_main 640 726 +86 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--procps/pgrep.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/procps/pgrep.c b/procps/pgrep.c
index 461abfa34..3d01c6cff 100644
--- a/procps/pgrep.c
+++ b/procps/pgrep.c
@@ -26,10 +26,11 @@
26//kbuild:lib-$(CONFIG_PKILL) += pgrep.o 26//kbuild:lib-$(CONFIG_PKILL) += pgrep.o
27 27
28//usage:#define pgrep_trivial_usage 28//usage:#define pgrep_trivial_usage
29//usage: "[-flnovx] [-s SID|-P PPID|PATTERN]" 29//usage: "[-flanovx] [-s SID|-P PPID|PATTERN]"
30//usage:#define pgrep_full_usage "\n\n" 30//usage:#define pgrep_full_usage "\n\n"
31//usage: "Display process(es) selected by regex PATTERN\n" 31//usage: "Display process(es) selected by regex PATTERN\n"
32//usage: "\n -l Show command name too" 32//usage: "\n -l Show command name too"
33//usage: "\n -a Show command line too"
33//usage: "\n -f Match against entire command line" 34//usage: "\n -f Match against entire command line"
34//usage: "\n -n Show the newest process only" 35//usage: "\n -n Show the newest process only"
35//usage: "\n -o Show the oldest process only" 36//usage: "\n -o Show the oldest process only"
@@ -55,13 +56,14 @@
55#include "xregex.h" 56#include "xregex.h"
56 57
57/* Idea taken from kill.c */ 58/* Idea taken from kill.c */
58#define pgrep (ENABLE_PGREP && applet_name[1] == 'g') 59#define pgrep (ENABLE_PGREP && (!ENABLE_PKILL || applet_name[1] == 'g'))
59#define pkill (ENABLE_PKILL && applet_name[1] == 'k') 60#define pkill (ENABLE_PKILL && (!ENABLE_PGREP || applet_name[1] == 'k'))
60 61
61enum { 62enum {
62 /* "vlfxons:P:" */ 63 /* "vlafxons:+P:+" */
63 OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */ 64 OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
64 OPTBIT_L, 65 OPTBIT_L,
66 OPTBIT_A,
65 OPTBIT_F, 67 OPTBIT_F,
66 OPTBIT_X, 68 OPTBIT_X,
67 OPTBIT_O, 69 OPTBIT_O,
@@ -72,6 +74,7 @@ enum {
72 74
73#define OPT_INVERT (opt & (1 << OPTBIT_V)) 75#define OPT_INVERT (opt & (1 << OPTBIT_V))
74#define OPT_LIST (opt & (1 << OPTBIT_L)) 76#define OPT_LIST (opt & (1 << OPTBIT_L))
77#define OPT_LISTFULL (opt & (1 << OPTBIT_A))
75#define OPT_FULL (opt & (1 << OPTBIT_F)) 78#define OPT_FULL (opt & (1 << OPTBIT_F))
76#define OPT_ANCHOR (opt & (1 << OPTBIT_X)) 79#define OPT_ANCHOR (opt & (1 << OPTBIT_X))
77#define OPT_FIRST (opt & (1 << OPTBIT_O)) 80#define OPT_FIRST (opt & (1 << OPTBIT_O))
@@ -82,7 +85,7 @@ enum {
82static void act(unsigned pid, char *cmd, int signo) 85static void act(unsigned pid, char *cmd, int signo)
83{ 86{
84 if (pgrep) { 87 if (pgrep) {
85 if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */ 88 if (option_mask32 & ((1 << OPTBIT_L)|(1 << OPTBIT_A))) /* -l or -a */
86 printf("%u %s\n", pid, cmd); 89 printf("%u %s\n", pid, cmd);
87 else 90 else
88 printf("%u\n", pid); 91 printf("%u\n", pid);
@@ -124,7 +127,7 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
124 /* Parse remaining options */ 127 /* Parse remaining options */
125 ppid2match = -1; 128 ppid2match = -1;
126 sid2match = -1; 129 sid2match = -1;
127 opt = getopt32(argv, "vlfxons:+P:+", &sid2match, &ppid2match); 130 opt = getopt32(argv, "vlafxons:+P:+", &sid2match, &ppid2match);
128 argv += optind; 131 argv += optind;
129 132
130 if (pkill && OPT_LIST) { /* -l: print the whole signal list */ 133 if (pkill && OPT_LIST) { /* -l: print the whole signal list */
@@ -152,6 +155,7 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
152 proc = NULL; 155 proc = NULL;
153 while ((proc = procps_scan(proc, scan_mask)) != NULL) { 156 while ((proc = procps_scan(proc, scan_mask)) != NULL) {
154 char *cmd; 157 char *cmd;
158 int cmdlen;
155 159
156 if (proc->pid == pid) 160 if (proc->pid == pid)
157 continue; 161 continue;
@@ -161,11 +165,15 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
161 if (sid2match >= 0 && sid2match != proc->sid) 165 if (sid2match >= 0 && sid2match != proc->sid)
162 continue; 166 continue;
163 167
168 cmdlen = -1;
164 cmd = proc->argv0; 169 cmd = proc->argv0;
165 if (!cmd) { 170 if (!cmd) {
166 cmd = proc->comm; 171 cmd = proc->comm;
167 } else { 172 } else {
168 int i = proc->argv_len; 173 int i = proc->argv_len;
174
175 if (!OPT_LISTFULL)
176 cmdlen = strlen(cmd); /* not -a: find first NUL */
169 /* 177 /*
170 * "sleep 11" looks like "sleep""\0""11""\0" in argv0. 178 * "sleep 11" looks like "sleep""\0""11""\0" in argv0.
171 * Make sure last "\0" does not get converted to " ": 179 * Make sure last "\0" does not get converted to " ":
@@ -190,6 +198,8 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
190 cmd_last = xstrdup(cmd); 198 cmd_last = xstrdup(cmd);
191 continue; 199 continue;
192 } 200 }
201 if (cmdlen >= 0)
202 cmd[cmdlen] = '\0';
193 act(proc->pid, cmd, signo); 203 act(proc->pid, cmd, signo);
194 if (OPT_FIRST) 204 if (OPT_FIRST)
195 break; 205 break;