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