Browse Source

some more fixes from rholzmann in Bug 716 ... make sure the code actually functions, then we worry about shrinking it ...

Mike Frysinger 19 years ago
parent
commit
6a84ed078a
1 changed files with 41 additions and 21 deletions
  1. 41 21
      libc/misc/utmp/utent.c

+ 41 - 21
libc/misc/utmp/utent.c

@@ -29,7 +29,6 @@ libc_hidden_proto(open)
 libc_hidden_proto(fcntl)
 libc_hidden_proto(fcntl)
 libc_hidden_proto(close)
 libc_hidden_proto(close)
 libc_hidden_proto(lseek)
 libc_hidden_proto(lseek)
-libc_hidden_proto(setutent)
 
 
 #ifdef __UCLIBC_HAS_THREADS__
 #ifdef __UCLIBC_HAS_THREADS__
 # include <pthread.h>
 # include <pthread.h>
@@ -46,11 +45,11 @@ static struct utmp static_utmp;
 static const char default_file_name[] = _PATH_UTMP;
 static const char default_file_name[] = _PATH_UTMP;
 static const char *static_ut_name = (const char *) default_file_name;
 static const char *static_ut_name = (const char *) default_file_name;
 
 
-void setutent(void)
+/* This function must be called with the LOCK held */
+static void __setutent(void)
 {
 {
     int ret;
     int ret;
 
 
-    LOCK;
     if (static_fd == -1) {
     if (static_fd == -1) {
 	if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
 	if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
 	    if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
 	    if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
@@ -66,34 +65,39 @@ void setutent(void)
 bummer:
 bummer:
 	    static_fd = -1;
 	    static_fd = -1;
 	    close(static_fd);
 	    close(static_fd);
-unlock_and_ret:
-	    UNLOCK;
 	    return;
 	    return;
 	}
 	}
     }
     }
     lseek(static_fd, 0, SEEK_SET);
     lseek(static_fd, 0, SEEK_SET);
-    goto unlock_and_ret;
+    return;
+}
+
+libc_hidden_proto(setutent)
+void setutent(void)
+{
+    LOCK;
+    __setutent();
+    UNLOCK;
 }
 }
 libc_hidden_def(setutent)
 libc_hidden_def(setutent)
 
 
+/* This function must be called with the LOCK held */
 static struct utmp *__getutent(int utmp_fd)
 static struct utmp *__getutent(int utmp_fd)
 {
 {
     struct utmp *ret = NULL;
     struct utmp *ret = NULL;
 
 
     if (utmp_fd == -1) {
     if (utmp_fd == -1) {
-	setutent();
+	__setutent();
     }
     }
     if (utmp_fd == -1) {
     if (utmp_fd == -1) {
 	return NULL;
 	return NULL;
     }
     }
 
 
-    LOCK;
     if (read(utmp_fd, (char *) &static_utmp, sizeof(struct utmp)) == sizeof(struct utmp)) 
     if (read(utmp_fd, (char *) &static_utmp, sizeof(struct utmp)) == sizeof(struct utmp)) 
     {
     {
 	ret = &static_utmp;
 	ret = &static_utmp;
     }
     }
 
 
-    UNLOCK;
     return ret;
     return ret;
 }
 }
 
 
@@ -106,15 +110,19 @@ void endutent(void)
     UNLOCK;
     UNLOCK;
 }
 }
 
 
-/* Locking is done in __getutent */
 struct utmp *getutent(void)
 struct utmp *getutent(void)
 {
 {
-    return __getutent(static_fd);
+    struct utmp *ret = NULL;
+
+    LOCK;
+    ret = __getutent(static_fd);
+    UNLOCK;
+
+    return ret;
 }
 }
 
 
-/* Locking is done in __getutent */
+/* This function must be called with the LOCK held */
-libc_hidden_proto(getutid)
+static struct utmp *__getutid(const struct utmp *utmp_entry)
-struct utmp *getutid (const struct utmp *utmp_entry)
 {
 {
     struct utmp *lutmp;
     struct utmp *lutmp;
 
 
@@ -139,22 +147,34 @@ struct utmp *getutid (const struct utmp *utmp_entry)
 
 
     return NULL;
     return NULL;
 }
 }
+
+libc_hidden_proto(getutid)
+struct utmp *getutid(const struct utmp *utmp_entry)
+{
+    struct utmp *ret = NULL;
+
+    LOCK;
+    ret = __getutid(utmp_entry);
+    UNLOCK;
+
+    return ret;
+}
 libc_hidden_def(getutid)
 libc_hidden_def(getutid)
 
 
-/* Locking is done in __getutent */
 struct utmp *getutline(const struct utmp *utmp_entry)
 struct utmp *getutline(const struct utmp *utmp_entry)
 {
 {
-    struct utmp *lutmp;
+    struct utmp *lutmp = NULL;
 
 
+    LOCK;
     while ((lutmp = __getutent(static_fd)) != NULL) {
     while ((lutmp = __getutent(static_fd)) != NULL) {
 	if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
 	if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
-		!strcmp(lutmp->ut_line, utmp_entry->ut_line))
+		!strcmp(lutmp->ut_line, utmp_entry->ut_line)) {
-	{
+	    break;
-	    return lutmp;
 	}
 	}
     }
     }
+    UNLOCK;
 
 
-    return NULL;
+    return lutmp;
 }
 }
 
 
 struct utmp *pututline (const struct utmp *utmp_entry)
 struct utmp *pututline (const struct utmp *utmp_entry)
@@ -164,7 +184,7 @@ struct utmp *pututline (const struct utmp *utmp_entry)
        the file pointer where they want it, everything will work out. */
        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 utmp), SEEK_CUR);
 
 
-    if (getutid(utmp_entry) != NULL)
+    if (__getutid(utmp_entry) != NULL)
 	lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
 	lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
     else
     else
 	lseek(static_fd, (off_t) 0, SEEK_END);
 	lseek(static_fd, (off_t) 0, SEEK_END);