1 /* Test ldconfig after /etc/ld.so.conf update and verify that a running process
2 observes changes to /etc/ld.so.cache.
3
4 Copyright (C) 2019-2022 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, see <https://www.gnu.org/licenses/>. */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <support/capture_subprocess.h>
27 #include <support/check.h>
28 #include <support/support.h>
29 #include <support/xdlfcn.h>
30 #include <support/xstdio.h>
31 #include <support/xunistd.h>
32
33
34 #define DSO "libldconfig-ld-mod.so"
35 #define DSO_DIR "/tmp/tst-ldconfig"
36 #define CONF "/etc/ld.so.conf"
37
38
39 static void
run_ldconfig(void * x)40 run_ldconfig (void *x __attribute__((unused)))
41 {
42 char *prog = xasprintf ("%s/ldconfig", support_install_rootsbindir);
43 char *args[] = { prog, NULL };
44
45 execv (args[0], args);
46 FAIL_EXIT1 ("execv: %m");
47 }
48
49
50 /* Create a new directory.
51 Copy a test shared object there.
52 Try to dlopen it by soname. This should fail.
53 (Directory is not searched.)
54 Run ldconfig.
55 Try to dlopen it again. It should still fail.
56 (Directory is still not searched.)
57 Add the directory to /etc/ld.so.conf.
58 Try to dlopen it again. It should still fail.
59 (The loader does not read /etc/ld.so.conf, only /etc/ld.so.cache.)
60 Run ldconfig.
61 Try to dlopen it again. This should finally succeed. */
62 static int
do_test(void)63 do_test (void)
64 {
65 struct support_capture_subprocess result;
66
67 /* Create the needed directories. */
68 xmkdirp ("/var/cache/ldconfig", 0777);
69 xmkdirp (DSO_DIR, 0777);
70
71 /* Rename the DSO to start with "lib" because there's an undocumented
72 filter in ldconfig where it ignores any file that doesn't start with
73 "lib" (for regular shared libraries) or "ld-" (for ld-linux-*). */
74 char *mod_src_path = xasprintf ("%s/tst-ldconfig-ld-mod.so",
75 support_libdir_prefix);
76 if (rename (mod_src_path, "/tmp/tst-ldconfig/libldconfig-ld-mod.so"))
77 FAIL_EXIT1 ("Renaming/moving the DSO failed: %m");
78 free (mod_src_path);
79
80
81 /* Open the DSO. We expect this to fail - tst-ldconfig directory
82 is not searched. */
83 TEST_VERIFY_EXIT (dlopen (DSO, RTLD_NOW | RTLD_GLOBAL) == NULL);
84
85 FILE *fp = xfopen (CONF, "a+");
86 if (!fp)
87 FAIL_EXIT1 ("creating /etc/ld.so.conf failed: %m");
88 xfclose (fp);
89
90 /* Run ldconfig. */
91 result = support_capture_subprocess (run_ldconfig, NULL);
92 support_capture_subprocess_check (&result, "execv", 0, sc_allow_none);
93
94 /* Try to dlopen the same DSO again, we expect this to fail again. */
95 TEST_VERIFY_EXIT (dlopen (DSO, RTLD_NOW | RTLD_GLOBAL) == NULL);
96
97 /* Add tst-ldconfig directory to /etc/ld.so.conf. */
98 fp = xfopen (CONF, "w");
99 if (!(fwrite (DSO_DIR, 1, sizeof (DSO_DIR), fp)))
100 FAIL_EXIT1 ("updating /etc/ld.so.conf failed: %m");
101 xfclose (fp);
102
103 /* Try to dlopen the same DSO again, we expect this to still fail. */
104 TEST_VERIFY_EXIT (dlopen (DSO, RTLD_NOW | RTLD_GLOBAL) == NULL);
105
106 /* Run ldconfig again. */
107 result = support_capture_subprocess (run_ldconfig, NULL);
108 support_capture_subprocess_check (&result, "execv", 0, sc_allow_none);
109 support_capture_subprocess_free (&result);
110
111 /* Finally, we expect dlopen to pass now. */
112 TEST_VERIFY_EXIT (dlopen (DSO, RTLD_NOW | RTLD_GLOBAL) != NULL);
113
114 return 0;
115 }
116
117 #include <support/test-driver.c>
118