From 8d9ac30572818bbe78ef08d6580308e013972df3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 26 Sep 2011 02:56:08 +0200 Subject: ps: add support for -l for !DESKTOP Signed-off-by: Denys Vlasenko --- procps/ps.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 18 deletions(-) (limited to 'procps/ps.c') diff --git a/procps/ps.c b/procps/ps.c index dcc0f7bd4..c98384d71 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -39,6 +39,12 @@ //usage: IF_FEATURE_PS_WIDE( //usage: "\n w Wide output" //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:#endif /* ENABLE_DESKTOP */ //usage: @@ -56,15 +62,15 @@ //usage: " 2990 andersen andersen R ps\n" #include "libbb.h" +#ifdef __linux__ +# include +#endif /* Absolute maximum on output line length */ enum { MAX_WIDTH = 2*1024 }; #if ENABLE_DESKTOP -#ifdef __linux__ -# include -#endif #include /* for times() */ #ifndef AT_CLKTCK # define AT_CLKTCK 17 @@ -625,15 +631,21 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) enum { OPT_Z = (1 << 0) * ENABLE_SELINUX, 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; /* 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 /* -w is a bit complicated */ int w_count = 0; 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 more than once, it is "unlimited" */ @@ -648,23 +660,47 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) # else /* -w is not supported, only -Z and/or -T */ 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 -#if ENABLE_SELINUX +# if ENABLE_SELINUX if ((opts & OPT_Z) && is_selinux_enabled()) { psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT | PSSCAN_STATE | PSSCAN_COMM; puts(" PID CONTEXT STAT COMMAND"); } 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"); } if (opts & OPT_T) { psscan_flags |= PSSCAN_TASKS; } +#endif p = 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 #endif { - const char *user = get_cached_username(p->uid); - //if (p->vsz == 0) - // len = printf("%5u %-8.8s %s ", - // p->pid, user, p->state); - //else + char buf6[6]; + smart_ulltoa5(p->vsz, buf6, " mgtpezy"); + buf6[5] = '\0'; +#if ENABLE_FEATURE_PS_LONG + 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]; - smart_ulltoa5(p->vsz, buf6, " mgtpezy"); - buf6[5] = '\0'; + const char *user = get_cached_username(p->uid); len = printf("%5u %-8.8s %s %s ", p->pid, user, buf6, p->state); } -- cgit v1.2.3-55-g6feb From b64bd16459636c8a7ccf75854e0a2df590d97dec Mon Sep 17 00:00:00 2001 From: Flemming Madsen Date: Tue, 27 Sep 2011 15:31:25 +0200 Subject: ps: with -l, show STIME too Signed-off-by: Flemming Madsen Signed-off-by: Denys Vlasenko --- procps/ps.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'procps/ps.c') diff --git a/procps/ps.c b/procps/ps.c index c98384d71..3815c1efa 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -676,21 +676,23 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) | 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 + * -F Flags (?) + * S State * UID,PID,PPID - * -C Processor utilization for scheduling + * -C CPU usage * -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 + * -NI Nice value + * -ADDR The address of the process (?) + * SZ The size in blocks of the core image * -WCHAN The event for which the process is waiting or sleeping * TTY - * TIME The cumulative execution time for the process + * TIME The cumulative execution time * CMD - * We don't show fileds marked with '-'. We show VSZ and RSS instead of SZ + * We don't show fields marked with '-'. + * We show VSZ and RSS instead of SZ. + * We also show STIME (standard says that -f shows it, -l doesn't). */ - puts("S UID PID PPID VSZ RSS TTY TIME CMD"); + puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD"); now = time(NULL); sysinfo(&info); } @@ -719,7 +721,7 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) buf6[5] = '\0'; #if ENABLE_FEATURE_PS_LONG if (opts & OPT_l) { - char bufr[6], strt[6]; + char bufr[6], stime_str[6]; char tty[2 * sizeof(int)*3 + 2]; char *endp; unsigned sut = (p->stime + p->utime) / 100; @@ -731,7 +733,10 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) bufr[5] = '\0'; if (p->tty_major == 136) - endp = stpcpy(tty, "pts/"); + /* It should be pts/N, not ptsN, but N > 9 + * will overflow field width... + */ + endp = stpcpy(tty, "pts"); else if (p->tty_major == 4) { endp = stpcpy(tty, "tty"); @@ -744,12 +749,12 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) 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 ", + strftime(stime_str, 6, (elapsed >= (24 * 60 * 60)) ? "%b%d" : "%H:%M", tm); + stime_str[5] = '\0'; + // S UID PID PPID VSZ RSS TTY STIME TIME CMD + len = printf("%c %5u %5u %5u %5s %5s %-5s %s %02u:%02u:%02u ", p->state[0], p->uid, p->pid, p->ppid, buf6, bufr, tty, - sut / 3600, (sut % 3600) / 60, sut % 60); + stime_str, sut / 3600, (sut % 3600) / 60, sut % 60); } else #endif { -- cgit v1.2.3-55-g6feb From 85b1f292c9c1526448db29b0f2590f8adefa6b04 Mon Sep 17 00:00:00 2001 From: Cristian Ionescu-Idbohrn Date: Thu, 29 Sep 2011 11:02:00 +0200 Subject: ps: fix for !FEATURE_PS_LONG build Signed-off-by: Cristian Ionescu-Idbohrn Signed-off-by: Denys Vlasenko --- procps/ps.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'procps/ps.c') diff --git a/procps/ps.c b/procps/ps.c index 3815c1efa..4727b218b 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -693,8 +693,10 @@ int ps_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) * We also show STIME (standard says that -f shows it, -l doesn't). */ puts("S UID PID PPID VSZ RSS TTY STIME TIME CMD"); +#if ENABLE_FEATURE_PS_LONG now = time(NULL); sysinfo(&info); +#endif } else { puts(" PID USER VSZ STAT COMMAND"); -- cgit v1.2.3-55-g6feb