summary refs log tree commit diff
path: root/sysdeps/unix/sysv/linux/getlogin.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2010-03-24 17:02:57 -0700
committerUlrich Drepper <drepper@redhat.com>2010-03-24 17:02:57 -0700
commitc8727fa6e5073d28ed6d0eb40a006ac2c1b9f9f3 (patch)
tree23369e9301bb60abd73e4384e7974fa32acd6e57 /sysdeps/unix/sysv/linux/getlogin.c
parentfd8ccb0427569ffdfbb70c8828029122f3459160 (diff)
downloadglibc-c8727fa6e5073d28ed6d0eb40a006ac2c1b9f9f3.tar.gz
glibc-c8727fa6e5073d28ed6d0eb40a006ac2c1b9f9f3.tar.xz
glibc-c8727fa6e5073d28ed6d0eb40a006ac2c1b9f9f3.zip
Fix Linux getlogin{_r,} implementation
The old implementation uses fd 0 to determine the login TTY.  This
was needed because using /dev/tty it is not possible to deduce the
login TTY.  For some time now there is the pseudo-file
/proc/self/loginuid which directly helps us to find the user.  Prefer
using this file.  It also works if stdin is closed, redirected, or
re-opened.
Diffstat (limited to 'sysdeps/unix/sysv/linux/getlogin.c')
-rw-r--r--sysdeps/unix/sysv/linux/getlogin.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/sysdeps/unix/sysv/linux/getlogin.c b/sysdeps/unix/sysv/linux/getlogin.c
new file mode 100644
index 0000000000..4d15db093d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/getlogin.c
@@ -0,0 +1,39 @@
+/* Copyright (C) 2010 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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#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)
+{
+  if (__getlogin_r_loginuid (name, sizeof (name)) == 0)
+    return name;
+
+  return getlogin_fd0 ();
+}