source: MondoRescue/branches/3.2/mindi-busybox/procps/ps.c@ 3232

Last change on this file since 3232 was 3232, checked in by Bruno Cornec, 10 years ago
  • Update mindi-busybox to 1.21.1
File size: 21.0 KB
RevLine 
[821]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>
[1765]6 * Fix for SELinux Support:(c)2007 Hiroshi Shinji <shiroshi@my.email.ne.jp>
[2725]7 * (c)2007 Yuichi Nakamura <ynakam@hitachisoft.jp>
[821]8 *
[2725]9 * Licensed under GPLv2, see file LICENSE in this source tree.
[821]10 */
11
[3232]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
[1765]64#include "libbb.h"
[3232]65#ifdef __linux__
66# include <sys/sysinfo.h>
67#endif
[1765]68
69/* Absolute maximum on output line length */
70enum { MAX_WIDTH = 2*1024 };
71
[3232]72#if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG
73static 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
[1765]97#if ENABLE_DESKTOP
98
[2725]99#include <sys/times.h> /* for times() */
100#ifndef AT_CLKTCK
[3232]101# define AT_CLKTCK 17
[2725]102#endif
103
[3232]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 */
[2725]124typedef 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
132struct 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 */
157static ptrdiff_t find_elf_note(ptrdiff_t findme)
158{
159 ptrdiff_t *ep = (ptrdiff_t *) environ;
160
[3232]161 while (*ep++)
162 continue;
[2725]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
173static 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
216static inline unsigned get_HZ_by_waiting(void)
217{
218 /* Better method? */
219 return 100;
220}
221#endif
222
223static 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
[3232]234 seconds_since_boot = get_uptime();
[2725]235
236 return kernel_HZ;
237}
238#endif
239
[1765]240/* Print value to buf, max size+1 chars (including trailing '\0') */
241
242static void func_user(char *buf, int size, const procps_status_t *ps)
243{
[2725]244#if 1
[1765]245 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
[2725]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
[1765]259}
260
[2725]261static 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
[1765]266static void func_comm(char *buf, int size, const procps_status_t *ps)
267{
268 safe_strncpy(buf, ps->comm, size+1);
269}
270
[3232]271static void func_state(char *buf, int size, const procps_status_t *ps)
272{
273 safe_strncpy(buf, ps->state, size+1);
274}
275
[1765]276static void func_args(char *buf, int size, const procps_status_t *ps)
277{
[2725]278 read_cmdline(buf, size+1, ps->pid, ps->comm);
[1765]279}
280
281static void func_pid(char *buf, int size, const procps_status_t *ps)
282{
283 sprintf(buf, "%*u", size, ps->pid);
284}
285
286static void func_ppid(char *buf, int size, const procps_status_t *ps)
287{
288 sprintf(buf, "%*u", size, ps->ppid);
289}
290
291static void func_pgid(char *buf, int size, const procps_status_t *ps)
292{
293 sprintf(buf, "%*u", size, ps->pgid);
294}
295
[2725]296static void put_lu(char *buf, int size, unsigned long u)
[1765]297{
[2725]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);
[1765]304}
305
306static void func_vsz(char *buf, int size, const procps_status_t *ps)
307{
[2725]308 put_lu(buf, size, ps->vsz);
[1765]309}
310
311static void func_rss(char *buf, int size, const procps_status_t *ps)
312{
[2725]313 put_lu(buf, size, ps->rss);
[1765]314}
315
316static 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
[2725]324#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
325
326static void func_rgroup(char *buf, int size, const procps_status_t *ps)
[1765]327{
[2725]328 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
[1765]329}
[821]330
[2725]331static 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
[1765]336static void func_nice(char *buf, int size, const procps_status_t *ps)
[821]337{
[2725]338 sprintf(buf, "%*d", size, ps->niceness);
[1765]339}
340
[2725]341#endif
342
343#if ENABLE_FEATURE_PS_TIME
344
[1765]345static void func_etime(char *buf, int size, const procps_status_t *ps)
346{
[2725]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);
[1765]357}
358
359static void func_time(char *buf, int size, const procps_status_t *ps)
360{
[2725]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);
[1765]369}
370
[2725]371#endif
372
373#if ENABLE_SELINUX
374static 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/*
381static void func_nice(char *buf, int size, const procps_status_t *ps)
382{
383 ps->???
384}
385
[1765]386static void func_pcpu(char *buf, int size, const procps_status_t *ps)
387{
388}
389*/
390
391static const ps_out_t out_spec[] = {
[3232]392/* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
[1765]393 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
[2725]394 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
[1765]395 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
[2725]396 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
[1765]397 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
398 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
399 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
[2725]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
[1765]412 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
413 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
[3232]414/* Not mandated, but useful: */
415 { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE },
[1765]416 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
417#if ENABLE_SELINUX
418 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
419#endif
420};
421
422static ps_out_t* new_out_t(void)
423{
[2725]424 out = xrealloc_vector(out, 2, out_cnt);
425 return &out[out_cnt++];
[1765]426}
427
428static const ps_out_t* find_out_spec(const char *name)
429{
[2725]430 unsigned i;
431 char buf[ARRAY_SIZE(out_spec)*7 + 1];
432 char *p = buf;
433
[1765]434 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
[2725]435 if (strncmp(name, out_spec[i].name6, 6) == 0)
[1765]436 return &out_spec[i];
[2725]437 p += sprintf(p, "%.6s,", out_spec[i].name6);
[1765]438 }
[2725]439 p[-1] = '\0';
440 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
[1765]441}
442
443static 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
[2725]482static void alloc_line_buffer(void)
[1765]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 " */
[2725]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 }
[1765]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
506static 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
528static void format_process(const procps_status_t *ps)
529{
[821]530 int i, len;
[1765]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}
[821]551
[3232]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
[2725]559int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
560int ps_main(int argc UNUSED_PARAM, char **argv)
[1765]561{
562 procps_status_t *p;
563 llist_t* opt_o = NULL;
[3232]564 char default_o[sizeof(DEFAULT_O_STR)];
[2725]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 };
[1765]577
[2725]578 INIT_G();
579
[1765]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
[2725]585 // -e Write information for all processes (equivalent to -A)
[1765]586 // -f Generate a full listing
587 // -l Generate a long listing
588 // -o col1,col2,col3=header
589 // Select which columns to display
[2725]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 */
[1765]595 opt_complementary = "o::";
[2725]596 opt = getopt32(argv, "Zo:aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
[1765]597 if (opt_o) {
598 do {
[2725]599 parse_o(llist_pop(&opt_o));
[1765]600 } while (opt_o);
601 } else {
[3232]602 /* Below: parse_o() needs char*, NOT const char*, can't give it default_o */
[821]603#if ENABLE_SELINUX
[2725]604 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
[1765]605 /* no -Z or no SELinux: do not show LABEL */
[2725]606 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
[1765]607 } else
[821]608#endif
[1765]609 {
610 strcpy(default_o, DEFAULT_O_STR);
611 }
612 parse_o(default_o);
613 }
[2725]614#if ENABLE_FEATURE_SHOW_THREADS
615 if (opt & OPT_T)
616 need_flags |= PSSCAN_TASKS;
617#endif
[821]618
[1765]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 }
[2725]627 alloc_line_buffer();
[1765]628 format_header();
629
630 p = NULL;
[2725]631 while ((p = procps_scan(p, need_flags)) != NULL) {
[1765]632 format_process(p);
633 }
634
635 return EXIT_SUCCESS;
636}
637
638
639#else /* !ENABLE_DESKTOP */
640
641
[2725]642int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
643int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
[1765]644{
[2725]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,
[3232]652 OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG,
[2725]653 };
[3232]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
[2725]660 int opts = 0;
661# if ENABLE_FEATURE_PS_WIDE
662 /* -w is a bit complicated */
[821]663 int w_count = 0;
[1765]664 opt_complementary = "-:ww";
[3232]665 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")
666 "w", &w_count);
[821]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 */
[1765]670 if (w_count) {
[2725]671 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
[821]672 } else {
[1765]673 get_terminal_width_height(0, &terminal_width, NULL);
[821]674 /* Go one less... */
[1765]675 if (--terminal_width > MAX_WIDTH)
676 terminal_width = MAX_WIDTH;
[821]677 }
[2725]678# else
679 /* -w is not supported, only -Z and/or -T */
680 opt_complementary = "-";
[3232]681 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
[2725]682# endif
683
[3232]684# if ENABLE_SELINUX
[2725]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
[3232]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 {
[2725]720 puts(" PID USER VSZ STAT COMMAND");
721 }
722 if (opts & OPT_T) {
723 psscan_flags |= PSSCAN_TASKS;
724 }
[3232]725#endif
[821]726
[2725]727 p = NULL;
728 while ((p = procps_scan(p, psscan_flags)) != NULL) {
729 int len;
[821]730#if ENABLE_SELINUX
[2725]731 if (psscan_flags & PSSCAN_CONTEXT) {
732 len = printf("%5u %-32.32s %s ",
[1765]733 p->pid,
734 p->context ? p->context : "unknown",
735 p->state);
736 } else
737#endif
738 {
[3232]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
[2725]780 {
[3232]781 const char *user = get_cached_username(p->uid);
[2725]782 len = printf("%5u %-8.8s %s %s ",
783 p->pid, user, buf6, p->state);
784 }
[821]785 }
786
[1765]787 {
788 int sz = terminal_width - len;
789 char buf[sz + 1];
790 read_cmdline(buf, sz, p->pid, p->comm);
791 puts(buf);
[821]792 }
793 }
[1765]794 if (ENABLE_FEATURE_CLEAN_UP)
795 clear_username_cache();
[821]796 return EXIT_SUCCESS;
797}
[1765]798
[2725]799#endif /* !ENABLE_DESKTOP */
Note: See TracBrowser for help on using the repository browser.