blob: 7f273d0a6b7e3d53e0f4bb489332f20353d4d716 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenef8b6c71999-10-20 08:05:35 +00002/*
Erik Andersen246cc6d2000-03-07 07:41:42 +00003 * Mini ps implementation(s) for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Denis Vlasenko0696b8a2007-06-06 07:40:16 +00006 * Fix for SELinux Support:(c)2007 Hiroshi Shinji <shiroshi@my.email.ne.jp>
Denis Vlasenkoc85bfca2008-02-03 22:04:19 +00007 * (c)2007 Yuichi Nakamura <ynakam@hitachisoft.jp>
Eric Andersenef8b6c71999-10-20 08:05:35 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersenbdfd0d72001-10-24 05:00:29 +000010 */
11
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage:#if ENABLE_DESKTOP
13//usage:
14//usage:#define ps_trivial_usage
15//usage: "[-o COL1,COL2=HEADER]" IF_FEATURE_SHOW_THREADS(" [-T]")
16//usage:#define ps_full_usage "\n\n"
17//usage: "Show list of processes\n"
18//usage: "\nOptions:"
19//usage: "\n -o COL1,COL2=HEADER Select columns for display"
20//usage: IF_FEATURE_SHOW_THREADS(
21//usage: "\n -T Show threads"
22//usage: )
23//usage:
24//usage:#else /* !ENABLE_DESKTOP */
25//usage:
26//usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE
27//usage:#define USAGE_PS "\nThis version of ps accepts no options"
28//usage:#else
29//usage:#define USAGE_PS "\nOptions:"
30//usage:#endif
31//usage:
32//usage:#define ps_trivial_usage
33//usage: ""
34//usage:#define ps_full_usage "\n\n"
35//usage: "Show list of processes\n"
36//usage: USAGE_PS
37//usage: IF_SELINUX(
38//usage: "\n -Z Show selinux context"
39//usage: )
40//usage: IF_FEATURE_PS_WIDE(
41//usage: "\n w Wide output"
42//usage: )
43//usage:
44//usage:#endif /* ENABLE_DESKTOP */
45//usage:
46//usage:#define ps_example_usage
47//usage: "$ ps\n"
48//usage: " PID Uid Gid State Command\n"
49//usage: " 1 root root S init\n"
50//usage: " 2 root root S [kflushd]\n"
51//usage: " 3 root root S [kupdate]\n"
52//usage: " 4 root root S [kpiod]\n"
53//usage: " 5 root root S [kswapd]\n"
54//usage: " 742 andersen andersen S [bash]\n"
55//usage: " 743 andersen andersen S -bash\n"
56//usage: " 745 root root S [getty]\n"
57//usage: " 2990 andersen andersen R ps\n"
58
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000059#include "libbb.h"
Erik Andersen2ac2fae2000-03-07 23:32:17 +000060
Denis Vlasenko7ce75942007-07-25 17:27:58 +000061/* Absolute maximum on output line length */
62enum { MAX_WIDTH = 2*1024 };
63
Denis Vlasenko94949192006-11-05 00:45:47 +000064#if ENABLE_DESKTOP
65
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000066#include <sys/times.h> /* for times() */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000067#ifndef AT_CLKTCK
Denys Vlasenko099e5282011-02-02 00:59:35 +010068# define AT_CLKTCK 17
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000069#endif
70
Denys Vlasenko099e5282011-02-02 00:59:35 +010071/* TODO:
72 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
73 * specifies (for XSI-conformant systems) following default columns
74 * (l and f mark columns shown with -l and -f respectively):
75 * F l Flags (octal and additive) associated with the process (??)
76 * S l The state of the process
77 * UID f,l The user ID; the login name is printed with -f
78 * PID The process ID
79 * PPID f,l The parent process
80 * C f,l Processor utilization
81 * PRI l The priority of the process; higher numbers mean lower priority
82 * NI l Nice value
83 * ADDR l The address of the process
84 * SZ l The size in blocks of the core image of the process
85 * WCHAN l The event for which the process is waiting or sleeping
86 * STIME f Starting time of the process
87 * TTY The controlling terminal for the process
88 * TIME The cumulative execution time for the process
89 * CMD The command name; the full command line is shown with -f
90 */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000091#if ENABLE_SELINUX
Denys Vlasenko099e5282011-02-02 00:59:35 +010092# define SELINUX_O_PREFIX "label,"
93# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000094#else
Denys Vlasenko099e5282011-02-02 00:59:35 +010095# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000096#endif
97
98typedef struct {
99 uint16_t width;
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200100 char name6[6];
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000101 const char *header;
102 void (*f)(char *buf, int size, const procps_status_t *ps);
103 int ps_flags;
104} ps_out_t;
105
106struct globals {
107 ps_out_t* out;
108 int out_cnt;
109 int print_header;
110 int need_flags;
111 char *buffer;
112 unsigned terminal_width;
113#if ENABLE_FEATURE_PS_TIME
114 unsigned kernel_HZ;
115 unsigned long long seconds_since_boot;
116#endif
117 char default_o[sizeof(DEFAULT_O_STR)];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100118} FIX_ALIASING;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000119#define G (*(struct globals*)&bb_common_bufsiz1)
120#define out (G.out )
121#define out_cnt (G.out_cnt )
122#define print_header (G.print_header )
123#define need_flags (G.need_flags )
124#define buffer (G.buffer )
125#define terminal_width (G.terminal_width )
126#define kernel_HZ (G.kernel_HZ )
127#define seconds_since_boot (G.seconds_since_boot)
128#define default_o (G.default_o )
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200129#define INIT_G() do { } while (0)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000130
131#if ENABLE_FEATURE_PS_TIME
132/* for ELF executables, notes are pushed before environment and args */
133static ptrdiff_t find_elf_note(ptrdiff_t findme)
134{
Denis Vlasenko6b404432008-01-07 16:13:14 +0000135 ptrdiff_t *ep = (ptrdiff_t *) environ;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000136
Denys Vlasenko099e5282011-02-02 00:59:35 +0100137 while (*ep++)
138 continue;
Denis Vlasenko6b404432008-01-07 16:13:14 +0000139 while (*ep) {
140 if (ep[0] == findme) {
141 return ep[1];
142 }
143 ep += 2;
144 }
145 return -1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000146}
147
148#if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
149static unsigned get_HZ_by_waiting(void)
150{
Denis Vlasenko6b404432008-01-07 16:13:14 +0000151 struct timeval tv1, tv2;
152 unsigned t1, t2, r, hz;
153 unsigned cnt = cnt; /* for compiler */
154 int diff;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000155
Denis Vlasenko6b404432008-01-07 16:13:14 +0000156 r = 0;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000157
Denis Vlasenko6b404432008-01-07 16:13:14 +0000158 /* Wait for times() to reach new tick */
159 t1 = times(NULL);
160 do {
161 t2 = times(NULL);
162 } while (t2 == t1);
163 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000164
Denis Vlasenko6b404432008-01-07 16:13:14 +0000165 do {
166 t1 = t2;
167 tv1.tv_usec = tv2.tv_usec;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000168
Denis Vlasenko6b404432008-01-07 16:13:14 +0000169 /* Wait exactly one times() tick */
170 do {
171 t2 = times(NULL);
172 } while (t2 == t1);
173 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000174
Denis Vlasenko6b404432008-01-07 16:13:14 +0000175 /* Calculate ticks per sec, rounding up to even */
176 diff = tv2.tv_usec - tv1.tv_usec;
177 if (diff <= 0) diff += 1000000;
178 hz = 1000000u / (unsigned)diff;
179 hz = (hz+1) & ~1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000180
181 /* Count how many same hz values we saw */
Denis Vlasenko6b404432008-01-07 16:13:14 +0000182 if (r != hz) {
183 r = hz;
184 cnt = 0;
185 }
186 cnt++;
187 } while (cnt < 3); /* exit if saw 3 same values */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000188
Denis Vlasenko6b404432008-01-07 16:13:14 +0000189 return r;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000190}
191#else
192static inline unsigned get_HZ_by_waiting(void)
193{
194 /* Better method? */
195 return 100;
196}
197#endif
198
199static unsigned get_kernel_HZ(void)
200{
201 //char buf[64];
202 struct sysinfo info;
203
204 if (kernel_HZ)
205 return kernel_HZ;
206
207 /* Works for ELF only, Linux 2.4.0+ */
208 kernel_HZ = find_elf_note(AT_CLKTCK);
209 if (kernel_HZ == (unsigned)-1)
210 kernel_HZ = get_HZ_by_waiting();
211
Denys Vlasenkofe360482010-05-04 14:59:01 +0200212 //if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100213 // bb_perror_msg_and_die("can't read %s", "/proc/uptime");
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000214 //buf[sizeof(buf)-1] = '\0';
215 ///sscanf(buf, "%llu", &seconds_since_boot);
216 sysinfo(&info);
217 seconds_since_boot = info.uptime;
218
219 return kernel_HZ;
220}
221#endif
222
Denis Vlasenko94949192006-11-05 00:45:47 +0000223/* Print value to buf, max size+1 chars (including trailing '\0') */
224
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000225static void func_user(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000226{
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000227#if 1
Denis Vlasenko94949192006-11-05 00:45:47 +0000228 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000229#else
230 /* "compatible" version, but it's larger */
231 /* procps 2.18 shows numeric UID if name overflows the field */
232 /* TODO: get_cached_username() returns numeric string if
233 * user has no passwd record, we will display it
234 * left-justified here; too long usernames are shown
235 * as _right-justified_ IDs. Is it worth fixing? */
236 const char *user = get_cached_username(ps->uid);
237 if (strlen(user) <= size)
Denis Vlasenkoc85bfca2008-02-03 22:04:19 +0000238 safe_strncpy(buf, user, size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000239 else
240 sprintf(buf, "%*u", size, (unsigned)ps->uid);
241#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000242}
243
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200244static void func_group(char *buf, int size, const procps_status_t *ps)
245{
246 safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
247}
248
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000249static void func_comm(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000250{
251 safe_strncpy(buf, ps->comm, size+1);
252}
253
Denys Vlasenko099e5282011-02-02 00:59:35 +0100254static void func_stat(char *buf, int size, const procps_status_t *ps)
255{
256 safe_strncpy(buf, ps->state, size+1);
257}
258
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000259static void func_args(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000260{
Denys Vlasenko3a0f6f22009-09-12 00:15:34 +0200261 read_cmdline(buf, size+1, ps->pid, ps->comm);
Denis Vlasenko94949192006-11-05 00:45:47 +0000262}
263
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000264static void func_pid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000265{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000266 sprintf(buf, "%*u", size, ps->pid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000267}
268
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000269static void func_ppid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000270{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000271 sprintf(buf, "%*u", size, ps->ppid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000272}
273
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000274static void func_pgid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000275{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000276 sprintf(buf, "%*u", size, ps->pgid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000277}
278
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000279static void put_lu(char *buf, int size, unsigned long u)
Denis Vlasenko94949192006-11-05 00:45:47 +0000280{
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000281 char buf4[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000282
283 /* see http://en.wikipedia.org/wiki/Tera */
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000284 smart_ulltoa4(u, buf4, " mgtpezy");
285 buf4[4] = '\0';
286 sprintf(buf, "%.*s", size, buf4);
Denis Vlasenko94949192006-11-05 00:45:47 +0000287}
288
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000289static void func_vsz(char *buf, int size, const procps_status_t *ps)
290{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000291 put_lu(buf, size, ps->vsz);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000292}
293
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000294static void func_rss(char *buf, int size, const procps_status_t *ps)
295{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000296 put_lu(buf, size, ps->rss);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000297}
298
299static void func_tty(char *buf, int size, const procps_status_t *ps)
300{
Denis Vlasenko31789a82007-06-19 14:43:17 +0000301 buf[0] = '?';
302 buf[1] = '\0';
303 if (ps->tty_major) /* tty field of "0" means "no tty" */
304 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000305}
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000306
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200307#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
308
309static void func_rgroup(char *buf, int size, const procps_status_t *ps)
310{
311 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
312}
313
314static void func_ruser(char *buf, int size, const procps_status_t *ps)
315{
316 safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
317}
318
319static void func_nice(char *buf, int size, const procps_status_t *ps)
320{
321 sprintf(buf, "%*d", size, ps->niceness);
322}
323
Denys Vlasenko9e072192010-05-23 00:34:42 +0200324#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200325
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000326#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko9e072192010-05-23 00:34:42 +0200327
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000328static void func_etime(char *buf, int size, const procps_status_t *ps)
329{
330 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
331 unsigned long mm;
332 unsigned ss;
333
334 mm = ps->start_time / get_kernel_HZ();
335 /* must be after get_kernel_HZ()! */
336 mm = seconds_since_boot - mm;
337 ss = mm % 60;
338 mm /= 60;
339 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
340}
341
342static void func_time(char *buf, int size, const procps_status_t *ps)
343{
344 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
345 unsigned long mm;
346 unsigned ss;
347
348 mm = (ps->utime + ps->stime) / get_kernel_HZ();
349 ss = mm % 60;
350 mm /= 60;
351 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
352}
Denys Vlasenko9e072192010-05-23 00:34:42 +0200353
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000354#endif
355
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000356#if ENABLE_SELINUX
357static void func_label(char *buf, int size, const procps_status_t *ps)
358{
359 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
360}
361#endif
362
Denis Vlasenko94949192006-11-05 00:45:47 +0000363/*
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000364static void func_nice(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000365{
366 ps->???
367}
368
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000369static void func_pcpu(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000370{
371}
372*/
373
Denis Vlasenko94949192006-11-05 00:45:47 +0000374static const ps_out_t out_spec[] = {
Denys Vlasenko099e5282011-02-02 00:59:35 +0100375/* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000376 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200377 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000378 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200379 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000380 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
381 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
382 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000383#if ENABLE_FEATURE_PS_TIME
384 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
385#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200386#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
387 { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
388 { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
389 { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
390// { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
391#endif
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000392#if ENABLE_FEATURE_PS_TIME
393 { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
394#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000395 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
396 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
Denys Vlasenko099e5282011-02-02 00:59:35 +0100397/* Not mandated, but useful: */
398 { 4 , "stat" ,"STAT" ,func_stat ,PSSCAN_STAT },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000399 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000400#if ENABLE_SELINUX
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000401 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000402#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000403};
404
Denis Vlasenko94949192006-11-05 00:45:47 +0000405static ps_out_t* new_out_t(void)
406{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000407 out = xrealloc_vector(out, 2, out_cnt);
408 return &out[out_cnt++];
Denis Vlasenko94949192006-11-05 00:45:47 +0000409}
410
411static const ps_out_t* find_out_spec(const char *name)
412{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000413 unsigned i;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200414 char buf[ARRAY_SIZE(out_spec)*7 + 1];
415 char *p = buf;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200416
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000417 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
Denys Vlasenko9e072192010-05-23 00:34:42 +0200418 if (strncmp(name, out_spec[i].name6, 6) == 0)
Denis Vlasenko94949192006-11-05 00:45:47 +0000419 return &out_spec[i];
Denys Vlasenko9e072192010-05-23 00:34:42 +0200420 p += sprintf(p, "%.6s,", out_spec[i].name6);
Denis Vlasenko94949192006-11-05 00:45:47 +0000421 }
Denys Vlasenko9e072192010-05-23 00:34:42 +0200422 p[-1] = '\0';
423 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
Denis Vlasenko94949192006-11-05 00:45:47 +0000424}
425
426static void parse_o(char* opt)
427{
428 ps_out_t* new;
429 // POSIX: "-o is blank- or comma-separated list" (FIXME)
430 char *comma, *equal;
431 while (1) {
432 comma = strchr(opt, ',');
433 equal = strchr(opt, '=');
434 if (comma && (!equal || equal > comma)) {
435 *comma = '\0';
436 *new_out_t() = *find_out_spec(opt);
437 *comma = ',';
438 opt = comma + 1;
439 continue;
440 }
441 break;
442 }
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000443 // opt points to last spec in comma separated list.
444 // This one can have =HEADER part.
Denis Vlasenko94949192006-11-05 00:45:47 +0000445 new = new_out_t();
446 if (equal)
447 *equal = '\0';
448 *new = *find_out_spec(opt);
449 if (equal) {
450 *equal = '=';
451 new->header = equal + 1;
452 // POSIX: the field widths shall be ... at least as wide as
453 // the header text (default or overridden value).
454 // If the header text is null, such as -o user=,
455 // the field width shall be at least as wide as the
456 // default header text
457 if (new->header[0]) {
458 new->width = strlen(new->header);
459 print_header = 1;
460 }
461 } else
462 print_header = 1;
463}
464
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200465static void alloc_line_buffer(void)
Denis Vlasenko94949192006-11-05 00:45:47 +0000466{
467 int i;
468 int width = 0;
469 for (i = 0; i < out_cnt; i++) {
Denis Vlasenko85818632007-04-19 14:47:11 +0000470 need_flags |= out[i].ps_flags;
Denis Vlasenko94949192006-11-05 00:45:47 +0000471 if (out[i].header[0]) {
472 print_header = 1;
473 }
474 width += out[i].width + 1; /* "FIELD " */
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200475 if ((int)(width - terminal_width) > 0) {
476 /* The rest does not fit on the screen */
477 //out[i].width -= (width - terminal_width - 1);
478 out_cnt = i + 1;
479 break;
480 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000481 }
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000482#if ENABLE_SELINUX
483 if (!is_selinux_enabled())
484 need_flags &= ~PSSCAN_CONTEXT;
485#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000486 buffer = xmalloc(width + 1); /* for trailing \0 */
487}
488
489static void format_header(void)
490{
491 int i;
492 ps_out_t* op;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000493 char *p;
494
Denis Vlasenko94949192006-11-05 00:45:47 +0000495 if (!print_header)
496 return;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000497 p = buffer;
Denis Vlasenko94949192006-11-05 00:45:47 +0000498 i = 0;
499 if (out_cnt) {
500 while (1) {
501 op = &out[i];
502 if (++i == out_cnt) /* do not pad last field */
503 break;
504 p += sprintf(p, "%-*s ", op->width, op->header);
505 }
506 strcpy(p, op->header);
507 }
508 printf("%.*s\n", terminal_width, buffer);
509}
510
511static void format_process(const procps_status_t *ps)
512{
513 int i, len;
514 char *p = buffer;
515 i = 0;
516 if (out_cnt) while (1) {
517 out[i].f(p, out[i].width, ps);
518 // POSIX: Any field need not be meaningful in all
519 // implementations. In such a case a hyphen ( '-' )
520 // should be output in place of the field value.
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000521 if (!p[0]) {
522 p[0] = '-';
523 p[1] = '\0';
Denis Vlasenko94949192006-11-05 00:45:47 +0000524 }
525 len = strlen(p);
526 p += len;
527 len = out[i].width - len + 1;
528 if (++i == out_cnt) /* do not pad last field */
529 break;
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000530 p += sprintf(p, "%*s", len, "");
Denis Vlasenko94949192006-11-05 00:45:47 +0000531 }
532 printf("%.*s\n", terminal_width, buffer);
533}
534
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000535int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000536int ps_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko94949192006-11-05 00:45:47 +0000537{
538 procps_status_t *p;
539 llist_t* opt_o = NULL;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200540 int opt;
541 enum {
542 OPT_Z = (1 << 0),
543 OPT_o = (1 << 1),
544 OPT_a = (1 << 2),
545 OPT_A = (1 << 3),
546 OPT_d = (1 << 4),
547 OPT_e = (1 << 5),
548 OPT_f = (1 << 6),
549 OPT_l = (1 << 7),
550 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
551 };
552
553 INIT_G();
Denis Vlasenko85818632007-04-19 14:47:11 +0000554
Denis Vlasenko94949192006-11-05 00:45:47 +0000555 // POSIX:
556 // -a Write information for all processes associated with terminals
557 // Implementations may omit session leaders from this list
558 // -A Write information for all processes
559 // -d Write information for all processes, except session leaders
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200560 // -e Write information for all processes (equivalent to -A)
Denis Vlasenko94949192006-11-05 00:45:47 +0000561 // -f Generate a full listing
562 // -l Generate a long listing
563 // -o col1,col2,col3=header
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000564 // Select which columns to display
Denys Vlasenko887d9632009-11-24 03:52:24 +0100565 /* We allow (and ignore) most of the above. FIXME.
566 * -T is picked for threads (POSIX hasn't it standardized).
567 * procps v3.2.7 supports -T and shows tids as SPID column,
568 * it also supports -L where it shows tids as LWP column.
569 */
Denis Vlasenko94949192006-11-05 00:45:47 +0000570 opt_complementary = "o::";
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200571 opt = getopt32(argv, "Zo:aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
Denis Vlasenko94949192006-11-05 00:45:47 +0000572 if (opt_o) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000573 do {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000574 parse_o(llist_pop(&opt_o));
Denis Vlasenko94949192006-11-05 00:45:47 +0000575 } while (opt_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000576 } else {
577 /* Below: parse_o() needs char*, NOT const char*... */
578#if ENABLE_SELINUX
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200579 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000580 /* no -Z or no SELinux: do not show LABEL */
Denis Vlasenko83cea0e2008-07-16 21:11:09 +0000581 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000582 } else
583#endif
584 {
585 strcpy(default_o, DEFAULT_O_STR);
586 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000587 parse_o(default_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000588 }
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200589#if ENABLE_FEATURE_SHOW_THREADS
590 if (opt & OPT_T)
591 need_flags |= PSSCAN_TASKS;
592#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000593
Denis Vlasenko450196c2007-03-28 21:57:12 +0000594 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
595 * and such large widths */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000596 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000597 if (isatty(1)) {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000598 get_terminal_width_height(0, &terminal_width, NULL);
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000599 if (--terminal_width > MAX_WIDTH)
600 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000601 }
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200602 alloc_line_buffer();
Denis Vlasenko94949192006-11-05 00:45:47 +0000603 format_header();
604
605 p = NULL;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200606 while ((p = procps_scan(p, need_flags)) != NULL) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000607 format_process(p);
608 }
609
610 return EXIT_SUCCESS;
611}
612
613
614#else /* !ENABLE_DESKTOP */
615
616
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000617int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000618int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersenef8b6c71999-10-20 08:05:35 +0000619{
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200620 procps_status_t *p;
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200621 int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
622 | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
Denys Vlasenko39601842010-04-07 18:03:34 +0200623 unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
624 enum {
625 OPT_Z = (1 << 0) * ENABLE_SELINUX,
626 OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
627 };
628 int opts = 0;
629 /* If we support any options, parse argv */
630#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200631# if ENABLE_FEATURE_PS_WIDE
Denys Vlasenko39601842010-04-07 18:03:34 +0200632 /* -w is a bit complicated */
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200633 int w_count = 0;
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000634 opt_complementary = "-:ww";
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200635 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")"w", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000636 /* if w is given once, GNU ps sets the width to 132,
637 * if w is given more than once, it is "unlimited"
638 */
Denis Vlasenko621204b2006-10-27 09:03:24 +0000639 if (w_count) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200640 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000641 } else {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000642 get_terminal_width_height(0, &terminal_width, NULL);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000643 /* Go one less... */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000644 if (--terminal_width > MAX_WIDTH)
645 terminal_width = MAX_WIDTH;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000646 }
Denys Vlasenko39601842010-04-07 18:03:34 +0200647# else
648 /* -w is not supported, only -Z and/or -T */
649 opt_complementary = "-";
650 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T"));
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200651# endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200652#endif
653
654#if ENABLE_SELINUX
655 if ((opts & OPT_Z) && is_selinux_enabled()) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200656 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
657 | PSSCAN_STATE | PSSCAN_COMM;
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000658 puts(" PID CONTEXT STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200659 } else
660#endif
661 {
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000662 puts(" PID USER VSZ STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200663 }
664 if (opts & OPT_T) {
665 psscan_flags |= PSSCAN_TASKS;
666 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000667
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200668 p = NULL;
669 while ((p = procps_scan(p, psscan_flags)) != NULL) {
Denys Vlasenko39601842010-04-07 18:03:34 +0200670 int len;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000671#if ENABLE_SELINUX
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200672 if (psscan_flags & PSSCAN_CONTEXT) {
Denis Vlasenko766f6592008-01-07 16:41:41 +0000673 len = printf("%5u %-32.32s %s ",
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000674 p->pid,
675 p->context ? p->context : "unknown",
676 p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +0000677 } else
Eric Andersen9e480452003-07-03 10:07:04 +0000678#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000679 {
680 const char *user = get_cached_username(p->uid);
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000681 //if (p->vsz == 0)
682 // len = printf("%5u %-8.8s %s ",
683 // p->pid, user, p->state);
684 //else
685 {
686 char buf6[6];
687 smart_ulltoa5(p->vsz, buf6, " mgtpezy");
688 buf6[5] = '\0';
Denis Vlasenko766f6592008-01-07 16:41:41 +0000689 len = printf("%5u %-8.8s %s %s ",
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000690 p->pid, user, buf6, p->state);
691 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000692 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000693
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000694 {
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000695 int sz = terminal_width - len;
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000696 char buf[sz + 1];
697 read_cmdline(buf, sz, p->pid, p->comm);
698 puts(buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000699 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000700 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000701 if (ENABLE_FEATURE_CLEAN_UP)
702 clear_username_cache();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000703 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000704}
Denis Vlasenko94949192006-11-05 00:45:47 +0000705
Denys Vlasenko39601842010-04-07 18:03:34 +0200706#endif /* !ENABLE_DESKTOP */