blob: 48b55a7858eff03b30f8ae8c388ea4cba252a9f5 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Erik Andersen2ac2fae2000-03-07 23:32:17 +000013
Denis Vlasenko7ce75942007-07-25 17:27:58 +000014/* Absolute maximum on output line length */
15enum { MAX_WIDTH = 2*1024 };
16
Denis Vlasenko94949192006-11-05 00:45:47 +000017#if ENABLE_DESKTOP
18
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000019#include <sys/times.h> /* for times() */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000020#ifndef AT_CLKTCK
21#define AT_CLKTCK 17
22#endif
23
24
25#if ENABLE_SELINUX
Denis Vlasenko83cea0e2008-07-16 21:11:09 +000026#define SELINUX_O_PREFIX "label,"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000027#define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000028#else
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000029#define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000030#endif
31
32typedef struct {
33 uint16_t width;
Denys Vlasenkofca70a82009-07-03 22:16:17 +020034 char name6[6];
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000035 const char *header;
36 void (*f)(char *buf, int size, const procps_status_t *ps);
37 int ps_flags;
38} ps_out_t;
39
40struct globals {
41 ps_out_t* out;
42 int out_cnt;
43 int print_header;
44 int need_flags;
45 char *buffer;
46 unsigned terminal_width;
47#if ENABLE_FEATURE_PS_TIME
48 unsigned kernel_HZ;
49 unsigned long long seconds_since_boot;
50#endif
51 char default_o[sizeof(DEFAULT_O_STR)];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010052} FIX_ALIASING;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000053#define G (*(struct globals*)&bb_common_bufsiz1)
54#define out (G.out )
55#define out_cnt (G.out_cnt )
56#define print_header (G.print_header )
57#define need_flags (G.need_flags )
58#define buffer (G.buffer )
59#define terminal_width (G.terminal_width )
60#define kernel_HZ (G.kernel_HZ )
61#define seconds_since_boot (G.seconds_since_boot)
62#define default_o (G.default_o )
Denys Vlasenkob410d4a2009-09-19 22:29:42 +020063#define INIT_G() do { } while (0)
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000064
65#if ENABLE_FEATURE_PS_TIME
66/* for ELF executables, notes are pushed before environment and args */
67static ptrdiff_t find_elf_note(ptrdiff_t findme)
68{
Denis Vlasenko6b404432008-01-07 16:13:14 +000069 ptrdiff_t *ep = (ptrdiff_t *) environ;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000070
Denis Vlasenko6b404432008-01-07 16:13:14 +000071 while (*ep++);
72 while (*ep) {
73 if (ep[0] == findme) {
74 return ep[1];
75 }
76 ep += 2;
77 }
78 return -1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000079}
80
81#if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
82static unsigned get_HZ_by_waiting(void)
83{
Denis Vlasenko6b404432008-01-07 16:13:14 +000084 struct timeval tv1, tv2;
85 unsigned t1, t2, r, hz;
86 unsigned cnt = cnt; /* for compiler */
87 int diff;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000088
Denis Vlasenko6b404432008-01-07 16:13:14 +000089 r = 0;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000090
Denis Vlasenko6b404432008-01-07 16:13:14 +000091 /* Wait for times() to reach new tick */
92 t1 = times(NULL);
93 do {
94 t2 = times(NULL);
95 } while (t2 == t1);
96 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000097
Denis Vlasenko6b404432008-01-07 16:13:14 +000098 do {
99 t1 = t2;
100 tv1.tv_usec = tv2.tv_usec;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000101
Denis Vlasenko6b404432008-01-07 16:13:14 +0000102 /* Wait exactly one times() tick */
103 do {
104 t2 = times(NULL);
105 } while (t2 == t1);
106 gettimeofday(&tv2, NULL);
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000107
Denis Vlasenko6b404432008-01-07 16:13:14 +0000108 /* Calculate ticks per sec, rounding up to even */
109 diff = tv2.tv_usec - tv1.tv_usec;
110 if (diff <= 0) diff += 1000000;
111 hz = 1000000u / (unsigned)diff;
112 hz = (hz+1) & ~1;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000113
114 /* Count how many same hz values we saw */
Denis Vlasenko6b404432008-01-07 16:13:14 +0000115 if (r != hz) {
116 r = hz;
117 cnt = 0;
118 }
119 cnt++;
120 } while (cnt < 3); /* exit if saw 3 same values */
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000121
Denis Vlasenko6b404432008-01-07 16:13:14 +0000122 return r;
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000123}
124#else
125static inline unsigned get_HZ_by_waiting(void)
126{
127 /* Better method? */
128 return 100;
129}
130#endif
131
132static unsigned get_kernel_HZ(void)
133{
134 //char buf[64];
135 struct sysinfo info;
136
137 if (kernel_HZ)
138 return kernel_HZ;
139
140 /* Works for ELF only, Linux 2.4.0+ */
141 kernel_HZ = find_elf_note(AT_CLKTCK);
142 if (kernel_HZ == (unsigned)-1)
143 kernel_HZ = get_HZ_by_waiting();
144
Denys Vlasenkofe360482010-05-04 14:59:01 +0200145 //if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100146 // bb_perror_msg_and_die("can't read %s", "/proc/uptime");
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000147 //buf[sizeof(buf)-1] = '\0';
148 ///sscanf(buf, "%llu", &seconds_since_boot);
149 sysinfo(&info);
150 seconds_since_boot = info.uptime;
151
152 return kernel_HZ;
153}
154#endif
155
Denis Vlasenko94949192006-11-05 00:45:47 +0000156/* Print value to buf, max size+1 chars (including trailing '\0') */
157
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000158static void func_user(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000159{
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000160#if 1
Denis Vlasenko94949192006-11-05 00:45:47 +0000161 safe_strncpy(buf, get_cached_username(ps->uid), size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000162#else
163 /* "compatible" version, but it's larger */
164 /* procps 2.18 shows numeric UID if name overflows the field */
165 /* TODO: get_cached_username() returns numeric string if
166 * user has no passwd record, we will display it
167 * left-justified here; too long usernames are shown
168 * as _right-justified_ IDs. Is it worth fixing? */
169 const char *user = get_cached_username(ps->uid);
170 if (strlen(user) <= size)
Denis Vlasenkoc85bfca2008-02-03 22:04:19 +0000171 safe_strncpy(buf, user, size+1);
Denis Vlasenko5de754a2008-02-02 16:43:51 +0000172 else
173 sprintf(buf, "%*u", size, (unsigned)ps->uid);
174#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000175}
176
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200177static void func_group(char *buf, int size, const procps_status_t *ps)
178{
179 safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
180}
181
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000182static void func_comm(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000183{
184 safe_strncpy(buf, ps->comm, size+1);
185}
186
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000187static void func_args(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000188{
Denys Vlasenko3a0f6f22009-09-12 00:15:34 +0200189 read_cmdline(buf, size+1, ps->pid, ps->comm);
Denis Vlasenko94949192006-11-05 00:45:47 +0000190}
191
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000192static void func_pid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000193{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000194 sprintf(buf, "%*u", size, ps->pid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000195}
196
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000197static void func_ppid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000198{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000199 sprintf(buf, "%*u", size, ps->ppid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000200}
201
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000202static void func_pgid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000203{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000204 sprintf(buf, "%*u", size, ps->pgid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000205}
206
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000207static void put_lu(char *buf, int size, unsigned long u)
Denis Vlasenko94949192006-11-05 00:45:47 +0000208{
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000209 char buf4[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000210
211 /* see http://en.wikipedia.org/wiki/Tera */
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000212 smart_ulltoa4(u, buf4, " mgtpezy");
213 buf4[4] = '\0';
214 sprintf(buf, "%.*s", size, buf4);
Denis Vlasenko94949192006-11-05 00:45:47 +0000215}
216
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000217static void func_vsz(char *buf, int size, const procps_status_t *ps)
218{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000219 put_lu(buf, size, ps->vsz);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000220}
221
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000222static void func_rss(char *buf, int size, const procps_status_t *ps)
223{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000224 put_lu(buf, size, ps->rss);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000225}
226
227static void func_tty(char *buf, int size, const procps_status_t *ps)
228{
Denis Vlasenko31789a82007-06-19 14:43:17 +0000229 buf[0] = '?';
230 buf[1] = '\0';
231 if (ps->tty_major) /* tty field of "0" means "no tty" */
232 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000233}
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000234
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200235#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
236
237static void func_rgroup(char *buf, int size, const procps_status_t *ps)
238{
239 safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
240}
241
242static void func_ruser(char *buf, int size, const procps_status_t *ps)
243{
244 safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
245}
246
247static void func_nice(char *buf, int size, const procps_status_t *ps)
248{
249 sprintf(buf, "%*d", size, ps->niceness);
250}
251
Denys Vlasenko9e072192010-05-23 00:34:42 +0200252#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200253
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000254#if ENABLE_FEATURE_PS_TIME
Denys Vlasenko9e072192010-05-23 00:34:42 +0200255
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000256static void func_etime(char *buf, int size, const procps_status_t *ps)
257{
258 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
259 unsigned long mm;
260 unsigned ss;
261
262 mm = ps->start_time / get_kernel_HZ();
263 /* must be after get_kernel_HZ()! */
264 mm = seconds_since_boot - mm;
265 ss = mm % 60;
266 mm /= 60;
267 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
268}
269
270static void func_time(char *buf, int size, const procps_status_t *ps)
271{
272 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
273 unsigned long mm;
274 unsigned ss;
275
276 mm = (ps->utime + ps->stime) / get_kernel_HZ();
277 ss = mm % 60;
278 mm /= 60;
279 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
280}
Denys Vlasenko9e072192010-05-23 00:34:42 +0200281
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000282#endif
283
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000284#if ENABLE_SELINUX
285static void func_label(char *buf, int size, const procps_status_t *ps)
286{
287 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
288}
289#endif
290
Denis Vlasenko94949192006-11-05 00:45:47 +0000291/*
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000292static void func_nice(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000293{
294 ps->???
295}
296
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000297static void func_pcpu(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000298{
299}
300*/
301
Denis Vlasenko94949192006-11-05 00:45:47 +0000302static const ps_out_t out_spec[] = {
303// Mandated by POSIX:
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000304 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200305 { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000306 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200307 { MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000308 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
309 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
310 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000311#if ENABLE_FEATURE_PS_TIME
312 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
313#endif
Denys Vlasenkofca70a82009-07-03 22:16:17 +0200314#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
315 { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
316 { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
317 { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
318// { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
319#endif
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000320#if ENABLE_FEATURE_PS_TIME
321 { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
322#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000323 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
324 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000325// Not mandated by POSIX, but useful:
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000326 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000327#if ENABLE_SELINUX
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000328 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000329#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000330};
331
Denis Vlasenko94949192006-11-05 00:45:47 +0000332static ps_out_t* new_out_t(void)
333{
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000334 out = xrealloc_vector(out, 2, out_cnt);
335 return &out[out_cnt++];
Denis Vlasenko94949192006-11-05 00:45:47 +0000336}
337
338static const ps_out_t* find_out_spec(const char *name)
339{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000340 unsigned i;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200341 char buf[ARRAY_SIZE(out_spec)*7 + 1];
342 char *p = buf;
Denys Vlasenko9e072192010-05-23 00:34:42 +0200343
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000344 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
Denys Vlasenko9e072192010-05-23 00:34:42 +0200345 if (strncmp(name, out_spec[i].name6, 6) == 0)
Denis Vlasenko94949192006-11-05 00:45:47 +0000346 return &out_spec[i];
Denys Vlasenko9e072192010-05-23 00:34:42 +0200347 p += sprintf(p, "%.6s,", out_spec[i].name6);
Denis Vlasenko94949192006-11-05 00:45:47 +0000348 }
Denys Vlasenko9e072192010-05-23 00:34:42 +0200349 p[-1] = '\0';
350 bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
Denis Vlasenko94949192006-11-05 00:45:47 +0000351}
352
353static void parse_o(char* opt)
354{
355 ps_out_t* new;
356 // POSIX: "-o is blank- or comma-separated list" (FIXME)
357 char *comma, *equal;
358 while (1) {
359 comma = strchr(opt, ',');
360 equal = strchr(opt, '=');
361 if (comma && (!equal || equal > comma)) {
362 *comma = '\0';
363 *new_out_t() = *find_out_spec(opt);
364 *comma = ',';
365 opt = comma + 1;
366 continue;
367 }
368 break;
369 }
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000370 // opt points to last spec in comma separated list.
371 // This one can have =HEADER part.
Denis Vlasenko94949192006-11-05 00:45:47 +0000372 new = new_out_t();
373 if (equal)
374 *equal = '\0';
375 *new = *find_out_spec(opt);
376 if (equal) {
377 *equal = '=';
378 new->header = equal + 1;
379 // POSIX: the field widths shall be ... at least as wide as
380 // the header text (default or overridden value).
381 // If the header text is null, such as -o user=,
382 // the field width shall be at least as wide as the
383 // default header text
384 if (new->header[0]) {
385 new->width = strlen(new->header);
386 print_header = 1;
387 }
388 } else
389 print_header = 1;
390}
391
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200392static void alloc_line_buffer(void)
Denis Vlasenko94949192006-11-05 00:45:47 +0000393{
394 int i;
395 int width = 0;
396 for (i = 0; i < out_cnt; i++) {
Denis Vlasenko85818632007-04-19 14:47:11 +0000397 need_flags |= out[i].ps_flags;
Denis Vlasenko94949192006-11-05 00:45:47 +0000398 if (out[i].header[0]) {
399 print_header = 1;
400 }
401 width += out[i].width + 1; /* "FIELD " */
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200402 if ((int)(width - terminal_width) > 0) {
403 /* The rest does not fit on the screen */
404 //out[i].width -= (width - terminal_width - 1);
405 out_cnt = i + 1;
406 break;
407 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000408 }
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000409#if ENABLE_SELINUX
410 if (!is_selinux_enabled())
411 need_flags &= ~PSSCAN_CONTEXT;
412#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000413 buffer = xmalloc(width + 1); /* for trailing \0 */
414}
415
416static void format_header(void)
417{
418 int i;
419 ps_out_t* op;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000420 char *p;
421
Denis Vlasenko94949192006-11-05 00:45:47 +0000422 if (!print_header)
423 return;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000424 p = buffer;
Denis Vlasenko94949192006-11-05 00:45:47 +0000425 i = 0;
426 if (out_cnt) {
427 while (1) {
428 op = &out[i];
429 if (++i == out_cnt) /* do not pad last field */
430 break;
431 p += sprintf(p, "%-*s ", op->width, op->header);
432 }
433 strcpy(p, op->header);
434 }
435 printf("%.*s\n", terminal_width, buffer);
436}
437
438static void format_process(const procps_status_t *ps)
439{
440 int i, len;
441 char *p = buffer;
442 i = 0;
443 if (out_cnt) while (1) {
444 out[i].f(p, out[i].width, ps);
445 // POSIX: Any field need not be meaningful in all
446 // implementations. In such a case a hyphen ( '-' )
447 // should be output in place of the field value.
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000448 if (!p[0]) {
449 p[0] = '-';
450 p[1] = '\0';
Denis Vlasenko94949192006-11-05 00:45:47 +0000451 }
452 len = strlen(p);
453 p += len;
454 len = out[i].width - len + 1;
455 if (++i == out_cnt) /* do not pad last field */
456 break;
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000457 p += sprintf(p, "%*s", len, "");
Denis Vlasenko94949192006-11-05 00:45:47 +0000458 }
459 printf("%.*s\n", terminal_width, buffer);
460}
461
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000462int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000463int ps_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko94949192006-11-05 00:45:47 +0000464{
465 procps_status_t *p;
466 llist_t* opt_o = NULL;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200467 int opt;
468 enum {
469 OPT_Z = (1 << 0),
470 OPT_o = (1 << 1),
471 OPT_a = (1 << 2),
472 OPT_A = (1 << 3),
473 OPT_d = (1 << 4),
474 OPT_e = (1 << 5),
475 OPT_f = (1 << 6),
476 OPT_l = (1 << 7),
477 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
478 };
479
480 INIT_G();
Denis Vlasenko85818632007-04-19 14:47:11 +0000481
Denis Vlasenko94949192006-11-05 00:45:47 +0000482 // POSIX:
483 // -a Write information for all processes associated with terminals
484 // Implementations may omit session leaders from this list
485 // -A Write information for all processes
486 // -d Write information for all processes, except session leaders
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200487 // -e Write information for all processes (equivalent to -A)
Denis Vlasenko94949192006-11-05 00:45:47 +0000488 // -f Generate a full listing
489 // -l Generate a long listing
490 // -o col1,col2,col3=header
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000491 // Select which columns to display
Denys Vlasenko887d9632009-11-24 03:52:24 +0100492 /* We allow (and ignore) most of the above. FIXME.
493 * -T is picked for threads (POSIX hasn't it standardized).
494 * procps v3.2.7 supports -T and shows tids as SPID column,
495 * it also supports -L where it shows tids as LWP column.
496 */
Denis Vlasenko94949192006-11-05 00:45:47 +0000497 opt_complementary = "o::";
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200498 opt = getopt32(argv, "Zo:aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
Denis Vlasenko94949192006-11-05 00:45:47 +0000499 if (opt_o) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000500 do {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000501 parse_o(llist_pop(&opt_o));
Denis Vlasenko94949192006-11-05 00:45:47 +0000502 } while (opt_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000503 } else {
504 /* Below: parse_o() needs char*, NOT const char*... */
505#if ENABLE_SELINUX
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200506 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000507 /* no -Z or no SELinux: do not show LABEL */
Denis Vlasenko83cea0e2008-07-16 21:11:09 +0000508 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000509 } else
510#endif
511 {
512 strcpy(default_o, DEFAULT_O_STR);
513 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000514 parse_o(default_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000515 }
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200516#if ENABLE_FEATURE_SHOW_THREADS
517 if (opt & OPT_T)
518 need_flags |= PSSCAN_TASKS;
519#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000520
Denis Vlasenko450196c2007-03-28 21:57:12 +0000521 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
522 * and such large widths */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000523 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000524 if (isatty(1)) {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000525 get_terminal_width_height(0, &terminal_width, NULL);
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000526 if (--terminal_width > MAX_WIDTH)
527 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000528 }
Denys Vlasenkoa05b2b82009-10-09 19:47:01 +0200529 alloc_line_buffer();
Denis Vlasenko94949192006-11-05 00:45:47 +0000530 format_header();
531
532 p = NULL;
Denys Vlasenkob410d4a2009-09-19 22:29:42 +0200533 while ((p = procps_scan(p, need_flags)) != NULL) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000534 format_process(p);
535 }
536
537 return EXIT_SUCCESS;
538}
539
540
541#else /* !ENABLE_DESKTOP */
542
543
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000544int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000545int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
Eric Andersenef8b6c71999-10-20 08:05:35 +0000546{
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200547 procps_status_t *p;
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200548 int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
549 | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
Denys Vlasenko39601842010-04-07 18:03:34 +0200550 unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
551 enum {
552 OPT_Z = (1 << 0) * ENABLE_SELINUX,
553 OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
554 };
555 int opts = 0;
556 /* If we support any options, parse argv */
557#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200558# if ENABLE_FEATURE_PS_WIDE
Denys Vlasenko39601842010-04-07 18:03:34 +0200559 /* -w is a bit complicated */
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200560 int w_count = 0;
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000561 opt_complementary = "-:ww";
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200562 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")"w", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000563 /* if w is given once, GNU ps sets the width to 132,
564 * if w is given more than once, it is "unlimited"
565 */
Denis Vlasenko621204b2006-10-27 09:03:24 +0000566 if (w_count) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200567 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000568 } else {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000569 get_terminal_width_height(0, &terminal_width, NULL);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000570 /* Go one less... */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000571 if (--terminal_width > MAX_WIDTH)
572 terminal_width = MAX_WIDTH;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000573 }
Denys Vlasenko39601842010-04-07 18:03:34 +0200574# else
575 /* -w is not supported, only -Z and/or -T */
576 opt_complementary = "-";
577 opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T"));
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200578# endif
Denys Vlasenko39601842010-04-07 18:03:34 +0200579#endif
580
581#if ENABLE_SELINUX
582 if ((opts & OPT_Z) && is_selinux_enabled()) {
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200583 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
584 | PSSCAN_STATE | PSSCAN_COMM;
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000585 puts(" PID CONTEXT STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200586 } else
587#endif
588 {
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000589 puts(" PID USER VSZ STAT COMMAND");
Denys Vlasenko39601842010-04-07 18:03:34 +0200590 }
591 if (opts & OPT_T) {
592 psscan_flags |= PSSCAN_TASKS;
593 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200595 p = NULL;
596 while ((p = procps_scan(p, psscan_flags)) != NULL) {
Denys Vlasenko39601842010-04-07 18:03:34 +0200597 int len;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000598#if ENABLE_SELINUX
Denys Vlasenkod75295c2009-09-21 23:58:43 +0200599 if (psscan_flags & PSSCAN_CONTEXT) {
Denis Vlasenko766f6592008-01-07 16:41:41 +0000600 len = printf("%5u %-32.32s %s ",
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000601 p->pid,
602 p->context ? p->context : "unknown",
603 p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +0000604 } else
Eric Andersen9e480452003-07-03 10:07:04 +0000605#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000606 {
607 const char *user = get_cached_username(p->uid);
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000608 //if (p->vsz == 0)
609 // len = printf("%5u %-8.8s %s ",
610 // p->pid, user, p->state);
611 //else
612 {
613 char buf6[6];
614 smart_ulltoa5(p->vsz, buf6, " mgtpezy");
615 buf6[5] = '\0';
Denis Vlasenko766f6592008-01-07 16:41:41 +0000616 len = printf("%5u %-8.8s %s %s ",
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000617 p->pid, user, buf6, p->state);
618 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000619 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000620
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000621 {
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000622 int sz = terminal_width - len;
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000623 char buf[sz + 1];
624 read_cmdline(buf, sz, p->pid, p->comm);
625 puts(buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000626 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000627 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000628 if (ENABLE_FEATURE_CLEAN_UP)
629 clear_username_cache();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000630 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000631}
Denis Vlasenko94949192006-11-05 00:45:47 +0000632
Denys Vlasenko39601842010-04-07 18:03:34 +0200633#endif /* !ENABLE_DESKTOP */