blob: 89cadad007d18f7f1645d72e1b9f2664974e2ab5 [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 */
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000302 smart_ulltoa4(u, buf4, " mgtpezy");
303 buf4[4] = '\0';
304 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)) {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000626 get_terminal_width_height(0, &terminal_width, NULL);
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
658 time_t now = now;
Denys Vlasenko6aab0612013-01-22 10:07:23 +0100659 unsigned long 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 {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000676 get_terminal_width_height(0, &terminal_width, NULL);
"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];
743 smart_ulltoa5(p->vsz, buf6, " mgtpezy");
744 buf6[5] = '\0';
745#if ENABLE_FEATURE_PS_LONG
746 if (opts & OPT_l) {
Flemming Madsenb64bd162011-09-27 15:31:25 +0200747 char bufr[6], stime_str[6];
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200748 char tty[2 * sizeof(int)*3 + 2];
749 char *endp;
750 unsigned sut = (p->stime + p->utime) / 100;
Michael Tokarevac42e3d2012-06-13 02:51:56 +0200751 unsigned elapsed = uptime - (p->start_time / 100);
Denys Vlasenko8d9ac302011-09-26 02:56:08 +0200752 time_t start = now - elapsed;
753 struct tm *tm = localtime(&start);
754
755 smart_ulltoa5(p->rss, bufr, " mgtpezy");
756 bufr[5] = '\0';
757
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;
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000792 char buf[sz + 1];
793 read_cmdline(buf, sz, p->pid, p->comm);
794 puts(buf);
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 */