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