blob: 65d62e256af9432d3a239969c48a02446ac1d9c4 [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 Vlasenkoe6a2f4c2016-04-21 16:26:30 +020065#include "common_bufsiz.h"
Denys Vlasenko8d9ac302011-09-26 02:56:08 +020066#ifdef __linux__
67# include <sys/sysinfo.h>
68#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +000069
Denis Vlasenko7ce75942007-07-25 17:27:58 +000070/* Absolute maximum on output line length */
71enum { MAX_WIDTH = 2*1024 };
72
Michael Tokarevac42e3d2012-06-13 02:51:56 +020073#if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG
Denys Vlasenko6aab0612013-01-22 10:07:23 +010074static unsigned long get_uptime(void)
Michael Tokarevac42e3d2012-06-13 02:51:56 +020075{
76#ifdef __linux__
77 struct sysinfo info;
78 if (sysinfo(&info) < 0)
79 return 0;
80 return info.uptime;
81#elif 1
Denys Vlasenko6aab0612013-01-22 10:07:23 +010082 unsigned long uptime;
83 char buf[sizeof(uptime)*3 + 2];
84 /* /proc/uptime is "UPTIME_SEC.NN IDLE_SEC.NN\n"
85 * (where IDLE is cumulative over all CPUs)
86 */
Michael Tokarevac42e3d2012-06-13 02:51:56 +020087 if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
Denys Vlasenko6aab0612013-01-22 10:07:23 +010088 bb_perror_msg_and_die("can't read '%s'", "/proc/uptime");
Michael Tokarevac42e3d2012-06-13 02:51:56 +020089 buf[sizeof(buf)-1] = '\0';
Denys Vlasenko6aab0612013-01-22 10:07:23 +010090 sscanf(buf, "%lu", &uptime);
Michael Tokarevac42e3d2012-06-13 02:51:56 +020091 return uptime;
92#else
93 struct timespec ts;
94 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
95 return 0;
96 return ts.tv_sec;
97#endif
98}
99#endif
100
Denis Vlasenko94949192006-11-05 00:45:47 +0000101#if ENABLE_DESKTOP
102
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000103#include <sys/times.h> /* for times() */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000104#ifndef AT_CLKTCK
Denys Vlasenko099e5282011-02-02 00:59:35 +0100105# define AT_CLKTCK 17
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000106#endif
107
Denys Vlasenko099e5282011-02-02 00:59:35 +0100108/* TODO:
109 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
110 * specifies (for XSI-conformant systems) following default columns
111 * (l and f mark columns shown with -l and -f respectively):
112 * F l Flags (octal and additive) associated with the process (??)
113 * S l The state of the process
114 * UID f,l The user ID; the login name is printed with -f
115 * PID The process ID
116 * PPID f,l The parent process
117 * C f,l Processor utilization
118 * PRI l The priority of the process; higher numbers mean lower priority
119 * NI l Nice value
120 * ADDR l The address of the process
121 * SZ l The size in blocks of the core image of the process
122 * WCHAN l The event for which the process is waiting or sleeping
123 * STIME f Starting time of the process
124 * TTY The controlling terminal for the process
125 * TIME The cumulative execution time for the process
126 * CMD The command name; the full command line is shown with -f
127 */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000128typedef struct {
129 uint16_t width;
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200130 char name6[6];
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000131 const char *header;
132 void (*f)(char *buf, int size, const procps_status_t *ps);
133 int ps_flags;
134} ps_out_t;
135
136struct globals {
137 ps_out_t* out;
138 int out_cnt;
139 int print_header;
140 int need_flags;
141 char *buffer;
142 unsigned terminal_width;
143#if ENABLE_FEATURE_PS_TIME
144 unsigned kernel_HZ;
Denys Vlasenko17d44362013-01-26 14:21:15 +0100145 unsigned long seconds_since_boot;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000146#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100147} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200148#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000149#define out (G.out )
150#define out_cnt (G.out_cnt )
151#define print_header (G.print_header )
152#define need_flags (G.need_flags )
153#define buffer (G.buffer )
154#define terminal_width (G.terminal_width )
155#define kernel_HZ (G.kernel_HZ )
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200156#define INIT_G() do { } while (0)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000157
158#if ENABLE_FEATURE_PS_TIME
159/* for ELF executables, notes are pushed before environment and args */
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100160static uintptr_t find_elf_note(uintptr_t findme)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000161{
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100162 uintptr_t *ep = (uintptr_t *) environ;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000163
Denys Vlasenko099e5282011-02-02 00:59:35 +0100164 while (*ep++)
165 continue;
Denis Vlasenko6b404432008-01-07 16:13:14 +0000166 while (*ep) {
167 if (ep[0] == findme) {
168 return ep[1];
169 }
170 ep += 2;
171 }
172 return -1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000173}
174
175#if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
176static unsigned get_HZ_by_waiting(void)
177{
Denis Vlasenko6b404432008-01-07 16:13:14 +0000178 struct timeval tv1, tv2;
179 unsigned t1, t2, r, hz;
180 unsigned cnt = cnt; /* for compiler */
181 int diff;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000182
Denis Vlasenko6b404432008-01-07 16:13:14 +0000183 r = 0;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000184
Denis Vlasenko6b404432008-01-07 16:13:14 +0000185 /* Wait for times() to reach new tick */
186 t1 = times(NULL);
187 do {
188 t2 = times(NULL);
189 } while (t2 == t1);
190 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000191
Denis Vlasenko6b404432008-01-07 16:13:14 +0000192 do {
193 t1 = t2;
194 tv1.tv_usec = tv2.tv_usec;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000195
Denis Vlasenko6b404432008-01-07 16:13:14 +0000196 /* Wait exactly one times() tick */
197 do {
198 t2 = times(NULL);
199 } while (t2 == t1);
200 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000201
Denis Vlasenko6b404432008-01-07 16:13:14 +0000202 /* Calculate ticks per sec, rounding up to even */
203 diff = tv2.tv_usec - tv1.tv_usec;
204 if (diff <= 0) diff += 1000000;
205 hz = 1000000u / (unsigned)diff;
206 hz = (hz+1) & ~1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000207
208 /* Count how many same hz values we saw */
Denis Vlasenko6b404432008-01-07 16:13:14 +0000209 if (r != hz) {
210 r = hz;
211 cnt = 0;
212 }
213 cnt++;
214 } while (cnt < 3); /* exit if saw 3 same values */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000215
Denis Vlasenko6b404432008-01-07 16:13:14 +0000216 return r;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000217}
218#else
219static inline unsigned get_HZ_by_waiting(void)
220{
221 /* Better method? */
222 return 100;
223}
224#endif
225
226static unsigned get_kernel_HZ(void)
227{
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000228 if (kernel_HZ)
229 return kernel_HZ;
230
231 /* Works for ELF only, Linux 2.4.0+ */
232 kernel_HZ = find_elf_note(AT_CLKTCK);
233 if (kernel_HZ == (unsigned)-1)
234 kernel_HZ = get_HZ_by_waiting();
235
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100236 G.seconds_since_boot = get_uptime();
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000237
238 return kernel_HZ;
239}
240#endif
241
Denis Vlasenko94949192006-11-05 00:45:47 +0000242/* Print value to buf, max size+1 chars (including trailing '\0') */
243
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000244static void func_user(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000245{
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000246#if 1
Denis Vlasenko94949192006-11-05 00:45:47 +0000247 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000248#else
249 /* "compatible" version, but it's larger */
250 /* procps 2.18 shows numeric UID if name overflows the field */
251 /* TODO: get_cached_username() returns numeric string if
252 * user has no passwd record, we will display it
253 * left-justified here; too long usernames are shown
254 * as _right-justified_ IDs. Is it worth fixing? */
255 const char *user = get_cached_username(ps->uid);
256 if (strlen(user) <= size)
Denis Vlasenkoc85bfca2008-02-03 22:04:19 +0000257 safe_strncpy(buf, user, size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000258 else
259 sprintf(buf, "%*u", size, (unsigned)ps->uid);
260#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000261}
262
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200263static void func_group(char *buf, int size, const procps_status_t *ps)
264{
265 safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
266}
267
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000268static void func_comm(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000269{
270 safe_strncpy(buf, ps->comm, size+1);
271}
272
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200273static void func_state(char *buf, int size, const procps_status_t *ps)
Denys Vlasenko099e5282011-02-02 00:59:35 +0100274{
275 safe_strncpy(buf, ps->state, size+1);
276}
277
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000278static void func_args(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000279{
Denys Vlasenko3a0f6f22009-09-12 00:15:34 +0200280 read_cmdline(buf, size+1, ps->pid, ps->comm);
Denis Vlasenko94949192006-11-05 00:45:47 +0000281}
282
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000283static void func_pid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000284{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000285 sprintf(buf, "%*u", size, ps->pid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000286}
287
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000288static void func_ppid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000289{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000290 sprintf(buf, "%*u", size, ps->ppid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000291}
292
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000293static void func_pgid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000294{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000295 sprintf(buf, "%*u", size, ps->pgid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000296}
297
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000298static void put_lu(char *buf, int size, unsigned long u)
Denis Vlasenko94949192006-11-05 00:45:47 +0000299{
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000300 char buf4[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000301
302 /* see http://en.wikipedia.org/wiki/Tera */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200303 smart_ulltoa4(u, buf4, " mgtpezy")[0] = '\0';
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000304 sprintf(buf, "%.*s", size, buf4);
Denis Vlasenko94949192006-11-05 00:45:47 +0000305}
306
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000307static void func_vsz(char *buf, int size, const procps_status_t *ps)
308{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000309 put_lu(buf, size, ps->vsz);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000310}
311
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000312static void func_rss(char *buf, int size, const procps_status_t *ps)
313{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000314 put_lu(buf, size, ps->rss);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000315}
316
317static void func_tty(char *buf, int size, const procps_status_t *ps)
318{
Denis Vlasenko31789a82007-06-19 14:43:17 +0000319 buf[0] = '?';
320 buf[1] = '\0';
321 if (ps->tty_major) /* tty field of "0" means "no tty" */
322 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000323}
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000324
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200325#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
326
327static void func_rgroup(char *buf, int size, const procps_status_t *ps)
328{
329 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
330}
331
332static void func_ruser(char *buf, int size, const procps_status_t *ps)
333{
334 safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
335}
336
337static void func_nice(char *buf, int size, const procps_status_t *ps)
338{
339 sprintf(buf, "%*d", size, ps->niceness);
340}
341
Denys Vlasenko9e072192010-05-23 00:34:42 +0200342#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200343
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000344#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko9e072192010-05-23 00:34:42 +0200345
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000346static void func_etime(char *buf, int size, const procps_status_t *ps)
347{
348 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
349 unsigned long mm;
350 unsigned ss;
351
352 mm = ps->start_time / get_kernel_HZ();
353 /* must be after get_kernel_HZ()! */
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100354 mm = G.seconds_since_boot - mm;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000355 ss = mm % 60;
356 mm /= 60;
357 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
358}
359
360static void func_time(char *buf, int size, const procps_status_t *ps)
361{
362 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
363 unsigned long mm;
364 unsigned ss;
365
366 mm = (ps->utime + ps->stime) / get_kernel_HZ();
367 ss = mm % 60;
368 mm /= 60;
369 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
370}
Denys Vlasenko9e072192010-05-23 00:34:42 +0200371
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000372#endif
373
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000374#if ENABLE_SELINUX
375static void func_label(char *buf, int size, const procps_status_t *ps)
376{
377 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
378}
379#endif
380
Denis Vlasenko94949192006-11-05 00:45:47 +0000381/*
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000382static void func_nice(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000383{
384 ps->???
385}
386
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000387static void func_pcpu(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000388{
389}
390*/
391
Denis Vlasenko94949192006-11-05 00:45:47 +0000392static const ps_out_t out_spec[] = {
Denys Vlasenko099e5282011-02-02 00:59:35 +0100393/* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000394 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200395 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000396 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200397 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000398 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
399 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
400 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000401#if ENABLE_FEATURE_PS_TIME
402 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
403#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200404#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
405 { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
406 { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
407 { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
408// { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
409#endif
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000410#if ENABLE_FEATURE_PS_TIME
411 { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
412#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000413 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
414 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
Denys Vlasenko099e5282011-02-02 00:59:35 +0100415/* Not mandated, but useful: */
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200416 { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000417 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000418#if ENABLE_SELINUX
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000419 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000420#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000421};
422
Denis Vlasenko94949192006-11-05 00:45:47 +0000423static ps_out_t* new_out_t(void)
424{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000425 out = xrealloc_vector(out, 2, out_cnt);
426 return &out[out_cnt++];
Denis Vlasenko94949192006-11-05 00:45:47 +0000427}
428
429static const ps_out_t* find_out_spec(const char *name)
430{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000431 unsigned i;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200432 char buf[ARRAY_SIZE(out_spec)*7 + 1];
433 char *p = buf;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200434
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000435 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
Denys Vlasenko9e072192010-05-23 00:34:42 +0200436 if (strncmp(name, out_spec[i].name6, 6) == 0)
Denis Vlasenko94949192006-11-05 00:45:47 +0000437 return &out_spec[i];
Denys Vlasenko9e072192010-05-23 00:34:42 +0200438 p += sprintf(p, "%.6s,", out_spec[i].name6);
Denis Vlasenko94949192006-11-05 00:45:47 +0000439 }
Denys Vlasenko9e072192010-05-23 00:34:42 +0200440 p[-1] = '\0';
441 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
Denis Vlasenko94949192006-11-05 00:45:47 +0000442}
443
444static void parse_o(char* opt)
445{
446 ps_out_t* new;
447 // POSIX: "-o is blank- or comma-separated list" (FIXME)
448 char *comma, *equal;
449 while (1) {
450 comma = strchr(opt, ',');
451 equal = strchr(opt, '=');
452 if (comma && (!equal || equal > comma)) {
453 *comma = '\0';
454 *new_out_t() = *find_out_spec(opt);
455 *comma = ',';
456 opt = comma + 1;
457 continue;
458 }
459 break;
460 }
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000461 // opt points to last spec in comma separated list.
462 // This one can have =HEADER part.
Denis Vlasenko94949192006-11-05 00:45:47 +0000463 new = new_out_t();
464 if (equal)
465 *equal = '\0';
466 *new = *find_out_spec(opt);
467 if (equal) {
468 *equal = '=';
469 new->header = equal + 1;
470 // POSIX: the field widths shall be ... at least as wide as
471 // the header text (default or overridden value).
472 // If the header text is null, such as -o user=,
473 // the field width shall be at least as wide as the
474 // default header text
475 if (new->header[0]) {
476 new->width = strlen(new->header);
477 print_header = 1;
478 }
479 } else
480 print_header = 1;
481}
482
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200483static void alloc_line_buffer(void)
Denis Vlasenko94949192006-11-05 00:45:47 +0000484{
485 int i;
486 int width = 0;
487 for (i = 0; i < out_cnt; i++) {
Denis Vlasenko85818632007-04-19 14:47:11 +0000488 need_flags |= out[i].ps_flags;
Denis Vlasenko94949192006-11-05 00:45:47 +0000489 if (out[i].header[0]) {
490 print_header = 1;
491 }
492 width += out[i].width + 1; /* "FIELD " */
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200493 if ((int)(width - terminal_width) > 0) {
494 /* The rest does not fit on the screen */
495 //out[i].width -= (width - terminal_width - 1);
496 out_cnt = i + 1;
497 break;
498 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000499 }
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000500#if ENABLE_SELINUX
501 if (!is_selinux_enabled())
502 need_flags &= ~PSSCAN_CONTEXT;
503#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000504 buffer = xmalloc(width + 1); /* for trailing \0 */
505}
506
507static void format_header(void)
508{
509 int i;
510 ps_out_t* op;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000511 char *p;
512
Denis Vlasenko94949192006-11-05 00:45:47 +0000513 if (!print_header)
514 return;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000515 p = buffer;
Denis Vlasenko94949192006-11-05 00:45:47 +0000516 i = 0;
517 if (out_cnt) {
518 while (1) {
519 op = &out[i];
520 if (++i == out_cnt) /* do not pad last field */
521 break;
522 p += sprintf(p, "%-*s ", op->width, op->header);
523 }
524 strcpy(p, op->header);
525 }
526 printf("%.*s\n", terminal_width, buffer);
527}
528
529static void format_process(const procps_status_t *ps)
530{
531 int i, len;
532 char *p = buffer;
533 i = 0;
534 if (out_cnt) while (1) {
535 out[i].f(p, out[i].width, ps);
536 // POSIX: Any field need not be meaningful in all
537 // implementations. In such a case a hyphen ( '-' )
538 // should be output in place of the field value.
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000539 if (!p[0]) {
540 p[0] = '-';
541 p[1] = '\0';
Denis Vlasenko94949192006-11-05 00:45:47 +0000542 }
543 len = strlen(p);
544 p += len;
545 len = out[i].width - len + 1;
546 if (++i == out_cnt) /* do not pad last field */
547 break;
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000548 p += sprintf(p, "%*s", len, "");
Denis Vlasenko94949192006-11-05 00:45:47 +0000549 }
550 printf("%.*s\n", terminal_width, buffer);
551}
552
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200553#if ENABLE_SELINUX
554# define SELINUX_O_PREFIX "label,"
555# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
556#else
557# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
558#endif
559
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000560int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000561int ps_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko94949192006-11-05 00:45:47 +0000562{
563 procps_status_t *p;
564 llist_t* opt_o = NULL;
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200565 char default_o[sizeof(DEFAULT_O_STR)];
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200566 int opt;
567 enum {
568 OPT_Z = (1 << 0),
569 OPT_o = (1 << 1),
570 OPT_a = (1 << 2),
571 OPT_A = (1 << 3),
572 OPT_d = (1 << 4),
573 OPT_e = (1 << 5),
574 OPT_f = (1 << 6),
575 OPT_l = (1 << 7),
576 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
577 };
578
579 INIT_G();
Denis Vlasenko85818632007-04-19 14:47:11 +0000580
Denis Vlasenko94949192006-11-05 00:45:47 +0000581 // POSIX:
582 // -a Write information for all processes associated with terminals
583 // Implementations may omit session leaders from this list
584 // -A Write information for all processes
585 // -d Write information for all processes, except session leaders
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200586 // -e Write information for all processes (equivalent to -A)
Denis Vlasenko94949192006-11-05 00:45:47 +0000587 // -f Generate a full listing
588 // -l Generate a long listing
589 // -o col1,col2,col3=header
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000590 // Select which columns to display
Denys Vlasenko887d9632009-11-24 03:52:24 +0100591 /* We allow (and ignore) most of the above. FIXME.
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100592 * -T is picked for threads (POSIX hasn't standardized it).
Denys Vlasenko887d9632009-11-24 03:52:24 +0100593 * procps v3.2.7 supports -T and shows tids as SPID column,
594 * it also supports -L where it shows tids as LWP column.
595 */
Denis Vlasenko94949192006-11-05 00:45:47 +0000596 opt_complementary = "o::";
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200597 opt = getopt32(argv, "Zo:aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
Denis Vlasenko94949192006-11-05 00:45:47 +0000598 if (opt_o) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000599 do {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000600 parse_o(llist_pop(&opt_o));
Denis Vlasenko94949192006-11-05 00:45:47 +0000601 } while (opt_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000602 } else {
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100603 /* Below: parse_o() needs char*, NOT const char*,
604 * can't pass it constant string. Need to make a copy first.
605 */
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000606#if ENABLE_SELINUX
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200607 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000608 /* no -Z or no SELinux: do not show LABEL */
Denis Vlasenko83cea0e2008-07-16 21:11:09 +0000609 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000610 } else
611#endif
612 {
613 strcpy(default_o, DEFAULT_O_STR);
614 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000615 parse_o(default_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000616 }
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200617#if ENABLE_FEATURE_SHOW_THREADS
618 if (opt & OPT_T)
619 need_flags |= PSSCAN_TASKS;
620#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000621
Denis Vlasenko450196c2007-03-28 21:57:12 +0000622 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
623 * and such large widths */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000624 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000625 if (isatty(1)) {
Denys Vlasenko641caae2015-10-23 01:44:22 +0200626 terminal_width = get_terminal_width(0);
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000627 if (--terminal_width > MAX_WIDTH)
628 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000629 }
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200630 alloc_line_buffer();
Denis Vlasenko94949192006-11-05 00:45:47 +0000631 format_header();
632
633 p = NULL;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200634 while ((p = procps_scan(p, need_flags)) != NULL) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000635 format_process(p);
636 }
637
638 return EXIT_SUCCESS;
639}
640
641
642#else /* !ENABLE_DESKTOP */
643
644
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000645int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000646int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersenef8b6c71999-10-20 08:05:35 +0000647{
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200648 procps_status_t *p;
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200649 int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
650 | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
Denys Vlasenko39601842010-04-07 18:03:34 +0200651 unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
652 enum {
653 OPT_Z = (1 << 0) * ENABLE_SELINUX,
654 OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200655 OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG,
Denys Vlasenko39601842010-04-07 18:03:34 +0200656 };
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200657#if ENABLE_FEATURE_PS_LONG
Denys Vlasenkoc9091d82015-07-01 19:07:24 +0200658 time_t now = now; /* for compiler */
659 unsigned long uptime = uptime;
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200660#endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200661 /* If we support any options, parse argv */
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200662#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE || ENABLE_FEATURE_PS_LONG
Denys Vlasenko42be9212012-09-25 12:48:10 +0200663 int opts = 0;
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200664# if ENABLE_FEATURE_PS_WIDE
Denys Vlasenko39601842010-04-07 18:03:34 +0200665 /* -w is a bit complicated */
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200666 int w_count = 0;
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000667 opt_complementary = "-:ww";
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200668 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")
669 "w", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000670 /* if w is given once, GNU ps sets the width to 132,
671 * if w is given more than once, it is "unlimited"
672 */
Denis Vlasenko621204b2006-10-27 09:03:24 +0000673 if (w_count) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200674 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000675 } else {
Denys Vlasenko641caae2015-10-23 01:44:22 +0200676 terminal_width = get_terminal_width(0);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000677 /* Go one less... */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000678 if (--terminal_width > MAX_WIDTH)
679 terminal_width = MAX_WIDTH;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000680 }
Denys Vlasenko39601842010-04-07 18:03:34 +0200681# else
682 /* -w is not supported, only -Z and/or -T */
683 opt_complementary = "-";
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200684 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200685# endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200686
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200687# if ENABLE_SELINUX
Denys Vlasenko39601842010-04-07 18:03:34 +0200688 if ((opts & OPT_Z) && is_selinux_enabled()) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200689 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
690 | PSSCAN_STATE | PSSCAN_COMM;
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000691 puts(" PID CONTEXT STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200692 } else
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200693# endif
694 if (opts & OPT_l) {
695 psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID
696 | PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM
697 | PSSCAN_VSZ | PSSCAN_RSS;
698/* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
699 * mandates for -l:
Flemming Madsenb64bd162011-09-27 15:31:25 +0200700 * -F Flags (?)
701 * S State
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200702 * UID,PID,PPID
Flemming Madsenb64bd162011-09-27 15:31:25 +0200703 * -C CPU usage
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200704 * -PRI The priority of the process; higher numbers mean lower priority
Flemming Madsenb64bd162011-09-27 15:31:25 +0200705 * -NI Nice value
706 * -ADDR The address of the process (?)
707 * SZ The size in blocks of the core image
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200708 * -WCHAN The event for which the process is waiting or sleeping
709 * TTY
Flemming Madsenb64bd162011-09-27 15:31:25 +0200710 * TIME The cumulative execution time
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200711 * CMD
Flemming Madsenb64bd162011-09-27 15:31:25 +0200712 * We don't show fields marked with '-'.
713 * We show VSZ and RSS instead of SZ.
714 * We also show STIME (standard says that -f shows it, -l doesn't).
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200715 */
Flemming Madsenb64bd162011-09-27 15:31:25 +0200716 puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD");
Denys Vlasenko42be9212012-09-25 12:48:10 +0200717# if ENABLE_FEATURE_PS_LONG
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200718 now = time(NULL);
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200719 uptime = get_uptime();
Denys Vlasenko42be9212012-09-25 12:48:10 +0200720# endif
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200721 }
722 else {
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000723 puts(" PID USER VSZ STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200724 }
725 if (opts & OPT_T) {
726 psscan_flags |= PSSCAN_TASKS;
727 }
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200728#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200730 p = NULL;
731 while ((p = procps_scan(p, psscan_flags)) != NULL) {
Denys Vlasenko39601842010-04-07 18:03:34 +0200732 int len;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000733#if ENABLE_SELINUX
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200734 if (psscan_flags & PSSCAN_CONTEXT) {
Denis Vlasenko766f6592008-01-07 16:41:41 +0000735 len = printf("%5u %-32.32s %s ",
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000736 p->pid,
737 p->context ? p->context : "unknown",
738 p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +0000739 } else
Eric Andersen9e480452003-07-03 10:07:04 +0000740#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000741 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200742 char buf6[6];
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200743 smart_ulltoa5(p->vsz, buf6, " mgtpezy")[0] = '\0';
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200744#if ENABLE_FEATURE_PS_LONG
745 if (opts & OPT_l) {
Flemming Madsenb64bd162011-09-27 15:31:25 +0200746 char bufr[6], stime_str[6];
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200747 char tty[2 * sizeof(int)*3 + 2];
748 char *endp;
749 unsigned sut = (p->stime + p->utime) / 100;
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200750 unsigned elapsed = uptime - (p->start_time / 100);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200751 time_t start = now - elapsed;
752 struct tm *tm = localtime(&start);
753
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200754 smart_ulltoa5(p->rss, bufr, " mgtpezy")[0] = '\0';
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200755
756 if (p->tty_major == 136)
Flemming Madsenb64bd162011-09-27 15:31:25 +0200757 /* It should be pts/N, not ptsN, but N > 9
758 * will overflow field width...
759 */
760 endp = stpcpy(tty, "pts");
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200761 else
762 if (p->tty_major == 4) {
763 endp = stpcpy(tty, "tty");
764 if (p->tty_minor >= 64) {
765 p->tty_minor -= 64;
766 *endp++ = 'S';
767 }
768 }
769 else
770 endp = tty + sprintf(tty, "%d:", p->tty_major);
771 strcpy(endp, utoa(p->tty_minor));
772
Flemming Madsenb64bd162011-09-27 15:31:25 +0200773 strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm);
774 stime_str[5] = '\0';
775 // S UID PID PPID VSZ RSS TTY STIME TIME CMD
776 len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ",
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200777 p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty,
Flemming Madsenb64bd162011-09-27 15:31:25 +0200778 stime_str, sut / 3600, (sut % 3600) / 60, sut % 60);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200779 } else
780#endif
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000781 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200782 const char *user = get_cached_username(p->uid);
Denis Vlasenko766f6592008-01-07 16:41:41 +0000783 len = printf("%5u %-8.8s %s %s ",
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000784 p->pid, user, buf6, p->state);
785 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000786 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000787
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000788 {
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000789 int sz = terminal_width - len;
Denys Vlasenkoc9091d82015-07-01 19:07:24 +0200790 if (sz >= 0) {
791 char buf[sz + 1];
792 read_cmdline(buf, sz, p->pid, p->comm);
793 puts(buf);
794 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000795 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000796 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000797 if (ENABLE_FEATURE_CLEAN_UP)
798 clear_username_cache();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000799 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000800}
Denis Vlasenko94949192006-11-05 00:45:47 +0000801
Denys Vlasenko39601842010-04-07 18:03:34 +0200802#endif /* !ENABLE_DESKTOP */