6ca6e8
commit 2d7550e6cfff541380d3a1f2ac33e76aaf1273de
6ca6e8
Author: Florian Weimer <fweimer@redhat.com>
6ca6e8
Date:   Wed Feb 8 18:11:04 2023 +0100
6ca6e8
6ca6e8
    elf: Smoke-test ldconfig -p against system /etc/ld.so.cache
6ca6e8
    
6ca6e8
    The test is sufficient to detect the ldconfig bug fixed in
6ca6e8
    commit 9fe6f6363886aae6b2b210cae3ed1f5921299083 ("elf: Fix 64 time_t
6ca6e8
    support for installed statically binaries").
6ca6e8
    
6ca6e8
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
6ca6e8
    (cherry picked from commit 9fd63e35371b9939e9153907c6a753e6960b68ad)
6ca6e8
6ca6e8
diff --git a/elf/Makefile b/elf/Makefile
6ca6e8
index 3a8590e0d3cc33ab..0daa8a85ec1a1bc5 100644
6ca6e8
--- a/elf/Makefile
6ca6e8
+++ b/elf/Makefile
6ca6e8
@@ -553,6 +553,7 @@ ifeq ($(run-built-tests),yes)
6ca6e8
 tests-special += \
6ca6e8
   $(objpfx)noload-mem.out \
6ca6e8
   $(objpfx)tst-ldconfig-X.out \
6ca6e8
+  $(objpfx)tst-ldconfig-p.out \
6ca6e8
   $(objpfx)tst-leaks1-mem.out \
6ca6e8
   $(objpfx)tst-rtld-help.out \
6ca6e8
   # tests-special
6ca6e8
@@ -2259,6 +2260,11 @@ $(objpfx)tst-ldconfig-X.out : tst-ldconfig-X.sh $(objpfx)ldconfig
6ca6e8
 		 '$(run-program-env)' > $@; \
6ca6e8
 	$(evaluate-test)
6ca6e8
 
6ca6e8
+$(objpfx)tst-ldconfig-p.out : tst-ldconfig-p.sh $(objpfx)ldconfig
6ca6e8
+	$(SHELL) $< '$(common-objpfx)' '$(test-wrapper-env)' \
6ca6e8
+		 '$(run-program-env)' > $@; \
6ca6e8
+	$(evaluate-test)
6ca6e8
+
6ca6e8
 # Test static linking of all the libraries we can possibly link
6ca6e8
 # together.  Note that in some configurations this may be less than the
6ca6e8
 # complete list of libraries we build but we try to maxmimize this list.
6ca6e8
diff --git a/elf/tst-ldconfig-p.sh b/elf/tst-ldconfig-p.sh
6ca6e8
new file mode 100644
6ca6e8
index 0000000000000000..ec937bf4ec04e8c0
6ca6e8
--- /dev/null
6ca6e8
+++ b/elf/tst-ldconfig-p.sh
6ca6e8
@@ -0,0 +1,77 @@
6ca6e8
+#!/bin/sh
6ca6e8
+# Test that ldconfig -p prints something useful.
6ca6e8
+# Copyright (C) 2023 Free Software Foundation, Inc.
6ca6e8
+# This file is part of the GNU C Library.
6ca6e8
+
6ca6e8
+# The GNU C Library is free software; you can redistribute it and/or
6ca6e8
+# modify it under the terms of the GNU Lesser General Public
6ca6e8
+# License as published by the Free Software Foundation; either
6ca6e8
+# version 2.1 of the License, or (at your option) any later version.
6ca6e8
+
6ca6e8
+# The GNU C Library is distributed in the hope that it will be useful,
6ca6e8
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
6ca6e8
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6ca6e8
+# Lesser General Public License for more details.
6ca6e8
+
6ca6e8
+# You should have received a copy of the GNU Lesser General Public
6ca6e8
+# License along with the GNU C Library; if not, see
6ca6e8
+# <https://www.gnu.org/licenses/>.
6ca6e8
+
6ca6e8
+# Check that the newly built ldconfig -p can dump the system
6ca6e8
+# /etc/ld.so.cache file.  This should always work even if the ABIs are
6ca6e8
+# not compatible, except in a cross-endian build (that presumably
6ca6e8
+# involves emulation when running ldconfig).
6ca6e8
+
6ca6e8
+common_objpfx=$1
6ca6e8
+test_wrapper_env=$2
6ca6e8
+run_program_env=$3
6ca6e8
+
6ca6e8
+if ! test -r /etc/ld.so.cache; then
6ca6e8
+    echo "warning: /etc/ld.so.cache does not exist, test skipped"
6ca6e8
+    exit 77
6ca6e8
+fi
6ca6e8
+
6ca6e8
+testout="${common_objpfx}elf/tst-ldconfig-p.out"
6ca6e8
+# Truncate file.
6ca6e8
+: > "$testout"
6ca6e8
+
6ca6e8
+${test_wrapper_env} \
6ca6e8
+${run_program_env} \
6ca6e8
+${common_objpfx}elf/ldconfig -p \
6ca6e8
+  $testroot/lib >>"$testout" 2>>"$testout"
6ca6e8
+status=$?
6ca6e8
+echo "info: ldconfig exit status: $status" >>"$testout"
6ca6e8
+
6ca6e8
+errors=0
6ca6e8
+case $status in
6ca6e8
+    (0)
6ca6e8
+	if head -n 1 "$testout" | \
6ca6e8
+		grep -q "libs found in cache \`/etc/ld.so.cache'\$" ; then
6ca6e8
+	    echo "info: initial string found" >>"$testout"
6ca6e8
+	else
6ca6e8
+	    echo "error: initial string not found" >>"$testout"
6ca6e8
+	    errors=1
6ca6e8
+	fi
6ca6e8
+	if grep -q "^	libc\.so\..* => " "$testout"; then
6ca6e8
+	    echo "info: libc.so.* string found" >>"$testout"
6ca6e8
+	else
6ca6e8
+	    echo "error: libc.so.* string not found" >>"$testout"
6ca6e8
+	    errors=1
6ca6e8
+	fi
6ca6e8
+	;;
6ca6e8
+    (1)
6ca6e8
+	if head -n 1 "$testout" | \
6ca6e8
+		grep -q ": Cache file has wrong endianness\.$" ; then
6ca6e8
+	    echo "info: cache file has wrong endianess" >> "$testout"
6ca6e8
+	else
6ca6e8
+	    echo "error: unexpected ldconfig error message" >> "$testout"
6ca6e8
+	    errors=1
6ca6e8
+	fi
6ca6e8
+	;;
6ca6e8
+    (*)
6ca6e8
+	echo "error: unexpected exit status" >> "$testout"
6ca6e8
+	errors=1
6ca6e8
+	;;
6ca6e8
+esac
6ca6e8
+
6ca6e8
+exit $errors