d8307d
commit 5a3afa9738f3dbbaf8c0a35665318c1af782111b
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Tue Aug 13 15:53:19 2019 +0200
d8307d
d8307d
    login: Replace macro-based control flow with function calls in utmp
d8307d
d8307d
diff --git a/login/utmp_file.c b/login/utmp_file.c
d8307d
index da1baa6948d0eb39..812de8fd3d099ce9 100644
d8307d
--- a/login/utmp_file.c
d8307d
+++ b/login/utmp_file.c
d8307d
@@ -52,58 +52,71 @@ static struct utmp last_entry;
d8307d
 /* Do-nothing handler for locking timeout.  */
d8307d
 static void timeout_handler (int signum) {};
d8307d
 
d8307d
-/* LOCK_FILE(fd, type) failure_statement
d8307d
-     attempts to get a lock on the utmp file referenced by FD.  If it fails,
d8307d
-     the failure_statement is executed, otherwise it is skipped.
d8307d
-   LOCKING_FAILED()
d8307d
-     jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE.
d8307d
-   UNLOCK_FILE(fd)
d8307d
-     unlocks the utmp file referenced by FD and performs the cleanup of
d8307d
-     LOCK_FILE.
d8307d
- */
d8307d
-#define LOCK_FILE(fd, type) \
d8307d
-{									      \
d8307d
-  struct flock fl;							      \
d8307d
-  struct sigaction action, old_action;					      \
d8307d
-  unsigned int old_timeout;						      \
d8307d
-									      \
d8307d
-  /* Cancel any existing alarm.  */					      \
d8307d
-  old_timeout = alarm (0);						      \
d8307d
-									      \
d8307d
-  /* Establish signal handler.  */					      \
d8307d
-  action.sa_handler = timeout_handler;					      \
d8307d
-  __sigemptyset (&action.sa_mask);					      \
d8307d
-  action.sa_flags = 0;							      \
d8307d
-  __sigaction (SIGALRM, &action, &old_action);				      \
d8307d
-									      \
d8307d
-  alarm (TIMEOUT);							      \
d8307d
-									      \
d8307d
-  /* Try to get the lock.  */						      \
d8307d
-  memset (&fl, '\0', sizeof (struct flock));				      \
d8307d
-  fl.l_type = (type);							      \
d8307d
-  fl.l_whence = SEEK_SET;						      \
d8307d
-  if (__fcntl64_nocancel ((fd), F_SETLKW, &fl) < 0)
d8307d
-
d8307d
-#define LOCKING_FAILED() \
d8307d
-  goto unalarm_return
d8307d
-
d8307d
-#define UNLOCK_FILE(fd) \
d8307d
-  /* Unlock the file.  */						      \
d8307d
-  fl.l_type = F_UNLCK;							      \
d8307d
-  __fcntl64_nocancel ((fd), F_SETLKW, &fl);				      \
d8307d
-									      \
d8307d
- unalarm_return:							      \
d8307d
-  /* Reset the signal handler and alarm.  We must reset the alarm	      \
d8307d
-     before resetting the handler so our alarm does not generate a	      \
d8307d
-     spurious SIGALRM seen by the user.  However, we cannot just set	      \
d8307d
-     the user's old alarm before restoring the handler, because then	      \
d8307d
-     it's possible our handler could catch the user alarm's SIGARLM	      \
d8307d
-     and then the user would never see the signal he expected.  */	      \
d8307d
-  alarm (0);								      \
d8307d
-  __sigaction (SIGALRM, &old_action, NULL);				      \
d8307d
-  if (old_timeout != 0)							      \
d8307d
-    alarm (old_timeout);						      \
d8307d
-} while (0)
d8307d
+
d8307d
+/* try_file_lock (LOCKING, FD, TYPE) returns true if the locking
d8307d
+   operation failed and recovery needs to be performed.
d8307d
+   (file_lock_restore (LOCKING) still needs to be called.)
d8307d
+
d8307d
+   file_unlock (FD) removes the lock (which must have been
d8307d
+   acquired).
d8307d
+
d8307d
+   file_lock_restore (LOCKING) is needed to clean up in both
d8307d
+   cases.  */
d8307d
+
d8307d
+struct file_locking
d8307d
+{
d8307d
+  struct sigaction old_action;
d8307d
+  unsigned int old_timeout;
d8307d
+};
d8307d
+
d8307d
+static bool
d8307d
+try_file_lock (struct file_locking *locking, int fd, int type)
d8307d
+{
d8307d
+  /* Cancel any existing alarm.  */
d8307d
+  locking->old_timeout = alarm (0);
d8307d
+
d8307d
+  /* Establish signal handler.  */
d8307d
+  struct sigaction action;
d8307d
+  action.sa_handler = timeout_handler;
d8307d
+  __sigemptyset (&action.sa_mask);
d8307d
+  action.sa_flags = 0;
d8307d
+  __sigaction (SIGALRM, &action, &locking->old_action);
d8307d
+
d8307d
+  alarm (TIMEOUT);
d8307d
+
d8307d
+  /* Try to get the lock.  */
d8307d
+ struct flock fl =
d8307d
+   {
d8307d
+    .l_type = type,
d8307d
+    fl.l_whence = SEEK_SET,
d8307d
+   };
d8307d
+ return __fcntl64_nocancel (fd, F_SETLKW, &fl) < 0;
d8307d
+}
d8307d
+
d8307d
+static void
d8307d
+file_unlock (int fd)
d8307d
+{
d8307d
+  struct flock fl =
d8307d
+    {
d8307d
+      .l_type = F_UNLCK,
d8307d
+    };
d8307d
+  __fcntl64_nocancel (fd, F_SETLKW, &fl);
d8307d
+}
d8307d
+
d8307d
+static void
d8307d
+file_lock_restore (struct file_locking *locking)
d8307d
+{
d8307d
+  /* Reset the signal handler and alarm.  We must reset the alarm
d8307d
+     before resetting the handler so our alarm does not generate a
d8307d
+     spurious SIGALRM seen by the user.  However, we cannot just set
d8307d
+     the user's old alarm before restoring the handler, because then
d8307d
+     it's possible our handler could catch the user alarm's SIGARLM
d8307d
+     and then the user would never see the signal he expected.  */
d8307d
+  alarm (0);
d8307d
+  __sigaction (SIGALRM, &locking->old_action, NULL);
d8307d
+  if (locking->old_timeout != 0)
d8307d
+    alarm (locking->old_timeout);
d8307d
+}
d8307d
 
d8307d
 #ifndef TRANSFORM_UTMP_FILE_NAME
d8307d
 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
d8307d
@@ -153,16 +166,16 @@ __libc_getutent_r (struct utmp *buffer, struct utmp **result)
d8307d
       return -1;
d8307d
     }
d8307d
 
d8307d
-  LOCK_FILE (file_fd, F_RDLCK)
d8307d
+  struct file_locking fl;
d8307d
+  if (try_file_lock (&fl, file_fd, F_RDLCK))
d8307d
+    nbytes = 0;
d8307d
+  else
d8307d
     {
d8307d
-      nbytes = 0;
d8307d
-      LOCKING_FAILED ();
d8307d
+      /* Read the next entry.  */
d8307d
+      nbytes = __read_nocancel (file_fd, &last_entry, sizeof (struct utmp));
d8307d
+      file_unlock (file_fd);
d8307d
     }
d8307d
-
d8307d
-  /* Read the next entry.  */
d8307d
-  nbytes = __read_nocancel (file_fd, &last_entry, sizeof (struct utmp));
d8307d
-
d8307d
-  UNLOCK_FILE (file_fd);
d8307d
+  file_lock_restore (&fl);
d8307d
 
d8307d
   if (nbytes != sizeof (struct utmp))
d8307d
     {
d8307d
@@ -188,10 +201,12 @@ internal_getut_r (const struct utmp *id, struct utmp *buffer,
d8307d
 {
d8307d
   int result = -1;
d8307d
 
d8307d
-  LOCK_FILE (file_fd, F_RDLCK)
d8307d
+  struct file_locking fl;
d8307d
+  if (try_file_lock (&fl, file_fd, F_RDLCK))
d8307d
     {
d8307d
       *lock_failed = true;
d8307d
-      LOCKING_FAILED ();
d8307d
+      file_lock_restore (&fl);
d8307d
+      return -1;
d8307d
     }
d8307d
 
d8307d
   if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
d8307d
@@ -241,7 +256,8 @@ internal_getut_r (const struct utmp *id, struct utmp *buffer,
d8307d
   result = 0;
d8307d
 
d8307d
 unlock_return:
d8307d
-  UNLOCK_FILE (file_fd);
d8307d
+  file_unlock (file_fd);
d8307d
+  file_lock_restore (&fl);
d8307d
 
d8307d
   return result;
d8307d
 }
d8307d
@@ -287,10 +303,12 @@ __libc_getutline_r (const struct utmp *line, struct utmp *buffer,
d8307d
       return -1;
d8307d
     }
d8307d
 
d8307d
-  LOCK_FILE (file_fd, F_RDLCK)
d8307d
+  struct file_locking fl;
d8307d
+  if (try_file_lock (&fl, file_fd, F_RDLCK))
d8307d
     {
d8307d
       *result = NULL;
d8307d
-      LOCKING_FAILED ();
d8307d
+      file_lock_restore (&fl);
d8307d
+      return -1;
d8307d
     }
d8307d
 
d8307d
   while (1)
d8307d
@@ -318,7 +336,8 @@ __libc_getutline_r (const struct utmp *line, struct utmp *buffer,
d8307d
   *result = buffer;
d8307d
 
d8307d
 unlock_return:
d8307d
-  UNLOCK_FILE (file_fd);
d8307d
+  file_unlock (file_fd);
d8307d
+  file_lock_restore (&fl);
d8307d
 
d8307d
   return ((*result == NULL) ? -1 : 0);
d8307d
 }
d8307d
@@ -375,10 +394,11 @@ __libc_pututline (const struct utmp *data)
d8307d
 	}
d8307d
     }
d8307d
 
d8307d
-  LOCK_FILE (file_fd, F_WRLCK)
d8307d
+  struct file_locking fl;
d8307d
+  if (try_file_lock (&fl, file_fd, F_WRLCK))
d8307d
     {
d8307d
-      pbuf = NULL;
d8307d
-      LOCKING_FAILED ();
d8307d
+      file_lock_restore (&fl);
d8307d
+      return NULL;
d8307d
     }
d8307d
 
d8307d
   if (found < 0)
d8307d
@@ -421,7 +441,8 @@ __libc_pututline (const struct utmp *data)
d8307d
     }
d8307d
 
d8307d
  unlock_return:
d8307d
-  UNLOCK_FILE (file_fd);
d8307d
+  file_unlock (file_fd);
d8307d
+  file_lock_restore (&fl);
d8307d
 
d8307d
   return pbuf;
d8307d
 }
d8307d
@@ -450,8 +471,13 @@ __libc_updwtmp (const char *file, const struct utmp *utmp)
d8307d
   if (fd < 0)
d8307d
     return -1;
d8307d
 
d8307d
-  LOCK_FILE (fd, F_WRLCK)
d8307d
-    LOCKING_FAILED ();
d8307d
+  struct file_locking fl;
d8307d
+  if (try_file_lock (&fl, fd, F_WRLCK))
d8307d
+    {
d8307d
+      file_lock_restore (&fl);
d8307d
+      __close_nocancel_nostatus (fd);
d8307d
+      return -1;
d8307d
+    }
d8307d
 
d8307d
   /* Remember original size of log file.  */
d8307d
   offset = __lseek64 (fd, 0, SEEK_END);
d8307d
@@ -477,7 +503,8 @@ __libc_updwtmp (const char *file, const struct utmp *utmp)
d8307d
   result = 0;
d8307d
 
d8307d
 unlock_return:
d8307d
-  UNLOCK_FILE (fd);
d8307d
+  file_unlock (file_fd);
d8307d
+  file_lock_restore (&fl);
d8307d
 
d8307d
   /* Close WTMP file.  */
d8307d
   __close_nocancel_nostatus (fd);