aboutsummaryrefslogtreecommitdiff
path: root/procps
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2017-07-18 15:58:52 +0100
committerRon Yorston <rmy@pobox.com>2017-07-18 15:58:52 +0100
commitb680f05ad449505e3d914bebd4c8d83bf768c094 (patch)
treec08ded13d430b0e7e0104f2eb594fad190ce98a3 /procps
parent258200ff81d5a9da54dab35acf36213eff1e399b (diff)
parent513a2457b65894b10b9fd6aa8753fca59eced08c (diff)
downloadbusybox-w32-b680f05ad449505e3d914bebd4c8d83bf768c094.tar.gz
busybox-w32-b680f05ad449505e3d914bebd4c8d83bf768c094.tar.bz2
busybox-w32-b680f05ad449505e3d914bebd4c8d83bf768c094.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'procps')
-rw-r--r--procps/pgrep.c38
-rw-r--r--procps/pstree.c2
2 files changed, 28 insertions, 12 deletions
diff --git a/procps/pgrep.c b/procps/pgrep.c
index e932a32bc..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,26 +155,37 @@ 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;
158 162
163 if (ppid2match >= 0 && ppid2match != proc->ppid)
164 continue;
165 if (sid2match >= 0 && sid2match != proc->sid)
166 continue;
167
168 cmdlen = -1;
159 cmd = proc->argv0; 169 cmd = proc->argv0;
160 if (!cmd) { 170 if (!cmd) {
161 cmd = proc->comm; 171 cmd = proc->comm;
162 } else { 172 } else {
163 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 */
177 /*
178 * "sleep 11" looks like "sleep""\0""11""\0" in argv0.
179 * Make sure last "\0" does not get converted to " ":
180 */
181 if (i && cmd[i-1] == '\0')
182 i--;
164 while (--i >= 0) { 183 while (--i >= 0) {
165 if ((unsigned char)cmd[i] < ' ') 184 if ((unsigned char)cmd[i] < ' ')
166 cmd[i] = ' '; 185 cmd[i] = ' ';
167 } 186 }
168 } 187 }
169 188
170 if (ppid2match >= 0 && ppid2match != proc->ppid)
171 continue;
172 if (sid2match >= 0 && sid2match != proc->sid)
173 continue;
174
175 /* NB: OPT_INVERT is always 0 or 1 */ 189 /* NB: OPT_INVERT is always 0 or 1 */
176 if (!argv[0] 190 if (!argv[0]
177 || (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */ 191 || (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
@@ -184,6 +198,8 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
184 cmd_last = xstrdup(cmd); 198 cmd_last = xstrdup(cmd);
185 continue; 199 continue;
186 } 200 }
201 if (cmdlen >= 0)
202 cmd[cmdlen] = '\0';
187 act(proc->pid, cmd, signo); 203 act(proc->pid, cmd, signo);
188 if (OPT_FIRST) 204 if (OPT_FIRST)
189 break; 205 break;
diff --git a/procps/pstree.c b/procps/pstree.c
index f97e99639..bc9f0c927 100644
--- a/procps/pstree.c
+++ b/procps/pstree.c
@@ -34,7 +34,7 @@
34 34
35struct child; 35struct child;
36 36
37#ifdef ENABLE_FEATURE_SHOW_THREADS 37#if ENABLE_FEATURE_SHOW_THREADS
38/* For threads, we add {...} around the comm, so we need two extra bytes */ 38/* For threads, we add {...} around the comm, so we need two extra bytes */
39# define COMM_DISP_LEN (COMM_LEN + 2) 39# define COMM_DISP_LEN (COMM_LEN + 2)
40#else 40#else