From 6ff9c31abc14f207265ab214370982ecb3bfe428 Mon Sep 17 00:00:00 2001 From: Bernhard Reutner-Fischer Date: Wed, 25 Mar 2015 23:59:45 +0100 Subject: utmp: favour POSIX utmpx over SVID utmp Note: _PATH_UTMPX == _PATH_UTMP and the utmp struct is identical to the utmpx struct so this only changes the external API entrypoints and NOT the underlying data source. This saves about 500b (~1300b from previously ~1950) while at it. Signed-off-by: Bernhard Reutner-Fischer --- libc/misc/utmp/utent.c | 121 +++++++++++++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 49 deletions(-) (limited to 'libc/misc/utmp/utent.c') diff --git a/libc/misc/utmp/utent.c b/libc/misc/utmp/utent.c index a258cb46d..3671bb05c 100644 --- a/libc/misc/utmp/utent.c +++ b/libc/misc/utmp/utent.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include "internal/utmp.h" #include #include @@ -27,17 +27,17 @@ __UCLIBC_MUTEX_STATIC(utmplock, PTHREAD_MUTEX_INITIALIZER); /* Some global crap */ static int static_fd = -1; -static struct utmp static_utmp; -static const char default_file_name[] = _PATH_UTMP; -static const char *static_ut_name = default_file_name; +static struct UT static_utmp; +static const char default_file[] = __DEFAULT_PATH_UTMP; +static const char *current_file = default_file; /* This function must be called with the LOCK held */ -static void __setutent_unlocked(void) +static void __set_unlocked(void) { if (static_fd < 0) { - static_fd = open_not_cancel_2(static_ut_name, O_RDWR | O_CLOEXEC); + static_fd = open_not_cancel_2(current_file, O_RDWR | O_CLOEXEC); if (static_fd < 0) { - static_fd = open_not_cancel_2(static_ut_name, O_RDONLY | O_CLOEXEC); + static_fd = open_not_cancel_2(current_file, O_RDONLY | O_CLOEXEC); if (static_fd < 0) { return; /* static_fd remains < 0 */ } @@ -51,22 +51,23 @@ static void __setutent_unlocked(void) lseek(static_fd, 0, SEEK_SET); } #if defined __UCLIBC_HAS_THREADS__ -void setutent(void) +void set(void) { __UCLIBC_MUTEX_LOCK(utmplock); - __setutent_unlocked(); + __set_unlocked(); __UCLIBC_MUTEX_UNLOCK(utmplock); } #else -strong_alias(__setutent_unlocked,setutent) +strong_alias(__set_unlocked,set) #endif -libc_hidden_def(setutent) +/* not used in libc_hidden_def(set) */ +other(setutxent,setutent) /* This function must be called with the LOCK held */ -static struct utmp *__getutent_unlocked(void) +static struct UT *__get_unlocked(void) { if (static_fd < 0) { - __setutent_unlocked(); + __set_unlocked(); if (static_fd < 0) return NULL; } @@ -79,21 +80,22 @@ static struct utmp *__getutent_unlocked(void) return NULL; } #if defined __UCLIBC_HAS_THREADS__ -struct utmp *getutent(void) +struct UT *get(void) { - struct utmp *ret; + struct UT *ret; __UCLIBC_MUTEX_LOCK(utmplock); - ret = __getutent_unlocked(); + ret = __get_unlocked(); __UCLIBC_MUTEX_UNLOCK(utmplock); return ret; } #else -strong_alias(__getutent_unlocked,getutent) +strong_alias(__get_unlocked,get) #endif -libc_hidden_def(getutent) +/* not used in libc_hidden_def(get) */ +other(getutxent,getutent) -void endutent(void) +void end(void) { __UCLIBC_MUTEX_LOCK(utmplock); if (static_fd >= 0) @@ -101,12 +103,13 @@ void endutent(void) static_fd = -1; __UCLIBC_MUTEX_UNLOCK(utmplock); } -libc_hidden_def(endutent) +/* not used in libc_hidden_def(end) */ +other(endutxent,endutent) /* This function must be called with the LOCK held */ -static struct utmp *__getutid_unlocked(const struct utmp *utmp_entry) +static struct UT *__getid_unlocked(const struct UT *utmp_entry) { - struct utmp *lutmp; + struct UT *lutmp; unsigned type; /* We use the fact that constants we are interested in are: */ @@ -114,7 +117,7 @@ static struct utmp *__getutid_unlocked(const struct utmp *utmp_entry) type = utmp_entry->ut_type - 1; type /= 4; - while ((lutmp = __getutent_unlocked()) != NULL) { + while ((lutmp = __get_unlocked()) != NULL) { if (type == 0 && lutmp->ut_type == utmp_entry->ut_type) { /* one of RUN_LVL, BOOT_TIME, NEW_TIME, OLD_TIME */ return lutmp; @@ -130,26 +133,27 @@ static struct utmp *__getutid_unlocked(const struct utmp *utmp_entry) return NULL; } #if defined __UCLIBC_HAS_THREADS__ -struct utmp *getutid(const struct utmp *utmp_entry) +struct UT *getid(const struct UT *utmp_entry) { - struct utmp *ret; + struct UT *ret; __UCLIBC_MUTEX_LOCK(utmplock); - ret = __getutid_unlocked(utmp_entry); + ret = __getid_unlocked(utmp_entry); __UCLIBC_MUTEX_UNLOCK(utmplock); return ret; } #else -strong_alias(__getutid_unlocked,getutid) +strong_alias(__getid_unlocked,getid) #endif -libc_hidden_def(getutid) +/* not used in libc_hidden_def(getid) */ +other(getutxid,getutid) -struct utmp *getutline(const struct utmp *utmp_entry) +struct UT *getline(const struct UT *utmp_entry) { - struct utmp *lutmp; + struct UT *lutmp; __UCLIBC_MUTEX_LOCK(utmplock); - while ((lutmp = __getutent_unlocked()) != NULL) { + while ((lutmp = __get_unlocked()) != NULL) { if (lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) { if (strncmp(lutmp->ut_line, utmp_entry->ut_line, sizeof(lutmp->ut_line)) == 0) { @@ -160,39 +164,41 @@ struct utmp *getutline(const struct utmp *utmp_entry) __UCLIBC_MUTEX_UNLOCK(utmplock); return lutmp; } -libc_hidden_def(getutline) +/* libc_hidden_def(getline) */ +other(getutxline,getutline) -struct utmp *pututline(const struct utmp *utmp_entry) +struct UT *putline(const struct UT *utmp_entry) { __UCLIBC_MUTEX_LOCK(utmplock); /* Ignore the return value. That way, if they've already positioned the file pointer where they want it, everything will work out. */ - lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR); + lseek(static_fd, (off_t) - sizeof(struct UT), SEEK_CUR); - if (__getutid_unlocked(utmp_entry) != NULL) - lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR); + if (__getid_unlocked(utmp_entry) != NULL) + lseek(static_fd, (off_t) - sizeof(struct UT), SEEK_CUR); else lseek(static_fd, (off_t) 0, SEEK_END); - if (write(static_fd, utmp_entry, sizeof(struct utmp)) - != sizeof(struct utmp)) + if (write(static_fd, utmp_entry, sizeof(struct UT)) + != sizeof(struct UT)) utmp_entry = NULL; __UCLIBC_MUTEX_UNLOCK(utmplock); - return (struct utmp *)utmp_entry; + return (struct UT *)utmp_entry; } -libc_hidden_def(pututline) +/* not used in libc_hidden_def(putline) */ +other(pututxline,pututline) -int utmpname(const char *new_ut_name) +int name(const char *new_file) { __UCLIBC_MUTEX_LOCK(utmplock); - if (new_ut_name != NULL) { - if (static_ut_name != default_file_name) - free((char *)static_ut_name); - static_ut_name = strdup(new_ut_name); - if (static_ut_name == NULL) { + if (new_file != NULL) { + if (current_file != default_file) + free((char *)current_file); + current_file = strdup(new_file); + if (current_file == NULL) { /* We should probably whine about out-of-memory * errors here... Instead just reset to the default */ - static_ut_name = default_file_name; + current_file = default_file; } } @@ -201,6 +207,23 @@ int utmpname(const char *new_ut_name) static_fd = -1; } __UCLIBC_MUTEX_UNLOCK(utmplock); - return 0; /* or maybe return -(static_ut_name != new_ut_name)? */ + return 0; /* or maybe return -(current_file != new_file)? */ } -libc_hidden_def(utmpname) +/* not used in libc_hidden_def(name) */ +other(utmpxname,utmpname) + +void updw(const char *wtmp_file, const struct UT *lutmp) +{ + int fd; + + fd = open_not_cancel_2(wtmp_file, O_APPEND | O_WRONLY); + if (fd >= 0) { + if (lockf(fd, F_LOCK, 0) == 0) { + write_not_cancel(fd, lutmp, sizeof(struct UT)); + lockf(fd, F_ULOCK, 0); + close_not_cancel_no_status(fd); + } + } +} +/* not used in libc_hidden_def(updw) */ +other(updwtmpx,updwtmp) -- cgit v1.2.3