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 the GPL version 2, see the file LICENSE in this tarball.
14 /* Absolute maximum on output line length */
15 enum { MAX_WIDTH
= 2*1024 };
19 #include <sys/times.h> /* for times() */
20 //#include <sys/sysinfo.h> /* for sysinfo() */
27 #define SELINUX_O_PREFIX "label,"
28 #define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" USE_FEATURE_PS_TIME(",time") ",args")
30 #define DEFAULT_O_STR ("pid,user" USE_FEATURE_PS_TIME(",time") ",args")
37 void (*f
)(char *buf
, int size
, const procps_status_t
*ps
);
47 unsigned terminal_width
;
48 #if ENABLE_FEATURE_PS_TIME
50 unsigned long long seconds_since_boot
;
52 char default_o
[sizeof(DEFAULT_O_STR
)];
54 #define G (*(struct globals*)&bb_common_bufsiz1)
56 #define out_cnt (G.out_cnt )
57 #define print_header (G.print_header )
58 #define need_flags (G.need_flags )
59 #define buffer (G.buffer )
60 #define terminal_width (G.terminal_width )
61 #define kernel_HZ (G.kernel_HZ )
62 #define seconds_since_boot (G.seconds_since_boot)
63 #define default_o (G.default_o )
65 #if ENABLE_FEATURE_PS_TIME
66 /* for ELF executables, notes are pushed before environment and args */
67 static ptrdiff_t find_elf_note(ptrdiff_t findme
)
69 ptrdiff_t *ep
= (ptrdiff_t *) environ
;
73 if (ep
[0] == findme
) {
81 #if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
82 static unsigned get_HZ_by_waiting(void)
84 struct timeval tv1
, tv2
;
85 unsigned t1
, t2
, r
, hz
;
86 unsigned cnt
= cnt
; /* for compiler */
91 /* Wait for times() to reach new tick */
96 gettimeofday(&tv2
, NULL
);
100 tv1
.tv_usec
= tv2
.tv_usec
;
102 /* Wait exactly one times() tick */
106 gettimeofday(&tv2
, NULL
);
108 /* Calculate ticks per sec, rounding up to even */
109 diff
= tv2
.tv_usec
- tv1
.tv_usec
;
110 if (diff
<= 0) diff
+= 1000000;
111 hz
= 1000000u / (unsigned)diff
;
114 /* Count how many same hz values we saw */
120 } while (cnt
< 3); /* exit if saw 3 same values */
125 static inline unsigned get_HZ_by_waiting(void)
132 static unsigned get_kernel_HZ(void)
140 /* Works for ELF only, Linux 2.4.0+ */
141 kernel_HZ
= find_elf_note(AT_CLKTCK
);
142 if (kernel_HZ
== (unsigned)-1)
143 kernel_HZ
= get_HZ_by_waiting();
145 //if (open_read_close("/proc/uptime", buf, sizeof(buf) <= 0)
146 // bb_perror_msg_and_die("cannot read %s", "/proc/uptime");
147 //buf[sizeof(buf)-1] = '\0';
148 ///sscanf(buf, "%llu", &seconds_since_boot);
150 seconds_since_boot
= info
.uptime
;
156 /* Print value to buf, max size+1 chars (including trailing '\0') */
158 static void func_user(char *buf
, int size
, const procps_status_t
*ps
)
161 safe_strncpy(buf
, get_cached_username(ps
->uid
), size
+1);
163 /* "compatible" version, but it's larger */
164 /* procps 2.18 shows numeric UID if name overflows the field */
165 /* TODO: get_cached_username() returns numeric string if
166 * user has no passwd record, we will display it
167 * left-justified here; too long usernames are shown
168 * as _right-justified_ IDs. Is it worth fixing? */
169 const char *user
= get_cached_username(ps
->uid
);
170 if (strlen(user
) <= size
)
171 safe_strncpy(buf
, user
, size
+1);
173 sprintf(buf
, "%*u", size
, (unsigned)ps
->uid
);
177 static void func_comm(char *buf
, int size
, const procps_status_t
*ps
)
179 safe_strncpy(buf
, ps
->comm
, size
+1);
182 static void func_args(char *buf
, int size
, const procps_status_t
*ps
)
184 read_cmdline(buf
, size
, ps
->pid
, ps
->comm
);
187 static void func_pid(char *buf
, int size
, const procps_status_t
*ps
)
189 sprintf(buf
, "%*u", size
, ps
->pid
);
192 static void func_ppid(char *buf
, int size
, const procps_status_t
*ps
)
194 sprintf(buf
, "%*u", size
, ps
->ppid
);
197 static void func_pgid(char *buf
, int size
, const procps_status_t
*ps
)
199 sprintf(buf
, "%*u", size
, ps
->pgid
);
202 static void put_lu(char *buf
, int size
, unsigned long u
)
206 /* see http://en.wikipedia.org/wiki/Tera */
207 smart_ulltoa4(u
, buf4
, " mgtpezy");
209 sprintf(buf
, "%.*s", size
, buf4
);
212 static void func_vsz(char *buf
, int size
, const procps_status_t
*ps
)
214 put_lu(buf
, size
, ps
->vsz
);
217 static void func_rss(char *buf
, int size
, const procps_status_t
*ps
)
219 put_lu(buf
, size
, ps
->rss
);
222 static void func_tty(char *buf
, int size
, const procps_status_t
*ps
)
226 if (ps
->tty_major
) /* tty field of "0" means "no tty" */
227 snprintf(buf
, size
+1, "%u,%u", ps
->tty_major
, ps
->tty_minor
);
230 #if ENABLE_FEATURE_PS_TIME
231 static void func_etime(char *buf
, int size
, const procps_status_t
*ps
)
233 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
237 mm
= ps
->start_time
/ get_kernel_HZ();
238 /* must be after get_kernel_HZ()! */
239 mm
= seconds_since_boot
- mm
;
242 snprintf(buf
, size
+1, "%3lu:%02u", mm
, ss
);
245 static void func_time(char *buf
, int size
, const procps_status_t
*ps
)
247 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
251 mm
= (ps
->utime
+ ps
->stime
) / get_kernel_HZ();
254 snprintf(buf
, size
+1, "%3lu:%02u", mm
, ss
);
259 static void func_label(char *buf
, int size
, const procps_status_t
*ps
)
261 safe_strncpy(buf
, ps
->context
? ps
->context
: "unknown", size
+1);
266 static void func_nice(char *buf, int size, const procps_status_t *ps)
271 static void func_pcpu(char *buf, int size, const procps_status_t *ps)
276 static const ps_out_t out_spec
[] = {
277 // Mandated by POSIX:
278 { 8 , "user" ,"USER" ,func_user
,PSSCAN_UIDGID
},
279 { 16 , "comm" ,"COMMAND",func_comm
,PSSCAN_COMM
},
280 { 256 , "args" ,"COMMAND",func_args
,PSSCAN_COMM
},
281 { 5 , "pid" ,"PID" ,func_pid
,PSSCAN_PID
},
282 { 5 , "ppid" ,"PPID" ,func_ppid
,PSSCAN_PPID
},
283 { 5 , "pgid" ,"PGID" ,func_pgid
,PSSCAN_PGID
},
284 #if ENABLE_FEATURE_PS_TIME
285 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime
,PSSCAN_START_TIME
},
287 // { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
288 // { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ },
289 // { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
290 // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID },
291 // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID },
292 #if ENABLE_FEATURE_PS_TIME
293 { 6 , "time" ,"TIME" ,func_time
,PSSCAN_STIME
| PSSCAN_UTIME
},
295 { 6 , "tty" ,"TT" ,func_tty
,PSSCAN_TTY
},
296 { 4 , "vsz" ,"VSZ" ,func_vsz
,PSSCAN_VSZ
},
297 // Not mandated by POSIX, but useful:
298 { 4 , "rss" ,"RSS" ,func_rss
,PSSCAN_RSS
},
300 { 35 , "label" ,"LABEL" ,func_label
,PSSCAN_CONTEXT
},
304 static ps_out_t
* new_out_t(void)
306 out
= xrealloc_vector(out
, 2, out_cnt
);
307 return &out
[out_cnt
++];
310 static const ps_out_t
* find_out_spec(const char *name
)
313 for (i
= 0; i
< ARRAY_SIZE(out_spec
); i
++) {
314 if (!strcmp(name
, out_spec
[i
].name
))
317 bb_error_msg_and_die("bad -o argument '%s'", name
);
320 static void parse_o(char* opt
)
323 // POSIX: "-o is blank- or comma-separated list" (FIXME)
326 comma
= strchr(opt
, ',');
327 equal
= strchr(opt
, '=');
328 if (comma
&& (!equal
|| equal
> comma
)) {
330 *new_out_t() = *find_out_spec(opt
);
337 // opt points to last spec in comma separated list.
338 // This one can have =HEADER part.
342 *new = *find_out_spec(opt
);
345 new->header
= equal
+ 1;
346 // POSIX: the field widths shall be ... at least as wide as
347 // the header text (default or overridden value).
348 // If the header text is null, such as -o user=,
349 // the field width shall be at least as wide as the
350 // default header text
351 if (new->header
[0]) {
352 new->width
= strlen(new->header
);
359 static void post_process(void)
363 for (i
= 0; i
< out_cnt
; i
++) {
364 need_flags
|= out
[i
].ps_flags
;
365 if (out
[i
].header
[0]) {
368 width
+= out
[i
].width
+ 1; /* "FIELD " */
371 if (!is_selinux_enabled())
372 need_flags
&= ~PSSCAN_CONTEXT
;
374 buffer
= xmalloc(width
+ 1); /* for trailing \0 */
377 static void format_header(void)
390 if (++i
== out_cnt
) /* do not pad last field */
392 p
+= sprintf(p
, "%-*s ", op
->width
, op
->header
);
394 strcpy(p
, op
->header
);
396 printf("%.*s\n", terminal_width
, buffer
);
399 static void format_process(const procps_status_t
*ps
)
404 if (out_cnt
) while (1) {
405 out
[i
].f(p
, out
[i
].width
, ps
);
406 // POSIX: Any field need not be meaningful in all
407 // implementations. In such a case a hyphen ( '-' )
408 // should be output in place of the field value.
415 len
= out
[i
].width
- len
+ 1;
416 if (++i
== out_cnt
) /* do not pad last field */
418 p
+= sprintf(p
, "%*s", len
, "");
420 printf("%.*s\n", terminal_width
, buffer
);
423 int ps_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
424 int ps_main(int argc UNUSED_PARAM
, char **argv
)
427 llist_t
* opt_o
= NULL
;
428 USE_SELINUX(int opt
;)
431 // -a Write information for all processes associated with terminals
432 // Implementations may omit session leaders from this list
433 // -A Write information for all processes
434 // -d Write information for all processes, except session leaders
435 // -e Write information for all processes (equivalent to -A.)
436 // -f Generate a full listing
437 // -l Generate a long listing
438 // -o col1,col2,col3=header
439 // Select which columns to display
440 /* We allow (and ignore) most of the above. FIXME */
441 opt_complementary
= "o::";
442 USE_SELINUX(opt
=) getopt32(argv
, "Zo:aAdefl", &opt_o
);
445 parse_o(llist_pop(&opt_o
));
448 /* Below: parse_o() needs char*, NOT const char*... */
450 if (!(opt
& 1) || !is_selinux_enabled()) {
451 /* no -Z or no SELinux: do not show LABEL */
452 strcpy(default_o
, DEFAULT_O_STR
+ sizeof(SELINUX_O_PREFIX
)-1);
456 strcpy(default_o
, DEFAULT_O_STR
);
462 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
463 * and such large widths */
464 terminal_width
= MAX_WIDTH
;
466 get_terminal_width_height(0, &terminal_width
, NULL
);
467 if (--terminal_width
> MAX_WIDTH
)
468 terminal_width
= MAX_WIDTH
;
473 while ((p
= procps_scan(p
, need_flags
))) {
481 #else /* !ENABLE_DESKTOP */
484 int ps_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
485 int ps_main(int argc UNUSED_PARAM
, char **argv UNUSED_PARAM
)
487 procps_status_t
*p
= NULL
;
489 SKIP_SELINUX(const) int use_selinux
= 0;
491 #if !ENABLE_FEATURE_PS_WIDE
492 enum { terminal_width
= 79 };
494 unsigned terminal_width
;
498 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
499 #if ENABLE_FEATURE_PS_WIDE
500 opt_complementary
= "-:ww";
501 USE_SELINUX(i
=) getopt32(argv
, USE_SELINUX("Z") "w", &w_count
);
502 /* if w is given once, GNU ps sets the width to 132,
503 * if w is given more than once, it is "unlimited"
506 terminal_width
= (w_count
==1) ? 132 : MAX_WIDTH
;
508 get_terminal_width_height(0, &terminal_width
, NULL
);
510 if (--terminal_width
> MAX_WIDTH
)
511 terminal_width
= MAX_WIDTH
;
513 #else /* only ENABLE_SELINUX */
514 i
= getopt32(argv
, "Z");
517 if ((i
& 1) && is_selinux_enabled())
518 use_selinux
= PSSCAN_CONTEXT
;
520 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
523 puts(" PID CONTEXT STAT COMMAND");
525 puts(" PID USER VSZ STAT COMMAND");
527 while ((p
= procps_scan(p
, 0
537 len
= printf("%5u %-32.32s %s ",
539 p
->context
? p
->context
: "unknown",
544 const char *user
= get_cached_username(p
->uid
);
546 // len = printf("%5u %-8.8s %s ",
547 // p->pid, user, p->state);
551 smart_ulltoa5(p
->vsz
, buf6
, " mgtpezy");
553 len
= printf("%5u %-8.8s %s %s ",
554 p
->pid
, user
, buf6
, p
->state
);
559 int sz
= terminal_width
- len
;
561 read_cmdline(buf
, sz
, p
->pid
, p
->comm
);
565 if (ENABLE_FEATURE_CLEAN_UP
)
566 clear_username_cache();
570 #endif /* ENABLE_DESKTOP */