1 /* Test ldconfig after /etc/ld.so.conf update and verify that a running process
2 observes changes to /etc/ld.so.cache.
4 Copyright (C) 2019-2022 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
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.
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.
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/>. */
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>
34 #define DSO "libldconfig-ld-mod.so"
35 #define DSO_DIR "/tmp/tst-ldconfig"
36 #define CONF "/etc/ld.so.conf"
40 run_ldconfig (void *x
__attribute__((unused
)))
42 char *prog
= xasprintf ("%s/ldconfig", support_install_rootsbindir
);
43 char *args
[] = { prog
, NULL
};
45 execv (args
[0], args
);
46 FAIL_EXIT1 ("execv: %m");
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.)
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.)
61 Try to dlopen it again. This should finally succeed. */
65 struct support_capture_subprocess result
;
67 /* Create the needed directories. */
68 xmkdirp ("/var/cache/ldconfig", 0777);
69 xmkdirp (DSO_DIR
, 0777);
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");
81 /* Open the DSO. We expect this to fail - tst-ldconfig directory
83 TEST_VERIFY_EXIT (dlopen (DSO
, RTLD_NOW
| RTLD_GLOBAL
) == NULL
);
85 FILE *fp
= xfopen (CONF
, "a+");
87 FAIL_EXIT1 ("creating /etc/ld.so.conf failed: %m");
91 result
= support_capture_subprocess (run_ldconfig
, NULL
);
92 support_capture_subprocess_check (&result
, "execv", 0, sc_allow_none
);
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
);
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");
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
);
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
);
111 /* Finally, we expect dlopen to pass now. */
112 TEST_VERIFY_EXIT (dlopen (DSO
, RTLD_NOW
| RTLD_GLOBAL
) != NULL
);
117 #include <support/test-driver.c>