1
/* vi: set sw=4 ts=4: */
3
* Mini ps implementation(s) for busybox
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>
9
* Licensed under GPLv2, see file LICENSE in this source tree.
12
//config: bool "ps (12 kb)"
15
//config: ps gives a snapshot of the current processes.
17
//config:config FEATURE_PS_WIDE
18
//config: bool "Enable wide output (-w)"
20
//config: depends on (PS || MINIPS) && !DESKTOP
22
//config: Support argument 'w' for wide output.
23
//config: If given once, 132 chars are printed, and if given more
24
//config: than once, the length is unlimited.
26
//config:config FEATURE_PS_LONG
27
//config: bool "Enable long output (-l)"
29
//config: depends on (PS || MINIPS) && !DESKTOP
31
//config: Support argument 'l' for long output.
32
//config: Adds fields PPID, RSS, START, TIME & TTY
34
//config:config FEATURE_PS_TIME
35
//config: bool "Enable -o time and -o etime specifiers"
37
//config: depends on (PS || MINIPS) && DESKTOP
39
//config:config FEATURE_PS_UNUSUAL_SYSTEMS
40
//config: bool "Support Linux prior to 2.4.0 and non-ELF systems"
42
//config: depends on FEATURE_PS_TIME
44
//config: Include support for measuring HZ on old kernels and non-ELF systems
45
//config: (if you are on Linux 2.4.0+ and use ELF, you don't need this)
47
//config:config FEATURE_PS_ADDITIONAL_COLUMNS
48
//config: bool "Enable -o rgroup, -o ruser, -o nice specifiers"
50
//config: depends on (PS || MINIPS) && DESKTOP
52
// APPLET_NOEXEC:name main location suid_type help
53
//applet:IF_PS( APPLET_NOEXEC(ps, ps, BB_DIR_BIN, BB_SUID_DROP, ps))
54
//applet:IF_MINIPS(APPLET_NOEXEC(minips, ps, BB_DIR_BIN, BB_SUID_DROP, ps))
56
//kbuild:lib-$(CONFIG_PS) += ps.o
57
//kbuild:lib-$(CONFIG_MINIPS) += ps.o
59
//usage:#if ENABLE_DESKTOP
61
//usage:#define ps_trivial_usage
62
//usage: "[-o COL1,COL2=HEADER]" IF_FEATURE_SHOW_THREADS(" [-T]")
63
//usage:#define ps_full_usage "\n\n"
64
//usage: "Show list of processes\n"
65
//usage: "\n -o COL1,COL2=HEADER Select columns for display"
66
//usage: IF_FEATURE_SHOW_THREADS(
67
//usage: "\n -T Show threads"
70
//usage:#else /* !ENABLE_DESKTOP */
72
//usage:#if !ENABLE_SELINUX && !ENABLE_FEATURE_PS_WIDE
73
//usage:#define USAGE_PS "\nThis version of ps accepts no options"
75
//usage:#define USAGE_PS ""
78
//usage:#define ps_trivial_usage
80
//usage:#define ps_full_usage "\n\n"
81
//usage: "Show list of processes\n"
84
//usage: "\n -Z Show selinux context"
86
//usage: IF_FEATURE_PS_WIDE(
87
//usage: "\n w Wide output"
89
//usage: IF_FEATURE_PS_LONG(
90
//usage: "\n l Long output"
92
//usage: IF_FEATURE_SHOW_THREADS(
93
//usage: "\n T Show threads"
96
//usage:#endif /* ENABLE_DESKTOP */
98
//usage:#define ps_example_usage
100
//usage: " PID Uid Gid State Command\n"
101
//usage: " 1 root root S init\n"
102
//usage: " 2 root root S [kflushd]\n"
103
//usage: " 3 root root S [kupdate]\n"
104
//usage: " 4 root root S [kpiod]\n"
105
//usage: " 5 root root S [kswapd]\n"
106
//usage: " 742 andersen andersen S [bash]\n"
107
//usage: " 743 andersen andersen S -bash\n"
108
//usage: " 745 root root S [getty]\n"
109
//usage: " 2990 andersen andersen R ps\n"
112
#include "common_bufsiz.h"
114
# include <sys/sysinfo.h>
117
/* Absolute maximum on output line length */
118
enum { MAX_WIDTH = 2*1024 };
120
#if ENABLE_FEATURE_PS_TIME || ENABLE_FEATURE_PS_LONG
121
static unsigned long get_uptime(void)
125
if (sysinfo(&info) < 0)
129
unsigned long uptime;
130
char buf[sizeof(uptime)*3 + 2];
131
/* /proc/uptime is "UPTIME_SEC.NN IDLE_SEC.NN\n"
132
* (where IDLE is cumulative over all CPUs)
134
if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0)
135
bb_perror_msg_and_die("can't read '%s'", "/proc/uptime");
136
buf[sizeof(buf)-1] = '\0';
137
sscanf(buf, "%lu", &uptime);
141
if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
150
* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
151
* specifies (for XSI-conformant systems) following default columns
152
* (l and f mark columns shown with -l and -f respectively):
153
* F l Flags (octal and additive) associated with the process (??)
154
* S l The state of the process
155
* UID f,l The user ID; the login name is printed with -f
157
* PPID f,l The parent process
158
* C f,l Processor utilization
159
* PRI l The priority of the process; higher numbers mean lower priority
161
* ADDR l The address of the process
162
* SZ l The size in blocks of the core image of the process
163
* WCHAN l The event for which the process is waiting or sleeping
164
* STIME f Starting time of the process
165
* TTY The controlling terminal for the process
166
* TIME The cumulative execution time for the process
167
* CMD The command name; the full command line is shown with -f
173
void (*f)(char *buf, int size, const procps_status_t *ps);
183
unsigned terminal_width;
184
#if ENABLE_FEATURE_PS_TIME
185
# if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS || !defined(__linux__)
188
unsigned long seconds_since_boot;
191
#define G (*(struct globals*)bb_common_bufsiz1)
193
#define out_cnt (G.out_cnt )
194
#define print_header (G.print_header )
195
#define need_flags (G.need_flags )
196
#define buffer (G.buffer )
197
#define terminal_width (G.terminal_width )
198
#define INIT_G() do { setup_common_bufsiz(); } while (0)
200
#if ENABLE_FEATURE_PS_TIME
201
# if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS || !defined(__linux__)
202
# define get_kernel_HZ() (G.kernel_HZ)
204
/* non-ancient Linux standardized on 100 for "times" freq */
205
# define get_kernel_HZ() ((unsigned)100)
209
/* Print value to buf, max size+1 chars (including trailing '\0') */
211
static void func_user(char *buf, int size, const procps_status_t *ps)
214
safe_strncpy(buf, get_cached_username(ps->uid), size+1);
216
/* "compatible" version, but it's larger */
217
/* procps 2.18 shows numeric UID if name overflows the field */
218
/* TODO: get_cached_username() returns numeric string if
219
* user has no passwd record, we will display it
220
* left-justified here; too long usernames are shown
221
* as _right-justified_ IDs. Is it worth fixing? */
222
const char *user = get_cached_username(ps->uid);
223
if (strlen(user) <= size)
224
safe_strncpy(buf, user, size+1);
226
sprintf(buf, "%*u", size, (unsigned)ps->uid);
230
static void func_group(char *buf, int size, const procps_status_t *ps)
232
safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
235
static void func_comm(char *buf, int size, const procps_status_t *ps)
237
safe_strncpy(buf, ps->comm, size+1);
240
static void func_state(char *buf, int size, const procps_status_t *ps)
242
safe_strncpy(buf, ps->state, size+1);
245
static void func_args(char *buf, int size, const procps_status_t *ps)
247
read_cmdline(buf, size+1, ps->pid, ps->comm);
250
static void func_pid(char *buf, int size, const procps_status_t *ps)
252
sprintf(buf, "%*u", size, ps->pid);
255
static void func_ppid(char *buf, int size, const procps_status_t *ps)
257
sprintf(buf, "%*u", size, ps->ppid);
260
static void func_pgid(char *buf, int size, const procps_status_t *ps)
262
sprintf(buf, "%*u", size, ps->pgid);
265
static void func_sid(char *buf, int size, const procps_status_t *ps)
267
sprintf(buf, "%*u", size, ps->sid);
270
static void put_lu(char *buf, int size, unsigned long u)
274
/* see http://en.wikipedia.org/wiki/Tera */
275
smart_ulltoa4(u, buf4, " mgtpezy")[0] = '\0';
276
sprintf(buf, "%.*s", size, buf4);
279
static void func_vsz(char *buf, int size, const procps_status_t *ps)
281
put_lu(buf, size, ps->vsz);
284
static void func_rss(char *buf, int size, const procps_status_t *ps)
286
put_lu(buf, size, ps->rss);
289
static void func_tty(char *buf, int size, const procps_status_t *ps)
293
if (ps->tty_major) /* tty field of "0" means "no tty" */
294
snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
297
#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
298
static void func_rgroup(char *buf, int size, const procps_status_t *ps)
300
safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
302
static void func_ruser(char *buf, int size, const procps_status_t *ps)
304
safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
306
static void func_nice(char *buf, int size, const procps_status_t *ps)
308
sprintf(buf, "%*d", size, ps->niceness);
312
#if ENABLE_FEATURE_PS_TIME
313
static void format_time(char *buf, int size, unsigned long tt)
317
/* Used to show "14453:50" if tt is large. Ugly.
318
* procps-ng 3.3.10 uses "[[dd-]hh:]mm:ss" format.
319
* TODO: switch to that?
322
/* Formatting for 5-char TIME column.
323
* NB: "size" is not always 5: ELAPSED is wider (7),
324
* not taking advantage of that (yet?).
329
snprintf(buf, size+1, "%2u:%02u", (unsigned)tt, ff);
335
snprintf(buf, size+1, "%2uh%02u", (unsigned)tt, ff);
341
snprintf(buf, size+1, "%2ud%02u", (unsigned)tt, ff);
344
snprintf(buf, size+1, "%4lud", tt);
346
static void func_etime(char *buf, int size, const procps_status_t *ps)
348
/* elapsed time [[dd-]hh:]mm:ss; here only mm:ss */
351
mm = ps->start_time / get_kernel_HZ();
352
mm = G.seconds_since_boot - mm;
353
format_time(buf, size, mm);
355
static void func_time(char *buf, int size, const procps_status_t *ps)
357
/* cumulative time [[dd-]hh:]mm:ss; here only mm:ss */
360
mm = (ps->utime + ps->stime) / get_kernel_HZ();
361
format_time(buf, size, mm);
366
static void func_label(char *buf, int size, const procps_status_t *ps)
368
safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
373
static void func_pcpu(char *buf, int size, const procps_status_t *ps)
378
static const ps_out_t out_spec[] ALIGN_PTR = {
379
/* Mandated by http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html: */
380
{ 8 , "user" ,"USER" ,func_user ,PSSCAN_UIDGID },
381
{ 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
382
{ 16 , "comm" ,"COMMAND",func_comm ,PSSCAN_COMM },
383
{ MAX_WIDTH , "args" ,"COMMAND",func_args ,PSSCAN_COMM },
384
{ 5 , "pid" ,"PID" ,func_pid ,PSSCAN_PID },
385
{ 5 , "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID },
386
{ 5 , "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID },
387
#if ENABLE_FEATURE_PS_TIME
388
{ sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
390
#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
391
{ 5 , "nice" ,"NI" ,func_nice ,PSSCAN_NICE },
392
{ 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_RUIDGID },
393
{ 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_RUIDGID },
394
// { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
396
#if ENABLE_FEATURE_PS_TIME
397
{ 5 , "time" ,"TIME" ,func_time ,PSSCAN_STIME | PSSCAN_UTIME },
399
{ 6 , "tty" ,"TT" ,func_tty ,PSSCAN_TTY },
400
{ 4 , "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ },
401
/* Not mandated, but useful: */
402
{ 5 , "sid" ,"SID" ,func_sid ,PSSCAN_SID },
403
{ 4 , "stat" ,"STAT" ,func_state ,PSSCAN_STATE },
404
{ 4 , "rss" ,"RSS" ,func_rss ,PSSCAN_RSS },
406
{ 35 , "label" ,"LABEL" ,func_label ,PSSCAN_CONTEXT },
410
static ps_out_t* new_out_t(void)
412
out = xrealloc_vector(out, 2, out_cnt);
413
return &out[out_cnt++];
416
static const ps_out_t* find_out_spec(const char *name)
419
char buf[ARRAY_SIZE(out_spec)*7 + 1];
422
for (i = 0; i < ARRAY_SIZE(out_spec); i++) {
423
if (strncmp(name, out_spec[i].name6, 6) == 0)
425
p += sprintf(p, "%.6s,", out_spec[i].name6);
428
bb_error_msg_and_die("bad -o argument '%s', supported arguments: %s", name, buf);
431
static void parse_o(char* opt)
434
// POSIX: "-o is blank- or comma-separated list" (FIXME)
437
comma = strchr(opt, ',');
438
equal = strchr(opt, '=');
439
if (comma && (!equal || equal > comma)) {
441
*new_out_t() = *find_out_spec(opt);
446
// opt points to last spec in comma separated list.
447
// This one can have =HEADER part.
451
*new = *find_out_spec(opt);
456
comma = strchr(equal, ',');
459
// POSIX: the field widths shall be ... at least as wide as
460
// the header text (default or overridden value).
461
// If the header text is null, such as -o user=,
462
// the field width shall be at least as wide as the
463
// default header text
464
if (new->header[0]) {
465
new->width = strlen(new->header);
469
//*comma = ','; /* no, new->header should stay NUL-terminated */
474
static void alloc_line_buffer(void)
478
for (i = 0; i < out_cnt; i++) {
479
need_flags |= out[i].ps_flags;
480
if (out[i].header[0]) {
483
width += out[i].width + 1; /* "FIELD " */
484
if ((int)(width - terminal_width) > 0) {
485
/* The rest does not fit on the screen */
486
//out[i].width -= (width - terminal_width - 1);
492
if (!is_selinux_enabled())
493
need_flags &= ~PSSCAN_CONTEXT;
495
buffer = xmalloc(width + 1); /* for trailing \0 */
498
static void format_header(void)
511
if (++i == out_cnt) /* do not pad last field */
513
p += sprintf(p, "%-*s ", op->width, op->header);
515
strcpy(p, op->header);
517
printf("%.*s\n", terminal_width, buffer);
520
static void format_process(const procps_status_t *ps)
525
if (out_cnt) while (1) {
526
out[i].f(p, out[i].width, ps);
527
// POSIX: Any field need not be meaningful in all
528
// implementations. In such a case a hyphen ( '-' )
529
// should be output in place of the field value.
536
len = out[i].width - len + 1;
537
if (++i == out_cnt) /* do not pad last field */
539
p += sprintf(p, "%*s", len, " "); /* " ", not "", to ensure separation of fields */
541
printf("%.*s\n", terminal_width, buffer);
545
# define SELINUX_O_PREFIX "label,"
546
# define DEFAULT_O_STR (SELINUX_O_PREFIX "pid,user" IF_FEATURE_PS_TIME(",time") ",args")
548
# define DEFAULT_O_STR ("pid,user" IF_FEATURE_PS_TIME(",time") ",args")
551
int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
552
int ps_main(int argc UNUSED_PARAM, char **argv)
555
llist_t* opt_o = NULL;
556
char default_o[sizeof(DEFAULT_O_STR)];
557
#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS
569
OPT_T = (1 << 8) * ENABLE_FEATURE_SHOW_THREADS,
573
#if ENABLE_FEATURE_PS_TIME
574
G.seconds_since_boot = get_uptime();
575
# if ENABLE_FEATURE_PS_UNUSUAL_SYSTEMS || !defined(__linux__)
576
G.kernel_HZ = bb_clk_tck(); /* this is sysconf(_SC_CLK_TCK) */
581
// -a Write information for all processes associated with terminals
582
// Implementations may omit session leaders from this list
583
// -A Write information for all processes
584
// -d Write information for all processes, except session leaders
585
// -e Write information for all processes (equivalent to -A)
586
// -f Generate a full listing
587
// -l Generate a long listing
588
// -o col1,col2,col3=header
589
// Select which columns to display
590
/* We allow (and ignore) most of the above. FIXME.
591
* -T is picked for threads (POSIX hasn't standardized it).
592
* procps v3.2.7 supports -T and shows tids as SPID column,
593
* it also supports -L where it shows tids as LWP column.
595
#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS
598
getopt32(argv, "Zo:*aAdefl"IF_FEATURE_SHOW_THREADS("T"), &opt_o);
602
parse_o(llist_pop(&opt_o));
605
/* Below: parse_o() needs char*, NOT const char*,
606
* can't pass it constant string. Need to make a copy first.
609
if (!(opt & OPT_Z) || !is_selinux_enabled()) {
610
/* no -Z or no SELinux: do not show LABEL */
611
strcpy(default_o, DEFAULT_O_STR + sizeof(SELINUX_O_PREFIX)-1);
615
strcpy(default_o, DEFAULT_O_STR);
619
#if ENABLE_FEATURE_SHOW_THREADS
621
need_flags |= PSSCAN_TASKS;
624
/* Was INT_MAX, but some libc's go belly up with printf("%.*s")
625
* and such large widths */
626
terminal_width = MAX_WIDTH;
628
terminal_width = get_terminal_width(0);
629
if (--terminal_width > MAX_WIDTH)
630
terminal_width = MAX_WIDTH;
636
while ((p = procps_scan(p, need_flags)) != NULL) {
644
#else /* !ENABLE_DESKTOP */
647
int ps_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
648
int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
651
int psscan_flags = PSSCAN_PID | PSSCAN_UIDGID
652
| PSSCAN_STATE | PSSCAN_VSZ | PSSCAN_COMM;
653
unsigned terminal_width IF_NOT_FEATURE_PS_WIDE(= 79);
655
OPT_Z = (1 << 0) * ENABLE_SELINUX,
656
OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
657
OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG,
659
#if ENABLE_FEATURE_PS_LONG
660
time_t now = now; /* for compiler */
661
unsigned long uptime = uptime;
663
/* If we support any options, parse argv */
664
#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE || ENABLE_FEATURE_PS_LONG
666
# if ENABLE_FEATURE_PS_WIDE
667
/* -w is a bit complicated */
669
make_all_argv_opts(argv);
670
opts = getopt32(argv, "^"
671
IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")"w"
675
/* if w is given once, GNU ps sets the width to 132,
676
* if w is given more than once, it is "unlimited"
679
terminal_width = (w_count == 1) ? 132 : MAX_WIDTH;
681
terminal_width = get_terminal_width(0);
683
if (--terminal_width > MAX_WIDTH)
684
terminal_width = MAX_WIDTH;
687
/* -w is not supported, only -Z and/or -T */
688
make_all_argv_opts(argv);
689
opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
693
if ((opts & OPT_Z) && is_selinux_enabled()) {
694
psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
695
| PSSCAN_STATE | PSSCAN_COMM;
696
puts(" PID CONTEXT STAT COMMAND");
700
psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID
701
| PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM
702
| PSSCAN_VSZ | PSSCAN_RSS;
703
/* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
709
* -PRI The priority of the process; higher numbers mean lower priority
711
* -ADDR The address of the process (?)
712
* SZ The size in blocks of the core image
713
* -WCHAN The event for which the process is waiting or sleeping
715
* TIME The cumulative execution time
717
* We don't show fields marked with '-'.
718
* We show VSZ and RSS instead of SZ.
719
* We also show STIME (standard says that -f shows it, -l doesn't).
721
puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD");
722
# if ENABLE_FEATURE_PS_LONG
724
uptime = get_uptime();
728
puts(" PID USER VSZ STAT COMMAND");
731
psscan_flags |= PSSCAN_TASKS;
736
while ((p = procps_scan(p, psscan_flags)) != NULL) {
739
if (psscan_flags & PSSCAN_CONTEXT) {
740
len = printf("%5u %-32.32s %s ",
742
p->context ? p->context : "unknown",
748
smart_ulltoa5(p->vsz, buf6, " mgtpezy")[0] = '\0';
749
#if ENABLE_FEATURE_PS_LONG
751
char bufr[6], stime_str[6];
752
char tty[2 * sizeof(int)*3 + 2];
754
unsigned sut = (p->stime + p->utime) / 100;
755
unsigned elapsed = uptime - (p->start_time / 100);
756
time_t start = now - elapsed;
757
struct tm *tm = localtime(&start);
759
smart_ulltoa5(p->rss, bufr, " mgtpezy")[0] = '\0';
761
if (p->tty_major == 136)
762
/* It should be pts/N, not ptsN, but N > 9
763
* will overflow field width...
765
endp = stpcpy(tty, "pts");
767
if (p->tty_major == 4) {
768
endp = stpcpy(tty, "tty");
769
if (p->tty_minor >= 64) {
775
endp = tty + sprintf(tty, "%d:", p->tty_major);
776
strcpy(endp, utoa(p->tty_minor));
778
strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm);
780
// S UID PID PPID VSZ RSS TTY STIME TIME CMD
781
len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ",
782
p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty,
783
stime_str, sut / 3600, (sut % 3600) / 60, sut % 60);
787
const char *user = get_cached_username(p->uid);
788
len = printf("%5u %-8.8s %s %s ",
789
p->pid, user, buf6, p->state);
794
int sz = terminal_width - len;
797
read_cmdline(buf, sz, p->pid, p->comm);
802
if (ENABLE_FEATURE_CLEAN_UP)
803
clear_username_cache();
807
#endif /* !ENABLE_DESKTOP */
2
* Mini ps implementation for busybox
5
* Copyright (C) 1999 by Lineo, inc.
6
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32
typedef struct proc_s {
34
cmd[16]; /* basename of executable file in call to exec(2) */
36
ruid, rgid, /* real only (sorry) */
38
ppid; /* pid of parent process */
40
state; /* single-char code for process state (S=sleeping) */
45
static int file2str(char *filename, char *ret, int cap)
49
if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) return -1;
50
if ( (num_read = read(fd, ret, cap - 1)) <= 0 ) return -1;
57
static void parse_proc_status(char* S, proc_t* P)
60
memset(P->cmd, 0, sizeof P->cmd);
61
sscanf (S, "Name:\t%15c", P->cmd);
62
tmp = strchr(P->cmd,'\n');
65
tmp = strstr (S,"State");
66
sscanf (tmp, "State:\t%c", &P->state);
68
tmp = strstr (S,"Pid:");
75
else fprintf(stderr, "Internal error!\n");
77
/* For busybox, ignoring effective, saved, etc */
78
tmp = strstr (S,"Uid:");
80
"Uid:\t%d", &P->ruid);
81
else fprintf(stderr, "Internal error!\n");
83
tmp = strstr (S,"Gid:");
85
"Gid:\t%d", &P->rgid);
86
else fprintf(stderr, "Internal error!\n");
91
extern int ps_main(int argc, char **argv)
97
char path[32], sbuf[512];
99
char groupName[10]="";
102
if ( argc>1 && **(argv+1) == '-' ) {
103
usage ("ps\n\nReport process status\n\nThis version of ps accepts no options.\n");
106
dir = opendir("/proc");
108
perror("Can't open /proc");
112
fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
113
while ((entry = readdir(dir)) != NULL) {
117
if (! isdigit(*entry->d_name))
119
sprintf(path, "/proc/%s/status", entry->d_name);
120
if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) {
121
parse_proc_status(sbuf, &p);
124
/* Make some adjustments as needed */
125
my_getpwuid( uidName, p.ruid);
126
my_getgrgid( groupName, p.rgid);
127
if (*uidName == '\0')
128
sprintf( uidName, "%d", p.ruid);
129
if (*groupName == '\0')
130
sprintf( groupName, "%d", p.rgid);
132
fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
133
sprintf(path, "/proc/%s/cmdline", entry->d_name);
134
file = fopen(path, "r");
140
while (((c = getc(file)) != EOF) && (i < 53)) {
147
fprintf(stdout, "%s", p.cmd);
148
fprintf(stdout, "\n");