blob: f9d346ccf60bd9fdca4aef7af2c48279a4ee5368 [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 *
Rob Landleye9a7a622006-09-22 02:52:41 +00009 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
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() */
20//#include <sys/sysinfo.h> /* for sysinfo() */
21#ifndef AT_CLKTCK
22#define AT_CLKTCK 17
23#endif
24
25
26#if ENABLE_SELINUX
27#define SELINIX_O_PREFIX "label,"
Denis Vlasenko56ea65c2008-01-06 03:26:53 +000028#define DEFAULT_O_STR (SELINIX_O_PREFIX "pid,user" USE_FEATURE_PS_TIME(",time") ",args")
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000029#else
Denis Vlasenko56ea65c2008-01-06 03:26:53 +000030#define DEFAULT_O_STR ("pid,user" USE_FEATURE_PS_TIME(",time") ",args")
Denis Vlasenko5fee2e12008-01-05 03:26:41 +000031#endif
32
33typedef struct {
34 uint16_t width;
35 char name[6];
36 const char *header;
37 void (*f)(char *buf, int size, const procps_status_t *ps);
38 int ps_flags;
39} ps_out_t;
40
41struct globals {
42 ps_out_t* out;
43 int out_cnt;
44 int print_header;
45 int need_flags;
46 char *buffer;
47 unsigned terminal_width;
48#if ENABLE_FEATURE_PS_TIME
49 unsigned kernel_HZ;
50 unsigned long long seconds_since_boot;
51#endif
52 char default_o[sizeof(DEFAULT_O_STR)];
53};
54#define G (*(struct globals*)&bb_common_bufsiz1)
55#define out (G.out )
56#define out_cnt (G.out_cnt )
57#define print_header (G.print_header )
58#define need_flags (G.need_flags )
59#define buffer (G.buffer )
60#define terminal_width (G.terminal_width )
61#define kernel_HZ (G.kernel_HZ )
62#define seconds_since_boot (G.seconds_since_boot)
63#define default_o (G.default_o )
64
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
145 //if (open_read_close("/proc/uptime", buf, sizeof(buf) <= 0)
146 // bb_perror_msg_and_die("cannot read %s", "/proc/uptime");
147 //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
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000177static void func_comm(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000178{
179 safe_strncpy(buf, ps->comm, size+1);
180}
181
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000182static void func_args(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000183{
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000184 read_cmdline(buf, size, ps->pid, ps->comm);
Denis Vlasenko94949192006-11-05 00:45:47 +0000185}
186
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000187static void func_pid(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000188{
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000189 sprintf(buf, "%*u", size, ps->pid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000190}
191
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000192static void func_ppid(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->ppid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000195}
196
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000197static void func_pgid(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->pgid);
Denis Vlasenko94949192006-11-05 00:45:47 +0000200}
201
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000202static void put_lu(char *buf, int size, unsigned long u)
Denis Vlasenko94949192006-11-05 00:45:47 +0000203{
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000204 char buf4[5];
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000205
206 /* see http://en.wikipedia.org/wiki/Tera */
Denis Vlasenko9b4c0722008-01-07 16:51:25 +0000207 smart_ulltoa4(u, buf4, " mgtpezy");
208 buf4[4] = '\0';
209 sprintf(buf, "%.*s", size, buf4);
Denis Vlasenko94949192006-11-05 00:45:47 +0000210}
211
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000212static void func_vsz(char *buf, int size, const procps_status_t *ps)
213{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000214 put_lu(buf, size, ps->vsz);
Denis Vlasenkodcf4de22007-05-01 20:07:29 +0000215}
216
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000217static void func_rss(char *buf, int size, const procps_status_t *ps)
218{
Denis Vlasenkofc1e9952007-08-29 18:23:36 +0000219 put_lu(buf, size, ps->rss);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000220}
221
222static void func_tty(char *buf, int size, const procps_status_t *ps)
223{
Denis Vlasenko31789a82007-06-19 14:43:17 +0000224 buf[0] = '?';
225 buf[1] = '\0';
226 if (ps->tty_major) /* tty field of "0" means "no tty" */
227 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000228}
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000229
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000230#if ENABLE_FEATURE_PS_TIME
231static void func_etime(char *buf, int size, const procps_status_t *ps)
232{
233 /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
234 unsigned long mm;
235 unsigned ss;
236
237 mm = ps->start_time / get_kernel_HZ();
238 /* must be after get_kernel_HZ()! */
239 mm = seconds_since_boot - mm;
240 ss = mm % 60;
241 mm /= 60;
242 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
243}
244
245static void func_time(char *buf, int size, const procps_status_t *ps)
246{
247 /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
248 unsigned long mm;
249 unsigned ss;
250
251 mm = (ps->utime + ps->stime) / get_kernel_HZ();
252 ss = mm % 60;
253 mm /= 60;
254 snprintf(buf, size+1, "%3lu:%02u", mm, ss);
255}
256#endif
257
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000258#if ENABLE_SELINUX
259static void func_label(char *buf, int size, const procps_status_t *ps)
260{
261 safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
262}
263#endif
264
Denis Vlasenko94949192006-11-05 00:45:47 +0000265/*
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000266static void func_nice(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000267{
268 ps->???
269}
270
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000271static void func_pcpu(char *buf, int size, const procps_status_t *ps)
Denis Vlasenko94949192006-11-05 00:45:47 +0000272{
273}
274*/
275
Denis Vlasenko94949192006-11-05 00:45:47 +0000276static const ps_out_t out_spec[] = {
277// Mandated by POSIX:
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000278 { 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
279 { 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
280 { 256 , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
281 { 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
282 { 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
283 { 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000284#if ENABLE_FEATURE_PS_TIME
285 { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
286#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000287// { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
288// { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ },
289// { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
290// { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID },
291// { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID },
Denis Vlasenko5fee2e12008-01-05 03:26:41 +0000292#if ENABLE_FEATURE_PS_TIME
293 { 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
294#endif
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000295 { 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
296 { 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000297// Not mandated by POSIX, but useful:
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000298 { 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000299#if ENABLE_SELINUX
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000300 { 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000301#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000302};
303
Denis Vlasenko94949192006-11-05 00:45:47 +0000304static ps_out_t* new_out_t(void)
305{
306 int i = out_cnt++;
307 out = xrealloc(out, out_cnt * sizeof(*out));
308 return &out[i];
309}
310
311static const ps_out_t* find_out_spec(const char *name)
312{
313 int i;
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000314 for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000315 if (!strcmp(name, out_spec[i].name))
316 return &out_spec[i];
317 }
318 bb_error_msg_and_die("bad -o argument '%s'", name);
319}
320
321static void parse_o(char* opt)
322{
323 ps_out_t* new;
324 // POSIX: "-o is blank- or comma-separated list" (FIXME)
325 char *comma, *equal;
326 while (1) {
327 comma = strchr(opt, ',');
328 equal = strchr(opt, '=');
329 if (comma && (!equal || equal > comma)) {
330 *comma = '\0';
331 *new_out_t() = *find_out_spec(opt);
332 *comma = ',';
333 opt = comma + 1;
334 continue;
335 }
336 break;
337 }
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000338 // opt points to last spec in comma separated list.
339 // This one can have =HEADER part.
Denis Vlasenko94949192006-11-05 00:45:47 +0000340 new = new_out_t();
341 if (equal)
342 *equal = '\0';
343 *new = *find_out_spec(opt);
344 if (equal) {
345 *equal = '=';
346 new->header = equal + 1;
347 // POSIX: the field widths shall be ... at least as wide as
348 // the header text (default or overridden value).
349 // If the header text is null, such as -o user=,
350 // the field width shall be at least as wide as the
351 // default header text
352 if (new->header[0]) {
353 new->width = strlen(new->header);
354 print_header = 1;
355 }
356 } else
357 print_header = 1;
358}
359
360static void post_process(void)
361{
362 int i;
363 int width = 0;
364 for (i = 0; i < out_cnt; i++) {
Denis Vlasenko85818632007-04-19 14:47:11 +0000365 need_flags |= out[i].ps_flags;
Denis Vlasenko94949192006-11-05 00:45:47 +0000366 if (out[i].header[0]) {
367 print_header = 1;
368 }
369 width += out[i].width + 1; /* "FIELD " */
370 }
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000371#if ENABLE_SELINUX
372 if (!is_selinux_enabled())
373 need_flags &= ~PSSCAN_CONTEXT;
374#endif
Denis Vlasenko94949192006-11-05 00:45:47 +0000375 buffer = xmalloc(width + 1); /* for trailing \0 */
376}
377
378static void format_header(void)
379{
380 int i;
381 ps_out_t* op;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000382 char *p;
383
Denis Vlasenko94949192006-11-05 00:45:47 +0000384 if (!print_header)
385 return;
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000386 p = buffer;
Denis Vlasenko94949192006-11-05 00:45:47 +0000387 i = 0;
388 if (out_cnt) {
389 while (1) {
390 op = &out[i];
391 if (++i == out_cnt) /* do not pad last field */
392 break;
393 p += sprintf(p, "%-*s ", op->width, op->header);
394 }
395 strcpy(p, op->header);
396 }
397 printf("%.*s\n", terminal_width, buffer);
398}
399
400static void format_process(const procps_status_t *ps)
401{
402 int i, len;
403 char *p = buffer;
404 i = 0;
405 if (out_cnt) while (1) {
406 out[i].f(p, out[i].width, ps);
407 // POSIX: Any field need not be meaningful in all
408 // implementations. In such a case a hyphen ( '-' )
409 // should be output in place of the field value.
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000410 if (!p[0]) {
411 p[0] = '-';
412 p[1] = '\0';
Denis Vlasenko94949192006-11-05 00:45:47 +0000413 }
414 len = strlen(p);
415 p += len;
416 len = out[i].width - len + 1;
417 if (++i == out_cnt) /* do not pad last field */
418 break;
Denis Vlasenko6dca70a2006-12-18 14:20:01 +0000419 p += sprintf(p, "%*s", len, "");
Denis Vlasenko94949192006-11-05 00:45:47 +0000420 }
421 printf("%.*s\n", terminal_width, buffer);
422}
423
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000424int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko94949192006-11-05 00:45:47 +0000425int ps_main(int argc, char **argv)
426{
427 procps_status_t *p;
428 llist_t* opt_o = NULL;
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000429 USE_SELINUX(int opt;)
Denis Vlasenko85818632007-04-19 14:47:11 +0000430
Denis Vlasenko94949192006-11-05 00:45:47 +0000431 // POSIX:
432 // -a Write information for all processes associated with terminals
433 // Implementations may omit session leaders from this list
434 // -A Write information for all processes
435 // -d Write information for all processes, except session leaders
436 // -e Write information for all processes (equivalent to -A.)
437 // -f Generate a full listing
438 // -l Generate a long listing
439 // -o col1,col2,col3=header
Denis Vlasenko516a0ca2007-04-19 14:46:14 +0000440 // Select which columns to display
Denis Vlasenko94949192006-11-05 00:45:47 +0000441 /* We allow (and ignore) most of the above. FIXME */
442 opt_complementary = "o::";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000443 USE_SELINUX(opt =) getopt32(argv, "Zo:aAdefl", &opt_o);
Denis Vlasenko94949192006-11-05 00:45:47 +0000444 if (opt_o) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000445 do {
446 parse_o(opt_o->data);
447 opt_o = opt_o->link;
448 } while (opt_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000449 } else {
450 /* Below: parse_o() needs char*, NOT const char*... */
451#if ENABLE_SELINUX
452 if (!(opt & 1) || !is_selinux_enabled()) {
453 /* no -Z or no SELinux: do not show LABEL */
454 strcpy(default_o, DEFAULT_O_STR + sizeof(SELINIX_O_PREFIX)-1);
455 } else
456#endif
457 {
458 strcpy(default_o, DEFAULT_O_STR);
459 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000460 parse_o(default_o);
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000461 }
Denis Vlasenko94949192006-11-05 00:45:47 +0000462 post_process();
463
Denis Vlasenko450196c2007-03-28 21:57:12 +0000464 /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
465 * and such large widths */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000466 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000467 if (isatty(1)) {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000468 get_terminal_width_height(0, &terminal_width, NULL);
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000469 if (--terminal_width > MAX_WIDTH)
470 terminal_width = MAX_WIDTH;
Denis Vlasenko94949192006-11-05 00:45:47 +0000471 }
472 format_header();
473
474 p = NULL;
Denis Vlasenko85818632007-04-19 14:47:11 +0000475 while ((p = procps_scan(p, need_flags))) {
Denis Vlasenko94949192006-11-05 00:45:47 +0000476 format_process(p);
477 }
478
479 return EXIT_SUCCESS;
480}
481
482
483#else /* !ENABLE_DESKTOP */
484
485
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000486int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000487int ps_main(int argc, char **argv)
Eric Andersenef8b6c71999-10-20 08:05:35 +0000488{
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000489 procps_status_t *p = NULL;
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000490 int len;
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000491 SKIP_SELINUX(const) int use_selinux = 0;
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000492 USE_SELINUX(int i;)
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000493#if !ENABLE_FEATURE_PS_WIDE
494 enum { terminal_width = 79 };
495#else
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000496 int terminal_width;
497 int w_count = 0;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000498#endif
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000499
500#if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000501#if ENABLE_FEATURE_PS_WIDE
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000502 opt_complementary = "-:ww";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000503 USE_SELINUX(i =) getopt32(argv, USE_SELINUX("Z") "w", &w_count);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000504 /* if w is given once, GNU ps sets the width to 132,
505 * if w is given more than once, it is "unlimited"
506 */
Denis Vlasenko621204b2006-10-27 09:03:24 +0000507 if (w_count) {
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000508 terminal_width = (w_count==1) ? 132 : MAX_WIDTH;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000509 } else {
Denis Vlasenkof893da82007-08-09 08:27:24 +0000510 get_terminal_width_height(0, &terminal_width, NULL);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000511 /* Go one less... */
Denis Vlasenko7ce75942007-07-25 17:27:58 +0000512 if (--terminal_width > MAX_WIDTH)
513 terminal_width = MAX_WIDTH;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000514 }
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000515#else /* only ENABLE_SELINUX */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000516 i = getopt32(argv, "Z");
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000517#endif
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000518#if ENABLE_SELINUX
Denis Vlasenkoaba7f492007-07-01 17:25:59 +0000519 if ((i & 1) && is_selinux_enabled())
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000520 use_selinux = PSSCAN_CONTEXT;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +0000521#endif
Denis Vlasenkod3ada322006-11-01 09:14:52 +0000522#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000523
Rob Landley60158cb2005-05-03 06:25:50 +0000524 if (use_selinux)
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000525 puts(" PID CONTEXT STAT COMMAND");
Eric Andersen9e480452003-07-03 10:07:04 +0000526 else
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000527 puts(" PID USER VSZ STAT COMMAND");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000529 while ((p = procps_scan(p, 0
530 | PSSCAN_PID
531 | PSSCAN_UIDGID
532 | PSSCAN_STATE
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000533 | PSSCAN_VSZ
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000534 | PSSCAN_COMM
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000535 | use_selinux
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000536 ))) {
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000537#if ENABLE_SELINUX
Denis Vlasenkobe905d52006-09-27 14:17:31 +0000538 if (use_selinux) {
Denis Vlasenko766f6592008-01-07 16:41:41 +0000539 len = printf("%5u %-32.32s %s ",
Denis Vlasenko0696b8a2007-06-06 07:40:16 +0000540 p->pid,
541 p->context ? p->context : "unknown",
542 p->state);
Denis Vlasenko92258542006-11-01 10:25:35 +0000543 } else
Eric Andersen9e480452003-07-03 10:07:04 +0000544#endif
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000545 {
546 const char *user = get_cached_username(p->uid);
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000547 //if (p->vsz == 0)
548 // len = printf("%5u %-8.8s %s ",
549 // p->pid, user, p->state);
550 //else
551 {
552 char buf6[6];
553 smart_ulltoa5(p->vsz, buf6, " mgtpezy");
554 buf6[5] = '\0';
Denis Vlasenko766f6592008-01-07 16:41:41 +0000555 len = printf("%5u %-8.8s %s %s ",
Denis Vlasenko56ea65c2008-01-06 03:26:53 +0000556 p->pid, user, buf6, p->state);
557 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000558 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000559
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000560 {
Denis Vlasenkodc7a5ea2007-07-01 14:53:06 +0000561 int sz = terminal_width - len;
Denis Vlasenko98ebab82007-06-30 14:47:41 +0000562 char buf[sz + 1];
563 read_cmdline(buf, sz, p->pid, p->comm);
564 puts(buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000566 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000567 if (ENABLE_FEATURE_CLEAN_UP)
568 clear_username_cache();
Matt Kraai3e856ce2000-12-01 02:55:13 +0000569 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000570}
Denis Vlasenko94949192006-11-05 00:45:47 +0000571
Denis Vlasenko40215162006-11-05 00:47:38 +0000572#endif /* ENABLE_DESKTOP */