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 /* Print value to buf, max size+1 chars (including trailing '\0') */
21 static void func_user(char *buf, int size, const procps_status_t *ps)
23 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
26 static void func_comm(char *buf, int size, const procps_status_t *ps)
28 safe_strncpy(buf, ps->comm, size+1);
31 static void func_args(char *buf, int size, const procps_status_t *ps)
33 read_cmdline(buf, size, ps->pid, ps->comm);
36 static void func_pid(char *buf, int size, const procps_status_t *ps)
38 sprintf(buf, "%*u", size, ps->pid);
41 static void func_ppid(char *buf, int size, const procps_status_t *ps)
43 sprintf(buf, "%*u", size, ps->ppid);
46 static void func_pgid(char *buf, int size, const procps_status_t *ps)
48 sprintf(buf, "%*u", size, ps->pgid);
51 static void put_lu(char *buf, int size, unsigned long u)
54 smart_ulltoa5( ((unsigned long long)u) << 10, buf5);
55 sprintf(buf, "%.*s", size, buf5);
58 static void func_vsz(char *buf, int size, const procps_status_t *ps)
60 put_lu(buf, size, ps->vsz);
63 static void func_rss(char *buf, int size, const procps_status_t *ps)
65 put_lu(buf, size, ps->rss);
68 static void func_tty(char *buf, int size, const procps_status_t *ps)
72 if (ps->tty_major) /* tty field of "0" means "no tty" */
73 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
77 static void func_label(char *buf, int size, const procps_status_t *ps)
79 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
84 static void func_nice(char *buf, int size, const procps_status_t *ps)
89 static void func_etime(char *buf, int size, const procps_status_t *ps)
91 elapled time [[dd-]hh:]mm:ss
94 static void func_time(char *buf, int size, const procps_status_t *ps)
96 cumulative time [[dd-]hh:]mm:ss
99 static void func_pcpu(char *buf, int size, const procps_status_t *ps)
108 void (*f)(char *buf, int size, const procps_status_t *ps);
112 static const ps_out_t out_spec[] = {
113 // Mandated by POSIX:
114 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
115 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
116 { 256 , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
117 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
118 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
119 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
120 // { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_ },
121 // { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
122 // { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ },
123 // { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
124 // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID },
125 // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID },
126 // { sizeof("TIME" )-1, "time" ,"TIME" ,func_time ,PSSCAN_ },
127 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
128 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
129 // Not mandated by POSIX, but useful:
130 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
132 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
137 #define SELINIX_O_PREFIX "label,"
138 #define DEFAULT_O_STR SELINIX_O_PREFIX "pid,user" /* TODO: ,vsz,stat */ ",args"
140 #define DEFAULT_O_STR "pid,user" /* TODO: ,vsz,stat */ ",args"
149 unsigned terminal_width;
150 char default_o[sizeof(DEFAULT_O_STR)];
152 #define G (*(struct globals*)&bb_common_bufsiz1)
154 #define out_cnt (G.out_cnt )
155 #define print_header (G.print_header )
156 #define need_flags (G.need_flags )
157 #define buffer (G.buffer )
158 #define terminal_width (G.terminal_width)
159 #define default_o (G.default_o )
161 static ps_out_t* new_out_t(void)
164 out = xrealloc(out, out_cnt * sizeof(*out));
168 static const ps_out_t* find_out_spec(const char *name)
171 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
172 if (!strcmp(name, out_spec[i].name))
175 bb_error_msg_and_die("bad -o argument '%s'", name);
178 static void parse_o(char* opt)
181 // POSIX: "-o is blank- or comma-separated list" (FIXME)
184 comma = strchr(opt, ',');
185 equal = strchr(opt, '=');
186 if (comma && (!equal || equal > comma)) {
188 *new_out_t() = *find_out_spec(opt);
195 // opt points to last spec in comma separated list.
196 // This one can have =HEADER part.
200 *new = *find_out_spec(opt);
203 new->header = equal + 1;
204 // POSIX: the field widths shall be ... at least as wide as
205 // the header text (default or overridden value).
206 // If the header text is null, such as -o user=,
207 // the field width shall be at least as wide as the
208 // default header text
209 if (new->header[0]) {
210 new->width = strlen(new->header);
217 static void post_process(void)
221 for (i = 0; i < out_cnt; i++) {
222 need_flags |= out[i].ps_flags;
223 if (out[i].header[0]) {
226 width += out[i].width + 1; /* "FIELD " */
229 if (!is_selinux_enabled())
230 need_flags &= ~PSSCAN_CONTEXT;
232 buffer = xmalloc(width + 1); /* for trailing \0 */
235 static void format_header(void)
248 if (++i == out_cnt) /* do not pad last field */
250 p += sprintf(p, "%-*s ", op->width, op->header);
252 strcpy(p, op->header);
254 printf("%.*s\n", terminal_width, buffer);
257 static void format_process(const procps_status_t *ps)
262 if (out_cnt) while (1) {
263 out[i].f(p, out[i].width, ps);
264 // POSIX: Any field need not be meaningful in all
265 // implementations. In such a case a hyphen ( '-' )
266 // should be output in place of the field value.
273 len = out[i].width - len + 1;
274 if (++i == out_cnt) /* do not pad last field */
276 p += sprintf(p, "%*s", len, "");
278 printf("%.*s\n", terminal_width, buffer);
281 int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
282 int ps_main(int argc, char **argv)
285 llist_t* opt_o = NULL;
286 USE_SELINUX(int opt;)
289 // -a Write information for all processes associated with terminals
290 // Implementations may omit session leaders from this list
291 // -A Write information for all processes
292 // -d Write information for all processes, except session leaders
293 // -e Write information for all processes (equivalent to -A.)
294 // -f Generate a full listing
295 // -l Generate a long listing
296 // -o col1,col2,col3=header
297 // Select which columns to display
298 /* We allow (and ignore) most of the above. FIXME */
299 opt_complementary = "o::";
300 USE_SELINUX(opt =) getopt32(argv, "Zo:aAdefl", &opt_o);
303 parse_o(opt_o->data);
307 /* Below: parse_o() needs char*, NOT const char*... */
309 if (!(opt & 1) || !is_selinux_enabled()) {
310 /* no -Z or no SELinux: do not show LABEL */
311 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINIX_O_PREFIX)-1);
315 strcpy(default_o, DEFAULT_O_STR);
321 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
322 * and such large widths */
323 terminal_width = MAX_WIDTH;
325 get_terminal_width_height(0, &terminal_width, NULL);
326 if (--terminal_width > MAX_WIDTH)
327 terminal_width = MAX_WIDTH;
332 while ((p = procps_scan(p, need_flags))) {
340 #else /* !ENABLE_DESKTOP */
343 int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
344 int ps_main(int argc, char **argv)
346 procps_status_t *p = NULL;
348 SKIP_SELINUX(const) int use_selinux = 0;
350 #if !ENABLE_FEATURE_PS_WIDE
351 enum { terminal_width = 79 };
357 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
358 #if ENABLE_FEATURE_PS_WIDE
359 opt_complementary = "-:ww";
360 USE_SELINUX(i =) getopt32(argv, USE_SELINUX("Z") "w", &w_count);
361 /* if w is given once, GNU ps sets the width to 132,
362 * if w is given more than once, it is "unlimited"
365 terminal_width = (w_count==1) ? 132 : MAX_WIDTH;
367 get_terminal_width_height(0, &terminal_width, NULL);
369 if (--terminal_width > MAX_WIDTH)
370 terminal_width = MAX_WIDTH;
372 #else /* only ENABLE_SELINUX */
373 i = getopt32(argv, "Z");
376 if ((i & 1) && is_selinux_enabled())
377 use_selinux = PSSCAN_CONTEXT;
379 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
382 puts(" PID Context Stat Command");
384 puts(" PID Uid VSZ Stat Command");
386 while ((p = procps_scan(p, 0
396 len = printf("%5u %-32s %s ",
398 p->context ? p->context : "unknown",
403 const char *user = get_cached_username(p->uid);
405 len = printf("%5u %-8s %s ",
406 p->pid, user, p->state);
408 len = printf("%5u %-8s %6lu %s ",
409 p->pid, user, p->vsz, p->state);
413 int sz = terminal_width - len;
415 read_cmdline(buf, sz, p->pid, p->comm);
419 if (ENABLE_FEATURE_CLEAN_UP)
420 clear_username_cache();
424 #endif /* ENABLE_DESKTOP */