From 276c89bb9568a22346555c2cd3cc83d80ad5e7d6 Mon Sep 17 00:00:00 2001 From: seb Date: Sun, 3 Mar 2013 15:46:28 +0100 Subject: [PATCH] pidstat's option -U updated. pidstat's option -U can now be followed by a user name. In this case, only tasks belonging to the specified user are displayed by iostat. pidstat manual page updated. --- CHANGES | 4 +++- man/pidstat.1 | 9 ++++++--- pidstat.c | 35 ++++++++++++++++++++++++++--------- pidstat.h | 3 +++ 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/CHANGES b/CHANGES index 502f412..60a678f 100644 --- a/CHANGES +++ b/CHANGES @@ -9,7 +9,9 @@ xxxx/xx/xx: Version 10.1.4 - Sebastien Godard (sysstat orange.fr) SIGINT (crtl/c). * pidstat now displays task's UID for all tasks. * pidstat: -U option added. This option tells pidstat to display - the username of the task instead of its UID. + the username of the task instead of its UID. When this option is + followed by a user name, then only tasks belonging to the + specified user are displayed by pidstat. * pidstat manual page updated. * Now use sigaction() instead of signal() for signals handling to avoid portability problems. diff --git a/man/pidstat.1 b/man/pidstat.1 index fa50547..21e8385 100644 --- a/man/pidstat.1 +++ b/man/pidstat.1 @@ -2,7 +2,7 @@ .SH NAME pidstat \- Report statistics for Linux tasks. .SH SYNOPSIS -.B pidstat [ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -U ] [ -u ] [ -V ] +.B pidstat [ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -U [ username ] ] [ -u ] [ -V ] .B [ -w ] [ -C .I comm .B ] [ -p { @@ -295,6 +295,11 @@ The identification number of the thread group leader. The identification number of the thread being monitored. .RE .RE +.IP "-U [ username ]" +Display the real user name of the tasks being monitored instead of the UID. +If +.I username +is specified, then only tasks belonging to the specified user are displayed. .IP -u Report CPU utilization. @@ -405,8 +410,6 @@ The command name of the task which is being monitored together with its children. .RE .RE -.IP -U -Display the real user name of the tasks being monitored instead of the UID. .IP -V Print version number then exit. .IP -w diff --git a/pidstat.c b/pidstat.c index 23c245a..2fee6e9 100644 --- a/pidstat.c +++ b/pidstat.c @@ -54,6 +54,7 @@ unsigned int *pid_array = NULL; struct pid_stats st_pid_null; struct tm ps_tstamp[3]; char commstr[MAX_COMM_LEN]; +char userstr[MAX_USER_LEN]; unsigned int pid_nr = 0; /* Nb of PID to display */ unsigned int pid_array_nr = 0; @@ -82,8 +83,8 @@ void usage(char *progname) progname); fprintf(stderr, _("Options are:\n" - "[ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -U ] [ -u ] [ -V ]\n" - "[ -w ] [ -C ] [ -p { [,...] | SELF | ALL } ]\n" + "[ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -U [ username ] ] [ -u ]\n" + "[ -V ] [ -w ] [ -C ] [ -p { [,...] | SELF | ALL } ]\n" "[ -T { TASK | CHILD | ALL } ]\n")); exit(1); } @@ -927,6 +928,7 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity, { int q, rc; regex_t regex; + struct passwd *pwdent; *pstc = st_pid_list[curr] + p; @@ -1028,7 +1030,6 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity, } else if (DISPLAY_PID(pidflag)) { - *pstp = st_pid_list[prev] + p; if (!(*pstp)->pid) @@ -1037,7 +1038,6 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity, } if (COMMAND_STRING(pidflag)) { - if (regcomp(®ex, commstr, REG_EXTENDED | REG_NOSUB) != 0) /* Error in preparing regex structure */ return -1; @@ -1049,6 +1049,14 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity, /* regex pattern not found in command name */ return -1; } + + if (USER_STRING(pidflag)) { + if ((pwdent = getpwuid((*pstc)->uid)) != NULL) { + if (strcmp(pwdent->pw_name, userstr)) + /* This PID doesn't belong to user */ + return -1; + } + } return 1; } @@ -2101,6 +2109,20 @@ int main(int argc, char **argv) usage(argv[0]); } } + + else if (!strcmp(argv[opt], "-U")) { + /* Display username instead of UID */ + pidflag |= P_D_USERNAME; + if (argv[++opt] && (argv[opt][0] != '-') && + (strspn(argv[opt], DIGITS) != strlen(argv[opt]))) { + strncpy(userstr, argv[opt++], MAX_USER_LEN); + userstr[MAX_USER_LEN - 1] = '\0'; + pidflag |= P_F_USERSTR; + if (!strlen(userstr)) { + usage(argv[0]); + } + } + } else if (!strncmp(argv[opt], "-", 1)) { for (i = 1; *(argv[opt] + i); i++) { @@ -2145,11 +2167,6 @@ int main(int argc, char **argv) pidflag |= P_D_TID; break; - case 'U': - /* Display username instead of UID */ - pidflag |= P_D_USERNAME; - break; - case 'u': /* Display CPU usage */ actflag |= P_A_CPU; diff --git a/pidstat.h b/pidstat.h index 76262ca..c4a4cab 100644 --- a/pidstat.h +++ b/pidstat.h @@ -17,6 +17,7 @@ #define MAX_COMM_LEN 128 #define MAX_CMDLINE_LEN 128 +#define MAX_USER_LEN 32 /* Activities */ #define P_A_CPU 0x01 @@ -48,6 +49,7 @@ #define P_D_ONELINE 0x040 #define P_D_CMDLINE 0x080 #define P_D_USERNAME 0x100 +#define P_F_USERSTR 0x200 #define DISPLAY_PID(m) (((m) & P_D_PID) == P_D_PID) #define DISPLAY_ALL_PID(m) (((m) & P_D_ALL_PID) == P_D_ALL_PID) @@ -58,6 +60,7 @@ #define DISPLAY_ONELINE(m) (((m) & P_D_ONELINE) == P_D_ONELINE) #define DISPLAY_CMDLINE(m) (((m) & P_D_CMDLINE) == P_D_CMDLINE) #define DISPLAY_USERNAME(m) (((m) & P_D_USERNAME) == P_D_USERNAME) +#define USER_STRING(m) (((m) & P_F_USERSTR) == P_F_USERSTR) #define F_NO_PID_IO 0x01 -- 2.40.0