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 static void func_user(char *buf, int size, const procps_status_t *ps)
18 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
21 static void func_comm(char *buf, int size, const procps_status_t *ps)
23 safe_strncpy(buf, ps->comm, size+1);
26 static void func_args(char *buf, int size, const procps_status_t *ps)
30 safe_strncpy(buf, ps->cmd, size+1);
32 sprintf(buf, "[%.*s]", size-2, ps->comm);
35 static void func_pid(char *buf, int size, const procps_status_t *ps)
37 sprintf(buf, "%*u", size, ps->pid);
40 static void func_ppid(char *buf, int size, const procps_status_t *ps)
42 sprintf(buf, "%*u", size, ps->ppid);
45 static void func_pgid(char *buf, int size, const procps_status_t *ps)
47 sprintf(buf, "%*u", size, ps->pgid);
50 static void func_vsz(char *buf, int size, const procps_status_t *ps)
53 smart_ulltoa5( ((unsigned long long)ps->vsz) << 10, buf5);
54 sprintf(buf, "%.*s", size, buf5);
57 static void func_rss(char *buf, int size, const procps_status_t *ps)
60 smart_ulltoa5( ((unsigned long long)ps->rss) << 10, buf5);
61 sprintf(buf, "%.*s", size, buf5);
64 static void func_tty(char *buf, int size, const procps_status_t *ps)
66 safe_strncpy(buf, ps->tty_str, size+1);
69 static void func_nice(char *buf, int size, const procps_status_t *ps)
74 static void func_etime(char *buf, int size, const procps_status_t *ps)
76 elapled time [[dd-]hh:]mm:ss
79 static void func_time(char *buf, int size, const procps_status_t *ps)
81 cumulative time [[dd-]hh:]mm:ss
84 static void func_pcpu(char *buf, int size, const procps_status_t *ps)
93 void (*f)(char *buf, int size, const procps_status_t *ps);
97 static const ps_out_t out_spec[] = {
99 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
100 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
101 { 256 , "args" ,"COMMAND",func_args ,PSSCAN_CMD|PSSCAN_COMM },
102 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
103 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
104 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
105 // { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_ },
106 // { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
107 // { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ },
108 // { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
109 // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID },
110 // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID },
111 // { sizeof("TIME" )-1, "time" ,"TIME" ,func_time ,PSSCAN_ },
112 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
113 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
114 // Not mandated by POSIX, but useful:
115 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
118 #define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) )
120 #define DEFAULT_O_STR "pid,user" /* TODO: ,vsz,stat */ ",args"
128 unsigned terminal_width;
129 char default_o[sizeof(DEFAULT_O_STR)];
131 #define G (*(struct globals*)&bb_common_bufsiz1)
133 #define out_cnt (G.out_cnt )
134 #define print_header (G.print_header )
135 #define need_flags (G.need_flags )
136 #define buffer (G.buffer )
137 #define terminal_width (G.terminal_width)
138 #define default_o (G.default_o )
140 static ps_out_t* new_out_t(void)
143 out = xrealloc(out, out_cnt * sizeof(*out));
147 static const ps_out_t* find_out_spec(const char *name)
150 for (i = 0; i < VEC_SIZE(out_spec); i++) {
151 if (!strcmp(name, out_spec[i].name))
154 bb_error_msg_and_die("bad -o argument '%s'", name);
157 static void parse_o(char* opt)
160 // POSIX: "-o is blank- or comma-separated list" (FIXME)
163 comma = strchr(opt, ',');
164 equal = strchr(opt, '=');
165 if (comma && (!equal || equal > comma)) {
167 *new_out_t() = *find_out_spec(opt);
174 // opt points to last spec in comma separated list.
175 // This one can have =HEADER part.
179 *new = *find_out_spec(opt);
182 new->header = equal + 1;
183 // POSIX: the field widths shall be ... at least as wide as
184 // the header text (default or overridden value).
185 // If the header text is null, such as -o user=,
186 // the field width shall be at least as wide as the
187 // default header text
188 if (new->header[0]) {
189 new->width = strlen(new->header);
196 static void post_process(void)
200 for (i = 0; i < out_cnt; i++) {
201 need_flags |= out[i].ps_flags;
202 if (out[i].header[0]) {
205 width += out[i].width + 1; /* "FIELD " */
207 buffer = xmalloc(width + 1); /* for trailing \0 */
210 static void format_header(void)
223 if (++i == out_cnt) /* do not pad last field */
225 p += sprintf(p, "%-*s ", op->width, op->header);
227 strcpy(p, op->header);
229 printf("%.*s\n", terminal_width, buffer);
232 static void format_process(const procps_status_t *ps)
237 if (out_cnt) while (1) {
238 out[i].f(p, out[i].width, ps);
239 // POSIX: Any field need not be meaningful in all
240 // implementations. In such a case a hyphen ( '-' )
241 // should be output in place of the field value.
248 len = out[i].width - len + 1;
249 if (++i == out_cnt) /* do not pad last field */
251 p += sprintf(p, "%*s", len, "");
253 printf("%.*s\n", terminal_width, buffer);
256 int ps_main(int argc, char **argv);
257 int ps_main(int argc, char **argv)
260 llist_t* opt_o = NULL;
262 /* Cannot be const: parse_o() will choke */
263 strcpy(default_o, DEFAULT_O_STR);
266 // -a Write information for all processes associated with terminals
267 // Implementations may omit session leaders from this list
268 // -A Write information for all processes
269 // -d Write information for all processes, except session leaders
270 // -e Write information for all processes (equivalent to -A.)
271 // -f Generate a full listing
272 // -l Generate a long listing
273 // -o col1,col2,col3=header
274 // Select which columns to display
275 /* We allow (and ignore) most of the above. FIXME */
276 opt_complementary = "o::";
277 getopt32(argc, argv, "o:aAdefl", &opt_o);
280 parse_o(opt_o->data);
287 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
288 * and such large widths */
289 terminal_width = 30000;
291 get_terminal_width_height(1, &terminal_width, NULL);
297 while ((p = procps_scan(p, need_flags))) {
305 #else /* !ENABLE_DESKTOP */
308 int ps_main(int argc, char **argv);
309 int ps_main(int argc, char **argv)
311 procps_status_t *p = NULL;
313 SKIP_SELINUX(const) int use_selinux = 0;
314 USE_SELINUX(security_context_t sid = NULL;)
315 #if !ENABLE_FEATURE_PS_WIDE
316 enum { terminal_width = 79 };
322 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
323 #if ENABLE_FEATURE_PS_WIDE
324 opt_complementary = "-:ww";
325 USE_SELINUX(i =) getopt32(argc, argv, USE_SELINUX("Z") "w", &w_count);
326 /* if w is given once, GNU ps sets the width to 132,
327 * if w is given more than once, it is "unlimited"
330 terminal_width = (w_count==1) ? 132 : INT_MAX;
332 get_terminal_width_height(1, &terminal_width, NULL);
336 #else /* only ENABLE_SELINUX */
337 i = getopt32(argc, argv, "Z");
340 if ((i & 1) && is_selinux_enabled())
343 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
346 puts(" PID Context Stat Command");
348 puts(" PID Uid VSZ Stat Command");
350 while ((p = procps_scan(p, 0
357 char *namecmd = p->cmd;
363 if (is_selinux_enabled()) {
364 if (getpidcon(p->pid, &sid) < 0)
369 /* I assume sid initialized with NULL */
370 len = strlen(sid) + 1;
371 safe_strncpy(sbuf, sid, len);
375 safe_strncpy(sbuf, "unknown", 7);
377 len = printf("%5u %-32s %s ", p->pid, sbuf, p->state);
381 const char *user = get_cached_username(p->uid);
383 len = printf("%5u %-8s %s ",
384 p->pid, user, p->state);
386 len = printf("%5u %-8s %6ld %s ",
387 p->pid, user, p->vsz, p->state);
390 i = terminal_width-len;
392 if (namecmd && namecmd[0]) {
395 if (strlen(namecmd) > (size_t)i)
402 if (strlen(namecmd) > ((size_t)i-2))
404 printf("[%s]\n", namecmd);
407 if (ENABLE_FEATURE_CLEAN_UP)
408 clear_username_cache();
412 #endif /* ENABLE_DESKTOP */