5f7b84
commit 76a7c103eb9060f9e3ba01d073ae4621a17d8b46
5f7b84
Author: Florian Weimer <fweimer@redhat.com>
5f7b84
Date:   Tue Nov 12 12:02:57 2019 +0100
5f7b84
5f7b84
    login: Introduce matches_last_entry to utmp processing
5f7b84
    
5f7b84
    This simplifies internal_getut_nolock and fixes a regression,
5f7b84
    introduced in commit be6b16d975683e6cca57852cd4cfe715b2a9d8b1
5f7b84
    ("login: Acquire write lock early in pututline [BZ #24882]")
5f7b84
    in pututxline because __utmp_equal can only compare process-related
5f7b84
    utmp entries.
5f7b84
    
5f7b84
    Fixes: be6b16d975683e6cca57852cd4cfe715b2a9d8b1
5f7b84
    Change-Id: Ib8a85002f7f87ee41590846d16d7e52bdb82f5a5
5f7b84
5f7b84
diff --git a/login/utmp_file.c b/login/utmp_file.c
5f7b84
index 6bba120db9cc574e..e653d14967c4fb7a 100644
5f7b84
--- a/login/utmp_file.c
5f7b84
+++ b/login/utmp_file.c
5f7b84
@@ -43,6 +43,25 @@ static off64_t file_offset;
5f7b84
 /* Cache for the last read entry.  */
5f7b84
 static struct utmp last_entry;
5f7b84
 
5f7b84
+/* Returns true if *ENTRY matches last_entry, based on
5f7b84
+   data->ut_type.  */
5f7b84
+static bool
5f7b84
+matches_last_entry (const struct utmp *data)
5f7b84
+{
5f7b84
+  if (file_offset <= 0)
5f7b84
+    /* Nothing has been read.  last_entry is stale and cannot match.  */
5f7b84
+    return false;
5f7b84
+
5f7b84
+  if (data->ut_type == RUN_LVL
5f7b84
+      || data->ut_type == BOOT_TIME
5f7b84
+      || data->ut_type == OLD_TIME
5f7b84
+      || data->ut_type == NEW_TIME)
5f7b84
+    /* For some entry types, only a type match is required.  */
5f7b84
+    return data->ut_type == last_entry.ut_type;
5f7b84
+  else
5f7b84
+    /* For the process-related entries, a full match is needed.  */
5f7b84
+    return __utmp_equal (&last_entry, data);
5f7b84
+}
5f7b84
 
5f7b84
 /* Locking timeout.  */
5f7b84
 #ifndef TIMEOUT
5f7b84
@@ -133,9 +152,6 @@ __libc_setutent (void)
5f7b84
   __lseek64 (file_fd, 0, SEEK_SET);
5f7b84
   file_offset = 0;
5f7b84
 
5f7b84
-  /* Make sure the entry won't match.  */
5f7b84
-  last_entry.ut_type = -1;
5f7b84
-
5f7b84
   return 1;
5f7b84
 }
5f7b84
 
5f7b84
@@ -191,48 +207,20 @@ __libc_getutent_r (struct utmp *buffer, struct utmp **result)
5f7b84
 static int
5f7b84
 internal_getut_nolock (const struct utmp *id)
5f7b84
 {
5f7b84
-  if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
5f7b84
-      || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
5f7b84
+  while (1)
5f7b84
     {
5f7b84
-      /* Search for next entry with type RUN_LVL, BOOT_TIME,
5f7b84
-	 OLD_TIME, or NEW_TIME.  */
5f7b84
-
5f7b84
-      while (1)
5f7b84
+      /* Read the next entry.  */
5f7b84
+      if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp))
5f7b84
+	  != sizeof (struct utmp))
5f7b84
 	{
5f7b84
-	  /* Read the next entry.  */
5f7b84
-	  if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp))
5f7b84
-	      != sizeof (struct utmp))
5f7b84
-	    {
5f7b84
-	      __set_errno (ESRCH);
5f7b84
-	      file_offset = -1l;
5f7b84
-	      return -1;
5f7b84
-	    }
5f7b84
-	  file_offset += sizeof (struct utmp);
5f7b84
-
5f7b84
-	  if (id->ut_type == last_entry.ut_type)
5f7b84
-	    break;
5f7b84
+	  __set_errno (ESRCH);
5f7b84
+	  file_offset = -1l;
5f7b84
+	  return -1;
5f7b84
 	}
5f7b84
-    }
5f7b84
-  else
5f7b84
-    {
5f7b84
-      /* Search for the next entry with the specified ID and with type
5f7b84
-	 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS.  */
5f7b84
-
5f7b84
-      while (1)
5f7b84
-	{
5f7b84
-	  /* Read the next entry.  */
5f7b84
-	  if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp))
5f7b84
-	      != sizeof (struct utmp))
5f7b84
-	    {
5f7b84
-	      __set_errno (ESRCH);
5f7b84
-	      file_offset = -1l;
5f7b84
-	      return -1;
5f7b84
-	    }
5f7b84
-	  file_offset += sizeof (struct utmp);
5f7b84
+      file_offset += sizeof (struct utmp);
5f7b84
 
5f7b84
-	  if (__utmp_equal (&last_entry, id))
5f7b84
-	    break;
5f7b84
-	}
5f7b84
+      if (matches_last_entry (id))
5f7b84
+	break;
5f7b84
     }
5f7b84
 
5f7b84
   return 0;
5f7b84
@@ -365,13 +353,7 @@ __libc_pututline (const struct utmp *data)
5f7b84
 
5f7b84
   /* Find the correct place to insert the data.  */
5f7b84
   bool found = false;
5f7b84
-  if (file_offset > 0
5f7b84
-      && ((last_entry.ut_type == data->ut_type
5f7b84
-	   && (last_entry.ut_type == RUN_LVL
5f7b84
-	       || last_entry.ut_type == BOOT_TIME
5f7b84
-	       || last_entry.ut_type == OLD_TIME
5f7b84
-	       || last_entry.ut_type == NEW_TIME))
5f7b84
-	  || __utmp_equal (&last_entry, data)))
5f7b84
+  if (matches_last_entry (data))
5f7b84
     {
5f7b84
       if (__lseek64 (file_fd, file_offset, SEEK_SET) < 0)
5f7b84
 	{
5f7b84
@@ -389,7 +371,7 @@ __libc_pututline (const struct utmp *data)
5f7b84
 	  found = false;
5f7b84
 	}
5f7b84
       else
5f7b84
-	found = __utmp_equal (&last_entry, data);
5f7b84
+	found = matches_last_entry (data);
5f7b84
     }
5f7b84
 
5f7b84
   if (!found)