1 | /* vi: set sw=4 ts=4: */
|
---|
2 | /*
|
---|
3 | * Mini ps implementation(s) for busybox
|
---|
4 | *
|
---|
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
---|
6 | * Fix for SELinux Support:(c)2007 Hiroshi Shinji <shiroshi@my.email.ne.jp>
|
---|
7 | * (c)2007 Yuichi Nakamura <ynakam@hitachisoft.jp>
|
---|
8 | *
|
---|
9 | * Licensed under GPLv2, see file LICENSE in this source tree.
|
---|
10 | */
|
---|
11 |
|
---|
12 | //usage:#if ENABLE_DESKTOP
|
---|
13 | //usage:
|
---|
14 | //usage:#define ps_trivial_usage
|
---|
15 | //usage: "[-o COL1,COL2=HEADER]" IF_FEATURE_SHOW_THREADS(" [-T]")
|
---|
16 | //usage:#define ps_full_usage "\n\n"
|
---|
17 | //usage: "Show list of processes\n"
|
---|
18 | //usage: "\n -o COL1,COL2=HEADER Select columns for display"
|
---|
19 | //usage: IF_FEATURE_SHOW_THREADS(
|
---|
20 | //usage: "\n -T Show threads"
|
---|
21 | //usage: )
|
---|
22 | //usage:
|
---|
23 | //usage:#else /* !ENABLE_DESKTOP */
|
---|
24 | //usage:
|
---|
25 | //usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE
|
---|
26 | //usage:#define USAGE_PS "\nThis version of ps accepts no options"
|
---|
27 | //usage:#else
|
---|
28 | //usage:#define USAGE_PS ""
|
---|
29 | //usage:#endif
|
---|
30 | //usage:
|
---|
31 | //usage:#define ps_trivial_usage
|
---|
32 | //usage: ""
|
---|
33 | //usage:#define ps_full_usage "\n\n"
|
---|
34 | //usage: "Show list of processes\n"
|
---|
35 | //usage: USAGE_PS
|
---|
36 | //usage: IF_SELINUX(
|
---|
37 | //usage: "\n -Z Show selinux context"
|
---|
38 | //usage: )
|
---|
39 | //usage: IF_FEATURE_PS_WIDE(
|
---|
40 | //usage: "\n w Wide output"
|
---|
41 | //usage: )
|
---|
42 | //usage: IF_FEATURE_PS_LONG(
|
---|
43 | //usage: "\n l Long output"
|
---|
44 | //usage: )
|
---|
45 | //usage: IF_FEATURE_SHOW_THREADS(
|
---|
46 | //usage: "\n T Show threads"
|
---|
47 | //usage: )
|
---|
48 | //usage:
|
---|
49 | //usage:#endif /* ENABLE_DESKTOP */
|
---|
50 | //usage:
|
---|
51 | //usage:#define ps_example_usage
|
---|
52 | //usage: "$ ps\n"
|
---|
53 | //usage: " PID Uid Gid State Command\n"
|
---|
54 | //usage: " 1 root root S init\n"
|
---|
55 | //usage: " 2 root root S [kflushd]\n"
|
---|
56 | //usage: " 3 root root S [kupdate]\n"
|
---|
57 | //usage: " 4 root root S [kpiod]\n"
|
---|
58 | //usage: " 5 root root S [kswapd]\n"
|
---|
59 | //usage: " 742 andersen andersen S [bash]\n"
|
---|
60 | //usage: " 743 andersen andersen S -bash\n"
|
---|
61 | //usage: " 745 root root S [getty]\n"
|
---|
62 | //usage: " 2990 andersen andersen R ps\n"
|
---|
63 |
|
---|
64 | #include "libbb.h"
|
---|
65 | #ifdef __linux__
|
---|
66 | # include <sys/sysinfo.h>
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | /* Absolute maximum on output line length */
|
---|
70 | enum { MAX_WIDTH = 2*1024 };
|
---|
71 |
|
---|
72 | #if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG
|
---|
73 | static long get_uptime(void)
|
---|
74 | {
|
---|
75 | #ifdef __linux__
|
---|
76 | struct sysinfo info;
|
---|
77 | if (sysinfo(&info) < 0)
|
---|
78 | return 0;
|
---|
79 | return info.uptime;
|
---|
80 | #elif 1
|
---|
81 | char buf[64];
|
---|
82 | long uptime;
|
---|
83 | if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
|
---|
84 | bb_perror_msg_and_die("can't read %s", "/proc/uptime");
|
---|
85 | buf[sizeof(buf)-1] = '\0';
|
---|
86 | sscanf(buf, "%l", &uptime);
|
---|
87 | return uptime;
|
---|
88 | #else
|
---|
89 | struct timespec ts;
|
---|
90 | if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
|
---|
91 | return 0;
|
---|
92 | return ts.tv_sec;
|
---|
93 | #endif
|
---|
94 | }
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | #if ENABLE_DESKTOP
|
---|
98 |
|
---|
99 | #include <sys/times.h> /* for times() */
|
---|
100 | #ifndef AT_CLKTCK
|
---|
101 | # define AT_CLKTCK 17
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | /* TODO:
|
---|
105 | * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
|
---|
106 | * specifies (for XSI-conformant systems) following default columns
|
---|
107 | * (l and f mark columns shown with -l and -f respectively):
|
---|
108 | * F l Flags (octal and additive) associated with the process (??)
|
---|
109 | * S l The state of the process
|
---|
110 | * UID f,l The user ID; the login name is printed with -f
|
---|
111 | * PID The process ID
|
---|
112 | * PPID f,l The parent process
|
---|
113 | * C f,l Processor utilization
|
---|
114 | * PRI l The priority of the process; higher numbers mean lower priority
|
---|
115 | * NI l Nice value
|
---|
116 | * ADDR l The address of the process
|
---|
117 | * SZ l The size in blocks of the core image of the process
|
---|
118 | * WCHAN l The event for which the process is waiting or sleeping
|
---|
119 | * STIME f Starting time of the process
|
---|
120 | * TTY The controlling terminal for the process
|
---|
121 | * TIME The cumulative execution time for the process
|
---|
122 | * CMD The command name; the full command line is shown with -f
|
---|
123 | */
|
---|
124 | typedef struct {
|
---|
125 | uint16_t width;
|
---|
126 | char name6[6];
|
---|
127 | const char *header;
|
---|
128 | void (*f)(char *buf, int size, const procps_status_t *ps);
|
---|
129 | int ps_flags;
|
---|
130 | } ps_out_t;
|
---|
131 |
|
---|
132 | struct globals {
|
---|
133 | ps_out_t* out;
|
---|
134 | int out_cnt;
|
---|
135 | int print_header;
|
---|
136 | int need_flags;
|
---|
137 | char *buffer;
|
---|
138 | unsigned terminal_width;
|
---|
139 | #if ENABLE_FEATURE_PS_TIME
|
---|
140 | unsigned kernel_HZ;
|
---|
141 | unsigned long long seconds_since_boot;
|
---|
142 | #endif
|
---|
143 | } FIX_ALIASING;
|
---|
144 | #define G (*(struct globals*)&bb_common_bufsiz1)
|
---|
145 | #define out (G.out )
|
---|
146 | #define out_cnt (G.out_cnt )
|
---|
147 | #define print_header (G.print_header )
|
---|
148 | #define need_flags (G.need_flags )
|
---|
149 | #define buffer (G.buffer )
|
---|
150 | #define terminal_width (G.terminal_width )
|
---|
151 | #define kernel_HZ (G.kernel_HZ )
|
---|
152 | #define seconds_since_boot (G.seconds_since_boot)
|
---|
153 | #define INIT_G() do { } while (0)
|
---|
154 |
|
---|
155 | #if ENABLE_FEATURE_PS_TIME
|
---|
156 | /* for ELF executables, notes are pushed before environment and args */
|
---|
157 | static ptrdiff_t find_elf_note(ptrdiff_t findme)
|
---|
158 | {
|
---|
159 | ptrdiff_t *ep = (ptrdiff_t *) environ;
|
---|
160 |
|
---|
161 | while (*ep++)
|
---|
162 | continue;
|
---|
163 | while (*ep) {
|
---|
164 | if (ep[0] == findme) {
|
---|
165 | return ep[1];
|
---|
166 | }
|
---|
167 | ep += 2;
|
---|
168 | }
|
---|
169 | return -1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | #if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
|
---|
173 | static unsigned get_HZ_by_waiting(void)
|
---|
174 | {
|
---|
175 | struct timeval tv1, tv2;
|
---|
176 | unsigned t1, t2, r, hz;
|
---|
177 | unsigned cnt = cnt; /* for compiler */
|
---|
178 | int diff;
|
---|
179 |
|
---|
180 | r = 0;
|
---|
181 |
|
---|
182 | /* Wait for times() to reach new tick */
|
---|
183 | t1 = times(NULL);
|
---|
184 | do {
|
---|
185 | t2 = times(NULL);
|
---|
186 | } while (t2 == t1);
|
---|
187 | gettimeofday(&tv2, NULL);
|
---|
188 |
|
---|
189 | do {
|
---|
190 | t1 = t2;
|
---|
191 | tv1.tv_usec = tv2.tv_usec;
|
---|
192 |
|
---|
193 | /* Wait exactly one times() tick */
|
---|
194 | do {
|
---|
195 | t2 = times(NULL);
|
---|
196 | } while (t2 == t1);
|
---|
197 | gettimeofday(&tv2, NULL);
|
---|
198 |
|
---|
199 | /* Calculate ticks per sec, rounding up to even */
|
---|
200 | diff = tv2.tv_usec - tv1.tv_usec;
|
---|
201 | if (diff <= 0) diff += 1000000;
|
---|
202 | hz = 1000000u / (unsigned)diff;
|
---|
203 | hz = (hz+1) & ~1;
|
---|
204 |
|
---|
205 | /* Count how many same hz values we saw */
|
---|
206 | if (r != hz) {
|
---|
207 | r = hz;
|
---|
208 | cnt = 0;
|
---|
209 | }
|
---|
210 | cnt++;
|
---|
211 | } while (cnt < 3); /* exit if saw 3 same values */
|
---|
212 |
|
---|
213 | return r;
|
---|
214 | }
|
---|
215 | #else
|
---|
216 | static inline unsigned get_HZ_by_waiting(void)
|
---|
217 | {
|
---|
218 | /* Better method? */
|
---|
219 | return 100;
|
---|
220 | }
|
---|
221 | #endif
|
---|
222 |
|
---|
223 | static unsigned get_kernel_HZ(void)
|
---|
224 | {
|
---|
225 |
|
---|
226 | if (kernel_HZ)
|
---|
227 | return kernel_HZ;
|
---|
228 |
|
---|
229 | /* Works for ELF only, Linux 2.4.0+ */
|
---|
230 | kernel_HZ = find_elf_note(AT_CLKTCK);
|
---|
231 | if (kernel_HZ == (unsigned)-1)
|
---|
232 | kernel_HZ = get_HZ_by_waiting();
|
---|
233 |
|
---|
234 | seconds_since_boot = get_uptime();
|
---|
235 |
|
---|
236 | return kernel_HZ;
|
---|
237 | }
|
---|
238 | #endif
|
---|
239 |
|
---|
240 | /* Print value to buf, max size+1 chars (including trailing '\0') */
|
---|
241 |
|
---|
242 | static void func_user(char *buf, int size, const procps_status_t *ps)
|
---|
243 | {
|
---|
244 | #if 1
|
---|
245 | safe_strncpy(buf, get_cached_username(ps->uid), size+1);
|
---|
246 | #else
|
---|
247 | /* "compatible" version, but it's larger */
|
---|
248 | /* procps 2.18 shows numeric UID if name overflows the field */
|
---|
249 | /* TODO: get_cached_username() returns numeric string if
|
---|
250 | * user has no passwd record, we will display it
|
---|
251 | * left-justified here; too long usernames are shown
|
---|
252 | * as _right-justified_ IDs. Is it worth fixing? */
|
---|
253 | const char *user = get_cached_username(ps->uid);
|
---|
254 | if (strlen(user) <= size)
|
---|
255 | safe_strncpy(buf, user, size+1);
|
---|
256 | else
|
---|
257 | sprintf(buf, "%*u", size, (unsigned)ps->uid);
|
---|
258 | #endif
|
---|
259 | }
|
---|
260 |
|
---|
261 | static void func_group(char *buf, int size, const procps_status_t *ps)
|
---|
262 | {
|
---|
263 | safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
|
---|
264 | }
|
---|
265 |
|
---|
266 | static void func_comm(char *buf, int size, const procps_status_t *ps)
|
---|
267 | {
|
---|
268 | safe_strncpy(buf, ps->comm, size+1);
|
---|
269 | }
|
---|
270 |
|
---|
271 | static void func_state(char *buf, int size, const procps_status_t *ps)
|
---|
272 | {
|
---|
273 | safe_strncpy(buf, ps->state, size+1);
|
---|
274 | }
|
---|
275 |
|
---|
276 | static void func_args(char *buf, int size, const procps_status_t *ps)
|
---|
277 | {
|
---|
278 | read_cmdline(buf, size+1, ps->pid, ps->comm);
|
---|
279 | }
|
---|
280 |
|
---|
281 | static void func_pid(char *buf, int size, const procps_status_t *ps)
|
---|
282 | {
|
---|
283 | sprintf(buf, "%*u", size, ps->pid);
|
---|
284 | }
|
---|
285 |
|
---|
286 | static void func_ppid(char *buf, int size, const procps_status_t *ps)
|
---|
287 | {
|
---|
288 | sprintf(buf, "%*u", size, ps->ppid);
|
---|
289 | }
|
---|
290 |
|
---|
291 | static void func_pgid(char *buf, int size, const procps_status_t *ps)
|
---|
292 | {
|
---|
293 | sprintf(buf, "%*u", size, ps->pgid);
|
---|
294 | }
|
---|
295 |
|
---|
296 | static void put_lu(char *buf, int size, unsigned long u)
|
---|
297 | {
|
---|
298 | char buf4[5];
|
---|
299 |
|
---|
300 | /* see http://en.wikipedia.org/wiki/Tera */
|
---|
301 | smart_ulltoa4(u, buf4, " mgtpezy");
|
---|
302 | buf4[4] = '\0';
|
---|
303 | sprintf(buf, "%.*s", size, buf4);
|
---|
304 | }
|
---|
305 |
|
---|
306 | static void func_vsz(char *buf, int size, const procps_status_t *ps)
|
---|
307 | {
|
---|
308 | put_lu(buf, size, ps->vsz);
|
---|
309 | }
|
---|
310 |
|
---|
311 | static void func_rss(char *buf, int size, const procps_status_t *ps)
|
---|
312 | {
|
---|
313 | put_lu(buf, size, ps->rss);
|
---|
314 | }
|
---|
315 |
|
---|
316 | static void func_tty(char *buf, int size, const procps_status_t *ps)
|
---|
317 | {
|
---|
318 | buf[0] = '?';
|
---|
319 | buf[1] = '\0';
|
---|
320 | if (ps->tty_major) /* tty field of "0" means "no tty" */
|
---|
321 | snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
|
---|
322 | }
|
---|
323 |
|
---|
324 | #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
|
---|
325 |
|
---|
326 | static void func_rgroup(char *buf, int size, const procps_status_t *ps)
|
---|
327 | {
|
---|
328 | safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
|
---|
329 | }
|
---|
330 |
|
---|
331 | static void func_ruser(char *buf, int size, const procps_status_t *ps)
|
---|
332 | {
|
---|
333 | safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
|
---|
334 | }
|
---|
335 |
|
---|
336 | static void func_nice(char *buf, int size, const procps_status_t *ps)
|
---|
337 | {
|
---|
338 | sprintf(buf, "%*d", size, ps->niceness);
|
---|
339 | }
|
---|
340 |
|
---|
341 | #endif
|
---|
342 |
|
---|
343 | #if ENABLE_FEATURE_PS_TIME
|
---|
344 |
|
---|
345 | static void func_etime(char *buf, int size, const procps_status_t *ps)
|
---|
346 | {
|
---|
347 | /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
|
---|
348 | unsigned long mm;
|
---|
349 | unsigned ss;
|
---|
350 |
|
---|
351 | mm = ps->start_time / get_kernel_HZ();
|
---|
352 | /* must be after get_kernel_HZ()! */
|
---|
353 | mm = seconds_since_boot - mm;
|
---|
354 | ss = mm % 60;
|
---|
355 | mm /= 60;
|
---|
356 | snprintf(buf, size+1, "%3lu:%02u", mm, ss);
|
---|
357 | }
|
---|
358 |
|
---|
359 | static void func_time(char *buf, int size, const procps_status_t *ps)
|
---|
360 | {
|
---|
361 | /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
|
---|
362 | unsigned long mm;
|
---|
363 | unsigned ss;
|
---|
364 |
|
---|
365 | mm = (ps->utime + ps->stime) / get_kernel_HZ();
|
---|
366 | ss = mm % 60;
|
---|
367 | mm /= 60;
|
---|
368 | snprintf(buf, size+1, "%3lu:%02u", mm, ss);
|
---|
369 | }
|
---|
370 |
|
---|
371 | #endif
|
---|
372 |
|
---|
373 | #if ENABLE_SELINUX
|
---|
374 | static void func_label(char *buf, int size, const procps_status_t *ps)
|
---|
375 | {
|
---|
376 | safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
|
---|
377 | }
|
---|
378 | #endif
|
---|
379 |
|
---|
380 | /*
|
---|
381 | static void func_nice(char *buf, int size, const procps_status_t *ps)
|
---|
382 | {
|
---|
383 | ps->???
|
---|
384 | }
|
---|
385 |
|
---|
386 | static void func_pcpu(char *buf, int size, const procps_status_t *ps)
|
---|
387 | {
|
---|
388 | }
|
---|
389 | */
|
---|
390 |
|
---|
391 | static const ps_out_t out_spec[] = {
|
---|
392 | /* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
|
---|
393 | { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
|
---|
394 | { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
|
---|
395 | { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
|
---|
396 | { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
|
---|
397 | { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
|
---|
398 | { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
|
---|
399 | { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
|
---|
400 | #if ENABLE_FEATURE_PS_TIME
|
---|
401 | { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
|
---|
402 | #endif
|
---|
403 | #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
|
---|
404 | { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
|
---|
405 | { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
|
---|
406 | { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
|
---|
407 | // { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
|
---|
408 | #endif
|
---|
409 | #if ENABLE_FEATURE_PS_TIME
|
---|
410 | { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
|
---|
411 | #endif
|
---|
412 | { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
|
---|
413 | { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
|
---|
414 | /* Not mandated, but useful: */
|
---|
415 | { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE },
|
---|
416 | { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
|
---|
417 | #if ENABLE_SELINUX
|
---|
418 | { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
|
---|
419 | #endif
|
---|
420 | };
|
---|
421 |
|
---|
422 | static ps_out_t* new_out_t(void)
|
---|
423 | {
|
---|
424 | out = xrealloc_vector(out, 2, out_cnt);
|
---|
425 | return &out[out_cnt++];
|
---|
426 | }
|
---|
427 |
|
---|
428 | static const ps_out_t* find_out_spec(const char *name)
|
---|
429 | {
|
---|
430 | unsigned i;
|
---|
431 | char buf[ARRAY_SIZE(out_spec)*7 + 1];
|
---|
432 | char *p = buf;
|
---|
433 |
|
---|
434 | for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
|
---|
435 | if (strncmp(name, out_spec[i].name6, 6) == 0)
|
---|
436 | return &out_spec[i];
|
---|
437 | p += sprintf(p, "%.6s,", out_spec[i].name6);
|
---|
438 | }
|
---|
439 | p[-1] = '\0';
|
---|
440 | bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
|
---|
441 | }
|
---|
442 |
|
---|
443 | static void parse_o(char* opt)
|
---|
444 | {
|
---|
445 | ps_out_t* new;
|
---|
446 | // POSIX: "-o is blank- or comma-separated list" (FIXME)
|
---|
447 | char *comma, *equal;
|
---|
448 | while (1) {
|
---|
449 | comma = strchr(opt, ',');
|
---|
450 | equal = strchr(opt, '=');
|
---|
451 | if (comma && (!equal || equal > comma)) {
|
---|
452 | *comma = '\0';
|
---|
453 | *new_out_t() = *find_out_spec(opt);
|
---|
454 | *comma = ',';
|
---|
455 | opt = comma + 1;
|
---|
456 | continue;
|
---|
457 | }
|
---|
458 | break;
|
---|
459 | }
|
---|
460 | // opt points to last spec in comma separated list.
|
---|
461 | // This one can have =HEADER part.
|
---|
462 | new = new_out_t();
|
---|
463 | if (equal)
|
---|
464 | *equal = '\0';
|
---|
465 | *new = *find_out_spec(opt);
|
---|
466 | if (equal) {
|
---|
467 | *equal = '=';
|
---|
468 | new->header = equal + 1;
|
---|
469 | // POSIX: the field widths shall be ... at least as wide as
|
---|
470 | // the header text (default or overridden value).
|
---|
471 | // If the header text is null, such as -o user=,
|
---|
472 | // the field width shall be at least as wide as the
|
---|
473 | // default header text
|
---|
474 | if (new->header[0]) {
|
---|
475 | new->width = strlen(new->header);
|
---|
476 | print_header = 1;
|
---|
477 | }
|
---|
478 | } else
|
---|
479 | print_header = 1;
|
---|
480 | }
|
---|
481 |
|
---|
482 | static void alloc_line_buffer(void)
|
---|
483 | {
|
---|
484 | int i;
|
---|
485 | int width = 0;
|
---|
486 | for (i = 0; i < out_cnt; i++) {
|
---|
487 | need_flags |= out[i].ps_flags;
|
---|
488 | if (out[i].header[0]) {
|
---|
489 | print_header = 1;
|
---|
490 | }
|
---|
491 | width += out[i].width + 1; /* "FIELD " */
|
---|
492 | if ((int)(width - terminal_width) > 0) {
|
---|
493 | /* The rest does not fit on the screen */
|
---|
494 | //out[i].width -= (width - terminal_width - 1);
|
---|
495 | out_cnt = i + 1;
|
---|
496 | break;
|
---|
497 | }
|
---|
498 | }
|
---|
499 | #if ENABLE_SELINUX
|
---|
500 | if (!is_selinux_enabled())
|
---|
501 | need_flags &= ~PSSCAN_CONTEXT;
|
---|
502 | #endif
|
---|
503 | buffer = xmalloc(width + 1); /* for trailing \0 */
|
---|
504 | }
|
---|
505 |
|
---|
506 | static void format_header(void)
|
---|
507 | {
|
---|
508 | int i;
|
---|
509 | ps_out_t* op;
|
---|
510 | char *p;
|
---|
511 |
|
---|
512 | if (!print_header)
|
---|
513 | return;
|
---|
514 | p = buffer;
|
---|
515 | i = 0;
|
---|
516 | if (out_cnt) {
|
---|
517 | while (1) {
|
---|
518 | op = &out[i];
|
---|
519 | if (++i == out_cnt) /* do not pad last field */
|
---|
520 | break;
|
---|
521 | p += sprintf(p, "%-*s ", op->width, op->header);
|
---|
522 | }
|
---|
523 | strcpy(p, op->header);
|
---|
524 | }
|
---|
525 | printf("%.*s\n", terminal_width, buffer);
|
---|
526 | }
|
---|
527 |
|
---|
528 | static void format_process(const procps_status_t *ps)
|
---|
529 | {
|
---|
530 | int i, len;
|
---|
531 | char *p = buffer;
|
---|
532 | i = 0;
|
---|
533 | if (out_cnt) while (1) {
|
---|
534 | out[i].f(p, out[i].width, ps);
|
---|
535 | // POSIX: Any field need not be meaningful in all
|
---|
536 | // implementations. In such a case a hyphen ( '-' )
|
---|
537 | // should be output in place of the field value.
|
---|
538 | if (!p[0]) {
|
---|
539 | p[0] = '-';
|
---|
540 | p[1] = '\0';
|
---|
541 | }
|
---|
542 | len = strlen(p);
|
---|
543 | p += len;
|
---|
544 | len = out[i].width - len + 1;
|
---|
545 | if (++i == out_cnt) /* do not pad last field */
|
---|
546 | break;
|
---|
547 | p += sprintf(p, "%*s", len, "");
|
---|
548 | }
|
---|
549 | printf("%.*s\n", terminal_width, buffer);
|
---|
550 | }
|
---|
551 |
|
---|
552 | #if ENABLE_SELINUX
|
---|
553 | # define SELINUX_O_PREFIX "label,"
|
---|
554 | # define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
|
---|
555 | #else
|
---|
556 | # define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
|
---|
557 | #endif
|
---|
558 |
|
---|
559 | int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
560 | int ps_main(int argc UNUSED_PARAM, char **argv)
|
---|
561 | {
|
---|
562 | procps_status_t *p;
|
---|
563 | llist_t* opt_o = NULL;
|
---|
564 | char default_o[sizeof(DEFAULT_O_STR)];
|
---|
565 | int opt;
|
---|
566 | enum {
|
---|
567 | OPT_Z = (1 << 0),
|
---|
568 | OPT_o = (1 << 1),
|
---|
569 | OPT_a = (1 << 2),
|
---|
570 | OPT_A = (1 << 3),
|
---|
571 | OPT_d = (1 << 4),
|
---|
572 | OPT_e = (1 << 5),
|
---|
573 | OPT_f = (1 << 6),
|
---|
574 | OPT_l = (1 << 7),
|
---|
575 | OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
|
---|
576 | };
|
---|
577 |
|
---|
578 | INIT_G();
|
---|
579 |
|
---|
580 | // POSIX:
|
---|
581 | // -a Write information for all processes associated with terminals
|
---|
582 | // Implementations may omit session leaders from this list
|
---|
583 | // -A Write information for all processes
|
---|
584 | // -d Write information for all processes, except session leaders
|
---|
585 | // -e Write information for all processes (equivalent to -A)
|
---|
586 | // -f Generate a full listing
|
---|
587 | // -l Generate a long listing
|
---|
588 | // -o col1,col2,col3=header
|
---|
589 | // Select which columns to display
|
---|
590 | /* We allow (and ignore) most of the above. FIXME.
|
---|
591 | * -T is picked for threads (POSIX hasn't it standardized).
|
---|
592 | * procps v3.2.7 supports -T and shows tids as SPID column,
|
---|
593 | * it also supports -L where it shows tids as LWP column.
|
---|
594 | */
|
---|
595 | opt_complementary = "o::";
|
---|
596 | opt = getopt32(argv, "Zo:aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
|
---|
597 | if (opt_o) {
|
---|
598 | do {
|
---|
599 | parse_o(llist_pop(&opt_o));
|
---|
600 | } while (opt_o);
|
---|
601 | } else {
|
---|
602 | /* Below: parse_o() needs char*, NOT const char*, can't give it default_o */
|
---|
603 | #if ENABLE_SELINUX
|
---|
604 | if (!(opt & OPT_Z) || !is_selinux_enabled()) {
|
---|
605 | /* no -Z or no SELinux: do not show LABEL */
|
---|
606 | strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
|
---|
607 | } else
|
---|
608 | #endif
|
---|
609 | {
|
---|
610 | strcpy(default_o, DEFAULT_O_STR);
|
---|
611 | }
|
---|
612 | parse_o(default_o);
|
---|
613 | }
|
---|
614 | #if ENABLE_FEATURE_SHOW_THREADS
|
---|
615 | if (opt & OPT_T)
|
---|
616 | need_flags |= PSSCAN_TASKS;
|
---|
617 | #endif
|
---|
618 |
|
---|
619 | /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
|
---|
620 | * and such large widths */
|
---|
621 | terminal_width = MAX_WIDTH;
|
---|
622 | if (isatty(1)) {
|
---|
623 | get_terminal_width_height(0, &terminal_width, NULL);
|
---|
624 | if (--terminal_width > MAX_WIDTH)
|
---|
625 | terminal_width = MAX_WIDTH;
|
---|
626 | }
|
---|
627 | alloc_line_buffer();
|
---|
628 | format_header();
|
---|
629 |
|
---|
630 | p = NULL;
|
---|
631 | while ((p = procps_scan(p, need_flags)) != NULL) {
|
---|
632 | format_process(p);
|
---|
633 | }
|
---|
634 |
|
---|
635 | return EXIT_SUCCESS;
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|
639 | #else /* !ENABLE_DESKTOP */
|
---|
640 |
|
---|
641 |
|
---|
642 | int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
643 | int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
---|
644 | {
|
---|
645 | procps_status_t *p;
|
---|
646 | int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
|
---|
647 | | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
|
---|
648 | unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
|
---|
649 | enum {
|
---|
650 | OPT_Z = (1 << 0) * ENABLE_SELINUX,
|
---|
651 | OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
|
---|
652 | OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG,
|
---|
653 | };
|
---|
654 | #if ENABLE_FEATURE_PS_LONG
|
---|
655 | time_t now = now;
|
---|
656 | long uptime;
|
---|
657 | #endif
|
---|
658 | /* If we support any options, parse argv */
|
---|
659 | #if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE || ENABLE_FEATURE_PS_LONG
|
---|
660 | int opts = 0;
|
---|
661 | # if ENABLE_FEATURE_PS_WIDE
|
---|
662 | /* -w is a bit complicated */
|
---|
663 | int w_count = 0;
|
---|
664 | opt_complementary = "-:ww";
|
---|
665 | opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")
|
---|
666 | "w", &w_count);
|
---|
667 | /* if w is given once, GNU ps sets the width to 132,
|
---|
668 | * if w is given more than once, it is "unlimited"
|
---|
669 | */
|
---|
670 | if (w_count) {
|
---|
671 | terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
|
---|
672 | } else {
|
---|
673 | get_terminal_width_height(0, &terminal_width, NULL);
|
---|
674 | /* Go one less... */
|
---|
675 | if (--terminal_width > MAX_WIDTH)
|
---|
676 | terminal_width = MAX_WIDTH;
|
---|
677 | }
|
---|
678 | # else
|
---|
679 | /* -w is not supported, only -Z and/or -T */
|
---|
680 | opt_complementary = "-";
|
---|
681 | opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
|
---|
682 | # endif
|
---|
683 |
|
---|
684 | # if ENABLE_SELINUX
|
---|
685 | if ((opts & OPT_Z) && is_selinux_enabled()) {
|
---|
686 | psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
|
---|
687 | | PSSCAN_STATE | PSSCAN_COMM;
|
---|
688 | puts(" PID CONTEXT STAT COMMAND");
|
---|
689 | } else
|
---|
690 | # endif
|
---|
691 | if (opts & OPT_l) {
|
---|
692 | psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID
|
---|
693 | | PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM
|
---|
694 | | PSSCAN_VSZ | PSSCAN_RSS;
|
---|
695 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
|
---|
696 | * mandates for -l:
|
---|
697 | * -F Flags (?)
|
---|
698 | * S State
|
---|
699 | * UID,PID,PPID
|
---|
700 | * -C CPU usage
|
---|
701 | * -PRI The priority of the process; higher numbers mean lower priority
|
---|
702 | * -NI Nice value
|
---|
703 | * -ADDR The address of the process (?)
|
---|
704 | * SZ The size in blocks of the core image
|
---|
705 | * -WCHAN The event for which the process is waiting or sleeping
|
---|
706 | * TTY
|
---|
707 | * TIME The cumulative execution time
|
---|
708 | * CMD
|
---|
709 | * We don't show fields marked with '-'.
|
---|
710 | * We show VSZ and RSS instead of SZ.
|
---|
711 | * We also show STIME (standard says that -f shows it, -l doesn't).
|
---|
712 | */
|
---|
713 | puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD");
|
---|
714 | # if ENABLE_FEATURE_PS_LONG
|
---|
715 | now = time(NULL);
|
---|
716 | uptime = get_uptime();
|
---|
717 | # endif
|
---|
718 | }
|
---|
719 | else {
|
---|
720 | puts(" PID USER VSZ STAT COMMAND");
|
---|
721 | }
|
---|
722 | if (opts & OPT_T) {
|
---|
723 | psscan_flags |= PSSCAN_TASKS;
|
---|
724 | }
|
---|
725 | #endif
|
---|
726 |
|
---|
727 | p = NULL;
|
---|
728 | while ((p = procps_scan(p, psscan_flags)) != NULL) {
|
---|
729 | int len;
|
---|
730 | #if ENABLE_SELINUX
|
---|
731 | if (psscan_flags & PSSCAN_CONTEXT) {
|
---|
732 | len = printf("%5u %-32.32s %s ",
|
---|
733 | p->pid,
|
---|
734 | p->context ? p->context : "unknown",
|
---|
735 | p->state);
|
---|
736 | } else
|
---|
737 | #endif
|
---|
738 | {
|
---|
739 | char buf6[6];
|
---|
740 | smart_ulltoa5(p->vsz, buf6, " mgtpezy");
|
---|
741 | buf6[5] = '\0';
|
---|
742 | #if ENABLE_FEATURE_PS_LONG
|
---|
743 | if (opts & OPT_l) {
|
---|
744 | char bufr[6], stime_str[6];
|
---|
745 | char tty[2 * sizeof(int)*3 + 2];
|
---|
746 | char *endp;
|
---|
747 | unsigned sut = (p->stime + p->utime) / 100;
|
---|
748 | unsigned elapsed = uptime - (p->start_time / 100);
|
---|
749 | time_t start = now - elapsed;
|
---|
750 | struct tm *tm = localtime(&start);
|
---|
751 |
|
---|
752 | smart_ulltoa5(p->rss, bufr, " mgtpezy");
|
---|
753 | bufr[5] = '\0';
|
---|
754 |
|
---|
755 | if (p->tty_major == 136)
|
---|
756 | /* It should be pts/N, not ptsN, but N > 9
|
---|
757 | * will overflow field width...
|
---|
758 | */
|
---|
759 | endp = stpcpy(tty, "pts");
|
---|
760 | else
|
---|
761 | if (p->tty_major == 4) {
|
---|
762 | endp = stpcpy(tty, "tty");
|
---|
763 | if (p->tty_minor >= 64) {
|
---|
764 | p->tty_minor -= 64;
|
---|
765 | *endp++ = 'S';
|
---|
766 | }
|
---|
767 | }
|
---|
768 | else
|
---|
769 | endp = tty + sprintf(tty, "%d:", p->tty_major);
|
---|
770 | strcpy(endp, utoa(p->tty_minor));
|
---|
771 |
|
---|
772 | strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm);
|
---|
773 | stime_str[5] = '\0';
|
---|
774 | // S UID PID PPID VSZ RSS TTY STIME TIME CMD
|
---|
775 | len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ",
|
---|
776 | p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty,
|
---|
777 | stime_str, sut / 3600, (sut % 3600) / 60, sut % 60);
|
---|
778 | } else
|
---|
779 | #endif
|
---|
780 | {
|
---|
781 | const char *user = get_cached_username(p->uid);
|
---|
782 | len = printf("%5u %-8.8s %s %s ",
|
---|
783 | p->pid, user, buf6, p->state);
|
---|
784 | }
|
---|
785 | }
|
---|
786 |
|
---|
787 | {
|
---|
788 | int sz = terminal_width - len;
|
---|
789 | char buf[sz + 1];
|
---|
790 | read_cmdline(buf, sz, p->pid, p->comm);
|
---|
791 | puts(buf);
|
---|
792 | }
|
---|
793 | }
|
---|
794 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
795 | clear_username_cache();
|
---|
796 | return EXIT_SUCCESS;
|
---|
797 | }
|
---|
798 |
|
---|
799 | #endif /* !ENABLE_DESKTOP */
|
---|