a9d52cf
From 7957425ef5ab365fc96ea0615f99705581c6dbd8 Mon Sep 17 00:00:00 2001
a9d52cf
From: =?UTF-8?q?Ond=C5=99ej=20Lyson=C4=9Bk?= <olysonek@redhat.com>
a9d52cf
Date: Mon, 12 Aug 2019 18:15:36 +0200
a9d52cf
Subject: [PATCH] Repeat pututxline() until it succeeds if it fails with EINTR
a9d52cf
a9d52cf
Since the pututxline() bug rhbz#1749439 is now fixed in glibc in
a9d52cf
Fedora and RHEL-8, we can implement a complete solution for the stale
a9d52cf
utmp entries issue originally reported as rhbz#1688848.
a9d52cf
a9d52cf
This patch is a followup to commit 896b3694ca062d7.
a9d52cf
a9d52cf
Resolves: rhbz#1688852
a9d52cf
Resolves: rhbz#1737433
a9d52cf
---
a9d52cf
 sysdeputil.c | 53 +++++++++++++---------------------------------------
a9d52cf
 1 file changed, 13 insertions(+), 40 deletions(-)
a9d52cf
a9d52cf
diff --git a/sysdeputil.c b/sysdeputil.c
a9d52cf
index 4fbcca7..75be680 100644
a9d52cf
--- a/sysdeputil.c
a9d52cf
+++ b/sysdeputil.c
a9d52cf
@@ -1203,7 +1203,7 @@ void
a9d52cf
 vsf_insert_uwtmp(const struct mystr* p_user_str,
a9d52cf
                  const struct mystr* p_host_str)
a9d52cf
 {
a9d52cf
-  int attempts;
a9d52cf
+  struct utmpx* p_res;
a9d52cf
 
a9d52cf
   if (sizeof(s_utent.ut_line) < 16)
a9d52cf
   {
a9d52cf
@@ -1233,34 +1233,21 @@ vsf_insert_uwtmp(const struct mystr* p_user_str,
a9d52cf
   vsf_sysutil_strcpy(s_utent.ut_host, str_getbuf(p_host_str),
a9d52cf
                      sizeof(s_utent.ut_host));
a9d52cf
   s_utent.ut_tv.tv_sec = vsf_sysutil_get_time_sec();
a9d52cf
-  for (attempts = 2; attempts > 0; --attempts)
a9d52cf
+  setutxent();
a9d52cf
+  do
a9d52cf
   {
a9d52cf
-    struct utmpx* p_res;
a9d52cf
-    setutxent();
a9d52cf
     p_res = pututxline(&s_utent);
a9d52cf
     /* For now we'll ignore errors other than EINTR and EAGAIN */
a9d52cf
-    if (p_res != NULL || (errno != EINTR && errno != EAGAIN))
a9d52cf
-    {
a9d52cf
-      break;
a9d52cf
-    }
a9d52cf
-  }
a9d52cf
-  if (attempts == 0)
a9d52cf
-  {
a9d52cf
-    /* This makes us skip pututxline() in vsf_remove_uwtmp() */
a9d52cf
-    s_uwtmp_inserted = -1;
a9d52cf
-  }
a9d52cf
-  else
a9d52cf
-  {
a9d52cf
-    s_uwtmp_inserted = 1;
a9d52cf
-    endutxent();
a9d52cf
-  }
a9d52cf
+  } while (p_res == NULL && (errno == EINTR || errno == EAGAIN));
a9d52cf
+  s_uwtmp_inserted = 1;
a9d52cf
+  endutxent();
a9d52cf
   updwtmpx(WTMPX_FILE, &s_utent);
a9d52cf
 }
a9d52cf
 
a9d52cf
 void
a9d52cf
 vsf_remove_uwtmp(void)
a9d52cf
 {
a9d52cf
-  int attempts;
a9d52cf
+  struct utmpx* p_res;
a9d52cf
 
a9d52cf
   if (!s_uwtmp_inserted)
a9d52cf
   {
a9d52cf
@@ -1270,27 +1257,13 @@ vsf_remove_uwtmp(void)
a9d52cf
   vsf_sysutil_memclr(s_utent.ut_user, sizeof(s_utent.ut_user));
a9d52cf
   vsf_sysutil_memclr(s_utent.ut_host, sizeof(s_utent.ut_host));
a9d52cf
   s_utent.ut_tv.tv_sec = 0;
a9d52cf
-  if (s_uwtmp_inserted == 1)
a9d52cf
+  setutxent();
a9d52cf
+  do
a9d52cf
   {
a9d52cf
-    for (attempts = 2; attempts > 0; --attempts)
a9d52cf
-    {
a9d52cf
-      struct utmpx* p_res;
a9d52cf
-      setutxent();
a9d52cf
-      p_res = pututxline(&s_utent);
a9d52cf
-      /* For now we'll ignore errors other than EINTR and EAGAIN */
a9d52cf
-      if (p_res != NULL || (errno != EINTR && errno != EAGAIN))
a9d52cf
-      {
a9d52cf
-        break;
a9d52cf
-      }
a9d52cf
-    }
a9d52cf
-    if (attempts != 0)
a9d52cf
-    {
a9d52cf
-      endutxent();
a9d52cf
-    }
a9d52cf
-  }
a9d52cf
-  /* Set s_uwtmp_inserted to 0 regardless of the result of
a9d52cf
-   * pututxline() to make sure we won't run this function twice.
a9d52cf
-   */
a9d52cf
+    p_res = pututxline(&s_utent);
a9d52cf
+    /* For now we'll ignore errors other than EINTR and EAGAIN */
a9d52cf
+  } while (p_res == NULL && (errno == EINTR || errno == EAGAIN));
a9d52cf
+  endutxent();
a9d52cf
   s_uwtmp_inserted = 0;
a9d52cf
   s_utent.ut_tv.tv_sec = vsf_sysutil_get_time_sec();
a9d52cf
   updwtmpx(WTMPX_FILE, &s_utent);
a9d52cf
-- 
a9d52cf
2.20.1
a9d52cf