]> granicus.if.org Git - sysstat/commitdiff
pidstat: Add a replacement function for getpwuid()
authorSebastien GODARD <sysstat@users.noreply.github.com>
Sat, 1 Feb 2020 13:47:31 +0000 (14:47 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Sat, 1 Feb 2020 13:47:31 +0000 (14:47 +0100)
This replacement function will be used in tests environment.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
pidstat.c
systest.c
systest.h

index bfadddf78bd872f8dc510b926d083e984afb419e..e62641c3c02dce0fd69eaf06281a4ff37034cb69 100644 (file)
--- a/pidstat.c
+++ b/pidstat.c
@@ -1334,7 +1334,7 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity,
        }
 
        if (USER_STRING(pidflag)) {
-               if ((pwdent = getpwuid((*pstc)->uid)) != NULL) {
+               if ((pwdent = __getpwuid((*pstc)->uid)) != NULL) {
                        if (strcmp(pwdent->pw_name, userstr))
                                /* This PID doesn't belong to user */
                                return -1;
@@ -1358,7 +1358,7 @@ void __print_line_id(struct pid_stats *pst, char c)
        char format[32];
        struct passwd *pwdent;
 
-       if (DISPLAY_USERNAME(pidflag) && ((pwdent = getpwuid(pst->uid)) != NULL)) {
+       if (DISPLAY_USERNAME(pidflag) && ((pwdent = __getpwuid(pst->uid)) != NULL)) {
                cprintf_in(IS_STR, " %8s", pwdent->pw_name, 0);
        }
        else {
index fdf32b34e6aa3852c140b0f39bff37f16924c1dc..d43d03c488aaf02a22bd4b21a2804c83cf95fbcc 100644 (file)
--- a/systest.c
+++ b/systest.c
 #include <time.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <pwd.h>
 #include <sys/stat.h>
 #include <sys/utsname.h>
 #include <sys/statvfs.h>
 #include <sys/time.h>
+#include <sys/types.h>
 
 #include "systest.h"
 
@@ -307,5 +309,33 @@ char *get_realname(char *name, char *c)
        return resolved_name;
 }
 
+/*
+ ***************************************************************************
+ * Replacement function for getpwuid() system call. Fill a dummy passwd
+ * structure containing the name of a user.
+ *
+ * IN:
+ * @uid                UID of the user
+ *
+ * RETURNS:
+ * Pointer on the passwd structure.
+ ***************************************************************************
+ */
+struct passwd *get_usrname(uid_t uid)
+{
+       static struct passwd pwd_ent;
+       static char pw_name[16];
+
+       pwd_ent.pw_name = pw_name;
+       if (!uid) {
+               strcpy(pwd_ent.pw_name, "root");
+       }
+       else {
+               strcpy(pwd_ent.pw_name, "testusr");
+       }
+
+       return &pwd_ent;
+}
+
 #endif /* TEST */
 
index 10f30ce5dc4b4a2925f7ac4ce2aa61910d790702..e8aba2ba93b9b8d8c6e6319772d7f434848d22d5 100644 (file)
--- a/systest.h
+++ b/systest.h
@@ -28,6 +28,7 @@
 #define __closedir(m)          close_list(m)
 #define __realpath(m,n)                get_realname(m,n)
 #define __gettimeofday(m,n)    get_day_time(m)
+#define __getpwuid(m)          get_usrname(m)
 
 #define ROOTDIR                "./tests/root"
 #define ROOTFILE       "root"
@@ -51,6 +52,7 @@
 #define __closedir(m)          closedir(m)
 #define __realpath(m,n)                realpath(m,n)
 #define __gettimeofday(m,n)    gettimeofday(m,n)
+#define __getpwuid(m)          getpwuid(m)
 
 #endif
 
@@ -75,6 +77,8 @@ void get_uname
        (struct utsname *);
 void get_unix_time
        (time_t *);
+struct passwd *get_usrname
+       (uid_t);
 void next_time_step
        (void);
 DIR *open_list