blob: 54e6c40fc5cc79a4a799fbe53b9ae60c132af3f5 [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
Kang-Che Sung753c4042017-08-19 14:18:27 +080020//config: depends on (PS || MINIPS) && !DESKTOP
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010021//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
Kang-Che Sung753c4042017-08-19 14:18:27 +080029//config: depends on (PS || MINIPS) && !DESKTOP
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010030//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
Kang-Che Sung753c4042017-08-19 14:18:27 +080037//config: depends on (PS || MINIPS) && DESKTOP
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010038//config: select PLATFORM_LINUX
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010039//config:
40//config:config FEATURE_PS_UNUSUAL_SYSTEMS
41//config: bool "Support Linux prior to 2.4.0 and non-ELF systems"
42//config: default n
43//config: depends on FEATURE_PS_TIME
44//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020045//config: Include support for measuring HZ on old kernels and non-ELF systems
46//config: (if you are on Linux 2.4.0+ and use ELF, you don't need this)
Kang-Che Sung753c4042017-08-19 14:18:27 +080047//config:
48//config:config FEATURE_PS_ADDITIONAL_COLUMNS
49//config: bool "Enable -o rgroup, -o ruser, -o nice specifiers"
50//config: default y
51//config: depends on (PS || MINIPS) && DESKTOP
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010052
Denys Vlasenkoab77e812017-08-18 19:15:29 +020053// APPLET_NOEXEC:name main location suid_type help
54//applet:IF_PS( APPLET_NOEXEC(ps, ps, BB_DIR_BIN, BB_SUID_DROP, ps))
55//applet:IF_MINIPS(APPLET_NOEXEC(minips, ps, BB_DIR_BIN, BB_SUID_DROP, ps))
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010056
57//kbuild:lib-$(CONFIG_PS) += ps.o
Denys Vlasenkoab77e812017-08-18 19:15:29 +020058//kbuild:lib-$(CONFIG_MINIPS) += ps.o
Eric Andersenbdfd0d72001-10-24 05:00:29 +000059
Pere Orga5bc8c002011-04-11 03:29:49 +020060//usage:#if ENABLE_DESKTOP
61//usage:
62//usage:#define ps_trivial_usage
63//usage: "[-o COL1,COL2=HEADER]" IF_FEATURE_SHOW_THREADS(" [-T]")
64//usage:#define ps_full_usage "\n\n"
65//usage: "Show list of processes\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020066//usage: "\n -o COL1,COL2=HEADER Select columns for display"
67//usage: IF_FEATURE_SHOW_THREADS(
68//usage: "\n -T Show threads"
69//usage: )
70//usage:
71//usage:#else /* !ENABLE_DESKTOP */
72//usage:
73//usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE
74//usage:#define USAGE_PS "\nThis version of ps accepts no options"
75//usage:#else
Denys Vlasenkod7efa062011-06-05 15:07:31 +020076//usage:#define USAGE_PS ""
Pere Orga5bc8c002011-04-11 03:29:49 +020077//usage:#endif
78//usage:
79//usage:#define ps_trivial_usage
80//usage: ""
81//usage:#define ps_full_usage "\n\n"
82//usage: "Show list of processes\n"
83//usage: USAGE_PS
84//usage: IF_SELINUX(
85//usage: "\n -Z Show selinux context"
86//usage: )
87//usage: IF_FEATURE_PS_WIDE(
88//usage: "\n w Wide output"
89//usage: )
Denys Vlasenko8d9ac302011-09-26 02:56:08 +020090//usage: IF_FEATURE_PS_LONG(
91//usage: "\n l Long output"
92//usage: )
93//usage: IF_FEATURE_SHOW_THREADS(
94//usage: "\n T Show threads"
95//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +020096//usage:
97//usage:#endif /* ENABLE_DESKTOP */
98//usage:
99//usage:#define ps_example_usage
100//usage: "$ ps\n"
101//usage: " PID Uid Gid State Command\n"
102//usage: " 1 root root S init\n"
103//usage: " 2 root root S [kflushd]\n"
104//usage: " 3 root root S [kupdate]\n"
105//usage: " 4 root root S [kpiod]\n"
106//usage: " 5 root root S [kswapd]\n"
107//usage: " 742 andersen andersen S [bash]\n"
108//usage: " 743 andersen andersen S -bash\n"
109//usage: " 745 root root S [getty]\n"
110//usage: " 2990 andersen andersen R ps\n"
111
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000112#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200113#include "common_bufsiz.h"
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200114#ifdef __linux__
115# include <sys/sysinfo.h>
116#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +0000117
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000118/* Absolute maximum on output line length */
119enum { MAX_WIDTH = 2*1024 };
120
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200121#if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100122static unsigned long get_uptime(void)
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200123{
124#ifdef __linux__
125 struct sysinfo info;
126 if (sysinfo(&info) < 0)
127 return 0;
128 return info.uptime;
129#elif 1
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100130 unsigned long uptime;
131 char buf[sizeof(uptime)*3 + 2];
132 /* /proc/uptime is "UPTIME_SEC.NN IDLE_SEC.NN\n"
133 * (where IDLE is cumulative over all CPUs)
134 */
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200135 if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100136 bb_perror_msg_and_die("can't read '%s'", "/proc/uptime");
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200137 buf[sizeof(buf)-1] = '\0';
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100138 sscanf(buf, "%lu", &uptime);
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200139 return uptime;
140#else
141 struct timespec ts;
142 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
143 return 0;
144 return ts.tv_sec;
145#endif
146}
147#endif
148
Denis Vlasenko94949192006-11-05 00:45:47 +0000149#if ENABLE_DESKTOP
Denys Vlasenko099e5282011-02-02 00:59:35 +0100150/* TODO:
151 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
152 * specifies (for XSI-conformant systems) following default columns
153 * (l and f mark columns shown with -l and -f respectively):
154 * F l Flags (octal and additive) associated with the process (??)
155 * S l The state of the process
156 * UID f,l The user ID; the login name is printed with -f
157 * PID The process ID
158 * PPID f,l The parent process
159 * C f,l Processor utilization
160 * PRI l The priority of the process; higher numbers mean lower priority
161 * NI l Nice value
162 * ADDR l The address of the process
163 * SZ l The size in blocks of the core image of the process
164 * WCHAN l The event for which the process is waiting or sleeping
165 * STIME f Starting time of the process
166 * TTY The controlling terminal for the process
167 * TIME The cumulative execution time for the process
168 * CMD The command name; the full command line is shown with -f
169 */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000170typedef struct {
171 uint16_t width;
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200172 char name6[6];
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000173 const char *header;
174 void (*f)(char *buf, int size, const procps_status_t *ps);
175 int ps_flags;
176} ps_out_t;
177
178struct globals {
179 ps_out_t* out;
180 int out_cnt;
181 int print_header;
182 int need_flags;
183 char *buffer;
184 unsigned terminal_width;
185#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko1fd8e662017-08-10 14:10:56 +0200186# if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS || !defined(__linux__)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000187 unsigned kernel_HZ;
Denys Vlasenko1fd8e662017-08-10 14:10:56 +0200188# endif
Denys Vlasenko17d44362013-01-26 14:21:15 +0100189 unsigned long seconds_since_boot;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000190#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100191} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200192#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000193#define out (G.out )
194#define out_cnt (G.out_cnt )
195#define print_header (G.print_header )
196#define need_flags (G.need_flags )
197#define buffer (G.buffer )
198#define terminal_width (G.terminal_width )
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200199#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000200
201#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko1fd8e662017-08-10 14:10:56 +0200202# if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS || !defined(__linux__)
203# define get_kernel_HZ() (G.kernel_HZ)
Denys Vlasenko00c18112017-08-05 22:25:00 +0200204# else
Denys Vlasenko1fd8e662017-08-10 14:10:56 +0200205 /* non-ancient Linux standardized on 100 for "times" freq */
206# define get_kernel_HZ() ((unsigned)100)
Denys Vlasenko00c18112017-08-05 22:25:00 +0200207# endif
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000208#endif
209
Denis Vlasenko94949192006-11-05 00:45:47 +0000210/* Print value to buf, max size+1 chars (including trailing '\0') */
211
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000212static void func_user(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000213{
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000214#if 1
Denis Vlasenko94949192006-11-05 00:45:47 +0000215 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000216#else
217 /* "compatible" version, but it's larger */
218 /* procps 2.18 shows numeric UID if name overflows the field */
219 /* TODO: get_cached_username() returns numeric string if
220 * user has no passwd record, we will display it
221 * left-justified here; too long usernames are shown
222 * as _right-justified_ IDs. Is it worth fixing? */
223 const char *user = get_cached_username(ps->uid);
224 if (strlen(user) <= size)
Denis Vlasenkoc85bfca2008-02-03 22:04:19 +0000225 safe_strncpy(buf, user, size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000226 else
227 sprintf(buf, "%*u", size, (unsigned)ps->uid);
228#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000229}
230
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200231static void func_group(char *buf, int size, const procps_status_t *ps)
232{
233 safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
234}
235
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000236static void func_comm(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000237{
238 safe_strncpy(buf, ps->comm, size+1);
239}
240
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200241static void func_state(char *buf, int size, const procps_status_t *ps)
Denys Vlasenko099e5282011-02-02 00:59:35 +0100242{
243 safe_strncpy(buf, ps->state, size+1);
244}
245
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000246static void func_args(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000247{
Denys Vlasenko3a0f6f22009-09-12 00:15:34 +0200248 read_cmdline(buf, size+1, ps->pid, ps->comm);
Denis Vlasenko94949192006-11-05 00:45:47 +0000249}
250
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000251static void func_pid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000252{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000253 sprintf(buf, "%*u", size, ps->pid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000254}
255
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000256static void func_ppid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000257{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000258 sprintf(buf, "%*u", size, ps->ppid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000259}
260
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000261static void func_pgid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000262{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000263 sprintf(buf, "%*u", size, ps->pgid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000264}
265
Denys Vlasenko5d725fd2017-07-29 02:20:17 +0200266static void func_sid(char *buf, int size, const procps_status_t *ps)
267{
268 sprintf(buf, "%*u", size, ps->sid);
269}
270
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000271static void put_lu(char *buf, int size, unsigned long u)
Denis Vlasenko94949192006-11-05 00:45:47 +0000272{
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000273 char buf4[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000274
275 /* see http://en.wikipedia.org/wiki/Tera */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200276 smart_ulltoa4(u, buf4, " mgtpezy")[0] = '\0';
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000277 sprintf(buf, "%.*s", size, buf4);
Denis Vlasenko94949192006-11-05 00:45:47 +0000278}
279
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000280static void func_vsz(char *buf, int size, const procps_status_t *ps)
281{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000282 put_lu(buf, size, ps->vsz);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000283}
284
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000285static void func_rss(char *buf, int size, const procps_status_t *ps)
286{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000287 put_lu(buf, size, ps->rss);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000288}
289
290static void func_tty(char *buf, int size, const procps_status_t *ps)
291{
Denis Vlasenko31789a82007-06-19 14:43:17 +0000292 buf[0] = '?';
293 buf[1] = '\0';
294 if (ps->tty_major) /* tty field of "0" means "no tty" */
295 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000296}
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000297
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200298#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200299static void func_rgroup(char *buf, int size, const procps_status_t *ps)
300{
301 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
302}
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200303static void func_ruser(char *buf, int size, const procps_status_t *ps)
304{
305 safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
306}
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200307static void func_nice(char *buf, int size, const procps_status_t *ps)
308{
309 sprintf(buf, "%*d", size, ps->niceness);
310}
Denys Vlasenko9e072192010-05-23 00:34:42 +0200311#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200312
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000313#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko9c7c63b2017-08-10 15:01:43 +0200314static void format_time(char *buf, int size, unsigned long tt)
315{
316 unsigned ff;
317
318 /* Used to show "14453:50" if tt is large. Ugly.
319 * procps-ng 3.3.10 uses "[[dd-]hh:]mm:ss" format.
320 * TODO: switch to that?
321 */
322
323 /* Formatting for 5-char TIME column.
324 * NB: "size" is not always 5: ELAPSED is wider (7),
325 * not taking advantage of that (yet?).
326 */
327 ff = tt % 60;
328 tt /= 60;
329 if (tt < 60) {
330 snprintf(buf, size+1, "%2u:%02u", (unsigned)tt, ff);
331 return;
332 }
333 ff = tt % 60;
334 tt /= 60;
335 if (tt < 24) {
336 snprintf(buf, size+1, "%2uh%02u", (unsigned)tt, ff);
337 return;
338 }
339 ff = tt % 24;
340 tt /= 24;
341 if (tt < 100) {
342 snprintf(buf, size+1, "%2ud%02u", (unsigned)tt, ff);
343 return;
344 }
345 snprintf(buf, size+1, "%4lud", tt);
346}
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000347static void func_etime(char *buf, int size, const procps_status_t *ps)
348{
349 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
350 unsigned long mm;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000351
352 mm = ps->start_time / get_kernel_HZ();
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100353 mm = G.seconds_since_boot - mm;
Denys Vlasenko9c7c63b2017-08-10 15:01:43 +0200354 format_time(buf, size, mm);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000355}
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000356static void func_time(char *buf, int size, const procps_status_t *ps)
357{
358 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
359 unsigned long mm;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000360
361 mm = (ps->utime + ps->stime) / get_kernel_HZ();
Denys Vlasenko9c7c63b2017-08-10 15:01:43 +0200362 format_time(buf, size, mm);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000363}
364#endif
365
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000366#if ENABLE_SELINUX
367static void func_label(char *buf, int size, const procps_status_t *ps)
368{
369 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
370}
371#endif
372
Denis Vlasenko94949192006-11-05 00:45:47 +0000373/*
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000374static void func_pcpu(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000375{
376}
377*/
378
Denis Vlasenko94949192006-11-05 00:45:47 +0000379static const ps_out_t out_spec[] = {
Denys Vlasenko099e5282011-02-02 00:59:35 +0100380/* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000381 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200382 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000383 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200384 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000385 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
386 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
387 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000388#if ENABLE_FEATURE_PS_TIME
389 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
390#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200391#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
392 { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
393 { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
394 { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
395// { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
396#endif
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000397#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko9c7c63b2017-08-10 15:01:43 +0200398 { 5 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000399#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000400 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
401 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
Denys Vlasenko099e5282011-02-02 00:59:35 +0100402/* Not mandated, but useful: */
Denys Vlasenko5d725fd2017-07-29 02:20:17 +0200403 { 5 , "sid" ,"SID" ,func_sid ,PSSCAN_SID },
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200404 { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000405 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000406#if ENABLE_SELINUX
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000407 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000408#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000409};
410
Denis Vlasenko94949192006-11-05 00:45:47 +0000411static ps_out_t* new_out_t(void)
412{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000413 out = xrealloc_vector(out, 2, out_cnt);
414 return &out[out_cnt++];
Denis Vlasenko94949192006-11-05 00:45:47 +0000415}
416
417static const ps_out_t* find_out_spec(const char *name)
418{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000419 unsigned i;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200420 char buf[ARRAY_SIZE(out_spec)*7 + 1];
421 char *p = buf;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200422
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000423 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
Denys Vlasenko9e072192010-05-23 00:34:42 +0200424 if (strncmp(name, out_spec[i].name6, 6) == 0)
Denis Vlasenko94949192006-11-05 00:45:47 +0000425 return &out_spec[i];
Denys Vlasenko9e072192010-05-23 00:34:42 +0200426 p += sprintf(p, "%.6s,", out_spec[i].name6);
Denis Vlasenko94949192006-11-05 00:45:47 +0000427 }
Denys Vlasenko9e072192010-05-23 00:34:42 +0200428 p[-1] = '\0';
429 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
Denis Vlasenko94949192006-11-05 00:45:47 +0000430}
431
432static void parse_o(char* opt)
433{
434 ps_out_t* new;
435 // POSIX: "-o is blank- or comma-separated list" (FIXME)
436 char *comma, *equal;
437 while (1) {
438 comma = strchr(opt, ',');
439 equal = strchr(opt, '=');
440 if (comma && (!equal || equal > comma)) {
441 *comma = '\0';
442 *new_out_t() = *find_out_spec(opt);
443 *comma = ',';
444 opt = comma + 1;
445 continue;
446 }
447 break;
448 }
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000449 // opt points to last spec in comma separated list.
450 // This one can have =HEADER part.
Denis Vlasenko94949192006-11-05 00:45:47 +0000451 new = new_out_t();
452 if (equal)
453 *equal = '\0';
454 *new = *find_out_spec(opt);
455 if (equal) {
456 *equal = '=';
457 new->header = equal + 1;
458 // POSIX: the field widths shall be ... at least as wide as
459 // the header text (default or overridden value).
460 // If the header text is null, such as -o user=,
461 // the field width shall be at least as wide as the
462 // default header text
463 if (new->header[0]) {
464 new->width = strlen(new->header);
465 print_header = 1;
466 }
467 } else
468 print_header = 1;
469}
470
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200471static void alloc_line_buffer(void)
Denis Vlasenko94949192006-11-05 00:45:47 +0000472{
473 int i;
474 int width = 0;
475 for (i = 0; i < out_cnt; i++) {
Denis Vlasenko85818632007-04-19 14:47:11 +0000476 need_flags |= out[i].ps_flags;
Denis Vlasenko94949192006-11-05 00:45:47 +0000477 if (out[i].header[0]) {
478 print_header = 1;
479 }
480 width += out[i].width + 1; /* "FIELD " */
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200481 if ((int)(width - terminal_width) > 0) {
482 /* The rest does not fit on the screen */
483 //out[i].width -= (width - terminal_width - 1);
484 out_cnt = i + 1;
485 break;
486 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000487 }
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000488#if ENABLE_SELINUX
489 if (!is_selinux_enabled())
490 need_flags &= ~PSSCAN_CONTEXT;
491#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000492 buffer = xmalloc(width + 1); /* for trailing \0 */
493}
494
495static void format_header(void)
496{
497 int i;
498 ps_out_t* op;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000499 char *p;
500
Denis Vlasenko94949192006-11-05 00:45:47 +0000501 if (!print_header)
502 return;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000503 p = buffer;
Denis Vlasenko94949192006-11-05 00:45:47 +0000504 i = 0;
505 if (out_cnt) {
506 while (1) {
507 op = &out[i];
508 if (++i == out_cnt) /* do not pad last field */
509 break;
510 p += sprintf(p, "%-*s ", op->width, op->header);
511 }
512 strcpy(p, op->header);
513 }
514 printf("%.*s\n", terminal_width, buffer);
515}
516
517static void format_process(const procps_status_t *ps)
518{
519 int i, len;
520 char *p = buffer;
521 i = 0;
522 if (out_cnt) while (1) {
523 out[i].f(p, out[i].width, ps);
524 // POSIX: Any field need not be meaningful in all
525 // implementations. In such a case a hyphen ( '-' )
526 // should be output in place of the field value.
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000527 if (!p[0]) {
528 p[0] = '-';
529 p[1] = '\0';
Denis Vlasenko94949192006-11-05 00:45:47 +0000530 }
531 len = strlen(p);
532 p += len;
533 len = out[i].width - len + 1;
534 if (++i == out_cnt) /* do not pad last field */
535 break;
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000536 p += sprintf(p, "%*s", len, "");
Denis Vlasenko94949192006-11-05 00:45:47 +0000537 }
538 printf("%.*s\n", terminal_width, buffer);
539}
540
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200541#if ENABLE_SELINUX
542# define SELINUX_O_PREFIX "label,"
543# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
544#else
545# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
546#endif
547
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000548int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000549int ps_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko94949192006-11-05 00:45:47 +0000550{
551 procps_status_t *p;
552 llist_t* opt_o = NULL;
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200553 char default_o[sizeof(DEFAULT_O_STR)];
Denys Vlasenko4e08a122017-01-16 17:31:05 +0100554#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200555 int opt;
Denys Vlasenko4e08a122017-01-16 17:31:05 +0100556#endif
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200557 enum {
558 OPT_Z = (1 << 0),
559 OPT_o = (1 << 1),
560 OPT_a = (1 << 2),
561 OPT_A = (1 << 3),
562 OPT_d = (1 << 4),
563 OPT_e = (1 << 5),
564 OPT_f = (1 << 6),
565 OPT_l = (1 << 7),
566 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
567 };
568
569 INIT_G();
Ron Yorston0ec0fac2017-08-22 11:21:56 +0100570#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko1fd8e662017-08-10 14:10:56 +0200571 G.seconds_since_boot = get_uptime();
Ron Yorston0ec0fac2017-08-22 11:21:56 +0100572# if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS || !defined(__linux__)
Denys Vlasenko1fd8e662017-08-10 14:10:56 +0200573 G.kernel_HZ = bb_clk_tck(); /* this is sysconf(_SC_CLK_TCK) */
Ron Yorston0ec0fac2017-08-22 11:21:56 +0100574# endif
Denys Vlasenko1fd8e662017-08-10 14:10:56 +0200575#endif
Denis Vlasenko85818632007-04-19 14:47:11 +0000576
Denis Vlasenko94949192006-11-05 00:45:47 +0000577 // POSIX:
578 // -a Write information for all processes associated with terminals
579 // Implementations may omit session leaders from this list
580 // -A Write information for all processes
581 // -d Write information for all processes, except session leaders
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200582 // -e Write information for all processes (equivalent to -A)
Denis Vlasenko94949192006-11-05 00:45:47 +0000583 // -f Generate a full listing
584 // -l Generate a long listing
585 // -o col1,col2,col3=header
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000586 // Select which columns to display
Denys Vlasenko887d9632009-11-24 03:52:24 +0100587 /* We allow (and ignore) most of the above. FIXME.
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100588 * -T is picked for threads (POSIX hasn't standardized it).
Denys Vlasenko887d9632009-11-24 03:52:24 +0100589 * procps v3.2.7 supports -T and shows tids as SPID column,
590 * it also supports -L where it shows tids as LWP column.
591 */
Denys Vlasenko4e08a122017-01-16 17:31:05 +0100592#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS
593 opt =
594#endif
595 getopt32(argv, "Zo:*aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
596
Denis Vlasenko94949192006-11-05 00:45:47 +0000597 if (opt_o) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000598 do {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000599 parse_o(llist_pop(&opt_o));
Denis Vlasenko94949192006-11-05 00:45:47 +0000600 } while (opt_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000601 } else {
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100602 /* Below: parse_o() needs char*, NOT const char*,
603 * can't pass it constant string. Need to make a copy first.
604 */
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000605#if ENABLE_SELINUX
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200606 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000607 /* no -Z or no SELinux: do not show LABEL */
Denis Vlasenko83cea0e2008-07-16 21:11:09 +0000608 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000609 } else
610#endif
611 {
612 strcpy(default_o, DEFAULT_O_STR);
613 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000614 parse_o(default_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000615 }
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200616#if ENABLE_FEATURE_SHOW_THREADS
617 if (opt & OPT_T)
618 need_flags |= PSSCAN_TASKS;
619#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000620
Denis Vlasenko450196c2007-03-28 21:57:12 +0000621 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
622 * and such large widths */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000623 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000624 if (isatty(1)) {
Denys Vlasenko641caae2015-10-23 01:44:22 +0200625 terminal_width = get_terminal_width(0);
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000626 if (--terminal_width > MAX_WIDTH)
627 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000628 }
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200629 alloc_line_buffer();
Denis Vlasenko94949192006-11-05 00:45:47 +0000630 format_header();
631
632 p = NULL;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200633 while ((p = procps_scan(p, need_flags)) != NULL) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000634 format_process(p);
635 }
636
637 return EXIT_SUCCESS;
638}
639
640
641#else /* !ENABLE_DESKTOP */
642
643
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000644int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000645int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersenef8b6c71999-10-20 08:05:35 +0000646{
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200647 procps_status_t *p;
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200648 int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
649 | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
Denys Vlasenko39601842010-04-07 18:03:34 +0200650 unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
651 enum {
652 OPT_Z = (1 << 0) * ENABLE_SELINUX,
653 OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200654 OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG,
Denys Vlasenko39601842010-04-07 18:03:34 +0200655 };
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200656#if ENABLE_FEATURE_PS_LONG
Denys Vlasenkoc9091d82015-07-01 19:07:24 +0200657 time_t now = now; /* for compiler */
658 unsigned long uptime = uptime;
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200659#endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200660 /* If we support any options, parse argv */
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200661#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE || ENABLE_FEATURE_PS_LONG
Denys Vlasenko42be9212012-09-25 12:48:10 +0200662 int opts = 0;
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200663# if ENABLE_FEATURE_PS_WIDE
Denys Vlasenko39601842010-04-07 18:03:34 +0200664 /* -w is a bit complicated */
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200665 int w_count = 0;
Denys Vlasenko727948e2017-08-04 16:23:42 +0200666 make_all_argv_opts(argv);
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200667 opts = getopt32(argv, "^"
668 IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")"w"
669 "\0" "ww",
670 &w_count
671 );
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000672 /* if w is given once, GNU ps sets the width to 132,
673 * if w is given more than once, it is "unlimited"
674 */
Denis Vlasenko621204b2006-10-27 09:03:24 +0000675 if (w_count) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200676 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000677 } else {
Denys Vlasenko641caae2015-10-23 01:44:22 +0200678 terminal_width = get_terminal_width(0);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000679 /* Go one less... */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000680 if (--terminal_width > MAX_WIDTH)
681 terminal_width = MAX_WIDTH;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000682 }
Denys Vlasenko39601842010-04-07 18:03:34 +0200683# else
684 /* -w is not supported, only -Z and/or -T */
Denys Vlasenko727948e2017-08-04 16:23:42 +0200685 make_all_argv_opts(argv);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200686 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200687# endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200688
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200689# if ENABLE_SELINUX
Denys Vlasenko39601842010-04-07 18:03:34 +0200690 if ((opts & OPT_Z) && is_selinux_enabled()) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200691 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
692 | PSSCAN_STATE | PSSCAN_COMM;
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000693 puts(" PID CONTEXT STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200694 } else
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200695# endif
696 if (opts & OPT_l) {
697 psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID
698 | PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM
699 | PSSCAN_VSZ | PSSCAN_RSS;
700/* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
701 * mandates for -l:
Flemming Madsenb64bd162011-09-27 15:31:25 +0200702 * -F Flags (?)
703 * S State
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200704 * UID,PID,PPID
Flemming Madsenb64bd162011-09-27 15:31:25 +0200705 * -C CPU usage
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200706 * -PRI The priority of the process; higher numbers mean lower priority
Flemming Madsenb64bd162011-09-27 15:31:25 +0200707 * -NI Nice value
708 * -ADDR The address of the process (?)
709 * SZ The size in blocks of the core image
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200710 * -WCHAN The event for which the process is waiting or sleeping
711 * TTY
Flemming Madsenb64bd162011-09-27 15:31:25 +0200712 * TIME The cumulative execution time
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200713 * CMD
Flemming Madsenb64bd162011-09-27 15:31:25 +0200714 * We don't show fields marked with '-'.
715 * We show VSZ and RSS instead of SZ.
716 * We also show STIME (standard says that -f shows it, -l doesn't).
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200717 */
Flemming Madsenb64bd162011-09-27 15:31:25 +0200718 puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD");
Denys Vlasenko42be9212012-09-25 12:48:10 +0200719# if ENABLE_FEATURE_PS_LONG
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200720 now = time(NULL);
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200721 uptime = get_uptime();
Denys Vlasenko42be9212012-09-25 12:48:10 +0200722# endif
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200723 }
724 else {
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000725 puts(" PID USER VSZ STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200726 }
727 if (opts & OPT_T) {
728 psscan_flags |= PSSCAN_TASKS;
729 }
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200730#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000731
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200732 p = NULL;
733 while ((p = procps_scan(p, psscan_flags)) != NULL) {
Denys Vlasenko39601842010-04-07 18:03:34 +0200734 int len;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000735#if ENABLE_SELINUX
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200736 if (psscan_flags & PSSCAN_CONTEXT) {
Denis Vlasenko766f6592008-01-07 16:41:41 +0000737 len = printf("%5u %-32.32s %s ",
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000738 p->pid,
739 p->context ? p->context : "unknown",
740 p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +0000741 } else
Eric Andersen9e480452003-07-03 10:07:04 +0000742#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000743 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200744 char buf6[6];
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200745 smart_ulltoa5(p->vsz, buf6, " mgtpezy")[0] = '\0';
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200746#if ENABLE_FEATURE_PS_LONG
747 if (opts & OPT_l) {
Flemming Madsenb64bd162011-09-27 15:31:25 +0200748 char bufr[6], stime_str[6];
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200749 char tty[2 * sizeof(int)*3 + 2];
750 char *endp;
751 unsigned sut = (p->stime + p->utime) / 100;
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200752 unsigned elapsed = uptime - (p->start_time / 100);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200753 time_t start = now - elapsed;
754 struct tm *tm = localtime(&start);
755
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200756 smart_ulltoa5(p->rss, bufr, " mgtpezy")[0] = '\0';
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200757
758 if (p->tty_major == 136)
Flemming Madsenb64bd162011-09-27 15:31:25 +0200759 /* It should be pts/N, not ptsN, but N > 9
760 * will overflow field width...
761 */
762 endp = stpcpy(tty, "pts");
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200763 else
764 if (p->tty_major == 4) {
765 endp = stpcpy(tty, "tty");
766 if (p->tty_minor >= 64) {
767 p->tty_minor -= 64;
768 *endp++ = 'S';
769 }
770 }
771 else
772 endp = tty + sprintf(tty, "%d:", p->tty_major);
773 strcpy(endp, utoa(p->tty_minor));
774
Flemming Madsenb64bd162011-09-27 15:31:25 +0200775 strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm);
776 stime_str[5] = '\0';
777 // S UID PID PPID VSZ RSS TTY STIME TIME CMD
778 len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ",
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200779 p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty,
Flemming Madsenb64bd162011-09-27 15:31:25 +0200780 stime_str, sut / 3600, (sut % 3600) / 60, sut % 60);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200781 } else
782#endif
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000783 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200784 const char *user = get_cached_username(p->uid);
Denis Vlasenko766f6592008-01-07 16:41:41 +0000785 len = printf("%5u %-8.8s %s %s ",
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000786 p->pid, user, buf6, p->state);
787 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000788 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000789
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000790 {
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000791 int sz = terminal_width - len;
Denys Vlasenkoc9091d82015-07-01 19:07:24 +0200792 if (sz >= 0) {
793 char buf[sz + 1];
794 read_cmdline(buf, sz, p->pid, p->comm);
795 puts(buf);
796 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000797 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000798 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000799 if (ENABLE_FEATURE_CLEAN_UP)
800 clear_username_cache();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000801 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000802}
Denis Vlasenko94949192006-11-05 00:45:47 +0000803
Denys Vlasenko39601842010-04-07 18:03:34 +0200804#endif /* !ENABLE_DESKTOP */