blob: c65fa012a2f2c0f5721d221c89f011ad8b39d928 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenef8b6c71999-10-20 08:05:35 +00002/*
Erik Andersen246cc6d2000-03-07 07:41:42 +00003 * Mini ps implementation(s) for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Denis Vlasenko0696b8a2007-06-06 07:40:16 +00006 * Fix for SELinux Support:(c)2007 Hiroshi Shinji <shiroshi@my.email.ne.jp>
Denis Vlasenkoc85bfca2008-02-03 22:04:19 +00007 * (c)2007 Yuichi Nakamura <ynakam@hitachisoft.jp>
Eric Andersenef8b6c71999-10-20 08:05:35 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersenbdfd0d72001-10-24 05:00:29 +000010 */
11
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage:#if ENABLE_DESKTOP
13//usage:
14//usage:#define ps_trivial_usage
15//usage: "[-o COL1,COL2=HEADER]" IF_FEATURE_SHOW_THREADS(" [-T]")
16//usage:#define ps_full_usage "\n\n"
17//usage: "Show list of processes\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020018//usage: "\n -o COL1,COL2=HEADER Select columns for display"
19//usage: IF_FEATURE_SHOW_THREADS(
20//usage: "\n -T Show threads"
21//usage: )
22//usage:
23//usage:#else /* !ENABLE_DESKTOP */
24//usage:
25//usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE
26//usage:#define USAGE_PS "\nThis version of ps accepts no options"
27//usage:#else
Denys Vlasenkod7efa062011-06-05 15:07:31 +020028//usage:#define USAGE_PS ""
Pere Orga5bc8c002011-04-11 03:29:49 +020029//usage:#endif
30//usage:
31//usage:#define ps_trivial_usage
32//usage: ""
33//usage:#define ps_full_usage "\n\n"
34//usage: "Show list of processes\n"
35//usage: USAGE_PS
36//usage: IF_SELINUX(
37//usage: "\n -Z Show selinux context"
38//usage: )
39//usage: IF_FEATURE_PS_WIDE(
40//usage: "\n w Wide output"
41//usage: )
Denys Vlasenko8d9ac302011-09-26 02:56:08 +020042//usage: IF_FEATURE_PS_LONG(
43//usage: "\n l Long output"
44//usage: )
45//usage: IF_FEATURE_SHOW_THREADS(
46//usage: "\n T Show threads"
47//usage: )
Pere Orga5bc8c002011-04-11 03:29:49 +020048//usage:
49//usage:#endif /* ENABLE_DESKTOP */
50//usage:
51//usage:#define ps_example_usage
52//usage: "$ ps\n"
53//usage: " PID Uid Gid State Command\n"
54//usage: " 1 root root S init\n"
55//usage: " 2 root root S [kflushd]\n"
56//usage: " 3 root root S [kupdate]\n"
57//usage: " 4 root root S [kpiod]\n"
58//usage: " 5 root root S [kswapd]\n"
59//usage: " 742 andersen andersen S [bash]\n"
60//usage: " 743 andersen andersen S -bash\n"
61//usage: " 745 root root S [getty]\n"
62//usage: " 2990 andersen andersen R ps\n"
63
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000064#include "libbb.h"
Denys Vlasenko8d9ac302011-09-26 02:56:08 +020065#ifdef __linux__
66# include <sys/sysinfo.h>
67#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +000068
Denis Vlasenko7ce75942007-07-25 17:27:58 +000069/* Absolute maximum on output line length */
70enum { MAX_WIDTH = 2*1024 };
71
Michael Tokarevac42e3d2012-06-13 02:51:56 +020072#if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG
Denys Vlasenko6aab0612013-01-22 10:07:23 +010073static unsigned long get_uptime(void)
Michael Tokarevac42e3d2012-06-13 02:51:56 +020074{
75#ifdef __linux__
76 struct sysinfo info;
77 if (sysinfo(&info) < 0)
78 return 0;
79 return info.uptime;
80#elif 1
Denys Vlasenko6aab0612013-01-22 10:07:23 +010081 unsigned long uptime;
82 char buf[sizeof(uptime)*3 + 2];
83 /* /proc/uptime is "UPTIME_SEC.NN IDLE_SEC.NN\n"
84 * (where IDLE is cumulative over all CPUs)
85 */
Michael Tokarevac42e3d2012-06-13 02:51:56 +020086 if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
Denys Vlasenko6aab0612013-01-22 10:07:23 +010087 bb_perror_msg_and_die("can't read '%s'", "/proc/uptime");
Michael Tokarevac42e3d2012-06-13 02:51:56 +020088 buf[sizeof(buf)-1] = '\0';
Denys Vlasenko6aab0612013-01-22 10:07:23 +010089 sscanf(buf, "%lu", &uptime);
Michael Tokarevac42e3d2012-06-13 02:51:56 +020090 return uptime;
91#else
92 struct timespec ts;
93 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
94 return 0;
95 return ts.tv_sec;
96#endif
97}
98#endif
99
Denis Vlasenko94949192006-11-05 00:45:47 +0000100#if ENABLE_DESKTOP
101
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000102#include <sys/times.h> /* for times() */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000103#ifndef AT_CLKTCK
Denys Vlasenko099e5282011-02-02 00:59:35 +0100104# define AT_CLKTCK 17
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000105#endif
106
Denys Vlasenko099e5282011-02-02 00:59:35 +0100107/* TODO:
108 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
109 * specifies (for XSI-conformant systems) following default columns
110 * (l and f mark columns shown with -l and -f respectively):
111 * F l Flags (octal and additive) associated with the process (??)
112 * S l The state of the process
113 * UID f,l The user ID; the login name is printed with -f
114 * PID The process ID
115 * PPID f,l The parent process
116 * C f,l Processor utilization
117 * PRI l The priority of the process; higher numbers mean lower priority
118 * NI l Nice value
119 * ADDR l The address of the process
120 * SZ l The size in blocks of the core image of the process
121 * WCHAN l The event for which the process is waiting or sleeping
122 * STIME f Starting time of the process
123 * TTY The controlling terminal for the process
124 * TIME The cumulative execution time for the process
125 * CMD The command name; the full command line is shown with -f
126 */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000127typedef struct {
128 uint16_t width;
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200129 char name6[6];
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000130 const char *header;
131 void (*f)(char *buf, int size, const procps_status_t *ps);
132 int ps_flags;
133} ps_out_t;
134
135struct globals {
136 ps_out_t* out;
137 int out_cnt;
138 int print_header;
139 int need_flags;
140 char *buffer;
141 unsigned terminal_width;
142#if ENABLE_FEATURE_PS_TIME
143 unsigned kernel_HZ;
Denys Vlasenko17d44362013-01-26 14:21:15 +0100144 unsigned long seconds_since_boot;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000145#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100146} FIX_ALIASING;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000147#define G (*(struct globals*)&bb_common_bufsiz1)
148#define out (G.out )
149#define out_cnt (G.out_cnt )
150#define print_header (G.print_header )
151#define need_flags (G.need_flags )
152#define buffer (G.buffer )
153#define terminal_width (G.terminal_width )
154#define kernel_HZ (G.kernel_HZ )
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200155#define INIT_G() do { } while (0)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000156
157#if ENABLE_FEATURE_PS_TIME
158/* for ELF executables, notes are pushed before environment and args */
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100159static uintptr_t find_elf_note(uintptr_t findme)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000160{
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100161 uintptr_t *ep = (uintptr_t *) environ;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000162
Denys Vlasenko099e5282011-02-02 00:59:35 +0100163 while (*ep++)
164 continue;
Denis Vlasenko6b404432008-01-07 16:13:14 +0000165 while (*ep) {
166 if (ep[0] == findme) {
167 return ep[1];
168 }
169 ep += 2;
170 }
171 return -1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000172}
173
174#if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
175static unsigned get_HZ_by_waiting(void)
176{
Denis Vlasenko6b404432008-01-07 16:13:14 +0000177 struct timeval tv1, tv2;
178 unsigned t1, t2, r, hz;
179 unsigned cnt = cnt; /* for compiler */
180 int diff;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000181
Denis Vlasenko6b404432008-01-07 16:13:14 +0000182 r = 0;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000183
Denis Vlasenko6b404432008-01-07 16:13:14 +0000184 /* Wait for times() to reach new tick */
185 t1 = times(NULL);
186 do {
187 t2 = times(NULL);
188 } while (t2 == t1);
189 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000190
Denis Vlasenko6b404432008-01-07 16:13:14 +0000191 do {
192 t1 = t2;
193 tv1.tv_usec = tv2.tv_usec;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000194
Denis Vlasenko6b404432008-01-07 16:13:14 +0000195 /* Wait exactly one times() tick */
196 do {
197 t2 = times(NULL);
198 } while (t2 == t1);
199 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000200
Denis Vlasenko6b404432008-01-07 16:13:14 +0000201 /* Calculate ticks per sec, rounding up to even */
202 diff = tv2.tv_usec - tv1.tv_usec;
203 if (diff <= 0) diff += 1000000;
204 hz = 1000000u / (unsigned)diff;
205 hz = (hz+1) & ~1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000206
207 /* Count how many same hz values we saw */
Denis Vlasenko6b404432008-01-07 16:13:14 +0000208 if (r != hz) {
209 r = hz;
210 cnt = 0;
211 }
212 cnt++;
213 } while (cnt < 3); /* exit if saw 3 same values */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000214
Denis Vlasenko6b404432008-01-07 16:13:14 +0000215 return r;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000216}
217#else
218static inline unsigned get_HZ_by_waiting(void)
219{
220 /* Better method? */
221 return 100;
222}
223#endif
224
225static unsigned get_kernel_HZ(void)
226{
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000227 if (kernel_HZ)
228 return kernel_HZ;
229
230 /* Works for ELF only, Linux 2.4.0+ */
231 kernel_HZ = find_elf_note(AT_CLKTCK);
232 if (kernel_HZ == (unsigned)-1)
233 kernel_HZ = get_HZ_by_waiting();
234
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100235 G.seconds_since_boot = get_uptime();
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000236
237 return kernel_HZ;
238}
239#endif
240
Denis Vlasenko94949192006-11-05 00:45:47 +0000241/* Print value to buf, max size+1 chars (including trailing '\0') */
242
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000243static void func_user(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000244{
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000245#if 1
Denis Vlasenko94949192006-11-05 00:45:47 +0000246 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000247#else
248 /* "compatible" version, but it's larger */
249 /* procps 2.18 shows numeric UID if name overflows the field */
250 /* TODO: get_cached_username() returns numeric string if
251 * user has no passwd record, we will display it
252 * left-justified here; too long usernames are shown
253 * as _right-justified_ IDs. Is it worth fixing? */
254 const char *user = get_cached_username(ps->uid);
255 if (strlen(user) <= size)
Denis Vlasenkoc85bfca2008-02-03 22:04:19 +0000256 safe_strncpy(buf, user, size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000257 else
258 sprintf(buf, "%*u", size, (unsigned)ps->uid);
259#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000260}
261
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200262static void func_group(char *buf, int size, const procps_status_t *ps)
263{
264 safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
265}
266
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000267static void func_comm(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000268{
269 safe_strncpy(buf, ps->comm, size+1);
270}
271
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200272static void func_state(char *buf, int size, const procps_status_t *ps)
Denys Vlasenko099e5282011-02-02 00:59:35 +0100273{
274 safe_strncpy(buf, ps->state, size+1);
275}
276
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000277static void func_args(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000278{
Denys Vlasenko3a0f6f22009-09-12 00:15:34 +0200279 read_cmdline(buf, size+1, ps->pid, ps->comm);
Denis Vlasenko94949192006-11-05 00:45:47 +0000280}
281
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000282static void func_pid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000283{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000284 sprintf(buf, "%*u", size, ps->pid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000285}
286
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000287static void func_ppid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000288{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000289 sprintf(buf, "%*u", size, ps->ppid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000290}
291
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000292static void func_pgid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000293{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000294 sprintf(buf, "%*u", size, ps->pgid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000295}
296
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000297static void put_lu(char *buf, int size, unsigned long u)
Denis Vlasenko94949192006-11-05 00:45:47 +0000298{
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000299 char buf4[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000300
301 /* see http://en.wikipedia.org/wiki/Tera */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200302 smart_ulltoa4(u, buf4, " mgtpezy")[0] = '\0';
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000303 sprintf(buf, "%.*s", size, buf4);
Denis Vlasenko94949192006-11-05 00:45:47 +0000304}
305
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000306static void func_vsz(char *buf, int size, const procps_status_t *ps)
307{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000308 put_lu(buf, size, ps->vsz);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000309}
310
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000311static void func_rss(char *buf, int size, const procps_status_t *ps)
312{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000313 put_lu(buf, size, ps->rss);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000314}
315
316static void func_tty(char *buf, int size, const procps_status_t *ps)
317{
Denis Vlasenko31789a82007-06-19 14:43:17 +0000318 buf[0] = '?';
319 buf[1] = '\0';
320 if (ps->tty_major) /* tty field of "0" means "no tty" */
321 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000322}
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000323
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200324#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
325
326static void func_rgroup(char *buf, int size, const procps_status_t *ps)
327{
328 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
329}
330
331static void func_ruser(char *buf, int size, const procps_status_t *ps)
332{
333 safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
334}
335
336static void func_nice(char *buf, int size, const procps_status_t *ps)
337{
338 sprintf(buf, "%*d", size, ps->niceness);
339}
340
Denys Vlasenko9e072192010-05-23 00:34:42 +0200341#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200342
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000343#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko9e072192010-05-23 00:34:42 +0200344
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000345static void func_etime(char *buf, int size, const procps_status_t *ps)
346{
347 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
348 unsigned long mm;
349 unsigned ss;
350
351 mm = ps->start_time / get_kernel_HZ();
352 /* must be after get_kernel_HZ()! */
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100353 mm = G.seconds_since_boot - mm;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000354 ss = mm % 60;
355 mm /= 60;
356 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
357}
358
359static void func_time(char *buf, int size, const procps_status_t *ps)
360{
361 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
362 unsigned long mm;
363 unsigned ss;
364
365 mm = (ps->utime + ps->stime) / get_kernel_HZ();
366 ss = mm % 60;
367 mm /= 60;
368 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
369}
Denys Vlasenko9e072192010-05-23 00:34:42 +0200370
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000371#endif
372
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000373#if ENABLE_SELINUX
374static void func_label(char *buf, int size, const procps_status_t *ps)
375{
376 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
377}
378#endif
379
Denis Vlasenko94949192006-11-05 00:45:47 +0000380/*
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000381static void func_nice(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000382{
383 ps->???
384}
385
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000386static void func_pcpu(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000387{
388}
389*/
390
Denis Vlasenko94949192006-11-05 00:45:47 +0000391static const ps_out_t out_spec[] = {
Denys Vlasenko099e5282011-02-02 00:59:35 +0100392/* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000393 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200394 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000395 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200396 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000397 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
398 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
399 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000400#if ENABLE_FEATURE_PS_TIME
401 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
402#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200403#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
404 { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
405 { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
406 { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
407// { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
408#endif
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000409#if ENABLE_FEATURE_PS_TIME
410 { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
411#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000412 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
413 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
Denys Vlasenko099e5282011-02-02 00:59:35 +0100414/* Not mandated, but useful: */
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200415 { 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000416 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000417#if ENABLE_SELINUX
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000418 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000419#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000420};
421
Denis Vlasenko94949192006-11-05 00:45:47 +0000422static ps_out_t* new_out_t(void)
423{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000424 out = xrealloc_vector(out, 2, out_cnt);
425 return &out[out_cnt++];
Denis Vlasenko94949192006-11-05 00:45:47 +0000426}
427
428static const ps_out_t* find_out_spec(const char *name)
429{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000430 unsigned i;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200431 char buf[ARRAY_SIZE(out_spec)*7 + 1];
432 char *p = buf;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200433
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000434 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
Denys Vlasenko9e072192010-05-23 00:34:42 +0200435 if (strncmp(name, out_spec[i].name6, 6) == 0)
Denis Vlasenko94949192006-11-05 00:45:47 +0000436 return &out_spec[i];
Denys Vlasenko9e072192010-05-23 00:34:42 +0200437 p += sprintf(p, "%.6s,", out_spec[i].name6);
Denis Vlasenko94949192006-11-05 00:45:47 +0000438 }
Denys Vlasenko9e072192010-05-23 00:34:42 +0200439 p[-1] = '\0';
440 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
Denis Vlasenko94949192006-11-05 00:45:47 +0000441}
442
443static void parse_o(char* opt)
444{
445 ps_out_t* new;
446 // POSIX: "-o is blank- or comma-separated list" (FIXME)
447 char *comma, *equal;
448 while (1) {
449 comma = strchr(opt, ',');
450 equal = strchr(opt, '=');
451 if (comma && (!equal || equal > comma)) {
452 *comma = '\0';
453 *new_out_t() = *find_out_spec(opt);
454 *comma = ',';
455 opt = comma + 1;
456 continue;
457 }
458 break;
459 }
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000460 // opt points to last spec in comma separated list.
461 // This one can have =HEADER part.
Denis Vlasenko94949192006-11-05 00:45:47 +0000462 new = new_out_t();
463 if (equal)
464 *equal = '\0';
465 *new = *find_out_spec(opt);
466 if (equal) {
467 *equal = '=';
468 new->header = equal + 1;
469 // POSIX: the field widths shall be ... at least as wide as
470 // the header text (default or overridden value).
471 // If the header text is null, such as -o user=,
472 // the field width shall be at least as wide as the
473 // default header text
474 if (new->header[0]) {
475 new->width = strlen(new->header);
476 print_header = 1;
477 }
478 } else
479 print_header = 1;
480}
481
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200482static void alloc_line_buffer(void)
Denis Vlasenko94949192006-11-05 00:45:47 +0000483{
484 int i;
485 int width = 0;
486 for (i = 0; i < out_cnt; i++) {
Denis Vlasenko85818632007-04-19 14:47:11 +0000487 need_flags |= out[i].ps_flags;
Denis Vlasenko94949192006-11-05 00:45:47 +0000488 if (out[i].header[0]) {
489 print_header = 1;
490 }
491 width += out[i].width + 1; /* "FIELD " */
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200492 if ((int)(width - terminal_width) > 0) {
493 /* The rest does not fit on the screen */
494 //out[i].width -= (width - terminal_width - 1);
495 out_cnt = i + 1;
496 break;
497 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000498 }
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000499#if ENABLE_SELINUX
500 if (!is_selinux_enabled())
501 need_flags &= ~PSSCAN_CONTEXT;
502#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000503 buffer = xmalloc(width + 1); /* for trailing \0 */
504}
505
506static void format_header(void)
507{
508 int i;
509 ps_out_t* op;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000510 char *p;
511
Denis Vlasenko94949192006-11-05 00:45:47 +0000512 if (!print_header)
513 return;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000514 p = buffer;
Denis Vlasenko94949192006-11-05 00:45:47 +0000515 i = 0;
516 if (out_cnt) {
517 while (1) {
518 op = &out[i];
519 if (++i == out_cnt) /* do not pad last field */
520 break;
521 p += sprintf(p, "%-*s ", op->width, op->header);
522 }
523 strcpy(p, op->header);
524 }
525 printf("%.*s\n", terminal_width, buffer);
526}
527
528static void format_process(const procps_status_t *ps)
529{
530 int i, len;
531 char *p = buffer;
532 i = 0;
533 if (out_cnt) while (1) {
534 out[i].f(p, out[i].width, ps);
535 // POSIX: Any field need not be meaningful in all
536 // implementations. In such a case a hyphen ( '-' )
537 // should be output in place of the field value.
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000538 if (!p[0]) {
539 p[0] = '-';
540 p[1] = '\0';
Denis Vlasenko94949192006-11-05 00:45:47 +0000541 }
542 len = strlen(p);
543 p += len;
544 len = out[i].width - len + 1;
545 if (++i == out_cnt) /* do not pad last field */
546 break;
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000547 p += sprintf(p, "%*s", len, "");
Denis Vlasenko94949192006-11-05 00:45:47 +0000548 }
549 printf("%.*s\n", terminal_width, buffer);
550}
551
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200552#if ENABLE_SELINUX
553# define SELINUX_O_PREFIX "label,"
554# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
555#else
556# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
557#endif
558
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000559int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000560int ps_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko94949192006-11-05 00:45:47 +0000561{
562 procps_status_t *p;
563 llist_t* opt_o = NULL;
Denys Vlasenko12ac6282011-06-18 15:49:47 +0200564 char default_o[sizeof(DEFAULT_O_STR)];
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200565 int opt;
566 enum {
567 OPT_Z = (1 << 0),
568 OPT_o = (1 << 1),
569 OPT_a = (1 << 2),
570 OPT_A = (1 << 3),
571 OPT_d = (1 << 4),
572 OPT_e = (1 << 5),
573 OPT_f = (1 << 6),
574 OPT_l = (1 << 7),
575 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
576 };
577
578 INIT_G();
Denis Vlasenko85818632007-04-19 14:47:11 +0000579
Denis Vlasenko94949192006-11-05 00:45:47 +0000580 // POSIX:
581 // -a Write information for all processes associated with terminals
582 // Implementations may omit session leaders from this list
583 // -A Write information for all processes
584 // -d Write information for all processes, except session leaders
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200585 // -e Write information for all processes (equivalent to -A)
Denis Vlasenko94949192006-11-05 00:45:47 +0000586 // -f Generate a full listing
587 // -l Generate a long listing
588 // -o col1,col2,col3=header
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000589 // Select which columns to display
Denys Vlasenko887d9632009-11-24 03:52:24 +0100590 /* We allow (and ignore) most of the above. FIXME.
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100591 * -T is picked for threads (POSIX hasn't standardized it).
Denys Vlasenko887d9632009-11-24 03:52:24 +0100592 * procps v3.2.7 supports -T and shows tids as SPID column,
593 * it also supports -L where it shows tids as LWP column.
594 */
Denis Vlasenko94949192006-11-05 00:45:47 +0000595 opt_complementary = "o::";
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200596 opt = getopt32(argv, "Zo:aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
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)) {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000625 get_terminal_width_height(0, &terminal_width, NULL);
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
657 time_t now = now;
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100658 unsigned long 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;
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000666 opt_complementary = "-:ww";
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200667 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")
668 "w", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000669 /* if w is given once, GNU ps sets the width to 132,
670 * if w is given more than once, it is "unlimited"
671 */
Denis Vlasenko621204b2006-10-27 09:03:24 +0000672 if (w_count) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200673 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000674 } else {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000675 get_terminal_width_height(0, &terminal_width, NULL);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000676 /* Go one less... */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000677 if (--terminal_width > MAX_WIDTH)
678 terminal_width = MAX_WIDTH;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000679 }
Denys Vlasenko39601842010-04-07 18:03:34 +0200680# else
681 /* -w is not supported, only -Z and/or -T */
682 opt_complementary = "-";
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200683 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200684# endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200685
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200686# if ENABLE_SELINUX
Denys Vlasenko39601842010-04-07 18:03:34 +0200687 if ((opts & OPT_Z) && is_selinux_enabled()) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200688 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
689 | PSSCAN_STATE | PSSCAN_COMM;
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000690 puts(" PID CONTEXT STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200691 } else
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200692# endif
693 if (opts & OPT_l) {
694 psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID
695 | PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM
696 | PSSCAN_VSZ | PSSCAN_RSS;
697/* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
698 * mandates for -l:
Flemming Madsenb64bd162011-09-27 15:31:25 +0200699 * -F Flags (?)
700 * S State
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200701 * UID,PID,PPID
Flemming Madsenb64bd162011-09-27 15:31:25 +0200702 * -C CPU usage
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200703 * -PRI The priority of the process; higher numbers mean lower priority
Flemming Madsenb64bd162011-09-27 15:31:25 +0200704 * -NI Nice value
705 * -ADDR The address of the process (?)
706 * SZ The size in blocks of the core image
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200707 * -WCHAN The event for which the process is waiting or sleeping
708 * TTY
Flemming Madsenb64bd162011-09-27 15:31:25 +0200709 * TIME The cumulative execution time
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200710 * CMD
Flemming Madsenb64bd162011-09-27 15:31:25 +0200711 * We don't show fields marked with '-'.
712 * We show VSZ and RSS instead of SZ.
713 * We also show STIME (standard says that -f shows it, -l doesn't).
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200714 */
Flemming Madsenb64bd162011-09-27 15:31:25 +0200715 puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD");
Denys Vlasenko42be9212012-09-25 12:48:10 +0200716# if ENABLE_FEATURE_PS_LONG
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200717 now = time(NULL);
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200718 uptime = get_uptime();
Denys Vlasenko42be9212012-09-25 12:48:10 +0200719# endif
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200720 }
721 else {
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000722 puts(" PID USER VSZ STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200723 }
724 if (opts & OPT_T) {
725 psscan_flags |= PSSCAN_TASKS;
726 }
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200727#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000728
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200729 p = NULL;
730 while ((p = procps_scan(p, psscan_flags)) != NULL) {
Denys Vlasenko39601842010-04-07 18:03:34 +0200731 int len;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000732#if ENABLE_SELINUX
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200733 if (psscan_flags & PSSCAN_CONTEXT) {
Denis Vlasenko766f6592008-01-07 16:41:41 +0000734 len = printf("%5u %-32.32s %s ",
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000735 p->pid,
736 p->context ? p->context : "unknown",
737 p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +0000738 } else
Eric Andersen9e480452003-07-03 10:07:04 +0000739#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000740 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200741 char buf6[6];
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200742 smart_ulltoa5(p->vsz, buf6, " mgtpezy")[0] = '\0';
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200743#if ENABLE_FEATURE_PS_LONG
744 if (opts & OPT_l) {
Flemming Madsenb64bd162011-09-27 15:31:25 +0200745 char bufr[6], stime_str[6];
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200746 char tty[2 * sizeof(int)*3 + 2];
747 char *endp;
748 unsigned sut = (p->stime + p->utime) / 100;
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200749 unsigned elapsed = uptime - (p->start_time / 100);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200750 time_t start = now - elapsed;
751 struct tm *tm = localtime(&start);
752
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200753 smart_ulltoa5(p->rss, bufr, " mgtpezy")[0] = '\0';
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200754
755 if (p->tty_major == 136)
Flemming Madsenb64bd162011-09-27 15:31:25 +0200756 /* It should be pts/N, not ptsN, but N > 9
757 * will overflow field width...
758 */
759 endp = stpcpy(tty, "pts");
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200760 else
761 if (p->tty_major == 4) {
762 endp = stpcpy(tty, "tty");
763 if (p->tty_minor >= 64) {
764 p->tty_minor -= 64;
765 *endp++ = 'S';
766 }
767 }
768 else
769 endp = tty + sprintf(tty, "%d:", p->tty_major);
770 strcpy(endp, utoa(p->tty_minor));
771
Flemming Madsenb64bd162011-09-27 15:31:25 +0200772 strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm);
773 stime_str[5] = '\0';
774 // S UID PID PPID VSZ RSS TTY STIME TIME CMD
775 len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ",
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200776 p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty,
Flemming Madsenb64bd162011-09-27 15:31:25 +0200777 stime_str, sut / 3600, (sut % 3600) / 60, sut % 60);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200778 } else
779#endif
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000780 {
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200781 const char *user = get_cached_username(p->uid);
Denis Vlasenko766f6592008-01-07 16:41:41 +0000782 len = printf("%5u %-8.8s %s %s ",
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000783 p->pid, user, buf6, p->state);
784 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000785 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000786
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000787 {
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000788 int sz = terminal_width - len;
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000789 char buf[sz + 1];
790 read_cmdline(buf, sz, p->pid, p->comm);
791 puts(buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000792 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000793 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000794 if (ENABLE_FEATURE_CLEAN_UP)
795 clear_username_cache();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000796 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000797}
Denis Vlasenko94949192006-11-05 00:45:47 +0000798
Denys Vlasenko39601842010-04-07 18:03:34 +0200799#endif /* !ENABLE_DESKTOP */