]> Savannah Git Hosting - gnulib.git/commitdiff
getlogin-tests: avoid false failure under sudo/ssh etc.
authorGuilherme de Almeida Suckevicz <guito.linux@gmail.com>
Wed, 14 May 2014 21:06:24 +0000 (22:06 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 14 May 2014 21:13:38 +0000 (22:13 +0100)
* modules/getlogin-tests (configure.ac): Check for ttyname().
* tests/test-getlogin.c (main): Don't depend on environment variables
to correlate with getlogin(), since sudo and ssh etc. can tamper
with the LOGNAME and USER env vars.  Instead lookup the name from
the uid associated with the stdin tty.

ChangeLog
modules/getlogin-tests
tests/test-getlogin.c

index a8b2ee08451c58cda1b8214060b7b47a2a32aa8d..19dfba597193e2df5151f8222294f42244fd3da1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2014-05-14  Guilherme de Almeida Suckevicz  <guito.linux@gmail.com>
+
+       getlogin-tests: avoid false failure under sudo/ssh etc.
+       * modules/getlogin-tests (configure.ac): Check for ttyname().
+       * tests/test-getlogin.c (main): Don't depend on environment variables
+       to correlate with getlogin(), since sudo and ssh etc. can tamper
+       with the LOGNAME and USER env vars.  Instead lookup the name from
+       the uid associated with the stdin tty.
+
 2014-05-11  Paul Eggert  <eggert@cs.ucla.edu>
 
        mbsstr, quotearg, xstrtol: pacify IRIX 6.5 cc
index 795fba784fa64ee0076ea5fbded9e69126b6ec74..6facd60fb03857ca08e35474389afdf1ebed2e36 100644 (file)
@@ -6,6 +6,7 @@ tests/macros.h
 Depends-on:
 
 configure.ac:
+AC_CHECK_FUNCS_ONCE([ttyname])
 
 Makefile.am:
 TESTS += test-getlogin
index 197aed8275d9ff946690c6f87d8843bf62e377e0..32fd89382bcbe575d9fc3083cc27aade8d101ef9 100644 (file)
@@ -27,6 +27,11 @@ SIGNATURE_CHECK (getlogin, char *, (void));
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+#include <pwd.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
 
 #include "macros.h"
 
@@ -62,11 +67,32 @@ main (void)
 #if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
   /* Unix platform */
   {
-    const char *name = getenv ("LOGNAME");
-    if (name == NULL || name[0] == '\0')
-      name = getenv ("USER");
-    if (name != NULL && name[0] != '\0')
-      ASSERT (strcmp (buf, name) == 0);
+# if HAVE_TTYNAME
+    const char *tty;
+    struct stat stat_buf;
+    struct passwd *pwd;
+
+    tty = ttyname (STDIN_FILENO);
+    if (tty == NULL)
+      {
+         ASSERT (errno == ENOTTY);
+
+         fprintf (stderr, "Skipping test: stdin is not a tty.\n");
+         return 77;
+      }
+
+    ASSERT (stat (tty, &stat_buf) == 0);
+
+    pwd = getpwuid (stat_buf.st_uid);
+    if (! pwd)
+      {
+         fprintf (stderr, "Skipping test: no name found for uid %d\n",
+                  stat_buf.st_uid);
+         return 77;
+      }
+
+    ASSERT (strcmp (pwd->pw_name, buf) == 0);
+# endif
   }
 #endif
 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__