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 put_u(char *buf, int size, unsigned u)
53 smart_ulltoa5( ((unsigned long long)u) << 10, buf5);
54 sprintf(buf, "%.*s", size, buf5);
57 static void func_vsz(char *buf, int size, const procps_status_t *ps)
59 put_u(buf, size, ps->vsz);
62 static void func_rss(char *buf, int size, const procps_status_t *ps)
64 put_u(buf, size, ps->rss);
67 static void func_tty(char *buf, int size, const procps_status_t *ps)
69 safe_strncpy(buf, ps->tty_str, size+1);
72 static void func_nice(char *buf, int size, const procps_status_t *ps)
77 static void func_etime(char *buf, int size, const procps_status_t *ps)
79 elapled time [[dd-]hh:]mm:ss
82 static void func_time(char *buf, int size, const procps_status_t *ps)
84 cumulative time [[dd-]hh:]mm:ss
87 static void func_pcpu(char *buf, int size, const procps_status_t *ps)
96 void (*f)(char *buf, int size, const procps_status_t *ps);
100 static const ps_out_t out_spec[] = {
101 // Mandated by POSIX:
102 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
103 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
104 { 256 , "args" ,"COMMAND",func_args ,PSSCAN_CMD|PSSCAN_COMM },
105 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
106 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
107 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
108 // { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_ },
109 // { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
110 // { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ },
111 // { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
112 // { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID },
113 // { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID },
114 // { sizeof("TIME" )-1, "time" ,"TIME" ,func_time ,PSSCAN_ },
115 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
116 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
117 // Not mandated by POSIX, but useful:
118 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
121 #define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) )
123 #define DEFAULT_O_STR "pid,user" /* TODO: ,vsz,stat */ ",args"
131 unsigned terminal_width;
132 char default_o[sizeof(DEFAULT_O_STR)];
134 #define G (*(struct globals*)&bb_common_bufsiz1)
136 #define out_cnt (G.out_cnt )
137 #define print_header (G.print_header )
138 #define need_flags (G.need_flags )
139 #define buffer (G.buffer )
140 #define terminal_width (G.terminal_width)
141 #define default_o (G.default_o )
143 static ps_out_t* new_out_t(void)
146 out = xrealloc(out, out_cnt * sizeof(*out));
150 static const ps_out_t* find_out_spec(const char *name)
153 for (i = 0; i < VEC_SIZE(out_spec); i++) {
154 if (!strcmp(name, out_spec[i].name))
157 bb_error_msg_and_die("bad -o argument '%s'", name);
160 static void parse_o(char* opt)
163 // POSIX: "-o is blank- or comma-separated list" (FIXME)
166 comma = strchr(opt, ',');
167 equal = strchr(opt, '=');
168 if (comma && (!equal || equal > comma)) {
170 *new_out_t() = *find_out_spec(opt);
177 // opt points to last spec in comma separated list.
178 // This one can have =HEADER part.
182 *new = *find_out_spec(opt);
185 new->header = equal + 1;
186 // POSIX: the field widths shall be ... at least as wide as
187 // the header text (default or overridden value).
188 // If the header text is null, such as -o user=,
189 // the field width shall be at least as wide as the
190 // default header text
191 if (new->header[0]) {
192 new->width = strlen(new->header);
199 static void post_process(void)
203 for (i = 0; i < out_cnt; i++) {
204 need_flags |= out[i].ps_flags;
205 if (out[i].header[0]) {
208 width += out[i].width + 1; /* "FIELD " */
210 buffer = xmalloc(width + 1); /* for trailing \0 */
213 static void format_header(void)
226 if (++i == out_cnt) /* do not pad last field */
228 p += sprintf(p, "%-*s ", op->width, op->header);
230 strcpy(p, op->header);
232 printf("%.*s\n", terminal_width, buffer);
235 static void format_process(const procps_status_t *ps)
240 if (out_cnt) while (1) {
241 out[i].f(p, out[i].width, ps);
242 // POSIX: Any field need not be meaningful in all
243 // implementations. In such a case a hyphen ( '-' )
244 // should be output in place of the field value.
251 len = out[i].width - len + 1;
252 if (++i == out_cnt) /* do not pad last field */
254 p += sprintf(p, "%*s", len, "");
256 printf("%.*s\n", terminal_width, buffer);
259 int ps_main(int argc, char **argv);
260 int ps_main(int argc, char **argv)
263 llist_t* opt_o = NULL;
265 /* Cannot be const: parse_o() will choke */
266 strcpy(default_o, DEFAULT_O_STR);
269 // -a Write information for all processes associated with terminals
270 // Implementations may omit session leaders from this list
271 // -A Write information for all processes
272 // -d Write information for all processes, except session leaders
273 // -e Write information for all processes (equivalent to -A.)
274 // -f Generate a full listing
275 // -l Generate a long listing
276 // -o col1,col2,col3=header
277 // Select which columns to display
278 /* We allow (and ignore) most of the above. FIXME */
279 opt_complementary = "o::";
280 getopt32(argc, argv, "o:aAdefl", &opt_o);
283 parse_o(opt_o->data);
290 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
291 * and such large widths */
292 terminal_width = 30000;
294 get_terminal_width_height(1, &terminal_width, NULL);
300 while ((p = procps_scan(p, need_flags))) {
308 #else /* !ENABLE_DESKTOP */
311 int ps_main(int argc, char **argv);
312 int ps_main(int argc, char **argv)
314 procps_status_t *p = NULL;
316 SKIP_SELINUX(const) int use_selinux = 0;
317 USE_SELINUX(security_context_t sid = NULL;)
318 #if !ENABLE_FEATURE_PS_WIDE
319 enum { terminal_width = 79 };
325 #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
326 #if ENABLE_FEATURE_PS_WIDE
327 opt_complementary = "-:ww";
328 USE_SELINUX(i =) getopt32(argc, argv, USE_SELINUX("Z") "w", &w_count);
329 /* if w is given once, GNU ps sets the width to 132,
330 * if w is given more than once, it is "unlimited"
333 terminal_width = (w_count==1) ? 132 : INT_MAX;
335 get_terminal_width_height(1, &terminal_width, NULL);
339 #else /* only ENABLE_SELINUX */
340 i = getopt32(argc, argv, "Z");
343 if ((i & 1) && is_selinux_enabled())
346 #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
349 puts(" PID Context Stat Command");
351 puts(" PID Uid VSZ Stat Command");
353 while ((p = procps_scan(p, 0
360 char *namecmd = p->cmd;
366 if (is_selinux_enabled()) {
367 if (getpidcon(p->pid, &sid) < 0)
372 /* I assume sid initialized with NULL */
373 len = strlen(sid) + 1;
374 safe_strncpy(sbuf, sid, len);
378 safe_strncpy(sbuf, "unknown", 7);
380 len = printf("%5u %-32s %s ", p->pid, sbuf, p->state);
384 const char *user = get_cached_username(p->uid);
386 len = printf("%5u %-8s %s ",
387 p->pid, user, p->state);
389 len = printf("%5u %-8s %6u %s ",
390 p->pid, user, p->vsz, p->state);
393 i = terminal_width-len;
395 if (namecmd && namecmd[0]) {
398 if (strlen(namecmd) > (size_t)i)
405 if (strlen(namecmd) > ((size_t)i-2))
407 printf("[%s]\n", namecmd);
410 if (ENABLE_FEATURE_CLEAN_UP)
411 clear_username_cache();
415 #endif /* ENABLE_DESKTOP */