*: remove "Options:" string from help texts
[oweals/busybox.git] / procps / ps.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ps implementation(s) for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  * Fix for SELinux Support:(c)2007 Hiroshi Shinji <shiroshi@my.email.ne.jp>
7  *                         (c)2007 Yuichi Nakamura <ynakam@hitachisoft.jp>
8  *
9  * Licensed under GPLv2, see file LICENSE in this source tree.
10  */
11
12 //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"
18 //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
28 //usage:#endif
29 //usage:
30 //usage:#define ps_trivial_usage
31 //usage:       ""
32 //usage:#define ps_full_usage "\n\n"
33 //usage:       "Show list of processes\n"
34 //usage:        USAGE_PS
35 //usage:        IF_SELINUX(
36 //usage:     "\n        -Z      Show selinux context"
37 //usage:        )
38 //usage:        IF_FEATURE_PS_WIDE(
39 //usage:     "\n        w       Wide output"
40 //usage:        )
41 //usage:
42 //usage:#endif /* ENABLE_DESKTOP */
43 //usage:
44 //usage:#define ps_example_usage
45 //usage:       "$ ps\n"
46 //usage:       "  PID  Uid      Gid State Command\n"
47 //usage:       "    1 root     root     S init\n"
48 //usage:       "    2 root     root     S [kflushd]\n"
49 //usage:       "    3 root     root     S [kupdate]\n"
50 //usage:       "    4 root     root     S [kpiod]\n"
51 //usage:       "    5 root     root     S [kswapd]\n"
52 //usage:       "  742 andersen andersen S [bash]\n"
53 //usage:       "  743 andersen andersen S -bash\n"
54 //usage:       "  745 root     root     S [getty]\n"
55 //usage:       " 2990 andersen andersen R ps\n"
56
57 #include "libbb.h"
58
59 /* Absolute maximum on output line length */
60 enum { MAX_WIDTH = 2*1024 };
61
62 #if ENABLE_DESKTOP
63
64 #include <sys/times.h> /* for times() */
65 #ifndef AT_CLKTCK
66 # define AT_CLKTCK 17
67 #endif
68
69 /* TODO:
70  * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
71  * specifies (for XSI-conformant systems) following default columns
72  * (l and f mark columns shown with -l and -f respectively):
73  * F     l   Flags (octal and additive) associated with the process (??)
74  * S     l   The state of the process
75  * UID   f,l The user ID; the login name is printed with -f
76  * PID       The process ID
77  * PPID  f,l The parent process
78  * C     f,l Processor utilization
79  * PRI   l   The priority of the process; higher numbers mean lower priority
80  * NI    l   Nice value
81  * ADDR  l   The address of the process
82  * SZ    l   The size in blocks of the core image of the process
83  * WCHAN l   The event for which the process is waiting or sleeping
84  * STIME f   Starting time of the process
85  * TTY       The controlling terminal for the process
86  * TIME      The cumulative execution time for the process
87  * CMD       The command name; the full command line is shown with -f
88  */
89 #if ENABLE_SELINUX
90 # define SELINUX_O_PREFIX "label,"
91 # define DEFAULT_O_STR    (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
92 #else
93 # define DEFAULT_O_STR    ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
94 #endif
95
96 typedef struct {
97         uint16_t width;
98         char name6[6];
99         const char *header;
100         void (*f)(char *buf, int size, const procps_status_t *ps);
101         int ps_flags;
102 } ps_out_t;
103
104 struct globals {
105         ps_out_t* out;
106         int out_cnt;
107         int print_header;
108         int need_flags;
109         char *buffer;
110         unsigned terminal_width;
111 #if ENABLE_FEATURE_PS_TIME
112         unsigned kernel_HZ;
113         unsigned long long seconds_since_boot;
114 #endif
115         char default_o[sizeof(DEFAULT_O_STR)];
116 } FIX_ALIASING;
117 #define G (*(struct globals*)&bb_common_bufsiz1)
118 #define out                (G.out               )
119 #define out_cnt            (G.out_cnt           )
120 #define print_header       (G.print_header      )
121 #define need_flags         (G.need_flags        )
122 #define buffer             (G.buffer            )
123 #define terminal_width     (G.terminal_width    )
124 #define kernel_HZ          (G.kernel_HZ         )
125 #define seconds_since_boot (G.seconds_since_boot)
126 #define default_o          (G.default_o         )
127 #define INIT_G() do { } while (0)
128
129 #if ENABLE_FEATURE_PS_TIME
130 /* for ELF executables, notes are pushed before environment and args */
131 static ptrdiff_t find_elf_note(ptrdiff_t findme)
132 {
133         ptrdiff_t *ep = (ptrdiff_t *) environ;
134
135         while (*ep++)
136                 continue;
137         while (*ep) {
138                 if (ep[0] == findme) {
139                         return ep[1];
140                 }
141                 ep += 2;
142         }
143         return -1;
144 }
145
146 #if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS
147 static unsigned get_HZ_by_waiting(void)
148 {
149         struct timeval tv1, tv2;
150         unsigned t1, t2, r, hz;
151         unsigned cnt = cnt; /* for compiler */
152         int diff;
153
154         r = 0;
155
156         /* Wait for times() to reach new tick */
157         t1 = times(NULL);
158         do {
159                 t2 = times(NULL);
160         } while (t2 == t1);
161         gettimeofday(&tv2, NULL);
162
163         do {
164                 t1 = t2;
165                 tv1.tv_usec = tv2.tv_usec;
166
167                 /* Wait exactly one times() tick */
168                 do {
169                         t2 = times(NULL);
170                 } while (t2 == t1);
171                 gettimeofday(&tv2, NULL);
172
173                 /* Calculate ticks per sec, rounding up to even */
174                 diff = tv2.tv_usec - tv1.tv_usec;
175                 if (diff <= 0) diff += 1000000;
176                 hz = 1000000u / (unsigned)diff;
177                 hz = (hz+1) & ~1;
178
179                 /* Count how many same hz values we saw */
180                 if (r != hz) {
181                         r = hz;
182                         cnt = 0;
183                 }
184                 cnt++;
185         } while (cnt < 3); /* exit if saw 3 same values */
186
187         return r;
188 }
189 #else
190 static inline unsigned get_HZ_by_waiting(void)
191 {
192         /* Better method? */
193         return 100;
194 }
195 #endif
196
197 static unsigned get_kernel_HZ(void)
198 {
199         //char buf[64];
200         struct sysinfo info;
201
202         if (kernel_HZ)
203                 return kernel_HZ;
204
205         /* Works for ELF only, Linux 2.4.0+ */
206         kernel_HZ = find_elf_note(AT_CLKTCK);
207         if (kernel_HZ == (unsigned)-1)
208                 kernel_HZ = get_HZ_by_waiting();
209
210         //if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
211         //      bb_perror_msg_and_die("can't read %s", "/proc/uptime");
212         //buf[sizeof(buf)-1] = '\0';
213         ///sscanf(buf, "%llu", &seconds_since_boot);
214         sysinfo(&info);
215         seconds_since_boot = info.uptime;
216
217         return kernel_HZ;
218 }
219 #endif
220
221 /* Print value to buf, max size+1 chars (including trailing '\0') */
222
223 static void func_user(char *buf, int size, const procps_status_t *ps)
224 {
225 #if 1
226         safe_strncpy(buf, get_cached_username(ps->uid), size+1);
227 #else
228         /* "compatible" version, but it's larger */
229         /* procps 2.18 shows numeric UID if name overflows the field */
230         /* TODO: get_cached_username() returns numeric string if
231          * user has no passwd record, we will display it
232          * left-justified here; too long usernames are shown
233          * as _right-justified_ IDs. Is it worth fixing? */
234         const char *user = get_cached_username(ps->uid);
235         if (strlen(user) <= size)
236                 safe_strncpy(buf, user, size+1);
237         else
238                 sprintf(buf, "%*u", size, (unsigned)ps->uid);
239 #endif
240 }
241
242 static void func_group(char *buf, int size, const procps_status_t *ps)
243 {
244         safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
245 }
246
247 static void func_comm(char *buf, int size, const procps_status_t *ps)
248 {
249         safe_strncpy(buf, ps->comm, size+1);
250 }
251
252 static void func_stat(char *buf, int size, const procps_status_t *ps)
253 {
254         safe_strncpy(buf, ps->state, size+1);
255 }
256
257 static void func_args(char *buf, int size, const procps_status_t *ps)
258 {
259         read_cmdline(buf, size+1, ps->pid, ps->comm);
260 }
261
262 static void func_pid(char *buf, int size, const procps_status_t *ps)
263 {
264         sprintf(buf, "%*u", size, ps->pid);
265 }
266
267 static void func_ppid(char *buf, int size, const procps_status_t *ps)
268 {
269         sprintf(buf, "%*u", size, ps->ppid);
270 }
271
272 static void func_pgid(char *buf, int size, const procps_status_t *ps)
273 {
274         sprintf(buf, "%*u", size, ps->pgid);
275 }
276
277 static void put_lu(char *buf, int size, unsigned long u)
278 {
279         char buf4[5];
280
281         /* see http://en.wikipedia.org/wiki/Tera */
282         smart_ulltoa4(u, buf4, " mgtpezy");
283         buf4[4] = '\0';
284         sprintf(buf, "%.*s", size, buf4);
285 }
286
287 static void func_vsz(char *buf, int size, const procps_status_t *ps)
288 {
289         put_lu(buf, size, ps->vsz);
290 }
291
292 static void func_rss(char *buf, int size, const procps_status_t *ps)
293 {
294         put_lu(buf, size, ps->rss);
295 }
296
297 static void func_tty(char *buf, int size, const procps_status_t *ps)
298 {
299         buf[0] = '?';
300         buf[1] = '\0';
301         if (ps->tty_major) /* tty field of "0" means "no tty" */
302                 snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
303 }
304
305 #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
306
307 static void func_rgroup(char *buf, int size, const procps_status_t *ps)
308 {
309         safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
310 }
311
312 static void func_ruser(char *buf, int size, const procps_status_t *ps)
313 {
314         safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
315 }
316
317 static void func_nice(char *buf, int size, const procps_status_t *ps)
318 {
319         sprintf(buf, "%*d", size, ps->niceness);
320 }
321
322 #endif
323
324 #if ENABLE_FEATURE_PS_TIME
325
326 static void func_etime(char *buf, int size, const procps_status_t *ps)
327 {
328         /* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
329         unsigned long mm;
330         unsigned ss;
331
332         mm = ps->start_time / get_kernel_HZ();
333         /* must be after get_kernel_HZ()! */
334         mm = seconds_since_boot - mm;
335         ss = mm % 60;
336         mm /= 60;
337         snprintf(buf, size+1, "%3lu:%02u", mm, ss);
338 }
339
340 static void func_time(char *buf, int size, const procps_status_t *ps)
341 {
342         /* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
343         unsigned long mm;
344         unsigned ss;
345
346         mm = (ps->utime + ps->stime) / get_kernel_HZ();
347         ss = mm % 60;
348         mm /= 60;
349         snprintf(buf, size+1, "%3lu:%02u", mm, ss);
350 }
351
352 #endif
353
354 #if ENABLE_SELINUX
355 static void func_label(char *buf, int size, const procps_status_t *ps)
356 {
357         safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
358 }
359 #endif
360
361 /*
362 static void func_nice(char *buf, int size, const procps_status_t *ps)
363 {
364         ps->???
365 }
366
367 static void func_pcpu(char *buf, int size, const procps_status_t *ps)
368 {
369 }
370 */
371
372 static const ps_out_t out_spec[] = {
373 /* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
374         { 8                  , "user"  ,"USER"   ,func_user  ,PSSCAN_UIDGID  },
375         { 8                  , "group" ,"GROUP"  ,func_group ,PSSCAN_UIDGID  },
376         { 16                 , "comm"  ,"COMMAND",func_comm  ,PSSCAN_COMM    },
377         { MAX_WIDTH          , "args"  ,"COMMAND",func_args  ,PSSCAN_COMM    },
378         { 5                  , "pid"   ,"PID"    ,func_pid   ,PSSCAN_PID     },
379         { 5                  , "ppid"  ,"PPID"   ,func_ppid  ,PSSCAN_PPID    },
380         { 5                  , "pgid"  ,"PGID"   ,func_pgid  ,PSSCAN_PGID    },
381 #if ENABLE_FEATURE_PS_TIME
382         { sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
383 #endif
384 #if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
385         { 5                  , "nice"  ,"NI"     ,func_nice  ,PSSCAN_NICE    },
386         { 8                  , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
387         { 8                  , "ruser" ,"RUSER"  ,func_ruser ,PSSCAN_RUIDGID },
388 //      { 5                  , "pcpu"  ,"%CPU"   ,func_pcpu  ,PSSCAN_        },
389 #endif
390 #if ENABLE_FEATURE_PS_TIME
391         { 6                  , "time"  ,"TIME"   ,func_time  ,PSSCAN_STIME | PSSCAN_UTIME },
392 #endif
393         { 6                  , "tty"   ,"TT"     ,func_tty   ,PSSCAN_TTY     },
394         { 4                  , "vsz"   ,"VSZ"    ,func_vsz   ,PSSCAN_VSZ     },
395 /* Not mandated, but useful: */
396         { 4                  , "stat"  ,"STAT"   ,func_stat  ,PSSCAN_STAT    },
397         { 4                  , "rss"   ,"RSS"    ,func_rss   ,PSSCAN_RSS     },
398 #if ENABLE_SELINUX
399         { 35                 , "label" ,"LABEL"  ,func_label ,PSSCAN_CONTEXT },
400 #endif
401 };
402
403 static ps_out_t* new_out_t(void)
404 {
405         out = xrealloc_vector(out, 2, out_cnt);
406         return &out[out_cnt++];
407 }
408
409 static const ps_out_t* find_out_spec(const char *name)
410 {
411         unsigned i;
412         char buf[ARRAY_SIZE(out_spec)*7 + 1];
413         char *p = buf;
414
415         for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
416                 if (strncmp(name, out_spec[i].name6, 6) == 0)
417                         return &out_spec[i];
418                 p += sprintf(p, "%.6s,", out_spec[i].name6);
419         }
420         p[-1] = '\0';
421         bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
422 }
423
424 static void parse_o(char* opt)
425 {
426         ps_out_t* new;
427         // POSIX: "-o is blank- or comma-separated list" (FIXME)
428         char *comma, *equal;
429         while (1) {
430                 comma = strchr(opt, ',');
431                 equal = strchr(opt, '=');
432                 if (comma && (!equal || equal > comma)) {
433                         *comma = '\0';
434                         *new_out_t() = *find_out_spec(opt);
435                         *comma = ',';
436                         opt = comma + 1;
437                         continue;
438                 }
439                 break;
440         }
441         // opt points to last spec in comma separated list.
442         // This one can have =HEADER part.
443         new = new_out_t();
444         if (equal)
445                 *equal = '\0';
446         *new = *find_out_spec(opt);
447         if (equal) {
448                 *equal = '=';
449                 new->header = equal + 1;
450                 // POSIX: the field widths shall be ... at least as wide as
451                 // the header text (default or overridden value).
452                 // If the header text is null, such as -o user=,
453                 // the field width shall be at least as wide as the
454                 // default header text
455                 if (new->header[0]) {
456                         new->width = strlen(new->header);
457                         print_header = 1;
458                 }
459         } else
460                 print_header = 1;
461 }
462
463 static void alloc_line_buffer(void)
464 {
465         int i;
466         int width = 0;
467         for (i = 0; i < out_cnt; i++) {
468                 need_flags |= out[i].ps_flags;
469                 if (out[i].header[0]) {
470                         print_header = 1;
471                 }
472                 width += out[i].width + 1; /* "FIELD " */
473                 if ((int)(width - terminal_width) > 0) {
474                         /* The rest does not fit on the screen */
475                         //out[i].width -= (width - terminal_width - 1);
476                         out_cnt = i + 1;
477                         break;
478                 }
479         }
480 #if ENABLE_SELINUX
481         if (!is_selinux_enabled())
482                 need_flags &= ~PSSCAN_CONTEXT;
483 #endif
484         buffer = xmalloc(width + 1); /* for trailing \0 */
485 }
486
487 static void format_header(void)
488 {
489         int i;
490         ps_out_t* op;
491         char *p;
492
493         if (!print_header)
494                 return;
495         p = buffer;
496         i = 0;
497         if (out_cnt) {
498                 while (1) {
499                         op = &out[i];
500                         if (++i == out_cnt) /* do not pad last field */
501                                 break;
502                         p += sprintf(p, "%-*s ", op->width, op->header);
503                 }
504                 strcpy(p, op->header);
505         }
506         printf("%.*s\n", terminal_width, buffer);
507 }
508
509 static void format_process(const procps_status_t *ps)
510 {
511         int i, len;
512         char *p = buffer;
513         i = 0;
514         if (out_cnt) while (1) {
515                 out[i].f(p, out[i].width, ps);
516                 // POSIX: Any field need not be meaningful in all
517                 // implementations. In such a case a hyphen ( '-' )
518                 // should be output in place of the field value.
519                 if (!p[0]) {
520                         p[0] = '-';
521                         p[1] = '\0';
522                 }
523                 len = strlen(p);
524                 p += len;
525                 len = out[i].width - len + 1;
526                 if (++i == out_cnt) /* do not pad last field */
527                         break;
528                 p += sprintf(p, "%*s", len, "");
529         }
530         printf("%.*s\n", terminal_width, buffer);
531 }
532
533 int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
534 int ps_main(int argc UNUSED_PARAM, char **argv)
535 {
536         procps_status_t *p;
537         llist_t* opt_o = NULL;
538         int opt;
539         enum {
540                 OPT_Z = (1 << 0),
541                 OPT_o = (1 << 1),
542                 OPT_a = (1 << 2),
543                 OPT_A = (1 << 3),
544                 OPT_d = (1 << 4),
545                 OPT_e = (1 << 5),
546                 OPT_f = (1 << 6),
547                 OPT_l = (1 << 7),
548                 OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
549         };
550
551         INIT_G();
552
553         // POSIX:
554         // -a  Write information for all processes associated with terminals
555         //     Implementations may omit session leaders from this list
556         // -A  Write information for all processes
557         // -d  Write information for all processes, except session leaders
558         // -e  Write information for all processes (equivalent to -A)
559         // -f  Generate a full listing
560         // -l  Generate a long listing
561         // -o col1,col2,col3=header
562         //     Select which columns to display
563         /* We allow (and ignore) most of the above. FIXME.
564          * -T is picked for threads (POSIX hasn't it standardized).
565          * procps v3.2.7 supports -T and shows tids as SPID column,
566          * it also supports -L where it shows tids as LWP column.
567          */
568         opt_complementary = "o::";
569         opt = getopt32(argv, "Zo:aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
570         if (opt_o) {
571                 do {
572                         parse_o(llist_pop(&opt_o));
573                 } while (opt_o);
574         } else {
575                 /* Below: parse_o() needs char*, NOT const char*... */
576 #if ENABLE_SELINUX
577                 if (!(opt & OPT_Z) || !is_selinux_enabled()) {
578                         /* no -Z or no SELinux: do not show LABEL */
579                         strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
580                 } else
581 #endif
582                 {
583                         strcpy(default_o, DEFAULT_O_STR);
584                 }
585                 parse_o(default_o);
586         }
587 #if ENABLE_FEATURE_SHOW_THREADS
588         if (opt & OPT_T)
589                 need_flags |= PSSCAN_TASKS;
590 #endif
591
592         /* Was INT_MAX, but some libc's go belly up with printf("%.*s")
593          * and such large widths */
594         terminal_width = MAX_WIDTH;
595         if (isatty(1)) {
596                 get_terminal_width_height(0, &terminal_width, NULL);
597                 if (--terminal_width > MAX_WIDTH)
598                         terminal_width = MAX_WIDTH;
599         }
600         alloc_line_buffer();
601         format_header();
602
603         p = NULL;
604         while ((p = procps_scan(p, need_flags)) != NULL) {
605                 format_process(p);
606         }
607
608         return EXIT_SUCCESS;
609 }
610
611
612 #else /* !ENABLE_DESKTOP */
613
614
615 int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
616 int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
617 {
618         procps_status_t *p;
619         int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
620                         | PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
621         unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
622         enum {
623                 OPT_Z = (1 << 0) * ENABLE_SELINUX,
624                 OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
625         };
626         int opts = 0;
627         /* If we support any options, parse argv */
628 #if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE
629 # if ENABLE_FEATURE_PS_WIDE
630         /* -w is a bit complicated */
631         int w_count = 0;
632         opt_complementary = "-:ww";
633         opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")"w", &w_count);
634         /* if w is given once, GNU ps sets the width to 132,
635          * if w is given more than once, it is "unlimited"
636          */
637         if (w_count) {
638                 terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
639         } else {
640                 get_terminal_width_height(0, &terminal_width, NULL);
641                 /* Go one less... */
642                 if (--terminal_width > MAX_WIDTH)
643                         terminal_width = MAX_WIDTH;
644         }
645 # else
646         /* -w is not supported, only -Z and/or -T */
647         opt_complementary = "-";
648         opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T"));
649 # endif
650 #endif
651
652 #if ENABLE_SELINUX
653         if ((opts & OPT_Z) && is_selinux_enabled()) {
654                 psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
655                                 | PSSCAN_STATE | PSSCAN_COMM;
656                 puts("  PID CONTEXT                          STAT COMMAND");
657         } else
658 #endif
659         {
660                 puts("  PID USER       VSZ STAT COMMAND");
661         }
662         if (opts & OPT_T) {
663                 psscan_flags |= PSSCAN_TASKS;
664         }
665
666         p = NULL;
667         while ((p = procps_scan(p, psscan_flags)) != NULL) {
668                 int len;
669 #if ENABLE_SELINUX
670                 if (psscan_flags & PSSCAN_CONTEXT) {
671                         len = printf("%5u %-32.32s %s  ",
672                                         p->pid,
673                                         p->context ? p->context : "unknown",
674                                         p->state);
675                 } else
676 #endif
677                 {
678                         const char *user = get_cached_username(p->uid);
679                         //if (p->vsz == 0)
680                         //      len = printf("%5u %-8.8s        %s ",
681                         //              p->pid, user, p->state);
682                         //else
683                         {
684                                 char buf6[6];
685                                 smart_ulltoa5(p->vsz, buf6, " mgtpezy");
686                                 buf6[5] = '\0';
687                                 len = printf("%5u %-8.8s %s %s  ",
688                                         p->pid, user, buf6, p->state);
689                         }
690                 }
691
692                 {
693                         int sz = terminal_width - len;
694                         char buf[sz + 1];
695                         read_cmdline(buf, sz, p->pid, p->comm);
696                         puts(buf);
697                 }
698         }
699         if (ENABLE_FEATURE_CLEAN_UP)
700                 clear_username_cache();
701         return EXIT_SUCCESS;
702 }
703
704 #endif /* !ENABLE_DESKTOP */