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