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. 8# 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. 12# 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. 15 16# Copyright (C) 2012-2023 Free Software Foundation, Inc. 17 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. 22 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. 27 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/>. 30 31. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src 32print_ver_ cp 33require_gcc_shared_ 34 35# Replace each stat call with a call to this wrapper. 36cat > k.c <<'EOF' || framework_failure_ 37#define _GNU_SOURCE 38#include <stdio.h> 39#include <sys/types.h> 40#include <dlfcn.h> 41 42#define __xstat __xstat_orig 43 44#include <sys/stat.h> 45#include <stddef.h> 46 47#undef __xstat 48 49int 50__xstat (int ver, const char *path, struct stat *st) 51{ 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); 59} 60EOF 61 62# Then compile/link it: 63gcc_shared_ k.c k.so \ 64 || framework_failure_ 'failed to build shared library' 65 66touch d2 || framework_failure_ 67echo xyz > src || framework_failure_ 68 69# Finally, run the test: 70LD_PRELOAD=$LD_PRELOAD:./k.so cp src d || fail=1 71 72test -f preloaded || skip_ 'LD_PRELOAD was ineffective?' 73 74compare src d || fail=1 75Exit $fail 76