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.
12 //usage:#if ENABLE_DESKTOP
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"
23 //usage:#else /* !ENABLE_DESKTOP */
25 //usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE
26 //usage:#define USAGE_PS "\nThis version of ps accepts no options"
28 //usage:#define USAGE_PS ""
31 //usage:#define ps_trivial_usage
33 //usage:#define ps_full_usage "\n\n"
34 //usage: "Show list of processes\n"
37 //usage: "\n -Z Show selinux context"
39 //usage: IF_FEATURE_PS_WIDE(
40 //usage: "\n w Wide output"
43 //usage:#endif /* ENABLE_DESKTOP */
45 //usage:#define ps_example_usage
47 //usage: " PID Uid Gid State Command\n"
48 //usage: " 1 root root S init\n"
49 //usage: " 2 root root S [kflushd]\n"
50 //usage: " 3 root root S [kupdate]\n"
51 //usage: " 4 root root S [kpiod]\n"
52 //usage: " 5 root root S [kswapd]\n"
53 //usage: " 742 andersen andersen S [bash]\n"
54 //usage: " 743 andersen andersen S -bash\n"
55 //usage: " 745 root root S [getty]\n"
56 //usage: " 2990 andersen andersen R ps\n"
60 /* Absolute maximum on output line length */
61 enum { MAX_WIDTH = 2*1024 };
65 #include <sys/times.h> /* for times() */
71 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
72 * specifies (for XSI-conformant systems) following default columns
73 * (l and f mark columns shown with -l and -f respectively):
74 * F l Flags (octal and additive) associated with the process (??)
75 * S l The state of the process
76 * UID f,l The user ID; the login name is printed with -f
78 * PPID f,l The parent process
79 * C f,l Processor utilization
80 * PRI l The priority of the process; higher numbers mean lower priority
82 * ADDR l The address of the process
83 * SZ l The size in blocks of the core image of the process
84 * WCHAN l The event for which the process is waiting or sleeping
85 * STIME f Starting time of the process
86 * TTY The controlling terminal for the process
87 * TIME The cumulative execution time for the process
88 * CMD The command name; the full command line is shown with -f
91 # define SELINUX_O_PREFIX "label,"
92 # define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
94 # define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
101 void (*f)(char *buf, int size, const procps_status_t *ps);
111 unsigned terminal_width;
112 #if ENABLE_FEATURE_PS_TIME
114 unsigned long long seconds_since_boot;
116 char default_o[sizeof(DEFAULT_O_STR)];
118 #define G (*(struct globals*)&bb_common_bufsiz1)
120 #define out_cnt (G.out_cnt )
121 #define print_header (G.print_header )
122 #define need_flags (G.need_flags )
123 #define buffer (G.buffer )
124 #define terminal_width (G.terminal_width )
125 #define kernel_HZ (G.kernel_HZ )
126 #define seconds_since_boot (G.seconds_since_boot)
127 #define default_o (G.default_o )
128 #define INIT_G() do { } while (0)
130 #if ENABLE_FEATURE_PS_TIME
131 /* for ELF executables, notes are pushed before environment and args */
132 static ptrdiff_t find_elf_note(ptrdiff_t findme)
134 ptrdiff_t *ep = (ptrdiff_t *) environ;
139 if (ep[0] == findme) {
147 #if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
148 static unsigned get_HZ_by_waiting(void)
150 struct timeval tv1, tv2;
151 unsigned t1, t2, r, hz;
152 unsigned cnt = cnt; /* for compiler */
157 /* Wait for times() to reach new tick */
162 gettimeofday(&tv2, NULL);
166 tv1.tv_usec = tv2.tv_usec;
168 /* Wait exactly one times() tick */
172 gettimeofday(&tv2, NULL);
174 /* Calculate ticks per sec, rounding up to even */
175 diff = tv2.tv_usec - tv1.tv_usec;
176 if (diff <= 0) diff += 1000000;
177 hz = 1000000u / (unsigned)diff;
180 /* Count how many same hz values we saw */
186 } while (cnt < 3); /* exit if saw 3 same values */
191 static inline unsigned get_HZ_by_waiting(void)
198 static unsigned get_kernel_HZ(void)
206 /* Works for ELF only, Linux 2.4.0+ */
207 kernel_HZ = find_elf_note(AT_CLKTCK);
208 if (kernel_HZ == (unsigned)-1)
209 kernel_HZ = get_HZ_by_waiting();
211 //if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
212 // bb_perror_msg_and_die("can't read %s", "/proc/uptime");
213 //buf[sizeof(buf)-1] = '\0';
214 ///sscanf(buf, "%llu", &seconds_since_boot);
216 seconds_since_boot = info.uptime;
222 /* Print value to buf, max size+1 chars (including trailing '\0') */
224 static void func_user(char *buf, int size, const procps_status_t *ps)
227 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
229 /* "compatible" version, but it's larger */
230 /* procps 2.18 shows numeric UID if name overflows the field */
231 /* TODO: get_cached_username() returns numeric string if
232 * user has no passwd record, we will display it
233 * left-justified here; too long usernames are shown
234 * as _right-justified_ IDs. Is it worth fixing? */
235 const char *user = get_cached_username(ps->uid);
236 if (strlen(user) <= size)
237 safe_strncpy(buf, user, size+1);
239 sprintf(buf, "%*u", size, (unsigned)ps->uid);
243 static void func_group(char *buf, int size, const procps_status_t *ps)
245 safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
248 static void func_comm(char *buf, int size, const procps_status_t *ps)
250 safe_strncpy(buf, ps->comm, size+1);
253 static void func_stat(char *buf, int size, const procps_status_t *ps)
255 safe_strncpy(buf, ps->state, size+1);
258 static void func_args(char *buf, int size, const procps_status_t *ps)
260 read_cmdline(buf, size+1, ps->pid, ps->comm);
263 static void func_pid(char *buf, int size, const procps_status_t *ps)
265 sprintf(buf, "%*u", size, ps->pid);
268 static void func_ppid(char *buf, int size, const procps_status_t *ps)
270 sprintf(buf, "%*u", size, ps->ppid);
273 static void func_pgid(char *buf, int size, const procps_status_t *ps)
275 sprintf(buf, "%*u", size, ps->pgid);
278 static void put_lu(char *buf, int size, unsigned long u)
282 /* see http://en.wikipedia.org/wiki/Tera */
283 smart_ulltoa4(u, buf4, " mgtpezy");
285 sprintf(buf, "%.*s", size, buf4);
288 static void func_vsz(char *buf, int size, const procps_status_t *ps)
290 put_lu(buf, size, ps->vsz);
293 static void func_rss(char *buf, int size, const procps_status_t *ps)
295 put_lu(buf, size, ps->rss);
298 static void func_tty(char *buf, int size, const procps_status_t *ps)
302 if (ps->tty_major) /* tty field of "0" means "no tty" */
303 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
306 #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
308 static void func_rgroup(char *buf, int size, const procps_status_t *ps)
310 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
313 static void func_ruser(char *buf, int size, const procps_status_t *ps)
315 safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
318 static void func_nice(char *buf, int size, const procps_status_t *ps)
320 sprintf(buf, "%*d", size, ps->niceness);
325 #if ENABLE_FEATURE_PS_TIME
327 static void func_etime(char *buf, int size, const procps_status_t *ps)
329 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
333 mm = ps->start_time / get_kernel_HZ();
334 /* must be after get_kernel_HZ()! */
335 mm = seconds_since_boot - mm;
338 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
341 static void func_time(char *buf, int size, const procps_status_t *ps)
343 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
347 mm = (ps->utime + ps->stime) / get_kernel_HZ();
350 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
356 static void func_label(char *buf, int size, const procps_status_t *ps)
358 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
363 static void func_nice(char *buf, int size, const procps_status_t *ps)
368 static void func_pcpu(char *buf, int size, const procps_status_t *ps)
373 static const ps_out_t out_spec[] = {
374 /* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
375 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
376 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
377 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
378 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
379 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
380 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
381 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
382 #if ENABLE_FEATURE_PS_TIME
383 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
385 #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
386 { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
387 { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
388 { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
389 // { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
391 #if ENABLE_FEATURE_PS_TIME
392 { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
394 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
395 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
396 /* Not mandated, but useful: */
397 { 4 , "stat" ,"STAT" ,func_stat ,PSSCAN_STAT },
398 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
400 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
404 static ps_out_t* new_out_t(void)
406 out = xrealloc_vector(out, 2, out_cnt);
407 return &out[out_cnt++];
410 static const ps_out_t* find_out_spec(const char *name)
413 char buf[ARRAY_SIZE(out_spec)*7 + 1];
416 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
417 if (strncmp(name, out_spec[i].name6, 6) == 0)
419 p += sprintf(p, "%.6s,", out_spec[i].name6);
422 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
425 static void parse_o(char* opt)
428 // POSIX: "-o is blank- or comma-separated list" (FIXME)
431 comma = strchr(opt, ',');
432 equal = strchr(opt, '=');
433 if (comma && (!equal || equal > comma)) {
435 *new_out_t() = *find_out_spec(opt);
442 // opt points to last spec in comma separated list.
443 // This one can have =HEADER part.
447 *new = *find_out_spec(opt);
450 new->header = equal + 1;
451 // POSIX: the field widths shall be ... at least as wide as
452 // the header text (default or overridden value).
453 // If the header text is null, such as -o user=,
454 // the field width shall be at least as wide as the
455 // default header text
456 if (new->header[0]) {
457 new->width = strlen(new->header);
464 static void alloc_line_buffer(void)
468 for (i = 0; i < out_cnt; i++) {
469 need_flags |= out[i].ps_flags;
470 if (out[i].header[0]) {
473 width += out[i].width + 1; /* "FIELD " */
474 if ((int)(width - terminal_width) > 0) {
475 /* The rest does not fit on the screen */
476 //out[i].width -= (width - terminal_width - 1);
482 if (!is_selinux_enabled())
483 need_flags &= ~PSSCAN_CONTEXT;
485 buffer = xmalloc(width + 1); /* for trailing \0 */
488 static void format_header(void)
501 if (++i == out_cnt) /* do not pad last field */
503 p += sprintf(p, "%-*s ", op->width, op->header);
505 strcpy(p, op->header);
507 printf("%.*s\n", terminal_width, buffer);
510 static void format_process(const procps_status_t *ps)
515 if (out_cnt) while (1) {
516 out[i].f(p, out[i].width, ps);
517 // POSIX: Any field need not be meaningful in all
518 // implementations. In such a case a hyphen ( '-' )
519 // should be output in place of the field value.
526 len = out[i].width - len + 1;
527 if (++i == out_cnt) /* do not pad last field */
529 p += sprintf(p, "%*s", len, "");
531 printf("%.*s\n", terminal_width, buffer);
534 int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
535 int ps_main(int argc UNUSED_PARAM, char **argv)
538 llist_t* opt_o = NULL;
549 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
555 // -a Write information for all processes associated with terminals
556 // Implementations may omit session leaders from this list
557 // -A Write information for all processes
558 // -d Write information for all processes, except session leaders
559 // -e Write information for all processes (equivalent to -A)
560 // -f Generate a full listing
561 // -l Generate a long listing
562 // -o col1,col2,col3=header
563 // Select which columns to display
564 /* We allow (and ignore) most of the above. FIXME.
565 * -T is picked for threads (POSIX hasn't it standardized).
566 * procps v3.2.7 supports -T and shows tids as SPID column,
567 * it also supports -L where it shows tids as LWP column.
569 opt_complementary = "o::";
570 opt = getopt32(argv, "Zo:aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
573 parse_o(llist_pop(&opt_o));
576 /* Below: parse_o() needs char*, NOT const char*... */
578 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
579 /* no -Z or no SELinux: do not show LABEL */
580 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
584 strcpy(default_o, DEFAULT_O_STR);
588 #if ENABLE_FEATURE_SHOW_THREADS
590 need_flags |= PSSCAN_TASKS;
593 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
594 * and such large widths */
595 terminal_width = MAX_WIDTH;
597 get_terminal_width_height(0, &terminal_width, NULL);
598 if (--terminal_width > MAX_WIDTH)
599 terminal_width = MAX_WIDTH;
605 while ((p = procps_scan(p, need_flags)) != NULL) {
613 #else /* !ENABLE_DESKTOP */
616 int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
617 int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
620 int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
621 | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
622 unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
624 OPT_Z = (1 << 0) * ENABLE_SELINUX,
625 OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
628 /* If we support any options, parse argv */
629 #if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE
630 # if ENABLE_FEATURE_PS_WIDE
631 /* -w is a bit complicated */
633 opt_complementary = "-:ww";
634 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")"w", &w_count);
635 /* if w is given once, GNU ps sets the width to 132,
636 * if w is given more than once, it is "unlimited"
639 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
641 get_terminal_width_height(0, &terminal_width, NULL);
643 if (--terminal_width > MAX_WIDTH)
644 terminal_width = MAX_WIDTH;
647 /* -w is not supported, only -Z and/or -T */
648 opt_complementary = "-";
649 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T"));
654 if ((opts & OPT_Z) && is_selinux_enabled()) {
655 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
656 | PSSCAN_STATE | PSSCAN_COMM;
657 puts(" PID CONTEXT STAT COMMAND");
661 puts(" PID USER VSZ STAT COMMAND");
664 psscan_flags |= PSSCAN_TASKS;
668 while ((p = procps_scan(p, psscan_flags)) != NULL) {
671 if (psscan_flags & PSSCAN_CONTEXT) {
672 len = printf("%5u %-32.32s %s ",
674 p->context ? p->context : "unknown",
679 const char *user = get_cached_username(p->uid);
681 // len = printf("%5u %-8.8s %s ",
682 // p->pid, user, p->state);
686 smart_ulltoa5(p->vsz, buf6, " mgtpezy");
688 len = printf("%5u %-8.8s %s %s ",
689 p->pid, user, buf6, p->state);
694 int sz = terminal_width - len;
696 read_cmdline(buf, sz, p->pid, p->comm);
700 if (ENABLE_FEATURE_CLEAN_UP)
701 clear_username_cache();
705 #endif /* !ENABLE_DESKTOP */