summaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
Diffstat (limited to 'libc')
-rw-r--r--libc/misc/utmp/utent.c86
1 files changed, 49 insertions, 37 deletions
diff --git a/libc/misc/utmp/utent.c b/libc/misc/utmp/utent.c
index 254040fed..c83f45e73 100644
--- a/libc/misc/utmp/utent.c
+++ b/libc/misc/utmp/utent.c
@@ -24,14 +24,26 @@
__UCLIBC_MUTEX_STATIC(utmplock, PTHREAD_MUTEX_INITIALIZER);
+/* Do not create extra unlocked functions if no locking is needed */
+#if defined __UCLIBC_HAS_THREADS__
+# define static_if_threaded static
+#else
+# define static_if_threaded /* nothing */
+# define __setutent setutent
+# define __getutent getutent
+# define __getutid getutid
+#endif
+
+
/* 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;
+
/* This function must be called with the LOCK held */
-static void __setutent(void)
+static_if_threaded void __setutent(void)
{
if (static_fd < 0) {
static_fd = open(static_ut_name, O_RDWR | O_CLOEXEC);
@@ -49,20 +61,19 @@ static void __setutent(void)
}
lseek(static_fd, 0, SEEK_SET);
}
-
+#if defined __UCLIBC_HAS_THREADS__
void setutent(void)
{
__UCLIBC_MUTEX_LOCK(utmplock);
__setutent();
__UCLIBC_MUTEX_UNLOCK(utmplock);
}
+#endif
libc_hidden_def(setutent)
/* This function must be called with the LOCK held */
-static struct utmp *__getutent(void)
+static_if_threaded struct utmp *__getutent(void)
{
- struct utmp *ret = NULL;
-
if (static_fd < 0) {
__setutent();
if (static_fd < 0) {
@@ -71,11 +82,22 @@ static struct utmp *__getutent(void)
}
if (read(static_fd, &static_utmp, sizeof(static_utmp)) == sizeof(static_utmp)) {
- ret = &static_utmp;
+ return &static_utmp;
}
+ return NULL;
+}
+#if defined __UCLIBC_HAS_THREADS__
+struct utmp *getutent(void)
+{
+ struct utmp *ret;
+
+ __UCLIBC_MUTEX_LOCK(utmplock);
+ ret = __getutent();
+ __UCLIBC_MUTEX_UNLOCK(utmplock);
return ret;
}
+#endif
void endutent(void)
{
@@ -86,43 +108,31 @@ void endutent(void)
__UCLIBC_MUTEX_UNLOCK(utmplock);
}
-struct utmp *getutent(void)
-{
- struct utmp *ret;
-
- __UCLIBC_MUTEX_LOCK(utmplock);
- ret = __getutent();
- __UCLIBC_MUTEX_UNLOCK(utmplock);
- return ret;
-}
-
/* This function must be called with the LOCK held */
-static struct utmp *__getutid(const struct utmp *utmp_entry)
+static_if_threaded struct utmp *__getutid(const struct utmp *utmp_entry)
{
struct utmp *lutmp;
+ unsigned type;
+
+ /* We use the fact that constants we are interested in are: */
+ /* RUN_LVL=1, ... OLD_TIME=4; INIT_PROCESS=5, ... USER_PROCESS=8 */
+ type = utmp_entry->ut_type - 1;
+ type /= 4;
while ((lutmp = __getutent()) != NULL) {
- if ( (utmp_entry->ut_type == RUN_LVL ||
- utmp_entry->ut_type == BOOT_TIME ||
- utmp_entry->ut_type == NEW_TIME ||
- utmp_entry->ut_type == OLD_TIME) &&
- lutmp->ut_type == utmp_entry->ut_type)
- {
- return lutmp;
- }
- if ( (utmp_entry->ut_type == INIT_PROCESS ||
- utmp_entry->ut_type == DEAD_PROCESS ||
- utmp_entry->ut_type == LOGIN_PROCESS ||
- utmp_entry->ut_type == USER_PROCESS) &&
- !strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
- {
- return lutmp;
- }
+ if (type == 0 && lutmp->ut_type == utmp_entry->ut_type) {
+ /* one of RUN_LVL, BOOT_TIME, NEW_TIME, OLD_TIME */
+ return lutmp;
+ }
+ if (type == 1 && strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)) == 0) {
+ /* INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, DEAD_PROCESS */
+ return lutmp;
+ }
}
return NULL;
}
-
+#if defined __UCLIBC_HAS_THREADS__
struct utmp *getutid(const struct utmp *utmp_entry)
{
struct utmp *ret;
@@ -132,6 +142,7 @@ struct utmp *getutid(const struct utmp *utmp_entry)
__UCLIBC_MUTEX_UNLOCK(utmplock);
return ret;
}
+#endif
struct utmp *getutline(const struct utmp *utmp_entry)
{
@@ -139,9 +150,10 @@ struct utmp *getutline(const struct utmp *utmp_entry)
__UCLIBC_MUTEX_LOCK(utmplock);
while ((lutmp = __getutent()) != NULL) {
- if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
- !strcmp(lutmp->ut_line, utmp_entry->ut_line)) {
- break;
+ 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) {
+ break;
+ }
}
}
__UCLIBC_MUTEX_UNLOCK(utmplock);