ps: add support for -l for !DESKTOP

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2011-09-26 02:56:08 +02:00
parent d184a728cf
commit 8d9ac30572
2 changed files with 94 additions and 19 deletions

View File

@ -90,12 +90,20 @@ config PS
config FEATURE_PS_WIDE config FEATURE_PS_WIDE
bool "Enable wide output option (-w)" bool "Enable wide output option (-w)"
default y default y
depends on PS depends on PS && !DESKTOP
help help
Support argument 'w' for wide output. Support argument 'w' for wide output.
If given once, 132 chars are printed, and if given more If given once, 132 chars are printed, and if given more
than once, the length is unlimited. than once, the length is unlimited.
config FEATURE_PS_LONG
bool "Enable long output option (-l)"
default y
depends on PS && !DESKTOP
help
Support argument 'l' for long output.
Adds fields PPID, RSS, START, TIME & TTY
config FEATURE_PS_TIME config FEATURE_PS_TIME
bool "Enable time and elapsed time output" bool "Enable time and elapsed time output"
default y default y

View File

@ -39,6 +39,12 @@
//usage: IF_FEATURE_PS_WIDE( //usage: IF_FEATURE_PS_WIDE(
//usage: "\n w Wide output" //usage: "\n w Wide output"
//usage: ) //usage: )
//usage: IF_FEATURE_PS_LONG(
//usage: "\n l Long output"
//usage: )
//usage: IF_FEATURE_SHOW_THREADS(
//usage: "\n T Show threads"
//usage: )
//usage: //usage:
//usage:#endif /* ENABLE_DESKTOP */ //usage:#endif /* ENABLE_DESKTOP */
//usage: //usage:
@ -56,15 +62,15 @@
//usage: " 2990 andersen andersen R ps\n" //usage: " 2990 andersen andersen R ps\n"
#include "libbb.h" #include "libbb.h"
#ifdef __linux__
# include <sys/sysinfo.h>
#endif
/* Absolute maximum on output line length */ /* Absolute maximum on output line length */
enum { MAX_WIDTH = 2*1024 }; enum { MAX_WIDTH = 2*1024 };
#if ENABLE_DESKTOP #if ENABLE_DESKTOP
#ifdef __linux__
# include <sys/sysinfo.h>
#endif
#include <sys/times.h> /* for times() */ #include <sys/times.h> /* for times() */
#ifndef AT_CLKTCK #ifndef AT_CLKTCK
# define AT_CLKTCK 17 # define AT_CLKTCK 17
@ -625,15 +631,21 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
enum { enum {
OPT_Z = (1 << 0) * ENABLE_SELINUX, OPT_Z = (1 << 0) * ENABLE_SELINUX,
OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS, OPT_T = (1 << ENABLE_SELINUX) * ENABLE_FEATURE_SHOW_THREADS,
OPT_l = (1 << ENABLE_SELINUX) * (1 << ENABLE_FEATURE_SHOW_THREADS) * ENABLE_FEATURE_PS_LONG,
}; };
#if ENABLE_FEATURE_PS_LONG
time_t now = now;
struct sysinfo info;
#endif
int opts = 0; int opts = 0;
/* If we support any options, parse argv */ /* If we support any options, parse argv */
#if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE #if ENABLE_SELINUX || ENABLE_FEATURE_SHOW_THREADS || ENABLE_FEATURE_PS_WIDE || ENABLE_FEATURE_PS_LONG
# if ENABLE_FEATURE_PS_WIDE # if ENABLE_FEATURE_PS_WIDE
/* -w is a bit complicated */ /* -w is a bit complicated */
int w_count = 0; int w_count = 0;
opt_complementary = "-:ww"; opt_complementary = "-:ww";
opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")"w", &w_count); opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l")
"w", &w_count);
/* if w is given once, GNU ps sets the width to 132, /* if w is given once, GNU ps sets the width to 132,
* if w is given more than once, it is "unlimited" * if w is given more than once, it is "unlimited"
*/ */
@ -648,23 +660,47 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
# else # else
/* -w is not supported, only -Z and/or -T */ /* -w is not supported, only -Z and/or -T */
opt_complementary = "-"; opt_complementary = "-";
opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")); opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
# endif # endif
#endif
#if ENABLE_SELINUX # if ENABLE_SELINUX
if ((opts & OPT_Z) && is_selinux_enabled()) { if ((opts & OPT_Z) && is_selinux_enabled()) {
psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
| PSSCAN_STATE | PSSCAN_COMM; | PSSCAN_STATE | PSSCAN_COMM;
puts(" PID CONTEXT STAT COMMAND"); puts(" PID CONTEXT STAT COMMAND");
} else } else
#endif # endif
{ if (opts & OPT_l) {
psscan_flags = PSSCAN_STATE | PSSCAN_UIDGID | PSSCAN_PID | PSSCAN_PPID
| PSSCAN_TTY | PSSCAN_STIME | PSSCAN_UTIME | PSSCAN_COMM
| PSSCAN_VSZ | PSSCAN_RSS;
/* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
* mandates for -l:
* -F Flags associated with the process (?)
* S The state of the process
* UID,PID,PPID
* -C Processor utilization for scheduling
* -PRI The priority of the process; higher numbers mean lower priority
* -NI Nice value; used in priority computation
* -ADDR The address of the process
* SZ The size in blocks of the core image of the process
* -WCHAN The event for which the process is waiting or sleeping
* TTY
* TIME The cumulative execution time for the process
* CMD
* We don't show fileds marked with '-'. We show VSZ and RSS instead of SZ
*/
puts("S UID PID PPID VSZ RSS TTY TIME CMD");
now = time(NULL);
sysinfo(&info);
}
else {
puts(" PID USER VSZ STAT COMMAND"); puts(" PID USER VSZ STAT COMMAND");
} }
if (opts & OPT_T) { if (opts & OPT_T) {
psscan_flags |= PSSCAN_TASKS; psscan_flags |= PSSCAN_TASKS;
} }
#endif
p = NULL; p = NULL;
while ((p = procps_scan(p, psscan_flags)) != NULL) { while ((p = procps_scan(p, psscan_flags)) != NULL) {
@ -678,15 +714,46 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
} else } else
#endif #endif
{ {
const char *user = get_cached_username(p->uid); char buf6[6];
//if (p->vsz == 0) smart_ulltoa5(p->vsz, buf6, " mgtpezy");
// len = printf("%5u %-8.8s %s ", buf6[5] = '\0';
// p->pid, user, p->state); #if ENABLE_FEATURE_PS_LONG
//else if (opts & OPT_l) {
char bufr[6], strt[6];
char tty[2 * sizeof(int)*3 + 2];
char *endp;
unsigned sut = (p->stime + p->utime) / 100;
unsigned elapsed = info.uptime - (p->start_time / 100);
time_t start = now - elapsed;
struct tm *tm = localtime(&start);
smart_ulltoa5(p->rss, bufr, " mgtpezy");
bufr[5] = '\0';
if (p->tty_major == 136)
endp = stpcpy(tty, "pts/");
else
if (p->tty_major == 4) {
endp = stpcpy(tty, "tty");
if (p->tty_minor >= 64) {
p->tty_minor -= 64;
*endp++ = 'S';
}
}
else
endp = tty + sprintf(tty, "%d:", p->tty_major);
strcpy(endp, utoa(p->tty_minor));
strftime(strt, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm);
strt[5] = '\0';
// S UID PID PPID VSZ RSS TTY TIME CMD
len = printf("%c %5u %5u %5u %5s %5s %-5s %02u:%02u:%02u ",
p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty,
sut / 3600, (sut % 3600) / 60, sut % 60);
} else
#endif
{ {
char buf6[6]; const char *user = get_cached_username(p->uid);
smart_ulltoa5(p->vsz, buf6, " mgtpezy");
buf6[5] = '\0';
len = printf("%5u %-8.8s %s %s ", len = printf("%5u %-8.8s %s %s ",
p->pid, user, buf6, p->state); p->pid, user, buf6, p->state);
} }