From 0d0e4e1fd2899f9a8642a5fa78e0d4a46451cc48 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 6 Jun 2022 11:55:04 +0200 Subject: [PATCH] getlogin, getlogin_r tests: Avoid test failure in specific environments. Reported by Letu Ren in . * modules/getlogin-tests (Depends-on): Add stdbool. * modules/getlogin_r-tests (Depends-on): Likewise. * tests/test-getlogin.h: Include stdbool.h. (test_getlogin_result): On Linux, skip the test if /proc/self/loginuid contains "-1". --- ChangeLog | 11 +++++++++++ modules/getlogin-tests | 1 + modules/getlogin_r-tests | 1 + tests/test-getlogin.h | 32 +++++++++++++++++++++++++++++--- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index a805d0b2ad..7c9319f33f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2022-06-06 Bruno Haible + + getlogin, getlogin_r tests: Avoid test failure in specific environments. + Reported by Letu Ren in + . + * modules/getlogin-tests (Depends-on): Add stdbool. + * modules/getlogin_r-tests (Depends-on): Likewise. + * tests/test-getlogin.h: Include stdbool.h. + (test_getlogin_result): On Linux, skip the test if /proc/self/loginuid + contains "-1". + 2022-05-21 Paul Eggert strstr-simple: pacify GCC 12.1 diff --git a/modules/getlogin-tests b/modules/getlogin-tests index 0ccd2ebb71..5056a9ca47 100644 --- a/modules/getlogin-tests +++ b/modules/getlogin-tests @@ -5,6 +5,7 @@ tests/signature.h tests/macros.h Depends-on: +stdbool configure.ac: diff --git a/modules/getlogin_r-tests b/modules/getlogin_r-tests index 190bc9148d..7918ebd183 100644 --- a/modules/getlogin_r-tests +++ b/modules/getlogin_r-tests @@ -5,6 +5,7 @@ tests/signature.h tests/macros.h Depends-on: +stdbool configure.ac: diff --git a/tests/test-getlogin.h b/tests/test-getlogin.h index 1a1573af4f..96d95ee1c1 100644 --- a/tests/test-getlogin.h +++ b/tests/test-getlogin.h @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -42,11 +43,34 @@ test_getlogin_result (const char *buf, int err) exit (77); } - /* It fails when stdin is not connected to a tty. */ ASSERT (err == ENOTTY || err == EINVAL /* seen on Linux/SPARC */ || err == ENXIO ); + +#if defined __linux__ + /* On Linux, it is possible to set up a chroot environment in such a way + that stdin is connected to a tty and nervertheless /proc/self/loginuid + contains "-1". In this situation, getlogin() and getlogin_r() fail; + this is expected. */ + bool loginuid_undefined = false; + /* Does the special file /proc/self/loginuid contain "-1"? */ + FILE *fp = fopen ("/proc/self/loginuid", "r"); + if (fp != NULL) + { + char buf[3]; + loginuid_undefined = + (fread (buf, 1, 3, fp) == 2 && buf[0] == '-' && buf[1] == '1'); + fclose (fp); + } + if (loginuid_undefined) + { + fprintf (stderr, "Skipping test: loginuid is undefined.\n"); + exit (77); + } +#endif + + /* It fails when stdin is not connected to a tty. */ #if !defined __hpux /* On HP-UX 11.11 it fails anyway. */ ASSERT (! isatty (0)); #endif @@ -63,8 +87,10 @@ test_getlogin_result (const char *buf, int err) if (!isatty (STDIN_FILENO)) { - fprintf (stderr, "Skipping test: stdin is not a tty.\n"); - exit (77); + /* We get here, for example, when running under 'nohup' or as part of a + non-interactive ssh command. */ + fprintf (stderr, "Skipping test: stdin is not a tty.\n"); + exit (77); } ASSERT (fstat (STDIN_FILENO, &stat_buf) == 0); -- 2.39.5