[PATCH] Added to ps ruser,group,rgroup,nice options for -o
David Krakov
krakov at gmail.com
Fri Jul 3 16:19:51 UTC 2009
Use FEATURE_PS_ENABLE_ADDITIONAL_COLUMNS to use.
Updated POSIX conformance accordingly.
Notice added scanning of /proc/<pid>/status file to procps to obtain
real user and group ids.
Tested with and without ENABLE_FEATURE_FAST_TOP (changes reading of
niceness value of a process in procps).
Signed-off-by: David Krakov <krakov at gmail.org>
---
docs/posix_conformance.txt | 2 +-
include/libbb.h | 8 +++++++-
libbb/procps.c | 44 +++++++++++++++++++++++++++++++++++++++++---
procps/Config.in | 8 ++++++++
procps/ps.c | 39 +++++++++++++++++++++++++++++++++------
5 files changed, 90 insertions(+), 11 deletions(-)
diff --git a/docs/posix_conformance.txt b/docs/posix_conformance.txt
index 5451a32..44ddf45 100644
--- a/docs/posix_conformance.txt
+++ b/docs/posix_conformance.txt
@@ -501,7 +501,7 @@ ps POSIX options
-g grouplist | no | no |
-l | no | no |
-n namelist | no | no |
- -o format | yes | no | not supported: ruser, group,
rgroup, pcpu
+ -o format | yes | no | not supported: pcpu
-p proclist | no | no |
-t termlist | no | no |
-u userlist | no | no |
diff --git a/include/libbb.h b/include/libbb.h
index e1c36a5..18efe98 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1276,7 +1276,12 @@ typedef struct procps_status_t {
unsigned sid;
unsigned uid;
unsigned gid;
- unsigned tty_major,tty_minor;
+#if ENABLE_FEATURE_PS_ADDITONAL_COLUMNS
+ unsigned ruid;
+ unsigned rgid;
+ long niceness;
+#endif
+ unsigned tty_major,tty_minor;
#if ENABLE_FEATURE_TOPMEM
unsigned long mapped_rw;
unsigned long mapped_ro;
@@ -1323,6 +1328,7 @@ enum {
IF_SELINUX(PSSCAN_CONTEXT = 1 << 17,)
PSSCAN_START_TIME = 1 << 18,
PSSCAN_CPU = 1 << 19,
+ PSSCAN_STATUS = 1 << 20,
/* These are all retrieved from proc/NN/stat in one go: */
PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID
/**/ | PSSCAN_COMM | PSSCAN_STATE
diff --git a/libbb/procps.c b/libbb/procps.c
index 445e709..434ced6 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -151,6 +151,18 @@ static unsigned long fast_strtoul_10(char **endptr)
*endptr = str + 1; /* We skip trailing space! */
return n;
}
+
+static long fast_strtol_10(char **endptr)
+{
+ int sign = **endptr == '-' ? -1 : 1;
+
+ if (sign == -1) {
+ ++*endptr;
+ }
+ return (long)fast_strtoul_10(endptr) * sign;
+}
+
+
static char *skip_fields(char *str, int count)
{
do {
@@ -208,7 +220,7 @@ procps_status_t* FAST_FUNC
procps_scan(procps_status_t* sp, int flags)
if (flags & PSSCAN_UIDGID) {
if (stat(filename, &sb))
break;
- /* Need comment - is this effective or real UID/GID? */
+ /* Effective UID/GID, not real */
sp->uid = sb.st_uid;
sp->gid = sb.st_gid;
}
@@ -264,7 +276,6 @@ procps_status_t* FAST_FUNC
procps_scan(procps_status_t* sp, int flags)
, &sp->last_seen_on_cpu
#endif
);
-
if (n < 11)
break;
#if ENABLE_FEATURE_TOP_SMP_PROCESS
@@ -293,7 +304,7 @@ procps_status_t* FAST_FUNC
procps_scan(procps_status_t* sp, int flags)
sp->utime = fast_strtoul_10(&cp);
sp->stime = fast_strtoul_10(&cp);
cp = skip_fields(cp, 3); /* cutime, cstime, priority */
- tasknice = fast_strtoul_10(&cp);
+ tasknice = fast_strtol_10(&cp);
cp = skip_fields(cp, 2); /* timeout, it_real_value */
sp->start_time = fast_strtoul_10(&cp);
/* vsz is in bytes and we want kb */
@@ -310,6 +321,10 @@ procps_status_t* FAST_FUNC
procps_scan(procps_status_t* sp, int flags)
#endif
#endif /* end of !ENABLE_FEATURE_TOP_SMP_PROCESS */
+#if ENABLE_FEATURE_PS_ADDITONAL_COLUMNS
+ sp->niceness = tasknice;
+#endif
+
if (sp->vsz == 0 && sp->state[0] != 'Z')
sp->state[1] = 'W';
else
@@ -372,7 +387,30 @@ procps_status_t* FAST_FUNC
procps_scan(procps_status_t* sp, int flags)
fclose(file);
}
#endif /* TOPMEM */
+#if ENABLE_FEATURE_PS_ADDITONAL_COLUMNS
+ if (flags & (PSSCAN_STATUS)) {
+ FILE *file;
+ strcpy(filename_tail, "/status");
+ file = fopen_for_read(filename);
+ if (!file)
+ break;
+ while (fgets(buf, sizeof(buf), file)) {
+ char *tp;
+#define SCAN_TWO(str, name1, name2) \
+ if (strncmp(buf, str, sizeof(str)-1) == 0) { \
+ tp = skip_whitespace(buf + sizeof(str)-1); \
+ sscanf(tp, "%u\t%u", &sp->name1, &sp->name2); \
+ continue; \
+ }
+ SCAN_TWO("Uid:" , ruid, uid );
+ SCAN_TWO("Gid:" , rgid, gid );
+#undef SCAN_TWO
+
+ }
+ fclose(file);
+ }
+#endif /* PS_ADDITONAL_COLUMNS */
#if 0 /* PSSCAN_CMD is not used */
if (flags & (PSSCAN_CMD|PSSCAN_ARGV0)) {
free(sp->argv0);
diff --git a/procps/Config.in b/procps/Config.in
index 702442a..5df8646 100644
--- a/procps/Config.in
+++ b/procps/Config.in
@@ -106,6 +106,14 @@ config FEATURE_PS_TIME
help
Support -o time and -o etime output specifiers.
+config FEATURE_PS_ADDITONAL_COLUMNS
+ bool "Enable additional ps columns"
+ default n
+ depends on PS && DESKTOP
+ help
+ Support -o group,rgroup,ruser,nice
+
+
config FEATURE_PS_UNUSUAL_SYSTEMS
bool "Support Linux prior to 2.4.0 and non-ELF systems"
default n
diff --git a/procps/ps.c b/procps/ps.c
index b9a4aef..bc48cdc 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -32,7 +32,7 @@ enum { MAX_WIDTH = 2*1024 };
typedef struct {
uint16_t width;
- char name[6];
+ char name[7];
const char *header;
void (*f)(char *buf, int size, const procps_status_t *ps);
int ps_flags;
@@ -227,6 +227,31 @@ static void func_tty(char *buf, int size, const
procps_status_t *ps)
snprintf(buf, size+1, "%u,%u", ps->tty_major, ps->tty_minor);
}
+
+#if ENABLE_FEATURE_PS_ADDITONAL_COLUMNS
+
+static void func_group(char *buf, int size, const procps_status_t *ps)
+{
+ safe_strncpy(buf, get_cached_groupname(ps->gid), size+1);
+}
+
+static void func_rgroup(char *buf, int size, const procps_status_t *ps)
+{
+ safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
+}
+
+static void func_ruser(char *buf, int size, const procps_status_t *ps)
+{
+ safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
+}
+
+static void func_nice(char *buf, int size, const procps_status_t *ps)
+{
+ sprintf(buf, "%*ld", size, ps->niceness);
+}
+
+#endif /* FEATURE_PS_ADDITONAL_COLUMNS */
+
#if ENABLE_FEATURE_PS_TIME
static void func_etime(char *buf, int size, const procps_status_t *ps)
{
@@ -284,11 +309,13 @@ static const ps_out_t out_spec[] = {
#if ENABLE_FEATURE_PS_TIME
{ sizeof("ELAPSED")-1, "etime" ,"ELAPSED",func_etime ,PSSCAN_START_TIME },
#endif
-// { sizeof("GROUP" )-1, "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
-// { sizeof("NI" )-1, "nice" ,"NI" ,func_nice ,PSSCAN_ },
-// { sizeof("%CPU" )-1, "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
-// { sizeof("RGROUP" )-1, "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID },
-// { sizeof("RUSER" )-1, "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID },
+#if ENABLE_FEATURE_PS_ADDITONAL_COLUMNS
+ { 8 , "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID },
+ { 5 , "nice" ,"NI" ,func_nice ,PSSCAN_STAT },
+ { 8 , "rgroup","RGROUP" ,func_rgroup,PSSCAN_STATUS },
+ { 8 , "ruser" ,"RUSER" ,func_ruser ,PSSCAN_STATUS },
+// { 5 , "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ },
+#endif
#if ENABLE_FEATURE_PS_TIME
{ 6 , "time" ,"TIME" ,func_time ,PSSCAN_STIME |
PSSCAN_UTIME },
#endif
--
1.5.4.3
More information about the busybox
mailing list