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.
16 /* Print value to buf, max size+1 chars (including trailing '\0') */
18 static void func_user(char *buf, int size, const procps_status_t *ps)
20 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
23 static void func_comm(char *buf, int size, const procps_status_t *ps)
25 safe_strncpy(buf, ps->comm, size+1);
28 static void func_args(char *buf, int size, const procps_status_t *ps)
32 safe_strncpy(buf, ps->cmd, size+1);
34 sprintf(buf, "[%.*s]", size-2, ps->comm);
37 static void func_pid(char *buf, int size, const procps_status_t *ps)
39 sprintf(buf, "%*u", size, ps->pid);
42 static void func_ppid(char *buf, int size, const procps_status_t *ps)
44 sprintf(buf, "%*u", size, ps->ppid);
47 static void func_pgid(char *buf, int size, const procps_status_t *ps)
49 sprintf(buf, "%*u", size, ps->pgid);
52 static void put_u(char *buf, int size, unsigned u)
55 smart_ulltoa5( ((unsigned long long)u) << 10, buf5);
56 sprintf(buf, "%.*s", size, buf5);
59 static void func_vsz(char *buf, int size, const procps_status_t *ps)
61 put_u(buf, size, ps->vsz);
64 static void func_rss(char *buf, int size, const procps_status_t *ps)
66 put_u(buf, size, ps->rss);
69 static void func_tty(char *buf, int size, const procps_status_t *ps)
71 safe_strncpy(buf, ps->tty_str, size+1);
75 static void func_label(char *buf, int size, const procps_status_t *ps)
77 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
82 static void func_nice(char *buf, int size, const procps_status_t *ps)
87 static void func_etime(char *buf, int size, const procps_status_t *ps)
89 elapled time [[dd-]hh:]mm:ss
92 static void func_time(char *buf, int size, const procps_status_t *ps)
94 cumulative time [[dd-]hh:]mm:ss
97 static void func_pcpu(char *buf, int size, const procps_status_t *ps)
106 void (*f)(char *buf, int size, const procps_status_t *ps);
110 static const ps_out_t out_spec[] = {
111 // Mandated by POSIX:
112 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
113 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
114 { 256 , "args" ,"COMMAND",func_args ,PSSCAN_CMD|PSSCAN_COMM },
115 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
116 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
117 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
118 // { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_ },
119 // { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
120 // { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ },
121 // { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
122 // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID },
123 // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID },
124 // { sizeof("TIME" )-1, "time" ,"TIME" ,func_time ,PSSCAN_ },
125 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
126 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
127 // Not mandated by POSIX, but useful:
128 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
130 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
134 #define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) )
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 < VEC_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);
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(argc, 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 = 30000;
325 get_terminal_width_height(1, &terminal_width, NULL);
331 while ((p = procps_scan(p, need_flags))) {
339 #else /* !ENABLE_DESKTOP */
342 int ps_main(int argc, char **argv);
343 int ps_main(int argc, char **argv)
345 procps_status_t *p = NULL;
347 SKIP_SELINUX(const) int use_selinux = 0;
348 #if !ENABLE_FEATURE_PS_WIDE
349 enum { terminal_width = 79 };
355 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
356 #if ENABLE_FEATURE_PS_WIDE
357 opt_complementary = "-:ww";
358 USE_SELINUX(i =) getopt32(argc, argv, USE_SELINUX("Z") "w", &w_count);
359 /* if w is given once, GNU ps sets the width to 132,
360 * if w is given more than once, it is "unlimited"
363 terminal_width = (w_count==1) ? 132 : INT_MAX;
365 get_terminal_width_height(1, &terminal_width, NULL);
369 #else /* only ENABLE_SELINUX */
370 i = getopt32(argc, argv, "Z");
373 if ((i & 1) && is_selinux_enabled())
374 use_selinux = PSSCAN_CONTEXT;
376 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
379 puts(" PID Context Stat Command");
381 puts(" PID Uid VSZ Stat Command");
383 while ((p = procps_scan(p, 0
391 char *namecmd = p->cmd;
394 len = printf("%5u %-32s %s ",
396 p->context ? p->context : "unknown",
401 const char *user = get_cached_username(p->uid);
403 len = printf("%5u %-8s %s ",
404 p->pid, user, p->state);
406 len = printf("%5u %-8s %6u %s ",
407 p->pid, user, p->vsz, p->state);
410 i = terminal_width-len;
412 if (namecmd && namecmd[0]) {
415 if (strlen(namecmd) > (size_t)i)
422 if (strlen(namecmd) > ((size_t)i-2))
424 printf("[%s]\n", namecmd);
427 if (ENABLE_FEATURE_CLEAN_UP)
428 clear_username_cache();
432 #endif /* ENABLE_DESKTOP */