[2/3] login: Remove utmp fallback for getlogin

Message ID 20231109170119.1664204-3-adhemerval.zanella@linaro.org
State Deferred
Headers
Series Make accounting database no-op |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Adhemerval Zanella Netto Nov. 9, 2023, 5:01 p.m. UTC
  The utmp is currently broken for 64 bit time_t support on some ABIs
where ut_tv is defined depending of the time_t AI used (BZ#30701).
It means that the fallback might eventually fail depending of the
environment.

The default getlogin is now based on getlogin_r, which allows to
consolidate Linux and Hurd implementation.

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 include/unistd.h                     |   3 -
 login/getlogin.c                     |  13 ++--
 sysdeps/mach/hurd/getlogin.c         |  35 ---------
 sysdeps/unix/getlogin.c              |  81 ---------------------
 sysdeps/unix/getlogin_r.c            | 103 ---------------------------
 sysdeps/unix/sysv/linux/getlogin.c   |  39 ----------
 sysdeps/unix/sysv/linux/getlogin_r.c |  34 ++-------
 7 files changed, 16 insertions(+), 292 deletions(-)
 delete mode 100644 sysdeps/mach/hurd/getlogin.c
 delete mode 100644 sysdeps/unix/getlogin.c
 delete mode 100644 sysdeps/unix/getlogin_r.c
 delete mode 100644 sysdeps/unix/sysv/linux/getlogin.c
  

Patch

diff --git a/include/unistd.h b/include/unistd.h
index e241603b81..f9bf9f133f 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -215,9 +215,6 @@  extern __pid_t __libc_fork (void);
    This always returns -1 and sets `errno' to EINTR.  */
 extern int __libc_pause (void);
 
-extern int __getlogin_r_loginuid (char *name, size_t namesize)
-     attribute_hidden;
-
 #  if IS_IN (rtld)
 #   include <dl-unistd.h>
 #  endif
diff --git a/login/getlogin.c b/login/getlogin.c
index 4c0e3b6660..9d6abf4462 100644
--- a/login/getlogin.c
+++ b/login/getlogin.c
@@ -18,14 +18,19 @@ 
 #include <stddef.h>
 #include <errno.h>
 #include <unistd.h>
+#include <limits.h>
+
+#ifndef LOGIN_NAME_MAX
+# define LOGIN_NAME_MAX 1024
+#endif
+static char name[LOGIN_NAME_MAX];
 
 /* Return the login name of the user, or NULL if it can't be determined.
    The returned pointer, if not NULL, is good only until the next call.  */
 char *
 getlogin (void)
 {
-  __set_errno (ENOSYS);
-  return NULL;
+  if (__getlogin_r (name, sizeof name) != 0)
+    return NULL;
+  return name[0] != '\0' ? name : NULL;
 }
-
-stub_warning (getlogin)
diff --git a/sysdeps/mach/hurd/getlogin.c b/sysdeps/mach/hurd/getlogin.c
deleted file mode 100644
index 3f1b9a6f3c..0000000000
--- a/sysdeps/mach/hurd/getlogin.c
+++ /dev/null
@@ -1,35 +0,0 @@ 
-/* Copyright (C) 1991-2023 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/>.  */
-
-#include <stddef.h>
-#include <errno.h>
-#include <unistd.h>
-#include <hurd.h>
-
-/* Return the login name of the user, or NULL if it can't be determined.
-   The returned pointer, if not NULL, is good only until the next call.  */
-char *
-getlogin (void)
-{
-  static char login[1024];	/* XXX */
-  error_t err;
-
-  if (err = __USEPORT (PROC, __proc_getlogin (port, login)))
-    return __hurd_fail (err), NULL;
-
-  return login;
-}
diff --git a/sysdeps/unix/getlogin.c b/sysdeps/unix/getlogin.c
deleted file mode 100644
index 225a24a2b5..0000000000
--- a/sysdeps/unix/getlogin.c
+++ /dev/null
@@ -1,81 +0,0 @@ 
-/* Copyright (C) 1991-2023 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/>.  */
-
-#include <errno.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdio.h>
-#include <limits.h>
-#include <fcntl.h>
-
-#include <utmp.h>
-
-static char name[UT_NAMESIZE + 1];
-
-/* Return the login name of the user, or NULL if it can't be determined.
-   The returned pointer, if not NULL, is good only until the next call.  */
-
-#ifdef STATIC
-STATIC
-#endif
-char *
-getlogin (void)
-{
-  char tty_pathname[2 + 2 * NAME_MAX];
-  char *real_tty_path = tty_pathname;
-  int err;
-  char *result = NULL;
-  struct utmp *ut, line, buffer;
-
-  /* Get name of tty connected to fd 0.  Return NULL if not a tty or
-     if fd 0 isn't open.  Note that a lot of documentation says that
-     getlogin() is based on the controlling terminal---what they
-     really mean is "the terminal connected to standard input".  The
-     getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all
-     return NULL if fd 0 has been closed, so this is the compatible
-     thing to do.  Note that ttyname(open("/dev/tty")) on those
-     systems returns /dev/tty, so that is not a possible solution for
-     getlogin().  */
-  err = __ttyname_r (0, real_tty_path, sizeof (tty_pathname));
-  if (err != 0)
-    {
-      __set_errno (err);
-      return NULL;
-    }
-
-  real_tty_path += 5;		/* Remove "/dev/".  */
-
-  __setutent ();
-  strncpy (line.ut_line, real_tty_path, sizeof line.ut_line);
-  if (__getutline_r (&line, &buffer, &ut) < 0)
-    {
-      if (errno == ESRCH)
-	/* The caller expects ENOENT if nothing is found.  */
-	__set_errno (ENOENT);
-      result = NULL;
-    }
-  else
-    {
-      strncpy (name, ut->ut_user, UT_NAMESIZE);
-      name[UT_NAMESIZE] = '\0';
-      result = name;
-    }
-
-  __endutent ();
-
-  return result;
-}
diff --git a/sysdeps/unix/getlogin_r.c b/sysdeps/unix/getlogin_r.c
deleted file mode 100644
index 2f7e8ac479..0000000000
--- a/sysdeps/unix/getlogin_r.c
+++ /dev/null
@@ -1,103 +0,0 @@ 
-/* Reentrant function to return the current login name.  Unix version.
-   Copyright (C) 1991-2023 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/>.  */
-
-#include <errno.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdio.h>
-#include <limits.h>
-#include <fcntl.h>
-
-#include <utmp.h>
-#include "../login/utmp-private.h"
-
-/* Return at most NAME_LEN characters of the login name of the user in NAME.
-   If it cannot be determined or some other error occurred, return the error
-   code.  Otherwise return 0.  */
-
-#ifdef STATIC
-STATIC
-#endif
-int
-__getlogin_r (char *name, size_t name_len)
-{
-  char tty_pathname[2 + 2 * NAME_MAX];
-  char *real_tty_path = tty_pathname;
-  int result;
-  struct utmp *ut, line, buffer;
-
-  /* Get name of tty connected to fd 0.  Return if not a tty or
-     if fd 0 isn't open.  Note that a lot of documentation says that
-     getlogin() is based on the controlling terminal---what they
-     really mean is "the terminal connected to standard input".  The
-     getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all
-     return NULL if fd 0 has been closed, so this is the compatible
-     thing to do.  Note that ttyname(open("/dev/tty")) on those
-     systems returns /dev/tty, so that is not a possible solution for
-     getlogin().  */
-
-  result = __ttyname_r (0, real_tty_path, sizeof (tty_pathname));
-
-  if (result != 0)
-    return result;
-
-  real_tty_path += 5;		/* Remove "/dev/".  */
-  strncpy (line.ut_line, real_tty_path, sizeof line.ut_line);
-
-  /* We don't use the normal entry points __setutent et al, because we
-     want setutent + getutline_r + endutent all to happen with the lock
-     held so that our search is thread-safe.  */
-
-  __libc_lock_lock (__libc_utmp_lock);
-  __libc_setutent ();
-  result = __libc_getutline_r (&line, &buffer, &ut);
-  if (result < 0)
-    {
-      if (errno == ESRCH)
-	/* The caller expects ENOENT if nothing is found.  */
-	result = ENOENT;
-      else
-	result = errno;
-    }
-  __libc_endutent ();
-  __libc_lock_unlock (__libc_utmp_lock);
-
-  if (result == 0)
-    {
-      size_t needed = __strnlen (ut->ut_user, UT_NAMESIZE) + 1;
-
-      if (needed > name_len)
-	{
-	  __set_errno (ERANGE);
-	  result = ERANGE;
-	}
-      else
-	{
-	  memcpy (name, ut->ut_user, needed - 1);
-	  name[needed - 1] = 0;
-	  result = 0;
-	}
-    }
-
-  return result;
-}
-#ifndef STATIC
-libc_hidden_def (__getlogin_r)
-weak_alias (__getlogin_r, getlogin_r)
-libc_hidden_weak (getlogin_r)
-#endif
diff --git a/sysdeps/unix/sysv/linux/getlogin.c b/sysdeps/unix/sysv/linux/getlogin.c
deleted file mode 100644
index df50b847e3..0000000000
--- a/sysdeps/unix/sysv/linux/getlogin.c
+++ /dev/null
@@ -1,39 +0,0 @@ 
-/* Copyright (C) 2010-2023 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/>.  */
-
-#include <pwd.h>
-#include <unistd.h>
-#include <not-cancel.h>
-
-#define STATIC static
-#define getlogin getlogin_fd0
-#include <sysdeps/unix/getlogin.c>
-#undef getlogin
-
-
-/* Return the login name of the user, or NULL if it can't be determined.
-   The returned pointer, if not NULL, is good only until the next call.  */
-
-char *
-getlogin (void)
-{
-  int res = __getlogin_r_loginuid (name, sizeof (name));
-  if (res >= 0)
-    return res == 0 ? name : NULL;
-
-  return getlogin_fd0 ();
-}
diff --git a/sysdeps/unix/sysv/linux/getlogin_r.c b/sysdeps/unix/sysv/linux/getlogin_r.c
index 4ae9a53503..3d3e75cc24 100644
--- a/sysdeps/unix/sysv/linux/getlogin_r.c
+++ b/sysdeps/unix/sysv/linux/getlogin_r.c
@@ -19,29 +19,24 @@ 
 #include <unistd.h>
 #include <not-cancel.h>
 #include <scratch_buffer.h>
+#include <intprops.h>
 
-#define STATIC static
-static int getlogin_r_fd0 (char *name, size_t namesize);
-#define __getlogin_r getlogin_r_fd0
-#include <sysdeps/unix/getlogin_r.c>
-#undef __getlogin_r
-
+/* Return at most NAME_LEN characters of the login name of the user in NAME.
+   If it cannot be determined or some other error occurred, return the error
+   code.  Otherwise return 0.
 
-/* Try to determine login name from /proc/self/loginuid and return 0
+   Try to determine login name from /proc/self/loginuid and return 0
    if successful.  If /proc/self/loginuid cannot be read return -1.
    Otherwise return the error number.  */
 
 int
-attribute_hidden
-__getlogin_r_loginuid (char *name, size_t namesize)
+__getlogin_r (char *name, size_t namesize)
 {
   int fd = __open_nocancel ("/proc/self/loginuid", O_RDONLY);
   if (fd == -1)
     return -1;
 
-  /* We are reading a 32-bit number.  12 bytes are enough for the text
-     representation.  If not, something is wrong.  */
-  char uidbuf[12];
+  char uidbuf[INT_BUFSIZE_BOUND (uid_t)];
   ssize_t n = TEMP_FAILURE_RETRY (__read_nocancel (fd, uidbuf,
 						   sizeof (uidbuf)));
   __close_nocancel_nostatus (fd);
@@ -98,21 +93,6 @@  __getlogin_r_loginuid (char *name, size_t namesize)
   scratch_buffer_free (&tmpbuf);
   return result;
 }
-
-
-/* Return at most NAME_LEN characters of the login name of the user in NAME.
-   If it cannot be determined or some other error occurred, return the error
-   code.  Otherwise return 0.  */
-
-int
-__getlogin_r (char *name, size_t namesize)
-{
-  int res = __getlogin_r_loginuid (name, namesize);
-  if (res >= 0)
-    return res;
-
-  return getlogin_r_fd0 (name, namesize);
-}
 libc_hidden_def (__getlogin_r)
 weak_alias (__getlogin_r, getlogin_r)
 libc_hidden_weak (getlogin_r)