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