From 5ba12f568dc47a05847243cf2bb5beaa45c5e7b4 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Sat, 24 Sep 2005 22:37:21 +0000 Subject: [PATCH] fix who_list on aix, struct utmp is not binary compatible --- src/os/aix/aix_sigar.c | 99 ++++++++++++++++++++++++++++++++++++++++++ src/sigar.c | 4 +- 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/src/os/aix/aix_sigar.c b/src/os/aix/aix_sigar.c index 1e892368..0d8462b7 100644 --- a/src/os/aix/aix_sigar.c +++ b/src/os/aix/aix_sigar.c @@ -873,6 +873,105 @@ int sigar_uptime_get(sigar_t *sigar, return SIGAR_OK; } +#define WHOCPY(dest, src) \ + SIGAR_SSTRCPY(dest, src); \ + if (sizeof(src) < sizeof(dest)) \ + dest[sizeof(src)] = '\0' + +static int who_v4(sigar_t *sigar, sigar_who_list_t *wholist, FILE *fp) +{ + struct utmp ut; + + while (fread(&ut, sizeof(ut), 1, fp) == 1) { + sigar_who_t *who; + + if (*ut.ut_name == '\0') { + continue; + } + + if (ut.ut_type != USER_PROCESS) { + continue; + } + + SIGAR_WHO_LIST_GROW(wholist); + who = &wholist->data[wholist->number++]; + + WHOCPY(who->user, ut.ut_user); + WHOCPY(who->device, ut.ut_line); + WHOCPY(who->host, ut.ut_host); + + who->time = ut.ut_time; + } + + return SIGAR_OK; +} + +static int who_v5(sigar_t *sigar, sigar_who_list_t *wholist, FILE *fp) +{ + struct utmp_v5 ut; + + while (fread(&ut, sizeof(ut), 1, fp) == 1) { + sigar_who_t *who; + + if (*ut.ut_name == '\0') { + continue; + } + + if (ut.ut_type != USER_PROCESS) { + continue; + } + + SIGAR_WHO_LIST_GROW(wholist); + who = &wholist->data[wholist->number++]; + + WHOCPY(who->user, ut.ut_user); + WHOCPY(who->device, ut.ut_line); + WHOCPY(who->host, ut.ut_host); + + who->time = ut.ut_time; + } + + return SIGAR_OK; +} + +static int sigar_who_utmp(sigar_t *sigar, + sigar_who_list_t *wholist) +{ + int status; + FILE *fp; + + if (!(fp = fopen(UTMP_FILE, "r"))) { + return errno; + } + + if (sigar->aix_version == 4) { + status = who_v4(sigar, wholist, fp); + } + else { + status = who_v5(sigar, wholist, fp); + } + + fclose(fp); + + return status; +} + +int sigar_who_list_get(sigar_t *sigar, + sigar_who_list_t *wholist) +{ + int status; + + sigar_who_list_create(wholist); + + status = sigar_who_utmp(sigar, wholist); + if (status != SIGAR_OK) { + sigar_who_list_destroy(sigar, wholist); + return status; + } + + return SIGAR_OK; +} + int sigar_loadavg_get(sigar_t *sigar, sigar_loadavg_t *loadavg) { diff --git a/src/sigar.c b/src/sigar.c index 349e07a9..a578d54c 100644 --- a/src/sigar.c +++ b/src/sigar.c @@ -789,7 +789,7 @@ static char *getpass(const char *prompt) # define ut_user ut_name #endif -#ifndef NETWARE +#if !defined(NETWARE) && !defined(_AIX) #define WHOCPY(dest, src) \ SIGAR_SSTRCPY(dest, src); \ @@ -1061,6 +1061,7 @@ int sigar_who_list_get(sigar_t *sigar, } #else +#ifndef _AIX int sigar_who_list_get(sigar_t *sigar, sigar_who_list_t *wholist) { @@ -1076,6 +1077,7 @@ int sigar_who_list_get(sigar_t *sigar, return SIGAR_OK; } +#endif static int sigar_get_default_gateway(sigar_t *sigar, char *gateway)