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>
7 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
14 /* Print value to buf, max size+1 chars (including trailing '\0') */
16 void func_user(char *buf, int size, const procps_status_t *ps)
18 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
21 void func_comm(char *buf, int size, const procps_status_t *ps)
23 safe_strncpy(buf, ps->comm, size+1);
26 void func_args(char *buf, int size, const procps_status_t *ps)
30 safe_strncpy(buf, ps->cmd, size+1);
32 snprintf(buf, size+1, "[%.*s]", size-2, ps->comm);
35 void func_pid(char *buf, int size, const procps_status_t *ps)
37 snprintf(buf, size+1, "%*u", size, ps->pid);
40 void func_ppid(char *buf, int size, const procps_status_t *ps)
42 snprintf(buf, size+1, "%*u", size, ps->ppid);
45 void func_pgid(char *buf, int size, const procps_status_t *ps)
47 snprintf(buf, size+1, "%*u", size, ps->pgid);
50 void func_rss(char *buf, int size, const procps_status_t *ps)
53 smart_ulltoa5( ((unsigned long long)ps->rss) << 10, buf5);
54 snprintf(buf, size+1, "%.*s", size, buf5);
58 void func_nice(char *buf, int size, const procps_status_t *ps)
63 void func_etime(char *buf, int size, const procps_status_t *ps)
65 elapled time [[dd-]hh:]mm:ss
68 void func_time(char *buf, int size, const procps_status_t *ps)
70 cumulative time [[dd-]hh:]mm:ss
73 void func_pcpu(char *buf, int size, const procps_status_t *ps)
77 void func_tty(char *buf, int size, const procps_status_t *ps)
85 void (*f)(char *buf, int size, const procps_status_t *ps);
90 static const ps_out_t out_spec[] = {
92 { "user" ,"USER" ,func_user ,PSSCAN_UIDGID,8 },
93 { "comm" ,"COMMAND",func_comm ,PSSCAN_COMM ,16 },
94 { "args" ,"COMMAND",func_args ,PSSCAN_CMD|PSSCAN_COMM,256 },
95 { "pid" ,"PID" ,func_pid ,PSSCAN_PID ,5 },
96 { "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID ,5 },
97 { "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID ,5 },
98 // { "etime" ,"ELAPSED",func_etime ,PSSCAN_ ,sizeof("ELAPSED")-1 },
99 // { "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID,sizeof("GROUP" )-1 },
100 // { "nice" ,"NI" ,func_nice ,PSSCAN_ ,sizeof("NI" )-1 },
101 // { "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ ,sizeof("%CPU" )-1 },
102 // { "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID,sizeof("RGROUP" )-1 },
103 // { "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID,sizeof("RUSER" )-1 },
104 // { "time" ,"TIME" ,func_time ,PSSCAN_ ,sizeof("TIME" )-1 },
105 // { "tty" ,"TT" ,func_tty ,PSSCAN_ ,sizeof("TT" )-1 },
106 // { "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ ,4 },
107 // Not mandated by POSIX:
108 { "rss" ,"RSS" ,func_rss ,PSSCAN_RSS ,4 },
111 #define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) )
113 static ps_out_t* out;
115 static int print_header;
118 static unsigned terminal_width;
121 static ps_out_t* new_out_t(void)
124 out = xrealloc(out, out_cnt * sizeof(*out));
128 static const ps_out_t* find_out_spec(const char *name)
131 for (i = 0; i < VEC_SIZE(out_spec); i++) {
132 if (!strcmp(name, out_spec[i].name))
135 bb_error_msg_and_die("bad -o argument '%s'", name);
138 static void parse_o(char* opt)
141 // POSIX: "-o is blank- or comma-separated list" (FIXME)
144 comma = strchr(opt, ',');
145 equal = strchr(opt, '=');
146 if (comma && (!equal || equal > comma)) {
148 *new_out_t() = *find_out_spec(opt);
158 *new = *find_out_spec(opt);
161 new->header = equal + 1;
162 // POSIX: the field widths shall be ... at least as wide as
163 // the header text (default or overridden value).
164 // If the header text is null, such as -o user=,
165 // the field width shall be at least as wide as the
166 // default header text
167 if (new->header[0]) {
168 new->width = strlen(new->header);
175 static void post_process(void)
179 for (i = 0; i < out_cnt; i++) {
180 ps_flags |= out[i].ps_flags;
181 if (out[i].header[0]) {
184 width += out[i].width + 1; /* "FIELD " */
186 buffer = xmalloc(width + 1); /* for trailing \0 */
189 static void format_header(void)
200 if (++i == out_cnt) /* do not pad last field */
202 p += sprintf(p, "%-*s ", op->width, op->header);
204 strcpy(p, op->header);
206 printf("%.*s\n", terminal_width, buffer);
209 static void format_process(const procps_status_t *ps)
214 if (out_cnt) while (1) {
215 out[i].f(p, out[i].width, ps);
216 // POSIX: Any field need not be meaningful in all
217 // implementations. In such a case a hyphen ( '-' )
218 // should be output in place of the field value.
225 len = out[i].width - len + 1;
226 if (++i == out_cnt) /* do not pad last field */
232 printf("%.*s\n", terminal_width, buffer);
235 /* Cannot be const: parse_o() will choke */
236 static char default_o[] = "pid,user" /* TODO: ,vsz,stat */ ",args";
238 int ps_main(int argc, char **argv)
241 llist_t* opt_o = NULL;
244 // -a Write information for all processes associated with terminals
245 // Implementations may omit session leaders from this list
246 // -A Write information for all processes
247 // -d Write information for all processes, except session leaders
248 // -e Write information for all processes (equivalent to -A.)
249 // -f Generate a full listing
250 // -l Generate a long listing
251 // -o col1,col2,col3=header
252 // Select which columns to distplay
253 /* We allow (and ignore) most of the above. FIXME */
254 opt_complementary = "o::";
255 getopt32(argc, argv, "o:aAdefl", &opt_o);
257 opt_o = rev_llist(opt_o);
259 parse_o(opt_o->data);
266 terminal_width = INT_MAX;
268 get_terminal_width_height(1, &terminal_width, NULL);
274 while ((p = procps_scan(p, ps_flags))) {
282 #else /* !ENABLE_DESKTOP */
285 int ps_main(int argc, char **argv)
287 procps_status_t *p = NULL;
289 SKIP_SELINUX(const) int use_selinux = 0;
290 USE_SELINUX(security_context_t sid = NULL;)
291 #if !ENABLE_FEATURE_PS_WIDE
292 enum { terminal_width = 79 };
298 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
299 #if ENABLE_FEATURE_PS_WIDE
300 opt_complementary = "-:ww";
301 USE_SELINUX(i =) getopt32(argc, argv, USE_SELINUX("c") "w", &w_count);
302 /* if w is given once, GNU ps sets the width to 132,
303 * if w is given more than once, it is "unlimited"
306 terminal_width = (w_count==1) ? 132 : INT_MAX;
308 get_terminal_width_height(1, &terminal_width, NULL);
312 #else /* only ENABLE_SELINUX */
313 i = getopt32(argc, argv, "c");
316 if ((i & 1) && is_selinux_enabled())
319 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
322 puts(" PID Context Stat Command");
324 puts(" PID Uid VmSize Stat Command");
326 while ((p = procps_scan(p, 0
333 char *namecmd = p->cmd;
339 if (is_selinux_enabled()) {
340 if (getpidcon(p->pid, &sid) < 0)
345 /* I assume sid initialized with NULL */
346 len = strlen(sid) + 1;
347 safe_strncpy(sbuf, sid, len);
351 safe_strncpy(sbuf, "unknown", 7);
353 len = printf("%5u %-32s %s ", p->pid, sbuf, p->state);
357 const char *user = get_cached_username(p->uid);
359 len = printf("%5u %-8s %s ",
360 p->pid, user, p->state);
362 len = printf("%5u %-8s %6ld %s ",
363 p->pid, user, p->rss, p->state);
366 i = terminal_width-len;
368 if (namecmd && namecmd[0]) {
371 if (strlen(namecmd) > (size_t)i)
378 if (strlen(namecmd) > ((size_t)i-2))
380 printf("[%s]\n", namecmd);
383 if (ENABLE_FEATURE_CLEAN_UP)
384 clear_username_cache();
388 #endif /* ENABLE_DESKTOP */