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-2024 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 | |
37 | |
38 | static void |
39 | run_ldconfig (void *x __attribute__((unused))) |
40 | { |
41 | char *prog = xasprintf (format: "%s/ldconfig" , support_install_rootsbindir); |
42 | char *args[] = { prog, NULL }; |
43 | |
44 | execv (path: args[0], argv: args); |
45 | FAIL_EXIT1 ("execv: %m" ); |
46 | } |
47 | |
48 | |
49 | /* Create a new directory. |
50 | Copy a test shared object there. |
51 | Try to dlopen it by soname. This should fail. |
52 | (Directory is not searched.) |
53 | Run ldconfig. |
54 | Try to dlopen it again. It should still fail. |
55 | (Directory is still not searched.) |
56 | Add the directory to /etc/ld.so.conf. |
57 | Try to dlopen it again. It should still fail. |
58 | (The loader does not read /etc/ld.so.conf, only /etc/ld.so.cache.) |
59 | Run ldconfig. |
60 | Try to dlopen it again. This should finally succeed. */ |
61 | static int |
62 | do_test (void) |
63 | { |
64 | struct support_capture_subprocess result; |
65 | |
66 | char *conf_path = xasprintf (format: "%s/ld.so.conf" , support_sysconfdir_prefix); |
67 | |
68 | /* Create the needed directories. */ |
69 | xmkdirp ("/var/cache/ldconfig" , 0777); |
70 | xmkdirp (DSO_DIR, 0777); |
71 | |
72 | /* Rename the DSO to start with "lib" because there's an undocumented |
73 | filter in ldconfig where it ignores any file that doesn't start with |
74 | "lib" (for regular shared libraries) or "ld-" (for ld-linux-*). */ |
75 | char *mod_src_path = xasprintf (format: "%s/tst-ldconfig-ld-mod.so" , |
76 | support_libdir_prefix); |
77 | if (rename (old: mod_src_path, new: "/tmp/tst-ldconfig/libldconfig-ld-mod.so" )) |
78 | FAIL_EXIT1 ("Renaming/moving the DSO failed: %m" ); |
79 | free (ptr: mod_src_path); |
80 | |
81 | |
82 | /* Open the DSO. We expect this to fail - tst-ldconfig directory |
83 | is not searched. */ |
84 | TEST_VERIFY_EXIT (dlopen (DSO, RTLD_NOW | RTLD_GLOBAL) == NULL); |
85 | |
86 | FILE *fp = xfopen (path: conf_path, mode: "a+" ); |
87 | if (!fp) |
88 | FAIL_EXIT1 ("creating %s failed: %m" , conf_path); |
89 | xfclose (fp); |
90 | |
91 | /* Run ldconfig. */ |
92 | result = support_capture_subprocess (callback: run_ldconfig, NULL); |
93 | support_capture_subprocess_check (&result, context: "execv" , status_or_signal: 0, allowed: sc_allow_none); |
94 | |
95 | /* Try to dlopen the same DSO again, we expect this to fail again. */ |
96 | TEST_VERIFY_EXIT (dlopen (DSO, RTLD_NOW | RTLD_GLOBAL) == NULL); |
97 | |
98 | /* Add tst-ldconfig directory to /etc/ld.so.conf. */ |
99 | fp = xfopen (path: conf_path, mode: "w" ); |
100 | if (!(fwrite (DSO_DIR, size: 1, n: sizeof (DSO_DIR), s: fp))) |
101 | FAIL_EXIT1 ("updating %s failed: %m" , conf_path); |
102 | xfclose (fp); |
103 | |
104 | /* Try to dlopen the same DSO again, we expect this to still fail. */ |
105 | TEST_VERIFY_EXIT (dlopen (DSO, RTLD_NOW | RTLD_GLOBAL) == NULL); |
106 | |
107 | /* Run ldconfig again. */ |
108 | result = support_capture_subprocess (callback: run_ldconfig, NULL); |
109 | support_capture_subprocess_check (&result, context: "execv" , status_or_signal: 0, allowed: sc_allow_none); |
110 | support_capture_subprocess_free (&result); |
111 | |
112 | /* Finally, we expect dlopen to pass now. */ |
113 | TEST_VERIFY_EXIT (dlopen (DSO, RTLD_NOW | RTLD_GLOBAL) != NULL); |
114 | |
115 | free (ptr: conf_path); |
116 | |
117 | return 0; |
118 | } |
119 | |
120 | #include <support/test-driver.c> |
121 | |