blob: 4727b218bbc2d7dfe607867953ccaea26ac7c732 [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"
Pere Orga5bc8c002011-04-11 03:29:49 +020018//usage: "\n -o COL1,COL2=HEADER Select columns for display"
19//usage: IF_FEATURE_SHOW_THREADS(
20//usage: "\n -T Show threads"
21//usage: )
22//usage:
23//usage:#else /* !ENABLE_DESKTOP */
24//usage:
25//usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE
26//usage:#define USAGE_PS "\nThis version of ps accepts no options"
27//usage:#else
Denys Vlasenkod7efa062011-06-05 15:07:31 +020028//usage:#define USAGE_PS ""
Pere Orga5bc8c002011-04-11 03:29:49 +020029//usage:#endif
30//usage:
31//usage:#define ps_trivial_usage
32//usage: ""
33//usage:#define ps_full_usage "\n\n"
34//usage: "Show list of processes\n"
35//usage: USAGE_PS
36//usage: IF_SELINUX(
37//usage: "\n -Z Show selinux context"
38//usage: )
39//usage: IF_FEATURE_PS_WIDE(
40//usage: "\n w Wide output"
41//usage: )
Denys Vlasenko8d9ac302011-09-26 02:56:08 +020042//usage: IF_FEATURE_PS_LONG(
43//usage: "\n l Long output"
44//usage: )
45//usage: IF_FEATURE_SHOW_THREADS(
46//usage: "\n T Show threads"
47//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +020048//usage:
49//usage:#endif /* ENABLE_DESKTOP */
50//usage:
51//usage:#define ps_example_usage
52//usage: "$ ps\n"
53//usage: " PID Uid Gid State Command\n"
54//usage: " 1 root root S init\n"
55//usage: " 2 root root S [kflushd]\n"
56//usage: " 3 root root S [kupdate]\n"
57//usage: " 4 root root S [kpiod]\n"
58//usage: " 5 root root S [kswapd]\n"
59//usage: " 742 andersen andersen S [bash]\n"
60//usage: " 743 andersen andersen S -bash\n"
61//usage: " 745 root root S [getty]\n"
62//usage: " 2990 andersen andersen R ps\n"
63
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000064#include "libbb.h"
Denys Vlasenko8d9ac302011-09-26 02:56:08 +020065#ifdef __linux__
66# include <sys/sysinfo.h>
67#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +000068
Denis Vlasenko7ce75942007-07-25 17:27:58 +000069/* Absolute maximum on output line length */
70enum { MAX_WIDTH = 2*1024 };
71
Denis Vlasenko94949192006-11-05 00:45:47 +000072#if ENABLE_DESKTOP
73
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000074#include <sys/times.h> /* for times() */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000075#ifndef AT_CLKTCK
Denys Vlasenko099e5282011-02-02 00:59:35 +010076# define AT_CLKTCK 17
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000077#endif
78
Denys Vlasenko099e5282011-02-02 00:59:35 +010079/* TODO:
80 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
81 * specifies (for XSI-conformant systems) following default columns
82 * (l and f mark columns shown with -l and -f respectively):
83 * F l Flags (octal and additive) associated with the process (??)
84 * S l The state of the process
85 * UID f,l The user ID; the login name is printed with -f
86 * PID The process ID
87 * PPID f,l The parent process
88 * C f,l Processor utilization
89 * PRI l The priority of the process; higher numbers mean lower priority
90 * NI l Nice value
91 * ADDR l The address of the process
92 * SZ l The size in blocks of the core image of the process
93 * WCHAN l The event for which the process is waiting or sleeping
94 * STIME f Starting time of the process
95 * TTY The controlling terminal for the process
96 * TIME The cumulative execution time for the process
97 * CMD The command name; the full command line is shown with -f
98 */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000099typedef struct {
100 uint16_t width;
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200101 char name6[6];
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000102 const char *header;
103 void (*f)(char *buf, int size, const procps_status_t *ps);
104 int ps_flags;
105} ps_out_t;
106
107struct globals {
108 ps_out_t* out;
109 int out_cnt;
110 int print_header;
111 int need_flags;
112 char *buffer;
113 unsigned terminal_width;
114#if ENABLE_FEATURE_PS_TIME
115 unsigned kernel_HZ;
116 unsigned long long seconds_since_boot;
117#endif
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)
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200128#define INIT_G() do { } while (0)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000129
130#if ENABLE_FEATURE_PS_TIME
131/* for ELF executables, notes are pushed before environment and args */
132static ptrdiff_t find_elf_note(ptrdiff_t findme)
133{
Denis Vlasenko6b404432008-01-07 16:13:14 +0000134 ptrdiff_t *ep = (ptrdiff_t *) environ;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000135
Denys Vlasenko099e5282011-02-02 00:59:35 +0100136 while (*ep++)
137 continue;
Denis Vlasenko6b404432008-01-07 16:13:14 +0000138 while (*ep) {
139 if (ep[0] == findme) {
140 return ep[1];
141 }
142 ep += 2;
143 }
144 return -1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000145}
146
147#if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
148static unsigned get_HZ_by_waiting(void)
149{
Denis Vlasenko6b404432008-01-07 16:13:14 +0000150 struct timeval tv1, tv2;
151 unsigned t1, t2, r, hz;
152 unsigned cnt = cnt; /* for compiler */
153 int diff;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000154
Denis Vlasenko6b404432008-01-07 16:13:14 +0000155 r = 0;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000156
Denis Vlasenko6b404432008-01-07 16:13:14 +0000157 /* Wait for times() to reach new tick */
158 t1 = times(NULL);
159 do {
160 t2 = times(NULL);
161 } while (t2 == t1);
162 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000163
Denis Vlasenko6b404432008-01-07 16:13:14 +0000164 do {
165 t1 = t2;
166 tv1.tv_usec = tv2.tv_usec;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000167
Denis Vlasenko6b404432008-01-07 16:13:14 +0000168 /* Wait exactly one times() tick */
169 do {
170 t2 = times(NULL);
171 } while (t2 == t1);
172 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000173
Denis Vlasenko6b404432008-01-07 16:13:14 +0000174 /* Calculate ticks per sec, rounding up to even */
175 diff = tv2.tv_usec - tv1.tv_usec;
176 if (diff <= 0) diff += 1000000;
177 hz = 1000000u / (unsigned)diff;
178 hz = (hz+1) & ~1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000179
180 /* Count how many same hz values we saw */
Denis Vlasenko6b404432008-01-07 16:13:14 +0000181 if (r != hz) {
182 r = hz;
183 cnt = 0;
184 }
185 cnt++;
186 } while (cnt < 3); /* exit if saw 3 same values */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000187
Denis Vlasenko6b404432008-01-07 16:13:14 +0000188 return r;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000189}
190#else
191static inline unsigned get_HZ_by_waiting(void)
192{
193 /* Better method? */
194 return 100;
195}
196#endif
197
198static unsigned get_kernel_HZ(void)
199{
200 //char buf[64];
201 struct sysinfo info;
202
203 if (kernel_HZ)
204 return kernel_HZ;
205
206 /* Works for ELF only, Linux 2.4.0+ */
207 kernel_HZ = find_elf_note(AT_CLKTCK);
208 if (kernel_HZ == (unsigned)-1)
209 kernel_HZ = get_HZ_by_waiting();
210
Denys Vlasenkofe360482010-05-04 14:59:01 +0200211 //if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100212 // bb_perror_msg_and_die("can't read %s", "/proc/uptime");
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000213 //buf[sizeof(buf)-1] = '\0';
214 ///sscanf(buf, "%llu", &seconds_since_boot);
215 sysinfo(&info);
216 seconds_since_boot = info.uptime;
217
218 return kernel_HZ;
219}
220#endif
221
Denis Vlasenko94949192006-11-05 00:45:47 +0000222/* Print value to buf, max size+1 chars (including trailing '\0') */
223
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000224static void func_user(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000225{
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000226#if 1
Denis Vlasenko94949192006-11-05 00:45:47 +0000227 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000228#else
229 /* "compatible" version, but it's larger */
230 /* procps 2.18 shows numeric UID if name overflows the field */
231 /* TODO: get_cached_username() returns numeric string if
232 * user has no passwd record, we will display it
233 * left-justified here; too long usernames are shown
234 * as _right-justified_ IDs. Is it worth fixing? */
235 const char *user = get_cached_username(ps->uid);
236 if (strlen(user) <= size)
Denis Vlasenkoc85bfca2008-02-03 22:04:19 +0000237 safe_strncpy(buf, user, size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000238 else
239 sprintf(buf, "%*u", size, (unsigned)ps->uid);
240#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000241}
242
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200243static void func_group(char *buf, int size, const procps_status_t *ps)
244{
245 safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
246}
247
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000248static void func_comm(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000249{
250 safe_strncpy(buf, ps->comm, size+1);
251}
252
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200253static void func_state(char *buf, int size, const procps_status_t *ps)
Denys Vlasenko099e5282011-02-02 00:59:35 +0100254{
255 safe_strncpy(buf, ps->state, size+1);
256}
257
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000258static void func_args(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000259{
Denys Vlasenko3a0f6f22009-09-12 00:15:34 +0200260 read_cmdline(buf, size+1, ps->pid, ps->comm);
Denis Vlasenko94949192006-11-05 00:45:47 +0000261}
262
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000263static void func_pid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000264{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000265 sprintf(buf, "%*u", size, ps->pid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000266}
267
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000268static void func_ppid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000269{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000270 sprintf(buf, "%*u", size, ps->ppid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000271}
272
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000273static void func_pgid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000274{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000275 sprintf(buf, "%*u", size, ps->pgid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000276}
277
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000278static void put_lu(char *buf, int size, unsigned long u)
Denis Vlasenko94949192006-11-05 00:45:47 +0000279{
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000280 char buf4[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000281
282 /* see http://en.wikipedia.org/wiki/Tera */
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000283 smart_ulltoa4(u, buf4, " mgtpezy");
284 buf4[4] = '\0';
285 sprintf(buf, "%.*s", size, buf4);
Denis Vlasenko94949192006-11-05 00:45:47 +0000286}
287
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000288static void func_vsz(char *buf, int size, const procps_status_t *ps)
289{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000290 put_lu(buf, size, ps->vsz);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000291}
292
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000293static void func_rss(char *buf, int size, const procps_status_t *ps)
294{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000295 put_lu(buf, size, ps->rss);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000296}
297
298static void func_tty(char *buf, int size, const procps_status_t *ps)
299{
Denis Vlasenko31789a82007-06-19 14:43:17 +0000300 buf[0] = '?';
301 buf[1] = '\0';
302 if (ps->tty_major) /* tty field of "0" means "no tty" */
303 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000304}
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000305
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200306#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
307
308static void func_rgroup(char *buf, int size, const procps_status_t *ps)
309{
310 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
311}
312
313static void func_ruser(char *buf, int size, const procps_status_t *ps)
314{
315 safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
316}
317
318static void func_nice(char *buf, int size, const procps_status_t *ps)
319{
320 sprintf(buf, "%*d", size, ps->niceness);
321}
322
Denys Vlasenko9e072192010-05-23 00:34:42 +0200323#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200324
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000325#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko9e072192010-05-23 00:34:42 +0200326
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000327static void func_etime(char *buf, int size, const procps_status_t *ps)
328{
329 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
330 unsigned long mm;
331 unsigned ss;
332
333 mm = ps->start_time / get_kernel_HZ();
334 /* must be after get_kernel_HZ()! */
335 mm = seconds_since_boot - mm;
336 ss = mm % 60;
337 mm /= 60;
338 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
339}
340
341static void func_time(char *buf, int size, const procps_status_t *ps)
342{
343 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
344 unsigned long mm;
345 unsigned ss;
346
347 mm = (ps->utime + ps->stime) / get_kernel_HZ();
348 ss = mm % 60;
349 mm /= 60;
350 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
351}
Denys Vlasenko9e072192010-05-23 00:34:42 +0200352
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000353#endif
354
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000355#if ENABLE_SELINUX
356static void func_label(char *buf, int size, const procps_status_t *ps)
357{
358 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
359}
360#endif
361
Denis Vlasenko94949192006-11-05 00:45:47 +0000362/*
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000363static void func_nice(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000364{
365 ps->???
366}
367
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000368static void func_pcpu(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000369{
370}
371*/
372
Denis Vlasenko94949192006-11-05 00:45:47 +0000373static const ps_out_t out_spec[] = {
Denys Vlasenko099e5282011-02-02 00:59:35 +0100374/* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000375 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200376 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000377 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200378 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000379 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
380 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
381 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000382#if ENABLE_FEATURE_PS_TIME
383 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
384#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200385#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
386 { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
387 { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
388 { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
389// { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
390#endif
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000391#if ENABLE_FEATURE_PS_TIME
392 { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
393#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000394 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
395 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
Denys Vlasenko099e5282011-02-02 00:59:35 +0100396/* Not mandated, but useful: */
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200397 { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000398 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000399#if ENABLE_SELINUX
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000400 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000401#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000402};
403
Denis Vlasenko94949192006-11-05 00:45:47 +0000404static ps_out_t* new_out_t(void)
405{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000406 out = xrealloc_vector(out, 2, out_cnt);
407 return &out[out_cnt++];
Denis Vlasenko94949192006-11-05 00:45:47 +0000408}
409
410static const ps_out_t* find_out_spec(const char *name)
411{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000412 unsigned i;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200413 char buf[ARRAY_SIZE(out_spec)*7 + 1];
414 char *p = buf;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200415
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000416 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
Denys Vlasenko9e072192010-05-23 00:34:42 +0200417 if (strncmp(name, out_spec[i].name6, 6) == 0)
Denis Vlasenko94949192006-11-05 00:45:47 +0000418 return &out_spec[i];
Denys Vlasenko9e072192010-05-23 00:34:42 +0200419 p += sprintf(p, "%.6s,", out_spec[i].name6);
Denis Vlasenko94949192006-11-05 00:45:47 +0000420 }
Denys Vlasenko9e072192010-05-23 00:34:42 +0200421 p[-1] = '\0';
422 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
Denis Vlasenko94949192006-11-05 00:45:47 +0000423}
424
425static void parse_o(char* opt)
426{
427 ps_out_t* new;
428 // POSIX: "-o is blank- or comma-separated list" (FIXME)
429 char *comma, *equal;
430 while (1) {
431 comma = strchr(opt, ',');
432 equal = strchr(opt, '=');
433 if (comma && (!equal || equal > comma)) {
434 *comma = '\0';
435 *new_out_t() = *find_out_spec(opt);
436 *comma = ',';
437 opt = comma + 1;
438 continue;
439 }
440 break;
441 }
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000442 // opt points to last spec in comma separated list.
443 // This one can have =HEADER part.
Denis Vlasenko94949192006-11-05 00:45:47 +0000444 new = new_out_t();
445 if (equal)
446 *equal = '\0';
447 *new = *find_out_spec(opt);
448 if (equal) {
449 *equal = '=';
450 new->header = equal + 1;
451 // POSIX: the field widths shall be ... at least as wide as
452 // the header text (default or overridden value).
453 // If the header text is null, such as -o user=,
454 // the field width shall be at least as wide as the
455 // default header text
456 if (new->header[0]) {
457 new->width = strlen(new->header);
458 print_header = 1;
459 }
460 } else
461 print_header = 1;
462}
463
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200464static void alloc_line_buffer(void)
Denis Vlasenko94949192006-11-05 00:45:47 +0000465{
466 int i;
467 int width = 0;
468 for (i = 0; i < out_cnt; i++) {
Denis Vlasenko85818632007-04-19 14:47:11 +0000469 need_flags |= out[i].ps_flags;
Denis Vlasenko94949192006-11-05 00:45:47 +0000470 if (out[i].header[0]) {
471 print_header = 1;
472 }
473 width += out[i].width + 1; /* "FIELD " */
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200474 if ((int)(width - terminal_width) > 0) {
475 /* The rest does not fit on the screen */
476 //out[i].width -= (width - terminal_width - 1);
477 out_cnt = i + 1;
478 break;
479 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000480 }
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000481#if ENABLE_SELINUX
482 if (!is_selinux_enabled())
483 need_flags &= ~PSSCAN_CONTEXT;
484#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000485 buffer = xmalloc(width + 1); /* for trailing \0 */
486}
487
488static void format_header(void)
489{
490 int i;
491 ps_out_t* op;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000492 char *p;
493
Denis Vlasenko94949192006-11-05 00:45:47 +0000494 if (!print_header)
495 return;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000496 p = buffer;
Denis Vlasenko94949192006-11-05 00:45:47 +0000497 i = 0;
498 if (out_cnt) {
499 while (1) {
500 op = &out[i];
501 if (++i == out_cnt) /* do not pad last field */
502 break;
503 p += sprintf(p, "%-*s ", op->width, op->header);
504 }
505 strcpy(p, op->header);
506 }
507 printf("%.*s\n", terminal_width, buffer);
508}
509
510static void format_process(const procps_status_t *ps)
511{
512 int i, len;
513 char *p = buffer;
514 i = 0;
515 if (out_cnt) while (1) {
516 out[i].f(p, out[i].width, ps);
517 // POSIX: Any field need not be meaningful in all
518 // implementations. In such a case a hyphen ( '-' )
519 // should be output in place of the field value.
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000520 if (!p[0]) {
521 p[0] = '-';
522 p[1] = '\0';
Denis Vlasenko94949192006-11-05 00:45:47 +0000523 }
524 len = strlen(p);
525 p += len;
526 len = out[i].width - len + 1;
527 if (++i == out_cnt) /* do not pad last field */
528 break;
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000529 p += sprintf(p, "%*s", len, "");
Denis Vlasenko94949192006-11-05 00:45:47 +0000530 }
531 printf("%.*s\n", terminal_width, buffer);
532}
533
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200534#if ENABLE_SELINUX
535# define SELINUX_O_PREFIX "label,"
536# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
537#else
538# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
539#endif
540
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000541int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000542int ps_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko94949192006-11-05 00:45:47 +0000543{
544 procps_status_t *p;
545 llist_t* opt_o = NULL;
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200546 char default_o[sizeof(DEFAULT_O_STR)];
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200547 int opt;
548 enum {
549 OPT_Z = (1 << 0),
550 OPT_o = (1 << 1),
551 OPT_a = (1 << 2),
552 OPT_A = (1 << 3),
553 OPT_d = (1 << 4),
554 OPT_e = (1 << 5),
555 OPT_f = (1 << 6),
556 OPT_l = (1 << 7),
557 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
558 };
559
560 INIT_G();
Denis Vlasenko85818632007-04-19 14:47:11 +0000561
Denis Vlasenko94949192006-11-05 00:45:47 +0000562 // POSIX:
563 // -a Write information for all processes associated with terminals
564 // Implementations may omit session leaders from this list
565 // -A Write information for all processes
566 // -d Write information for all processes, except session leaders
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200567 // -e Write information for all processes (equivalent to -A)
Denis Vlasenko94949192006-11-05 00:45:47 +0000568 // -f Generate a full listing
569 // -l Generate a long listing
570 // -o col1,col2,col3=header
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000571 // Select which columns to display
Denys Vlasenko887d9632009-11-24 03:52:24 +0100572 /* We allow (and ignore) most of the above. FIXME.
573 * -T is picked for threads (POSIX hasn't it standardized).
574 * procps v3.2.7 supports -T and shows tids as SPID column,
575 * it also supports -L where it shows tids as LWP column.
576 */
Denis Vlasenko94949192006-11-05 00:45:47 +0000577 opt_complementary = "o::";
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200578 opt = getopt32(argv, "Zo:aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
Denis Vlasenko94949192006-11-05 00:45:47 +0000579 if (opt_o) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000580 do {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000581 parse_o(llist_pop(&opt_o));
Denis Vlasenko94949192006-11-05 00:45:47 +0000582 } while (opt_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000583 } else {
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200584 /* Below: parse_o() needs char*, NOT const char*, can't give it default_o */
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000585#if ENABLE_SELINUX
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200586 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000587 /* no -Z or no SELinux: do not show LABEL */
Denis Vlasenko83cea0e2008-07-16 21:11:09 +0000588 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000589 } else
590#endif
591 {
592 strcpy(default_o, DEFAULT_O_STR);
593 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000594 parse_o(default_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000595 }
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200596#if ENABLE_FEATURE_SHOW_THREADS
597 if (opt & OPT_T)
598 need_flags |= PSSCAN_TASKS;
599#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000600
Denis Vlasenko450196c2007-03-28 21:57:12 +0000601 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
602 * and such large widths */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000603 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000604 if (isatty(1)) {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000605 get_terminal_width_height(0, &terminal_width, NULL);
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000606 if (--terminal_width > MAX_WIDTH)
607 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000608 }
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200609 alloc_line_buffer();
Denis Vlasenko94949192006-11-05 00:45:47 +0000610 format_header();
611
612 p = NULL;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200613 while ((p = procps_scan(p, need_flags)) != NULL) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000614 format_process(p);
615 }
616
617 return EXIT_SUCCESS;
618}
619
620
621#else /* !ENABLE_DESKTOP */
622
623
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000624int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000625int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersenef8b6c71999-10-20 08:05:35 +0000626{
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200627 procps_status_t *p;
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200628 int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
629 | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
Denys Vlasenko39601842010-04-07 18:03:34 +0200630 unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
631 enum {
632 OPT_Z = (1 << 0) * ENABLE_SELINUX,
633 OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200634 OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG,
Denys Vlasenko39601842010-04-07 18:03:34 +0200635 };
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200636#if ENABLE_FEATURE_PS_LONG
637 time_t now = now;
638 struct sysinfo info;
639#endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200640 int opts = 0;
641 /* If we support any options, parse argv */
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200642#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE || ENABLE_FEATURE_PS_LONG
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200643# if ENABLE_FEATURE_PS_WIDE
Denys Vlasenko39601842010-04-07 18:03:34 +0200644 /* -w is a bit complicated */
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200645 int w_count = 0;
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000646 opt_complementary = "-:ww";
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200647 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")
648 "w", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000649 /* if w is given once, GNU ps sets the width to 132,
650 * if w is given more than once, it is "unlimited"
651 */
Denis Vlasenko621204b2006-10-27 09:03:24 +0000652 if (w_count) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200653 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000654 } else {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000655 get_terminal_width_height(0, &terminal_width, NULL);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000656 /* Go one less... */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000657 if (--terminal_width > MAX_WIDTH)
658 terminal_width = MAX_WIDTH;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000659 }
Denys Vlasenko39601842010-04-07 18:03:34 +0200660# else
661 /* -w is not supported, only -Z and/or -T */
662 opt_complementary = "-";
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200663 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200664# endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200665
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200666# if ENABLE_SELINUX
Denys Vlasenko39601842010-04-07 18:03:34 +0200667 if ((opts & OPT_Z) && is_selinux_enabled()) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200668 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
669 | PSSCAN_STATE | PSSCAN_COMM;
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000670 puts(" PID CONTEXT STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200671 } else
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200672# endif
673 if (opts & OPT_l) {
674 psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID
675 | PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM
676 | PSSCAN_VSZ | PSSCAN_RSS;
677/* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
678 * mandates for -l:
Flemming Madsenb64bd162011-09-27 15:31:25 +0200679 * -F Flags (?)
680 * S State
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200681 * UID,PID,PPID
Flemming Madsenb64bd162011-09-27 15:31:25 +0200682 * -C CPU usage
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200683 * -PRI The priority of the process; higher numbers mean lower priority
Flemming Madsenb64bd162011-09-27 15:31:25 +0200684 * -NI Nice value
685 * -ADDR The address of the process (?)
686 * SZ The size in blocks of the core image
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200687 * -WCHAN The event for which the process is waiting or sleeping
688 * TTY
Flemming Madsenb64bd162011-09-27 15:31:25 +0200689 * TIME The cumulative execution time
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200690 * CMD
Flemming Madsenb64bd162011-09-27 15:31:25 +0200691 * We don't show fields marked with '-'.
692 * We show VSZ and RSS instead of SZ.
693 * We also show STIME (standard says that -f shows it, -l doesn't).
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200694 */
Flemming Madsenb64bd162011-09-27 15:31:25 +0200695 puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD");
Cristian Ionescu-Idbohrn85b1f292011-09-29 11:02:00 +0200696#if ENABLE_FEATURE_PS_LONG
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200697 now = time(NULL);
698 sysinfo(&info);
Cristian Ionescu-Idbohrn85b1f292011-09-29 11:02:00 +0200699#endif
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200700 }
701 else {
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000702 puts(" PID USER VSZ STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200703 }
704 if (opts & OPT_T) {
705 psscan_flags |= PSSCAN_TASKS;
706 }
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200707#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200709 p = NULL;
710 while ((p = procps_scan(p, psscan_flags)) != NULL) {
Denys Vlasenko39601842010-04-07 18:03:34 +0200711 int len;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000712#if ENABLE_SELINUX
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200713 if (psscan_flags & PSSCAN_CONTEXT) {
Denis Vlasenko766f6592008-01-07 16:41:41 +0000714 len = printf("%5u %-32.32s %s ",
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000715 p->pid,
716 p->context ? p->context : "unknown",
717 p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +0000718 } else
Eric Andersen9e480452003-07-03 10:07:04 +0000719#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000720 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200721 char buf6[6];
722 smart_ulltoa5(p->vsz, buf6, " mgtpezy");
723 buf6[5] = '\0';
724#if ENABLE_FEATURE_PS_LONG
725 if (opts & OPT_l) {
Flemming Madsenb64bd162011-09-27 15:31:25 +0200726 char bufr[6], stime_str[6];
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200727 char tty[2 * sizeof(int)*3 + 2];
728 char *endp;
729 unsigned sut = (p->stime + p->utime) / 100;
730 unsigned elapsed = info.uptime - (p->start_time / 100);
731 time_t start = now - elapsed;
732 struct tm *tm = localtime(&start);
733
734 smart_ulltoa5(p->rss, bufr, " mgtpezy");
735 bufr[5] = '\0';
736
737 if (p->tty_major == 136)
Flemming Madsenb64bd162011-09-27 15:31:25 +0200738 /* It should be pts/N, not ptsN, but N > 9
739 * will overflow field width...
740 */
741 endp = stpcpy(tty, "pts");
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200742 else
743 if (p->tty_major == 4) {
744 endp = stpcpy(tty, "tty");
745 if (p->tty_minor >= 64) {
746 p->tty_minor -= 64;
747 *endp++ = 'S';
748 }
749 }
750 else
751 endp = tty + sprintf(tty, "%d:", p->tty_major);
752 strcpy(endp, utoa(p->tty_minor));
753
Flemming Madsenb64bd162011-09-27 15:31:25 +0200754 strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm);
755 stime_str[5] = '\0';
756 // S UID PID PPID VSZ RSS TTY STIME TIME CMD
757 len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ",
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200758 p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty,
Flemming Madsenb64bd162011-09-27 15:31:25 +0200759 stime_str, sut / 3600, (sut % 3600) / 60, sut % 60);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200760 } else
761#endif
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000762 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200763 const char *user = get_cached_username(p->uid);
Denis Vlasenko766f6592008-01-07 16:41:41 +0000764 len = printf("%5u %-8.8s %s %s ",
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000765 p->pid, user, buf6, p->state);
766 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000767 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000768
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000769 {
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000770 int sz = terminal_width - len;
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000771 char buf[sz + 1];
772 read_cmdline(buf, sz, p->pid, p->comm);
773 puts(buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000775 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000776 if (ENABLE_FEATURE_CLEAN_UP)
777 clear_username_cache();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000778 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000779}
Denis Vlasenko94949192006-11-05 00:45:47 +0000780
Denys Vlasenko39601842010-04-07 18:03:34 +0200781#endif /* !ENABLE_DESKTOP */