[v2,2/7] Consolidate and simplify internal utmp definitions

Message ID 20200805185915.2025314-2-adhemerval.zanella@linaro.org
State Superseded
Headers
Series [v2,1/7] login: Move gnu utmpx to default implementation |

Commit Message

Adhemerval Zanella Aug. 5, 2020, 6:59 p.m. UTC
  The TRANSFORM_UTMP_FILE_NAME macro is moved to the inline function
utmp_file_name_time32 at utmp-path.h and the __utmp_equal function
is removed and inlined on its only usage  (matches_last_entry at
utmp_file.c).

Checked with make check run-built-tests=no on all afftected ABIs.
---
 login/updwtmp.c                              | 14 +----
 sysdeps/gnu/utmp_file.c => login/utmp-path.h | 32 ++++++++----
 login/utmp_file.c                            | 26 ++++++---
 manual/users.texi                            |  4 +-
 sysdeps/generic/paths.h                      |  8 +--
 sysdeps/generic/utmp-equal.h                 | 42 ---------------
 sysdeps/unix/sysv/linux/paths.h              |  6 ++-
 sysdeps/unix/sysv/linux/updwtmp.c            | 37 -------------
 sysdeps/unix/sysv/linux/utmp-path.h          | 55 ++++++++++++++++++++
 sysdeps/unix/sysv/linux/utmp_file.c          | 37 -------------
 10 files changed, 107 insertions(+), 154 deletions(-)
 rename sysdeps/gnu/utmp_file.c => login/utmp-path.h (50%)
 delete mode 100644 sysdeps/generic/utmp-equal.h
 delete mode 100644 sysdeps/unix/sysv/linux/updwtmp.c
 create mode 100644 sysdeps/unix/sysv/linux/utmp-path.h
 delete mode 100644 sysdeps/unix/sysv/linux/utmp_file.c
  

Patch

diff --git a/login/updwtmp.c b/login/updwtmp.c
index 489c28b553..42a2152b60 100644
--- a/login/updwtmp.c
+++ b/login/updwtmp.c
@@ -21,22 +21,12 @@ 
 #include <unistd.h>
 
 #include "utmp-private.h"
-
-#ifndef TRANSFORM_UTMP_FILE_NAME
-# define TRANSFORM_UTMP_FILE_NAME(file_name)	\
-  ((strcmp (file_name, _PATH_UTMP "x") == 0	\
-    && __access (_PATH_UTMP "x", F_OK) != 0)	\
-   ? _PATH_UTMP					\
-   : ((strcmp (file_name, _PATH_WTMP "x") == 0	\
-       && __access (_PATH_WTMP "x", F_OK) != 0)	\
-      ? _PATH_WTMP				\
-      : file_name))
-#endif
+#include <utmp-path.h>
 
 void
 __updwtmp (const char *wtmp_file, const struct utmp *utmp)
 {
-  const char *file_name = TRANSFORM_UTMP_FILE_NAME (wtmp_file);
+  const char *file_name = utmp_file_name_time32 (wtmp_file);
 
   __libc_updwtmp (file_name, utmp);
 }
diff --git a/sysdeps/gnu/utmp_file.c b/login/utmp-path.h
similarity index 50%
rename from sysdeps/gnu/utmp_file.c
rename to login/utmp-path.h
index 25ceaa897c..c284c8c398 100644
--- a/sysdeps/gnu/utmp_file.c
+++ b/login/utmp-path.h
@@ -1,6 +1,6 @@ 
-/* Copyright (C) 1998-2020 Free Software Foundation, Inc.
+/* Handle {u,w}tmp and {u,w}tmpx file name usage.
+   Copyright (C) 1998-2020 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
-   Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1998.
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -16,16 +16,26 @@ 
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#ifndef _UTMP_PATH_H
+#define _UTMP_PATH_H 1
+
 #include <string.h>
 #include <unistd.h>
 
-#define TRANSFORM_UTMP_FILE_NAME(file_name)	\
-  ((strcmp (file_name, _PATH_UTMP "x") == 0	\
-    && __access (_PATH_UTMP "x", F_OK) != 0)	\
-   ? _PATH_UTMP					\
-   : ((strcmp (file_name, _PATH_WTMP "x") == 0	\
-       && __access (_PATH_WTMP "x", F_OK) != 0)	\
-      ? _PATH_WTMP				\
-      : file_name))
+/* The function returns the utmp database for 32-bit utmp{x} entries based
+   on FILE_NAME.  If the argument ends with 'x' and the file does not
+   exits the default old utmp{x} name is returned instead.  */
+static inline const char *
+utmp_file_name_time32 (const char *file_name)
+{
+  if (strcmp (file_name, _PATH_UTMP_BASE "x") == 0
+      && __access (_PATH_UTMP_BASE "x", F_OK) != 0)
+    return _PATH_UTMP_BASE;
+  else if (strcmp (file_name, _PATH_WTMP_BASE "x") == 0
+	   && __access (_PATH_WTMP_BASE "x", F_OK) != 0)
+    return _PATH_UTMP_BASE;
+
+  return file_name;
+}
 
-#include <login/utmp_file.c>
+#endif /* _UTMP_PATH_H  */
diff --git a/login/utmp_file.c b/login/utmp_file.c
index f095139c51..4f4f0cf4e4 100644
--- a/login/utmp_file.c
+++ b/login/utmp_file.c
@@ -32,7 +32,7 @@ 
 #include <not-cancel.h>
 
 #include "utmp-private.h"
-#include "utmp-equal.h"
+#include <utmp-path.h>
 
 
 /* Descriptor for the file and position.  */
@@ -60,7 +60,21 @@  matches_last_entry (const struct utmp *data)
     return data->ut_type == last_entry.ut_type;
   else
     /* For the process-related entries, a full match is needed.  */
-    return __utmp_equal (&last_entry, data);
+    return (data->ut_type == INIT_PROCESS
+	    || data->ut_type == LOGIN_PROCESS
+	    || data->ut_type == USER_PROCESS
+	    || data->ut_type == DEAD_PROCESS)
+      && (last_entry.ut_type == INIT_PROCESS
+	  || last_entry.ut_type == LOGIN_PROCESS
+	  || last_entry.ut_type == USER_PROCESS
+	  || last_entry.ut_type == DEAD_PROCESS)
+      && (data->ut_id[0] && last_entry.ut_id[0]
+	  ? strncmp (data->ut_id, last_entry.ut_id,
+		     sizeof last_entry.ut_id)
+	    == 0
+	  : (strncmp (data->ut_line, last_entry.ut_line,
+		      sizeof last_entry.ut_line)
+	     == 0));
 }
 
 /* Locking timeout.  */
@@ -129,10 +143,6 @@  file_unlock (int fd)
   __fcntl64_nocancel (fd, F_SETLKW, &fl);
 }
 
-#ifndef TRANSFORM_UTMP_FILE_NAME
-# define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
-#endif
-
 int
 __libc_setutent (void)
 {
@@ -140,7 +150,7 @@  __libc_setutent (void)
     {
       const char *file_name;
 
-      file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
+      file_name = utmp_file_name_time32 (__libc_utmp_file_name);
 
       file_writable = false;
       file_fd = __open_nocancel
@@ -353,7 +363,7 @@  __libc_pututline (const struct utmp *data)
   if (! file_writable)
     {
       /* We must make the file descriptor writable before going on.  */
-      const char *file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
+      const char *file_name = utmp_file_name_time32 (__libc_utmp_file_name);
 
       int new_fd = __open_nocancel
 	(file_name, O_RDWR | O_LARGEFILE | O_CLOEXEC);
diff --git a/manual/users.texi b/manual/users.texi
index ec22ce6c1c..dcaca1dcf1 100644
--- a/manual/users.texi
+++ b/manual/users.texi
@@ -1234,7 +1234,7 @@  over again.
 @c   pututline_unknown @mtasurace:utent @acsfd
 @c    setutent_unknown dup @mtasurace:utent @acsfd
 @c   pututline_file @mtascusig:ALRM @mtascutimer @acsfd
-@c    TRANSFORM_UTMP_FILE_NAME ok
+@c    utmp_file_name_time32 ok
 @c     strcmp dup ok
 @c     acesss dup ok
 @c    open_not_cancel_2 dup @acsfd
@@ -1427,7 +1427,7 @@  the following function:
 @standards{SVID, utmp.h}
 @safety{@prelim{}@mtunsafe{@mtascusig{:ALRM} @mtascutimer{}}@asunsafe{}@acunsafe{@acsfd{}}}
 @c updwtmp @mtascusig:ALRM @mtascutimer @acsfd
-@c  TRANSFORM_UTMP_FILE_NAME dup ok
+@c  utmp_file_name_time32 dup ok
 @c  *libc_utmp_file_functions->updwtmp = updwtmp_file @mtascusig:ALRM @mtascutimer @acsfd
 @c   open_not_cancel_2 dup @acsfd
 @c   LOCK_FILE dup @mtascusig:ALRM @mtascutimer
diff --git a/sysdeps/generic/paths.h b/sysdeps/generic/paths.h
index 893b4c2286..99a791ce31 100644
--- a/sysdeps/generic/paths.h
+++ b/sysdeps/generic/paths.h
@@ -60,10 +60,12 @@ 
 #define	_PATH_SHELLS	"/etc/shells"
 #define	_PATH_TTY	"/dev/tty"
 #define	_PATH_UNIX	"/vmunix"
-#define	_PATH_UTMP	"/var/run/utmp"
-#define	_PATH_UTMP_DB	"/var/run/utmp.db"
+#define	_PATH_UTMP_BASE	"/var/run/utmp"
+#define	_PATH_UTMP	_PATH_UTMP_BASE
+#define	_PATH_UTMP_DB	_PATH_UTMP_BASE ".db"
 #define	_PATH_VI	"/usr/bin/vi"
-#define	_PATH_WTMP	"/var/log/wtmp"
+#define	_PATH_WTMP_BASE	"/var/log/wtmp"
+#define	_PATH_WTMP	_PATH_WTMP_BASE
 
 /* Provide trailing slash, since mostly used for building pathnames. */
 #define	_PATH_DEV	"/dev/"
diff --git a/sysdeps/generic/utmp-equal.h b/sysdeps/generic/utmp-equal.h
deleted file mode 100644
index cd0e3e79a0..0000000000
--- a/sysdeps/generic/utmp-equal.h
+++ /dev/null
@@ -1,42 +0,0 @@ 
-/* Helper function for utmp functions to see if two entries are equal.
-   Copyright (C) 1996-2020 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-   Contributed by Ulrich Drepper <drepper@cygnus.com>
-   and Paul Janzen <pcj@primenet.com>, 1996.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <https://www.gnu.org/licenses/>.  */
-
-#include <string.h>
-#include <utmp.h>
-
-#include "utmp-private.h"
-
-/* Test whether two entries match.  */
-static int
-__utmp_equal (const struct utmp *entry, const struct utmp *match)
-{
-  return (entry->ut_type == INIT_PROCESS
-          || entry->ut_type == LOGIN_PROCESS
-          || entry->ut_type == USER_PROCESS
-          || entry->ut_type == DEAD_PROCESS)
-    && (match->ut_type == INIT_PROCESS
-        || match->ut_type == LOGIN_PROCESS
-        || match->ut_type == USER_PROCESS
-        || match->ut_type == DEAD_PROCESS)
-    && (entry->ut_id[0] && match->ut_id[0]
-        ? strncmp (entry->ut_id, match->ut_id, sizeof match->ut_id) == 0
-        : (strncmp (entry->ut_line, match->ut_line, sizeof match->ut_line)
-           == 0));
-}
diff --git a/sysdeps/unix/sysv/linux/paths.h b/sysdeps/unix/sysv/linux/paths.h
index 1342ab3a96..3b8aeab788 100644
--- a/sysdeps/unix/sysv/linux/paths.h
+++ b/sysdeps/unix/sysv/linux/paths.h
@@ -61,9 +61,11 @@ 
 #define	_PATH_SHELLS	"/etc/shells"
 #define	_PATH_TTY	"/dev/tty"
 #define	_PATH_UNIX	"/boot/vmlinux"
-#define	_PATH_UTMP	"/var/run/utmp"
+#define	_PATH_UTMP_BASE	"/var/run/utmp"
+#define	_PATH_UTMP	_PATH_UTMP_BASE
 #define	_PATH_VI	"/usr/bin/vi"
-#define	_PATH_WTMP	"/var/log/wtmp"
+#define	_PATH_WTMP_BASE	"/var/log/wtmp"
+#define	_PATH_WTMP	_PATH_WTMP_BASE
 
 /* Provide trailing slash, since mostly used for building pathnames. */
 #define	_PATH_DEV	"/dev/"
diff --git a/sysdeps/unix/sysv/linux/updwtmp.c b/sysdeps/unix/sysv/linux/updwtmp.c
deleted file mode 100644
index 333317bfe2..0000000000
--- a/sysdeps/unix/sysv/linux/updwtmp.c
+++ /dev/null
@@ -1,37 +0,0 @@ 
-/* Copyright (C) 1998-2020 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-   Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1998.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <https://www.gnu.org/licenses/>.  */
-
-#include <string.h>
-#include <unistd.h>
-
-#define TRANSFORM_UTMP_FILE_NAME(file_name)		\
-  ((strcmp (file_name, _PATH_UTMP) == 0			\
-    && __access (_PATH_UTMP "x", F_OK) == 0)		\
-   ? (_PATH_UTMP "x")					\
-   : ((strcmp (file_name, _PATH_WTMP) == 0		\
-       && __access ( _PATH_WTMP "x", F_OK) == 0)	\
-      ? (_PATH_WTMP "x")				\
-      : ((strcmp (file_name, _PATH_UTMP "x") == 0	\
-	  && __access (_PATH_UTMP "x", F_OK) != 0)	\
-	 ? _PATH_UTMP					\
-	 : ((strcmp (file_name, _PATH_WTMP "x") == 0	\
-	     && __access (_PATH_WTMP "x", F_OK) != 0)	\
-	    ? _PATH_WTMP				\
-	    : file_name))))
-
-#include <login/updwtmp.c>
diff --git a/sysdeps/unix/sysv/linux/utmp-path.h b/sysdeps/unix/sysv/linux/utmp-path.h
new file mode 100644
index 0000000000..a377bc9dba
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/utmp-path.h
@@ -0,0 +1,55 @@ 
+/* Handle {u,w}tmp and {u,w}tmpx file name usage.
+   Copyright (C) 1998-2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _UTMP_PATH_H
+#define _UTMP_PATH_H 1
+
+#include <string.h>
+#include <unistd.h>
+
+
+/* The function returns the utmp database for 32-bit utmp{x} entries based
+   on FILE_NAME:
+
+   - if the default {x,w}utmp name is used but a file name ending with 'x'
+     exists it is returned instead
+
+   - if the argument ends with 'x' and the file is not accessible the default
+     {x,w}utmp is returned instead.
+
+   - if neither 1. nor 2. applies, the FILE_NAME is returned instead.  */
+static inline const char *
+utmp_file_name_time32 (const char *file_name)
+{
+  if (strcmp (file_name, _PATH_UTMP_BASE) == 0
+      && __access (_PATH_UTMP_BASE "x", F_OK) == 0)
+    return _PATH_UTMP_BASE "x";
+  else if (strcmp (file_name, _PATH_WTMP_BASE) == 0
+           && __access (_PATH_WTMP_BASE "x", F_OK) == 0)
+    return _PATH_WTMP_BASE "x";
+  else if (strcmp (file_name, _PATH_UTMP_BASE "x") == 0
+           && __access (_PATH_UTMP_BASE "x", F_OK) != 0)
+    return _PATH_UTMP_BASE;
+  else if (strcmp (file_name, _PATH_WTMP_BASE "x") == 0
+           && __access (_PATH_WTMP_BASE "x", F_OK) != 0)
+    return _PATH_WTMP_BASE;
+
+  return file_name;
+}
+
+#endif /* _UTMP_PATH_H  */
diff --git a/sysdeps/unix/sysv/linux/utmp_file.c b/sysdeps/unix/sysv/linux/utmp_file.c
deleted file mode 100644
index 71da027a5f..0000000000
--- a/sysdeps/unix/sysv/linux/utmp_file.c
+++ /dev/null
@@ -1,37 +0,0 @@ 
-/* Copyright (C) 1998-2020 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-   Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1998.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, see
-   <https://www.gnu.org/licenses/>.  */
-
-#include <string.h>
-#include <unistd.h>
-
-#define TRANSFORM_UTMP_FILE_NAME(file_name)		\
-  ((strcmp (file_name, _PATH_UTMP) == 0			\
-    && __access (_PATH_UTMP "x", F_OK) == 0)		\
-   ? (_PATH_UTMP "x")					\
-   : ((strcmp (file_name, _PATH_WTMP) == 0		\
-       && __access ( _PATH_WTMP "x", F_OK) == 0)	\
-      ? (_PATH_WTMP "x")				\
-      : ((strcmp (file_name, _PATH_UTMP "x") == 0	\
-	  && __access (_PATH_UTMP "x", F_OK) != 0)	\
-	 ? _PATH_UTMP					\
-	 : ((strcmp (file_name, _PATH_WTMP "x") == 0	\
-	     && __access (_PATH_WTMP "x", F_OK) != 0)	\
-	    ? _PATH_WTMP				\
-	    : file_name))))
-
-#include <login/utmp_file.c>