blob: afd9813131d5eb0b8a00535bf3febd9498ecbf41 [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 */
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010011//config:config PS
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020012//config: bool "ps (11 kb)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010013//config: default y
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: ps gives a snapshot of the current processes.
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010016//config:
17//config:config FEATURE_PS_WIDE
Denys Vlasenko00c18112017-08-05 22:25:00 +020018//config: bool "Enable wide output (-w)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010019//config: default y
20//config: depends on PS && !DESKTOP
21//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//config: Support argument 'w' for wide output.
23//config: If given once, 132 chars are printed, and if given more
24//config: than once, the length is unlimited.
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010025//config:
26//config:config FEATURE_PS_LONG
Denys Vlasenko00c18112017-08-05 22:25:00 +020027//config: bool "Enable long output (-l)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010028//config: default y
29//config: depends on PS && !DESKTOP
30//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020031//config: Support argument 'l' for long output.
32//config: Adds fields PPID, RSS, START, TIME & TTY
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010033//config:
34//config:config FEATURE_PS_TIME
Denys Vlasenko00c18112017-08-05 22:25:00 +020035//config: bool "Enable -o time and -o etime specifiers"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010036//config: default y
37//config: depends on PS && DESKTOP
38//config: select PLATFORM_LINUX
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010039//config:
Denys Vlasenko00c18112017-08-05 22:25:00 +020040//config:config FEATURE_PS_ADDITIONAL_COLUMNS
41//config: bool "Enable -o rgroup, -o ruser, -o nice specifiers"
42//config: default y
43//config: depends on PS && DESKTOP
44//config:
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010045//config:config FEATURE_PS_UNUSUAL_SYSTEMS
46//config: bool "Support Linux prior to 2.4.0 and non-ELF systems"
47//config: default n
48//config: depends on FEATURE_PS_TIME
49//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020050//config: Include support for measuring HZ on old kernels and non-ELF systems
51//config: (if you are on Linux 2.4.0+ and use ELF, you don't need this)
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010052
53//applet:IF_PS(APPLET(ps, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko00c18112017-08-05 22:25:00 +020054/* can't be NOEXEC: uses ELF aux vector. To have it, we must be a normal, execed process */
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010055
56//kbuild:lib-$(CONFIG_PS) += ps.o
Eric Andersenbdfd0d72001-10-24 05:00:29 +000057
Pere Orga5bc8c002011-04-11 03:29:49 +020058//usage:#if ENABLE_DESKTOP
59//usage:
60//usage:#define ps_trivial_usage
61//usage: "[-o COL1,COL2=HEADER]" IF_FEATURE_SHOW_THREADS(" [-T]")
62//usage:#define ps_full_usage "\n\n"
63//usage: "Show list of processes\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020064//usage: "\n -o COL1,COL2=HEADER Select columns for display"
65//usage: IF_FEATURE_SHOW_THREADS(
66//usage: "\n -T Show threads"
67//usage: )
68//usage:
69//usage:#else /* !ENABLE_DESKTOP */
70//usage:
71//usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE
72//usage:#define USAGE_PS "\nThis version of ps accepts no options"
73//usage:#else
Denys Vlasenkod7efa062011-06-05 15:07:31 +020074//usage:#define USAGE_PS ""
Pere Orga5bc8c002011-04-11 03:29:49 +020075//usage:#endif
76//usage:
77//usage:#define ps_trivial_usage
78//usage: ""
79//usage:#define ps_full_usage "\n\n"
80//usage: "Show list of processes\n"
81//usage: USAGE_PS
82//usage: IF_SELINUX(
83//usage: "\n -Z Show selinux context"
84//usage: )
85//usage: IF_FEATURE_PS_WIDE(
86//usage: "\n w Wide output"
87//usage: )
Denys Vlasenko8d9ac302011-09-26 02:56:08 +020088//usage: IF_FEATURE_PS_LONG(
89//usage: "\n l Long output"
90//usage: )
91//usage: IF_FEATURE_SHOW_THREADS(
92//usage: "\n T Show threads"
93//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +020094//usage:
95//usage:#endif /* ENABLE_DESKTOP */
96//usage:
97//usage:#define ps_example_usage
98//usage: "$ ps\n"
99//usage: " PID Uid Gid State Command\n"
100//usage: " 1 root root S init\n"
101//usage: " 2 root root S [kflushd]\n"
102//usage: " 3 root root S [kupdate]\n"
103//usage: " 4 root root S [kpiod]\n"
104//usage: " 5 root root S [kswapd]\n"
105//usage: " 742 andersen andersen S [bash]\n"
106//usage: " 743 andersen andersen S -bash\n"
107//usage: " 745 root root S [getty]\n"
108//usage: " 2990 andersen andersen R ps\n"
109
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000110#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200111#include "common_bufsiz.h"
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200112#ifdef __linux__
113# include <sys/sysinfo.h>
114#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +0000115
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000116/* Absolute maximum on output line length */
117enum { MAX_WIDTH = 2*1024 };
118
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200119#if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100120static unsigned long get_uptime(void)
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200121{
122#ifdef __linux__
123 struct sysinfo info;
124 if (sysinfo(&info) < 0)
125 return 0;
126 return info.uptime;
127#elif 1
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100128 unsigned long uptime;
129 char buf[sizeof(uptime)*3 + 2];
130 /* /proc/uptime is "UPTIME_SEC.NN IDLE_SEC.NN\n"
131 * (where IDLE is cumulative over all CPUs)
132 */
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200133 if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100134 bb_perror_msg_and_die("can't read '%s'", "/proc/uptime");
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200135 buf[sizeof(buf)-1] = '\0';
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100136 sscanf(buf, "%lu", &uptime);
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200137 return uptime;
138#else
139 struct timespec ts;
140 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
141 return 0;
142 return ts.tv_sec;
143#endif
144}
145#endif
146
Denis Vlasenko94949192006-11-05 00:45:47 +0000147#if ENABLE_DESKTOP
148
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000149#include <sys/times.h> /* for times() */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000150#ifndef AT_CLKTCK
Denys Vlasenko099e5282011-02-02 00:59:35 +0100151# define AT_CLKTCK 17
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000152#endif
153
Denys Vlasenko099e5282011-02-02 00:59:35 +0100154/* TODO:
155 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
156 * specifies (for XSI-conformant systems) following default columns
157 * (l and f mark columns shown with -l and -f respectively):
158 * F l Flags (octal and additive) associated with the process (??)
159 * S l The state of the process
160 * UID f,l The user ID; the login name is printed with -f
161 * PID The process ID
162 * PPID f,l The parent process
163 * C f,l Processor utilization
164 * PRI l The priority of the process; higher numbers mean lower priority
165 * NI l Nice value
166 * ADDR l The address of the process
167 * SZ l The size in blocks of the core image of the process
168 * WCHAN l The event for which the process is waiting or sleeping
169 * STIME f Starting time of the process
170 * TTY The controlling terminal for the process
171 * TIME The cumulative execution time for the process
172 * CMD The command name; the full command line is shown with -f
173 */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000174typedef struct {
175 uint16_t width;
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200176 char name6[6];
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000177 const char *header;
178 void (*f)(char *buf, int size, const procps_status_t *ps);
179 int ps_flags;
180} ps_out_t;
181
182struct globals {
183 ps_out_t* out;
184 int out_cnt;
185 int print_header;
186 int need_flags;
187 char *buffer;
188 unsigned terminal_width;
189#if ENABLE_FEATURE_PS_TIME
190 unsigned kernel_HZ;
Denys Vlasenko17d44362013-01-26 14:21:15 +0100191 unsigned long seconds_since_boot;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000192#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100193} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200194#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000195#define out (G.out )
196#define out_cnt (G.out_cnt )
197#define print_header (G.print_header )
198#define need_flags (G.need_flags )
199#define buffer (G.buffer )
200#define terminal_width (G.terminal_width )
201#define kernel_HZ (G.kernel_HZ )
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200202#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000203
204#if ENABLE_FEATURE_PS_TIME
205/* for ELF executables, notes are pushed before environment and args */
Denys Vlasenko00c18112017-08-05 22:25:00 +0200206/* try "LD_SHOW_AUXV=1 /bin/true" */
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100207static uintptr_t find_elf_note(uintptr_t findme)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000208{
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100209 uintptr_t *ep = (uintptr_t *) environ;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000210
Denys Vlasenko099e5282011-02-02 00:59:35 +0100211 while (*ep++)
212 continue;
Denis Vlasenko6b404432008-01-07 16:13:14 +0000213 while (*ep) {
214 if (ep[0] == findme) {
215 return ep[1];
216 }
217 ep += 2;
218 }
219 return -1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000220}
221
Denys Vlasenko00c18112017-08-05 22:25:00 +0200222# if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000223static unsigned get_HZ_by_waiting(void)
224{
Denis Vlasenko6b404432008-01-07 16:13:14 +0000225 struct timeval tv1, tv2;
226 unsigned t1, t2, r, hz;
227 unsigned cnt = cnt; /* for compiler */
228 int diff;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000229
Denis Vlasenko6b404432008-01-07 16:13:14 +0000230 r = 0;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000231
Denis Vlasenko6b404432008-01-07 16:13:14 +0000232 /* Wait for times() to reach new tick */
233 t1 = times(NULL);
234 do {
235 t2 = times(NULL);
236 } while (t2 == t1);
237 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000238
Denis Vlasenko6b404432008-01-07 16:13:14 +0000239 do {
240 t1 = t2;
241 tv1.tv_usec = tv2.tv_usec;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000242
Denis Vlasenko6b404432008-01-07 16:13:14 +0000243 /* Wait exactly one times() tick */
244 do {
245 t2 = times(NULL);
246 } while (t2 == t1);
247 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000248
Denis Vlasenko6b404432008-01-07 16:13:14 +0000249 /* Calculate ticks per sec, rounding up to even */
250 diff = tv2.tv_usec - tv1.tv_usec;
251 if (diff <= 0) diff += 1000000;
252 hz = 1000000u / (unsigned)diff;
253 hz = (hz+1) & ~1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000254
255 /* Count how many same hz values we saw */
Denis Vlasenko6b404432008-01-07 16:13:14 +0000256 if (r != hz) {
257 r = hz;
258 cnt = 0;
259 }
260 cnt++;
261 } while (cnt < 3); /* exit if saw 3 same values */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000262
Denis Vlasenko6b404432008-01-07 16:13:14 +0000263 return r;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000264}
Denys Vlasenko00c18112017-08-05 22:25:00 +0200265# else
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000266static inline unsigned get_HZ_by_waiting(void)
267{
268 /* Better method? */
269 return 100;
270}
Denys Vlasenko00c18112017-08-05 22:25:00 +0200271# endif
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000272
273static unsigned get_kernel_HZ(void)
274{
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000275 if (kernel_HZ)
276 return kernel_HZ;
277
278 /* Works for ELF only, Linux 2.4.0+ */
279 kernel_HZ = find_elf_note(AT_CLKTCK);
280 if (kernel_HZ == (unsigned)-1)
281 kernel_HZ = get_HZ_by_waiting();
282
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100283 G.seconds_since_boot = get_uptime();
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000284
285 return kernel_HZ;
286}
287#endif
288
Denis Vlasenko94949192006-11-05 00:45:47 +0000289/* Print value to buf, max size+1 chars (including trailing '\0') */
290
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000291static void func_user(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000292{
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000293#if 1
Denis Vlasenko94949192006-11-05 00:45:47 +0000294 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000295#else
296 /* "compatible" version, but it's larger */
297 /* procps 2.18 shows numeric UID if name overflows the field */
298 /* TODO: get_cached_username() returns numeric string if
299 * user has no passwd record, we will display it
300 * left-justified here; too long usernames are shown
301 * as _right-justified_ IDs. Is it worth fixing? */
302 const char *user = get_cached_username(ps->uid);
303 if (strlen(user) <= size)
Denis Vlasenkoc85bfca2008-02-03 22:04:19 +0000304 safe_strncpy(buf, user, size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000305 else
306 sprintf(buf, "%*u", size, (unsigned)ps->uid);
307#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000308}
309
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200310static void func_group(char *buf, int size, const procps_status_t *ps)
311{
312 safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
313}
314
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000315static void func_comm(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000316{
317 safe_strncpy(buf, ps->comm, size+1);
318}
319
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200320static void func_state(char *buf, int size, const procps_status_t *ps)
Denys Vlasenko099e5282011-02-02 00:59:35 +0100321{
322 safe_strncpy(buf, ps->state, size+1);
323}
324
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000325static void func_args(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000326{
Denys Vlasenko3a0f6f22009-09-12 00:15:34 +0200327 read_cmdline(buf, size+1, ps->pid, ps->comm);
Denis Vlasenko94949192006-11-05 00:45:47 +0000328}
329
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000330static void func_pid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000331{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000332 sprintf(buf, "%*u", size, ps->pid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000333}
334
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000335static void func_ppid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000336{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000337 sprintf(buf, "%*u", size, ps->ppid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000338}
339
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000340static void func_pgid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000341{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000342 sprintf(buf, "%*u", size, ps->pgid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000343}
344
Denys Vlasenko5d725fd2017-07-29 02:20:17 +0200345static void func_sid(char *buf, int size, const procps_status_t *ps)
346{
347 sprintf(buf, "%*u", size, ps->sid);
348}
349
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000350static void put_lu(char *buf, int size, unsigned long u)
Denis Vlasenko94949192006-11-05 00:45:47 +0000351{
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000352 char buf4[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000353
354 /* see http://en.wikipedia.org/wiki/Tera */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200355 smart_ulltoa4(u, buf4, " mgtpezy")[0] = '\0';
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000356 sprintf(buf, "%.*s", size, buf4);
Denis Vlasenko94949192006-11-05 00:45:47 +0000357}
358
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000359static void func_vsz(char *buf, int size, const procps_status_t *ps)
360{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000361 put_lu(buf, size, ps->vsz);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000362}
363
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000364static void func_rss(char *buf, int size, const procps_status_t *ps)
365{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000366 put_lu(buf, size, ps->rss);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000367}
368
369static void func_tty(char *buf, int size, const procps_status_t *ps)
370{
Denis Vlasenko31789a82007-06-19 14:43:17 +0000371 buf[0] = '?';
372 buf[1] = '\0';
373 if (ps->tty_major) /* tty field of "0" means "no tty" */
374 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000375}
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000376
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200377#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
378
379static void func_rgroup(char *buf, int size, const procps_status_t *ps)
380{
381 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
382}
383
384static void func_ruser(char *buf, int size, const procps_status_t *ps)
385{
386 safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
387}
388
389static void func_nice(char *buf, int size, const procps_status_t *ps)
390{
391 sprintf(buf, "%*d", size, ps->niceness);
392}
393
Denys Vlasenko9e072192010-05-23 00:34:42 +0200394#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200395
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000396#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko9e072192010-05-23 00:34:42 +0200397
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000398static void func_etime(char *buf, int size, const procps_status_t *ps)
399{
400 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
401 unsigned long mm;
402 unsigned ss;
403
404 mm = ps->start_time / get_kernel_HZ();
405 /* must be after get_kernel_HZ()! */
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100406 mm = G.seconds_since_boot - mm;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000407 ss = mm % 60;
408 mm /= 60;
409 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
410}
411
412static void func_time(char *buf, int size, const procps_status_t *ps)
413{
414 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
415 unsigned long mm;
416 unsigned ss;
417
418 mm = (ps->utime + ps->stime) / get_kernel_HZ();
419 ss = mm % 60;
420 mm /= 60;
421 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
422}
Denys Vlasenko9e072192010-05-23 00:34:42 +0200423
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000424#endif
425
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000426#if ENABLE_SELINUX
427static void func_label(char *buf, int size, const procps_status_t *ps)
428{
429 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
430}
431#endif
432
Denis Vlasenko94949192006-11-05 00:45:47 +0000433/*
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000434static void func_pcpu(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000435{
436}
437*/
438
Denis Vlasenko94949192006-11-05 00:45:47 +0000439static const ps_out_t out_spec[] = {
Denys Vlasenko099e5282011-02-02 00:59:35 +0100440/* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000441 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200442 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000443 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200444 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000445 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
446 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
447 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000448#if ENABLE_FEATURE_PS_TIME
449 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
450#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200451#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
452 { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
453 { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
454 { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
455// { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
456#endif
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000457#if ENABLE_FEATURE_PS_TIME
458 { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
459#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000460 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
461 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
Denys Vlasenko099e5282011-02-02 00:59:35 +0100462/* Not mandated, but useful: */
Denys Vlasenko5d725fd2017-07-29 02:20:17 +0200463 { 5 , "sid" ,"SID" ,func_sid ,PSSCAN_SID },
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200464 { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000465 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000466#if ENABLE_SELINUX
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000467 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000468#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000469};
470
Denis Vlasenko94949192006-11-05 00:45:47 +0000471static ps_out_t* new_out_t(void)
472{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000473 out = xrealloc_vector(out, 2, out_cnt);
474 return &out[out_cnt++];
Denis Vlasenko94949192006-11-05 00:45:47 +0000475}
476
477static const ps_out_t* find_out_spec(const char *name)
478{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000479 unsigned i;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200480 char buf[ARRAY_SIZE(out_spec)*7 + 1];
481 char *p = buf;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200482
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000483 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
Denys Vlasenko9e072192010-05-23 00:34:42 +0200484 if (strncmp(name, out_spec[i].name6, 6) == 0)
Denis Vlasenko94949192006-11-05 00:45:47 +0000485 return &out_spec[i];
Denys Vlasenko9e072192010-05-23 00:34:42 +0200486 p += sprintf(p, "%.6s,", out_spec[i].name6);
Denis Vlasenko94949192006-11-05 00:45:47 +0000487 }
Denys Vlasenko9e072192010-05-23 00:34:42 +0200488 p[-1] = '\0';
489 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
Denis Vlasenko94949192006-11-05 00:45:47 +0000490}
491
492static void parse_o(char* opt)
493{
494 ps_out_t* new;
495 // POSIX: "-o is blank- or comma-separated list" (FIXME)
496 char *comma, *equal;
497 while (1) {
498 comma = strchr(opt, ',');
499 equal = strchr(opt, '=');
500 if (comma && (!equal || equal > comma)) {
501 *comma = '\0';
502 *new_out_t() = *find_out_spec(opt);
503 *comma = ',';
504 opt = comma + 1;
505 continue;
506 }
507 break;
508 }
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000509 // opt points to last spec in comma separated list.
510 // This one can have =HEADER part.
Denis Vlasenko94949192006-11-05 00:45:47 +0000511 new = new_out_t();
512 if (equal)
513 *equal = '\0';
514 *new = *find_out_spec(opt);
515 if (equal) {
516 *equal = '=';
517 new->header = equal + 1;
518 // POSIX: the field widths shall be ... at least as wide as
519 // the header text (default or overridden value).
520 // If the header text is null, such as -o user=,
521 // the field width shall be at least as wide as the
522 // default header text
523 if (new->header[0]) {
524 new->width = strlen(new->header);
525 print_header = 1;
526 }
527 } else
528 print_header = 1;
529}
530
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200531static void alloc_line_buffer(void)
Denis Vlasenko94949192006-11-05 00:45:47 +0000532{
533 int i;
534 int width = 0;
535 for (i = 0; i < out_cnt; i++) {
Denis Vlasenko85818632007-04-19 14:47:11 +0000536 need_flags |= out[i].ps_flags;
Denis Vlasenko94949192006-11-05 00:45:47 +0000537 if (out[i].header[0]) {
538 print_header = 1;
539 }
540 width += out[i].width + 1; /* "FIELD " */
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200541 if ((int)(width - terminal_width) > 0) {
542 /* The rest does not fit on the screen */
543 //out[i].width -= (width - terminal_width - 1);
544 out_cnt = i + 1;
545 break;
546 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000547 }
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000548#if ENABLE_SELINUX
549 if (!is_selinux_enabled())
550 need_flags &= ~PSSCAN_CONTEXT;
551#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000552 buffer = xmalloc(width + 1); /* for trailing \0 */
553}
554
555static void format_header(void)
556{
557 int i;
558 ps_out_t* op;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000559 char *p;
560
Denis Vlasenko94949192006-11-05 00:45:47 +0000561 if (!print_header)
562 return;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000563 p = buffer;
Denis Vlasenko94949192006-11-05 00:45:47 +0000564 i = 0;
565 if (out_cnt) {
566 while (1) {
567 op = &out[i];
568 if (++i == out_cnt) /* do not pad last field */
569 break;
570 p += sprintf(p, "%-*s ", op->width, op->header);
571 }
572 strcpy(p, op->header);
573 }
574 printf("%.*s\n", terminal_width, buffer);
575}
576
577static void format_process(const procps_status_t *ps)
578{
579 int i, len;
580 char *p = buffer;
581 i = 0;
582 if (out_cnt) while (1) {
583 out[i].f(p, out[i].width, ps);
584 // POSIX: Any field need not be meaningful in all
585 // implementations. In such a case a hyphen ( '-' )
586 // should be output in place of the field value.
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000587 if (!p[0]) {
588 p[0] = '-';
589 p[1] = '\0';
Denis Vlasenko94949192006-11-05 00:45:47 +0000590 }
591 len = strlen(p);
592 p += len;
593 len = out[i].width - len + 1;
594 if (++i == out_cnt) /* do not pad last field */
595 break;
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000596 p += sprintf(p, "%*s", len, "");
Denis Vlasenko94949192006-11-05 00:45:47 +0000597 }
598 printf("%.*s\n", terminal_width, buffer);
599}
600
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200601#if ENABLE_SELINUX
602# define SELINUX_O_PREFIX "label,"
603# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
604#else
605# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
606#endif
607
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000608int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000609int ps_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko94949192006-11-05 00:45:47 +0000610{
611 procps_status_t *p;
612 llist_t* opt_o = NULL;
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200613 char default_o[sizeof(DEFAULT_O_STR)];
Denys Vlasenko4e08a122017-01-16 17:31:05 +0100614#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200615 int opt;
Denys Vlasenko4e08a122017-01-16 17:31:05 +0100616#endif
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200617 enum {
618 OPT_Z = (1 << 0),
619 OPT_o = (1 << 1),
620 OPT_a = (1 << 2),
621 OPT_A = (1 << 3),
622 OPT_d = (1 << 4),
623 OPT_e = (1 << 5),
624 OPT_f = (1 << 6),
625 OPT_l = (1 << 7),
626 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
627 };
628
629 INIT_G();
Denis Vlasenko85818632007-04-19 14:47:11 +0000630
Denis Vlasenko94949192006-11-05 00:45:47 +0000631 // POSIX:
632 // -a Write information for all processes associated with terminals
633 // Implementations may omit session leaders from this list
634 // -A Write information for all processes
635 // -d Write information for all processes, except session leaders
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200636 // -e Write information for all processes (equivalent to -A)
Denis Vlasenko94949192006-11-05 00:45:47 +0000637 // -f Generate a full listing
638 // -l Generate a long listing
639 // -o col1,col2,col3=header
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000640 // Select which columns to display
Denys Vlasenko887d9632009-11-24 03:52:24 +0100641 /* We allow (and ignore) most of the above. FIXME.
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100642 * -T is picked for threads (POSIX hasn't standardized it).
Denys Vlasenko887d9632009-11-24 03:52:24 +0100643 * procps v3.2.7 supports -T and shows tids as SPID column,
644 * it also supports -L where it shows tids as LWP column.
645 */
Denys Vlasenko4e08a122017-01-16 17:31:05 +0100646#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS
647 opt =
648#endif
649 getopt32(argv, "Zo:*aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
650
Denis Vlasenko94949192006-11-05 00:45:47 +0000651 if (opt_o) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000652 do {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000653 parse_o(llist_pop(&opt_o));
Denis Vlasenko94949192006-11-05 00:45:47 +0000654 } while (opt_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000655 } else {
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100656 /* Below: parse_o() needs char*, NOT const char*,
657 * can't pass it constant string. Need to make a copy first.
658 */
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000659#if ENABLE_SELINUX
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200660 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000661 /* no -Z or no SELinux: do not show LABEL */
Denis Vlasenko83cea0e2008-07-16 21:11:09 +0000662 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000663 } else
664#endif
665 {
666 strcpy(default_o, DEFAULT_O_STR);
667 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000668 parse_o(default_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000669 }
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200670#if ENABLE_FEATURE_SHOW_THREADS
671 if (opt & OPT_T)
672 need_flags |= PSSCAN_TASKS;
673#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000674
Denis Vlasenko450196c2007-03-28 21:57:12 +0000675 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
676 * and such large widths */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000677 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000678 if (isatty(1)) {
Denys Vlasenko641caae2015-10-23 01:44:22 +0200679 terminal_width = get_terminal_width(0);
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000680 if (--terminal_width > MAX_WIDTH)
681 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000682 }
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200683 alloc_line_buffer();
Denis Vlasenko94949192006-11-05 00:45:47 +0000684 format_header();
685
686 p = NULL;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200687 while ((p = procps_scan(p, need_flags)) != NULL) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000688 format_process(p);
689 }
690
691 return EXIT_SUCCESS;
692}
693
694
695#else /* !ENABLE_DESKTOP */
696
697
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000698int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000699int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersenef8b6c71999-10-20 08:05:35 +0000700{
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200701 procps_status_t *p;
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200702 int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
703 | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
Denys Vlasenko39601842010-04-07 18:03:34 +0200704 unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
705 enum {
706 OPT_Z = (1 << 0) * ENABLE_SELINUX,
707 OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200708 OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG,
Denys Vlasenko39601842010-04-07 18:03:34 +0200709 };
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200710#if ENABLE_FEATURE_PS_LONG
Denys Vlasenkoc9091d82015-07-01 19:07:24 +0200711 time_t now = now; /* for compiler */
712 unsigned long uptime = uptime;
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200713#endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200714 /* If we support any options, parse argv */
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200715#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE || ENABLE_FEATURE_PS_LONG
Denys Vlasenko42be9212012-09-25 12:48:10 +0200716 int opts = 0;
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200717# if ENABLE_FEATURE_PS_WIDE
Denys Vlasenko39601842010-04-07 18:03:34 +0200718 /* -w is a bit complicated */
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200719 int w_count = 0;
Denys Vlasenko727948e2017-08-04 16:23:42 +0200720 make_all_argv_opts(argv);
721 opt_complementary = "ww";
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200722 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")
723 "w", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000724 /* if w is given once, GNU ps sets the width to 132,
725 * if w is given more than once, it is "unlimited"
726 */
Denis Vlasenko621204b2006-10-27 09:03:24 +0000727 if (w_count) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200728 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000729 } else {
Denys Vlasenko641caae2015-10-23 01:44:22 +0200730 terminal_width = get_terminal_width(0);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000731 /* Go one less... */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000732 if (--terminal_width > MAX_WIDTH)
733 terminal_width = MAX_WIDTH;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000734 }
Denys Vlasenko39601842010-04-07 18:03:34 +0200735# else
736 /* -w is not supported, only -Z and/or -T */
Denys Vlasenko727948e2017-08-04 16:23:42 +0200737 make_all_argv_opts(argv);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200738 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200739# endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200740
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200741# if ENABLE_SELINUX
Denys Vlasenko39601842010-04-07 18:03:34 +0200742 if ((opts & OPT_Z) && is_selinux_enabled()) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200743 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
744 | PSSCAN_STATE | PSSCAN_COMM;
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000745 puts(" PID CONTEXT STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200746 } else
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200747# endif
748 if (opts & OPT_l) {
749 psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID
750 | PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM
751 | PSSCAN_VSZ | PSSCAN_RSS;
752/* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
753 * mandates for -l:
Flemming Madsenb64bd162011-09-27 15:31:25 +0200754 * -F Flags (?)
755 * S State
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200756 * UID,PID,PPID
Flemming Madsenb64bd162011-09-27 15:31:25 +0200757 * -C CPU usage
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200758 * -PRI The priority of the process; higher numbers mean lower priority
Flemming Madsenb64bd162011-09-27 15:31:25 +0200759 * -NI Nice value
760 * -ADDR The address of the process (?)
761 * SZ The size in blocks of the core image
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200762 * -WCHAN The event for which the process is waiting or sleeping
763 * TTY
Flemming Madsenb64bd162011-09-27 15:31:25 +0200764 * TIME The cumulative execution time
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200765 * CMD
Flemming Madsenb64bd162011-09-27 15:31:25 +0200766 * We don't show fields marked with '-'.
767 * We show VSZ and RSS instead of SZ.
768 * We also show STIME (standard says that -f shows it, -l doesn't).
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200769 */
Flemming Madsenb64bd162011-09-27 15:31:25 +0200770 puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD");
Denys Vlasenko42be9212012-09-25 12:48:10 +0200771# if ENABLE_FEATURE_PS_LONG
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200772 now = time(NULL);
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200773 uptime = get_uptime();
Denys Vlasenko42be9212012-09-25 12:48:10 +0200774# endif
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200775 }
776 else {
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000777 puts(" PID USER VSZ STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200778 }
779 if (opts & OPT_T) {
780 psscan_flags |= PSSCAN_TASKS;
781 }
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200782#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200784 p = NULL;
785 while ((p = procps_scan(p, psscan_flags)) != NULL) {
Denys Vlasenko39601842010-04-07 18:03:34 +0200786 int len;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000787#if ENABLE_SELINUX
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200788 if (psscan_flags & PSSCAN_CONTEXT) {
Denis Vlasenko766f6592008-01-07 16:41:41 +0000789 len = printf("%5u %-32.32s %s ",
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000790 p->pid,
791 p->context ? p->context : "unknown",
792 p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +0000793 } else
Eric Andersen9e480452003-07-03 10:07:04 +0000794#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000795 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200796 char buf6[6];
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200797 smart_ulltoa5(p->vsz, buf6, " mgtpezy")[0] = '\0';
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200798#if ENABLE_FEATURE_PS_LONG
799 if (opts & OPT_l) {
Flemming Madsenb64bd162011-09-27 15:31:25 +0200800 char bufr[6], stime_str[6];
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200801 char tty[2 * sizeof(int)*3 + 2];
802 char *endp;
803 unsigned sut = (p->stime + p->utime) / 100;
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200804 unsigned elapsed = uptime - (p->start_time / 100);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200805 time_t start = now - elapsed;
806 struct tm *tm = localtime(&start);
807
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200808 smart_ulltoa5(p->rss, bufr, " mgtpezy")[0] = '\0';
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200809
810 if (p->tty_major == 136)
Flemming Madsenb64bd162011-09-27 15:31:25 +0200811 /* It should be pts/N, not ptsN, but N > 9
812 * will overflow field width...
813 */
814 endp = stpcpy(tty, "pts");
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200815 else
816 if (p->tty_major == 4) {
817 endp = stpcpy(tty, "tty");
818 if (p->tty_minor >= 64) {
819 p->tty_minor -= 64;
820 *endp++ = 'S';
821 }
822 }
823 else
824 endp = tty + sprintf(tty, "%d:", p->tty_major);
825 strcpy(endp, utoa(p->tty_minor));
826
Flemming Madsenb64bd162011-09-27 15:31:25 +0200827 strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm);
828 stime_str[5] = '\0';
829 // S UID PID PPID VSZ RSS TTY STIME TIME CMD
830 len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ",
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200831 p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty,
Flemming Madsenb64bd162011-09-27 15:31:25 +0200832 stime_str, sut / 3600, (sut % 3600) / 60, sut % 60);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200833 } else
834#endif
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000835 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200836 const char *user = get_cached_username(p->uid);
Denis Vlasenko766f6592008-01-07 16:41:41 +0000837 len = printf("%5u %-8.8s %s %s ",
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000838 p->pid, user, buf6, p->state);
839 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000840 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000841
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000842 {
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000843 int sz = terminal_width - len;
Denys Vlasenkoc9091d82015-07-01 19:07:24 +0200844 if (sz >= 0) {
845 char buf[sz + 1];
846 read_cmdline(buf, sz, p->pid, p->comm);
847 puts(buf);
848 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000850 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000851 if (ENABLE_FEATURE_CLEAN_UP)
852 clear_username_cache();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000853 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000854}
Denis Vlasenko94949192006-11-05 00:45:47 +0000855
Denys Vlasenko39601842010-04-07 18:03:34 +0200856#endif /* !ENABLE_DESKTOP */