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