1 /* vi: set sw=4 ts=4: */
3 * Mini ps implementation(s) for busybox
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>
9 * Licensed under GPLv2, see file LICENSE in this source tree.
15 //config: ps gives a snapshot of the current processes.
17 //config:config FEATURE_PS_WIDE
18 //config: bool "Enable wide output option (-w)"
20 //config: depends on PS && !DESKTOP
22 //config: Support argument 'w' for wide output.
23 //config: If given once, 132 chars are printed, and if given more
24 //config: than once, the length is unlimited.
26 //config:config FEATURE_PS_LONG
27 //config: bool "Enable long output option (-l)"
29 //config: depends on PS && !DESKTOP
31 //config: Support argument 'l' for long output.
32 //config: Adds fields PPID, RSS, START, TIME & TTY
34 //config:config FEATURE_PS_TIME
35 //config: bool "Support -o time and -o etime output specifiers"
37 //config: depends on PS && DESKTOP
38 //config: select PLATFORM_LINUX
40 //config:config FEATURE_PS_UNUSUAL_SYSTEMS
41 //config: bool "Support Linux prior to 2.4.0 and non-ELF systems"
43 //config: depends on FEATURE_PS_TIME
45 //config: Include support for measuring HZ on old kernels and non-ELF systems
46 //config: (if you are on Linux 2.4.0+ and use ELF, you don't need this)
48 //config:config FEATURE_PS_ADDITIONAL_COLUMNS
49 //config: bool "Support -o rgroup, -o ruser, -o nice specifiers"
51 //config: depends on PS && DESKTOP
53 //applet:IF_PS(APPLET(ps, BB_DIR_BIN, BB_SUID_DROP))
55 //kbuild:lib-$(CONFIG_PS) += ps.o
57 //usage:#if ENABLE_DESKTOP
59 //usage:#define ps_trivial_usage
60 //usage: "[-o COL1,COL2=HEADER]" IF_FEATURE_SHOW_THREADS(" [-T]")
61 //usage:#define ps_full_usage "\n\n"
62 //usage: "Show list of processes\n"
63 //usage: "\n -o COL1,COL2=HEADER Select columns for display"
64 //usage: IF_FEATURE_SHOW_THREADS(
65 //usage: "\n -T Show threads"
68 //usage:#else /* !ENABLE_DESKTOP */
70 //usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE
71 //usage:#define USAGE_PS "\nThis version of ps accepts no options"
73 //usage:#define USAGE_PS ""
76 //usage:#define ps_trivial_usage
78 //usage:#define ps_full_usage "\n\n"
79 //usage: "Show list of processes\n"
82 //usage: "\n -Z Show selinux context"
84 //usage: IF_FEATURE_PS_WIDE(
85 //usage: "\n w Wide output"
87 //usage: IF_FEATURE_PS_LONG(
88 //usage: "\n l Long output"
90 //usage: IF_FEATURE_SHOW_THREADS(
91 //usage: "\n T Show threads"
94 //usage:#endif /* ENABLE_DESKTOP */
96 //usage:#define ps_example_usage
98 //usage: " PID Uid Gid State Command\n"
99 //usage: " 1 root root S init\n"
100 //usage: " 2 root root S [kflushd]\n"
101 //usage: " 3 root root S [kupdate]\n"
102 //usage: " 4 root root S [kpiod]\n"
103 //usage: " 5 root root S [kswapd]\n"
104 //usage: " 742 andersen andersen S [bash]\n"
105 //usage: " 743 andersen andersen S -bash\n"
106 //usage: " 745 root root S [getty]\n"
107 //usage: " 2990 andersen andersen R ps\n"
110 #include "common_bufsiz.h"
112 # include <sys/sysinfo.h>
115 /* Absolute maximum on output line length */
116 enum { MAX_WIDTH = 2*1024 };
118 #if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG
119 static unsigned long get_uptime(void)
123 if (sysinfo(&info) < 0)
127 unsigned long uptime;
128 char buf[sizeof(uptime)*3 + 2];
129 /* /proc/uptime is "UPTIME_SEC.NN IDLE_SEC.NN\n"
130 * (where IDLE is cumulative over all CPUs)
132 if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
133 bb_perror_msg_and_die("can't read '%s'", "/proc/uptime");
134 buf[sizeof(buf)-1] = '\0';
135 sscanf(buf, "%lu", &uptime);
139 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
148 #include <sys/times.h> /* for times() */
150 # define AT_CLKTCK 17
154 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
155 * specifies (for XSI-conformant systems) following default columns
156 * (l and f mark columns shown with -l and -f respectively):
157 * F l Flags (octal and additive) associated with the process (??)
158 * S l The state of the process
159 * UID f,l The user ID; the login name is printed with -f
161 * PPID f,l The parent process
162 * C f,l Processor utilization
163 * PRI l The priority of the process; higher numbers mean lower priority
165 * ADDR l The address of the process
166 * SZ l The size in blocks of the core image of the process
167 * WCHAN l The event for which the process is waiting or sleeping
168 * STIME f Starting time of the process
169 * TTY The controlling terminal for the process
170 * TIME The cumulative execution time for the process
171 * CMD The command name; the full command line is shown with -f
177 void (*f)(char *buf, int size, const procps_status_t *ps);
187 unsigned terminal_width;
188 #if ENABLE_FEATURE_PS_TIME
190 unsigned long seconds_since_boot;
193 #define G (*(struct globals*)bb_common_bufsiz1)
195 #define out_cnt (G.out_cnt )
196 #define print_header (G.print_header )
197 #define need_flags (G.need_flags )
198 #define buffer (G.buffer )
199 #define terminal_width (G.terminal_width )
200 #define kernel_HZ (G.kernel_HZ )
201 #define INIT_G() do { setup_common_bufsiz(); } while (0)
203 #if ENABLE_FEATURE_PS_TIME
204 /* for ELF executables, notes are pushed before environment and args */
205 static uintptr_t find_elf_note(uintptr_t findme)
207 uintptr_t *ep = (uintptr_t *) environ;
212 if (ep[0] == findme) {
220 #if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
221 static unsigned get_HZ_by_waiting(void)
223 struct timeval tv1, tv2;
224 unsigned t1, t2, r, hz;
225 unsigned cnt = cnt; /* for compiler */
230 /* Wait for times() to reach new tick */
235 gettimeofday(&tv2, NULL);
239 tv1.tv_usec = tv2.tv_usec;
241 /* Wait exactly one times() tick */
245 gettimeofday(&tv2, NULL);
247 /* Calculate ticks per sec, rounding up to even */
248 diff = tv2.tv_usec - tv1.tv_usec;
249 if (diff <= 0) diff += 1000000;
250 hz = 1000000u / (unsigned)diff;
253 /* Count how many same hz values we saw */
259 } while (cnt < 3); /* exit if saw 3 same values */
264 static inline unsigned get_HZ_by_waiting(void)
271 static unsigned get_kernel_HZ(void)
276 /* Works for ELF only, Linux 2.4.0+ */
277 kernel_HZ = find_elf_note(AT_CLKTCK);
278 if (kernel_HZ == (unsigned)-1)
279 kernel_HZ = get_HZ_by_waiting();
281 G.seconds_since_boot = get_uptime();
287 /* Print value to buf, max size+1 chars (including trailing '\0') */
289 static void func_user(char *buf, int size, const procps_status_t *ps)
292 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
294 /* "compatible" version, but it's larger */
295 /* procps 2.18 shows numeric UID if name overflows the field */
296 /* TODO: get_cached_username() returns numeric string if
297 * user has no passwd record, we will display it
298 * left-justified here; too long usernames are shown
299 * as _right-justified_ IDs. Is it worth fixing? */
300 const char *user = get_cached_username(ps->uid);
301 if (strlen(user) <= size)
302 safe_strncpy(buf, user, size+1);
304 sprintf(buf, "%*u", size, (unsigned)ps->uid);
308 static void func_group(char *buf, int size, const procps_status_t *ps)
310 safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
313 static void func_comm(char *buf, int size, const procps_status_t *ps)
315 safe_strncpy(buf, ps->comm, size+1);
318 static void func_state(char *buf, int size, const procps_status_t *ps)
320 safe_strncpy(buf, ps->state, size+1);
323 static void func_args(char *buf, int size, const procps_status_t *ps)
325 read_cmdline(buf, size+1, ps->pid, ps->comm);
328 static void func_pid(char *buf, int size, const procps_status_t *ps)
330 sprintf(buf, "%*u", size, ps->pid);
333 static void func_ppid(char *buf, int size, const procps_status_t *ps)
335 sprintf(buf, "%*u", size, ps->ppid);
338 static void func_pgid(char *buf, int size, const procps_status_t *ps)
340 sprintf(buf, "%*u", size, ps->pgid);
343 static void put_lu(char *buf, int size, unsigned long u)
347 /* see http://en.wikipedia.org/wiki/Tera */
348 smart_ulltoa4(u, buf4, " mgtpezy")[0] = '\0';
349 sprintf(buf, "%.*s", size, buf4);
352 static void func_vsz(char *buf, int size, const procps_status_t *ps)
354 put_lu(buf, size, ps->vsz);
357 static void func_rss(char *buf, int size, const procps_status_t *ps)
359 put_lu(buf, size, ps->rss);
362 static void func_tty(char *buf, int size, const procps_status_t *ps)
366 if (ps->tty_major) /* tty field of "0" means "no tty" */
367 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
370 #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
372 static void func_rgroup(char *buf, int size, const procps_status_t *ps)
374 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
377 static void func_ruser(char *buf, int size, const procps_status_t *ps)
379 safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
382 static void func_nice(char *buf, int size, const procps_status_t *ps)
384 sprintf(buf, "%*d", size, ps->niceness);
389 #if ENABLE_FEATURE_PS_TIME
391 static void func_etime(char *buf, int size, const procps_status_t *ps)
393 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
397 mm = ps->start_time / get_kernel_HZ();
398 /* must be after get_kernel_HZ()! */
399 mm = G.seconds_since_boot - mm;
402 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
405 static void func_time(char *buf, int size, const procps_status_t *ps)
407 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
411 mm = (ps->utime + ps->stime) / get_kernel_HZ();
414 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
420 static void func_label(char *buf, int size, const procps_status_t *ps)
422 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
427 static void func_nice(char *buf, int size, const procps_status_t *ps)
432 static void func_pcpu(char *buf, int size, const procps_status_t *ps)
437 static const ps_out_t out_spec[] = {
438 /* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
439 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
440 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
441 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
442 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
443 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
444 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
445 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
446 #if ENABLE_FEATURE_PS_TIME
447 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
449 #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
450 { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
451 { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
452 { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
453 // { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
455 #if ENABLE_FEATURE_PS_TIME
456 { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
458 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
459 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
460 /* Not mandated, but useful: */
461 { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE },
462 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
464 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
468 static ps_out_t* new_out_t(void)
470 out = xrealloc_vector(out, 2, out_cnt);
471 return &out[out_cnt++];
474 static const ps_out_t* find_out_spec(const char *name)
477 char buf[ARRAY_SIZE(out_spec)*7 + 1];
480 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
481 if (strncmp(name, out_spec[i].name6, 6) == 0)
483 p += sprintf(p, "%.6s,", out_spec[i].name6);
486 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
489 static void parse_o(char* opt)
492 // POSIX: "-o is blank- or comma-separated list" (FIXME)
495 comma = strchr(opt, ',');
496 equal = strchr(opt, '=');
497 if (comma && (!equal || equal > comma)) {
499 *new_out_t() = *find_out_spec(opt);
506 // opt points to last spec in comma separated list.
507 // This one can have =HEADER part.
511 *new = *find_out_spec(opt);
514 new->header = equal + 1;
515 // POSIX: the field widths shall be ... at least as wide as
516 // the header text (default or overridden value).
517 // If the header text is null, such as -o user=,
518 // the field width shall be at least as wide as the
519 // default header text
520 if (new->header[0]) {
521 new->width = strlen(new->header);
528 static void alloc_line_buffer(void)
532 for (i = 0; i < out_cnt; i++) {
533 need_flags |= out[i].ps_flags;
534 if (out[i].header[0]) {
537 width += out[i].width + 1; /* "FIELD " */
538 if ((int)(width - terminal_width) > 0) {
539 /* The rest does not fit on the screen */
540 //out[i].width -= (width - terminal_width - 1);
546 if (!is_selinux_enabled())
547 need_flags &= ~PSSCAN_CONTEXT;
549 buffer = xmalloc(width + 1); /* for trailing \0 */
552 static void format_header(void)
565 if (++i == out_cnt) /* do not pad last field */
567 p += sprintf(p, "%-*s ", op->width, op->header);
569 strcpy(p, op->header);
571 printf("%.*s\n", terminal_width, buffer);
574 static void format_process(const procps_status_t *ps)
579 if (out_cnt) while (1) {
580 out[i].f(p, out[i].width, ps);
581 // POSIX: Any field need not be meaningful in all
582 // implementations. In such a case a hyphen ( '-' )
583 // should be output in place of the field value.
590 len = out[i].width - len + 1;
591 if (++i == out_cnt) /* do not pad last field */
593 p += sprintf(p, "%*s", len, "");
595 printf("%.*s\n", terminal_width, buffer);
599 # define SELINUX_O_PREFIX "label,"
600 # define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
602 # define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
605 int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
606 int ps_main(int argc UNUSED_PARAM, char **argv)
609 llist_t* opt_o = NULL;
610 char default_o[sizeof(DEFAULT_O_STR)];
621 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
627 // -a Write information for all processes associated with terminals
628 // Implementations may omit session leaders from this list
629 // -A Write information for all processes
630 // -d Write information for all processes, except session leaders
631 // -e Write information for all processes (equivalent to -A)
632 // -f Generate a full listing
633 // -l Generate a long listing
634 // -o col1,col2,col3=header
635 // Select which columns to display
636 /* We allow (and ignore) most of the above. FIXME.
637 * -T is picked for threads (POSIX hasn't standardized it).
638 * procps v3.2.7 supports -T and shows tids as SPID column,
639 * it also supports -L where it shows tids as LWP column.
641 opt = getopt32(argv, "Zo:*aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
644 parse_o(llist_pop(&opt_o));
647 /* Below: parse_o() needs char*, NOT const char*,
648 * can't pass it constant string. Need to make a copy first.
651 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
652 /* no -Z or no SELinux: do not show LABEL */
653 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
657 strcpy(default_o, DEFAULT_O_STR);
661 #if ENABLE_FEATURE_SHOW_THREADS
663 need_flags |= PSSCAN_TASKS;
666 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
667 * and such large widths */
668 terminal_width = MAX_WIDTH;
670 terminal_width = get_terminal_width(0);
671 if (--terminal_width > MAX_WIDTH)
672 terminal_width = MAX_WIDTH;
678 while ((p = procps_scan(p, need_flags)) != NULL) {
686 #else /* !ENABLE_DESKTOP */
689 int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
690 int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
693 int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
694 | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
695 unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
697 OPT_Z = (1 << 0) * ENABLE_SELINUX,
698 OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
699 OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG,
701 #if ENABLE_FEATURE_PS_LONG
702 time_t now = now; /* for compiler */
703 unsigned long uptime = uptime;
705 /* If we support any options, parse argv */
706 #if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE || ENABLE_FEATURE_PS_LONG
708 # if ENABLE_FEATURE_PS_WIDE
709 /* -w is a bit complicated */
711 opt_complementary = "-:ww";
712 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")
714 /* if w is given once, GNU ps sets the width to 132,
715 * if w is given more than once, it is "unlimited"
718 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
720 terminal_width = get_terminal_width(0);
722 if (--terminal_width > MAX_WIDTH)
723 terminal_width = MAX_WIDTH;
726 /* -w is not supported, only -Z and/or -T */
727 opt_complementary = "-";
728 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
732 if ((opts & OPT_Z) && is_selinux_enabled()) {
733 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
734 | PSSCAN_STATE | PSSCAN_COMM;
735 puts(" PID CONTEXT STAT COMMAND");
739 psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID
740 | PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM
741 | PSSCAN_VSZ | PSSCAN_RSS;
742 /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
748 * -PRI The priority of the process; higher numbers mean lower priority
750 * -ADDR The address of the process (?)
751 * SZ The size in blocks of the core image
752 * -WCHAN The event for which the process is waiting or sleeping
754 * TIME The cumulative execution time
756 * We don't show fields marked with '-'.
757 * We show VSZ and RSS instead of SZ.
758 * We also show STIME (standard says that -f shows it, -l doesn't).
760 puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD");
761 # if ENABLE_FEATURE_PS_LONG
763 uptime = get_uptime();
767 puts(" PID USER VSZ STAT COMMAND");
770 psscan_flags |= PSSCAN_TASKS;
775 while ((p = procps_scan(p, psscan_flags)) != NULL) {
778 if (psscan_flags & PSSCAN_CONTEXT) {
779 len = printf("%5u %-32.32s %s ",
781 p->context ? p->context : "unknown",
787 smart_ulltoa5(p->vsz, buf6, " mgtpezy")[0] = '\0';
788 #if ENABLE_FEATURE_PS_LONG
790 char bufr[6], stime_str[6];
791 char tty[2 * sizeof(int)*3 + 2];
793 unsigned sut = (p->stime + p->utime) / 100;
794 unsigned elapsed = uptime - (p->start_time / 100);
795 time_t start = now - elapsed;
796 struct tm *tm = localtime(&start);
798 smart_ulltoa5(p->rss, bufr, " mgtpezy")[0] = '\0';
800 if (p->tty_major == 136)
801 /* It should be pts/N, not ptsN, but N > 9
802 * will overflow field width...
804 endp = stpcpy(tty, "pts");
806 if (p->tty_major == 4) {
807 endp = stpcpy(tty, "tty");
808 if (p->tty_minor >= 64) {
814 endp = tty + sprintf(tty, "%d:", p->tty_major);
815 strcpy(endp, utoa(p->tty_minor));
817 strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm);
819 // S UID PID PPID VSZ RSS TTY STIME TIME CMD
820 len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ",
821 p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty,
822 stime_str, sut / 3600, (sut % 3600) / 60, sut % 60);
826 const char *user = get_cached_username(p->uid);
827 len = printf("%5u %-8.8s %s %s ",
828 p->pid, user, buf6, p->state);
833 int sz = terminal_width - len;
836 read_cmdline(buf, sz, p->pid, p->comm);
841 if (ENABLE_FEATURE_CLEAN_UP)
842 clear_username_cache();
846 #endif /* !ENABLE_DESKTOP */