maint: silence deprecated module warning
[coreutils.git] / tests / cp / nfs-removal-race.sh
blob9c5764db71c8147b344c2db710407aba5c3a6bb1
1 #!/bin/sh
2 # Running cp S D on an NFS client while another client has just removed D
3 # would lead (w/coreutils-8.16 and earlier) to cp's initial stat call
4 # seeing (via stale NFS cache) that D exists, so that cp would then call
5 # open without the O_CREAT flag. Yet, the open must actually consult
6 # the server, which confesses that D has been deleted, thus causing the
7 # open call to fail with ENOENT.
9 # This test simulates that situation by intercepting stat for a nonexistent
10 # destination, D, and making the stat fill in the result struct for another
11 # file and return 0.
13 # This test is skipped on systems that lack LD_PRELOAD support; that's fine.
14 # Similarly, on a system that lacks <dlfcn.h> or __xstat, skipping it is fine.
16 # Copyright (C) 2012-2024 Free Software Foundation, Inc.
18 # This program is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU General Public License as published by
20 # the Free Software Foundation, either version 3 of the License, or
21 # (at your option) any later version.
23 # This program is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program. If not, see <https://www.gnu.org/licenses/>.
31 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
32 print_ver_ cp
33 require_gcc_shared_
35 # Replace each stat call with a call to this wrapper.
36 cat > k.c <<'EOF' || framework_failure_
37 #define _GNU_SOURCE
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <dlfcn.h>
42 #define __xstat __xstat_orig
44 #include <sys/stat.h>
45 #include <stddef.h>
47 #undef __xstat
49 int
50 __xstat (int ver, const char *path, struct stat *st)
52 static int (*real_stat)(int ver, const char *path, struct stat *st) = NULL;
53 fclose(fopen("preloaded", "w"));
54 if (!real_stat)
55 real_stat = dlsym (RTLD_NEXT, "__xstat");
56 /* When asked to stat nonexistent "d",
57 return results suggesting it exists. */
58 return real_stat (ver, *path == 'd' && path[1] == 0 ? "d2" : path, st);
60 EOF
62 # Then compile/link it:
63 gcc_shared_ k.c k.so \
64 || framework_failure_ 'failed to build shared library'
66 touch d2 || framework_failure_
67 echo xyz > src || framework_failure_
69 # Finally, run the test:
70 LD_PRELOAD=$LD_PRELOAD:./k.so cp src d || fail=1
72 test -f preloaded || skip_ 'LD_PRELOAD was ineffective?'
74 compare src d || fail=1
75 Exit $fail