getlogin: Fix link error with option --with-systemd

Bruno Haible via Gnulib discussion list <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Message-ID <2825888.iUBGbCP9Lu@cagnes>
Building a test directory for modules
  getlogin getlogin_r
configured with
  gl_cv_func_getlogin_works=no gl_cv_func_getlogin_r_works=no ./configure --with-systemd
I get a link error:

gcc -Wno-error -Wno-error -g   -o test-getlogin test-getlogin.o libtests.a ../gllib/libgnu.a libtests.a ../gllib/libgnu.a libtests.a    
/usr/bin/x86_64-linux-gnu-ld.bfd: ../gllib/libgnu.a(readutmp.o): in function `read_utmp_from_systemd':
/GNULIB/testdir4/gllib/readutmp.c:825:(.text+0x13a8): undefined reference to `sd_get_sessions'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:833:(.text+0x13f6): undefined reference to `sd_session_get_start_time'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:840:(.text+0x1476): undefined reference to `sd_session_get_seat'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:847:(.text+0x14ad): undefined reference to `sd_session_get_tty'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:851:(.text+0x14d6): undefined reference to `sd_session_get_type'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:856:(.text+0x151a): undefined reference to `sd_session_get_service'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:860:(.text+0x153f): undefined reference to `sd_session_get_uid'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:878:(.text+0x166a): undefined reference to `sd_session_get_service'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:890:(.text+0x16b9): undefined reference to `sd_session_get_username'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:894:(.text+0x16e1): undefined reference to `sd_session_get_leader'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:898:(.text+0x1705): undefined reference to `sd_session_get_class'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:905:(.text+0x1758): undefined reference to `sd_session_get_remote_host'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:910:(.text+0x178d): undefined reference to `sd_session_get_type'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:915:(.text+0x17d1): undefined reference to `sd_session_get_display'
/usr/bin/x86_64-linux-gnu-ld.bfd: /GNULIB/testdir4/gllib/readutmp.c:926:(.text+0x1816): undefined reference to `sd_session_get_remote_user'
collect2: error: ld returned 1 exit status
make[4]: *** [Makefile:3805: test-getlogin] Error 1

This patch fixes it.


2026-08-27  Bruno Haible  <[email protected]>

	getlogin: Fix link error with option --with-systemd (regr. 2025-03-09).
	Reported by Helmut Grohne <[email protected]> in
	<https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1129960>.
	* m4/getlogin_r.m4 (gl_FUNC_GETLOGIN_R): Inline the gl_LIB_GETLOGIN
	macro, assigning to GETLOGIN_R_LIB instead of GETLOGIN_LIB.
	* m4/getlogin.m4 (gl_FUNC_GETLOGIN): Inline the gl_LIB_GETLOGIN macro.
	Consider $READUTMP_LIB for $GETLOGIN_LIB.
	(gl_LIB_GETLOGIN): Remove macro.
	* modules/getlogin (configure.ac): Don't require gl_LIB_GETLOGIN.
	* modules/getlogin_r (configure.ac): Likewise.
	* modules/getlogin_r-tests (Makefile.am): Update link requirements for
	test-getlogin_r.
	* NEWS: Mention the change in the getlogin_r module.

diff --git a/NEWS b/NEWS
index 28a5869ee8..6d811da229 100644
--- a/NEWS
+++ b/NEWS
@@ -78,6 +78,9 @@ User visible incompatible changes
 
 Date        Modules         Changes
 
+2026-08-27  getlogin_r      The link requirements are changed from
+                            $(GETLOGIN_LIB) to $(GETLOGIN_R_LIB).
+
 2026-08-10  eloop-threshold Now include <min-eloop-threshold.h> and use
                             MIN_ELOOP_THRESHOLD rather than including
                             <eloop-threshold.h> and using __eloop_threshold ().
diff --git a/m4/getlogin.m4 b/m4/getlogin.m4
index adf171472d..4f8fa8b0e7 100644
--- a/m4/getlogin.m4
+++ b/m4/getlogin.m4
@@ -1,5 +1,5 @@
 # getlogin.m4
-# serial 8
+# serial 9
 dnl Copyright (C) 2010-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -9,6 +9,8 @@
 AC_DEFUN([gl_FUNC_GETLOGIN],
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_REQUIRE([AC_CANONICAL_HOST])
+  AC_REQUIRE([gl_READUTMP])
   AC_CHECK_DECLS_ONCE([getlogin])
   if test $ac_cv_have_decl_getlogin = no; then
     HAVE_DECL_GETLOGIN=0
@@ -18,7 +20,6 @@ AC_DEFUN([gl_FUNC_GETLOGIN]
     HAVE_GETLOGIN=0
   else
     dnl On musl libc, getlogin returns getenv ("LOGNAME").
-    AC_REQUIRE([AC_CANONICAL_HOST])
     AC_CACHE_CHECK([whether getlogin works],
       [gl_cv_func_getlogin_works],
       [
@@ -64,19 +65,16 @@ AC_DEFUN([gl_FUNC_GETLOGIN]
       *) REPLACE_GETLOGIN=1 ;;
     esac
   fi
-])
 
-dnl Determines the library needed by the implementation of the
-dnl getlogin and getlogin_r functions.
-AC_DEFUN([gl_LIB_GETLOGIN],
-[
-  AC_REQUIRE([AC_CANONICAL_HOST])
   case $host_os in
     mingw* | windows*)
       GETLOGIN_LIB='-ladvapi32' ;;
     *)
       GETLOGIN_LIB= ;;
   esac
+  if test $HAVE_GETLOGIN = 0 || test $REPLACE_GETLOGIN = 1; then
+    GETLOGIN_LIB="${GETLOGIN_LIB} ${READUTMP_LIB}"
+  fi
   AC_SUBST([GETLOGIN_LIB])
   dnl For backward compatibility.
   LIB_GETLOGIN="$GETLOGIN_LIB"
diff --git a/m4/getlogin_r.m4 b/m4/getlogin_r.m4
index de91f10728..9c53e3a749 100644
--- a/m4/getlogin_r.m4
+++ b/m4/getlogin_r.m4
@@ -1,5 +1,5 @@
 # getlogin_r.m4
-# serial 17
+# serial 18
 dnl Copyright (C) 2005-2007, 2009-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -14,6 +14,7 @@
 AC_DEFUN([gl_FUNC_GETLOGIN_R],
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_REQUIRE([AC_CANONICAL_HOST])
 
   dnl Persuade glibc <unistd.h> to declare getlogin_r().
   dnl Persuade Solaris <unistd.h> to provide the POSIX compliant declaration of
@@ -36,7 +37,6 @@ AC_DEFUN([gl_FUNC_GETLOGIN_R]
     dnl On Mac OS X 10.13, getlogin_r returns a truncated result
     dnl if the buffer is not large enough.
     dnl On musl libc, getlogin_r returns getenv ("LOGNAME").
-    AC_REQUIRE([AC_CANONICAL_HOST])
     AC_CACHE_CHECK([whether getlogin_r works],
       [gl_cv_func_getlogin_r_works],
       [
@@ -95,6 +95,14 @@ AC_DEFUN([gl_FUNC_GETLOGIN_R]
       *) REPLACE_GETLOGIN_R=1 ;;
     esac
   fi
+
+  case $host_os in
+    mingw* | windows*)
+      GETLOGIN_R_LIB='-ladvapi32' ;;
+    *)
+      GETLOGIN_R_LIB= ;;
+  esac
+  AC_SUBST([GETLOGIN_R_LIB])
 ])
 
 AC_DEFUN([gl_PREREQ_GETLOGIN_R],
diff --git a/modules/getlogin b/modules/getlogin
index ddb221bd9d..c7be9863b9 100644
--- a/modules/getlogin
+++ b/modules/getlogin
@@ -15,7 +15,6 @@ gl_FUNC_GETLOGIN
 gl_CONDITIONAL([GL_COND_OBJ_GETLOGIN],
                [test $HAVE_GETLOGIN = 0 || test $REPLACE_GETLOGIN = 1])
 gl_UNISTD_MODULE_INDICATOR([getlogin])
-AC_REQUIRE([gl_LIB_GETLOGIN])
 
 Makefile.am:
 if GL_COND_OBJ_GETLOGIN
diff --git a/modules/getlogin_r b/modules/getlogin_r
index d2a00aca78..2abfbb8737 100644
--- a/modules/getlogin_r
+++ b/modules/getlogin_r
@@ -21,7 +21,6 @@ AM_COND_IF([GL_COND_OBJ_GETLOGIN_R], [
   gl_PREREQ_GETLOGIN_R
 ])
 gl_UNISTD_MODULE_INDICATOR([getlogin_r])
-AC_REQUIRE([gl_LIB_GETLOGIN])
 
 Makefile.am:
 if GL_COND_OBJ_GETLOGIN_R
@@ -32,7 +31,7 @@ Include:
 <unistd.h>
 
 Link:
-$(GETLOGIN_LIB)
+$(GETLOGIN_R_LIB)
 
 License:
 LGPLv2+
diff --git a/modules/getlogin_r-tests b/modules/getlogin_r-tests
index 2f34f41b7c..09d1abd56d 100644
--- a/modules/getlogin_r-tests
+++ b/modules/getlogin_r-tests
@@ -13,4 +13,4 @@ configure.ac:
 Makefile.am:
 TESTS += test-getlogin_r
 check_PROGRAMS += test-getlogin_r
-test_getlogin_r_LDADD = $(LDADD) $(GETLOGIN_LIB)
+test_getlogin_r_LDADD = $(LDADD) $(GETLOGIN_R_LIB)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.