summaryrefslogtreecommitdiffstats
path: root/tests/dd/fail-ftruncate-fstat.sh
blob: 219273bbc2174830eb46d5fcf922f1b78d854151 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh
# Check that 'dd' does not continue copying if ftruncate and fstat fail.

# Copyright (C) 2025-2026 Free Software Foundation, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
print_ver_ dd
require_gcc_shared_
getlimits_

cat > k.c <<'EOF' || framework_failure_
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>

int
ftruncate (int fd, off_t length)
{
  /* Prove that LD_PRELOAD works: create the evidence file "x".  */
  fclose (fopen ("x", "w"));

  /* Pretend failure.  */
  errno = EPERM;
  return -1;
}

int
fstat (int fd, struct stat *statbuf)
{
  /* Prove that LD_PRELOAD works: create the evidence file "y".  */
  fclose (fopen ("y", "w"));

  /* Pretend failure.  */
  errno = EPERM;
  return -1;
}
EOF

# Then compile/link it:
gcc_shared_ k.c k.so \
  || framework_failure_ 'failed to build shared library'

# Setup the file "out" and preserve it's original contents in "exp-out".
yes | head -n 2048 | tr -d '\n' > out || framework_failure_
cp out exp-out || framework_failure_

LD_PRELOAD=$LD_PRELOAD:./k.so dd if=/dev/zero of=out count=1 \
                              seek=1 status=none 2>err
ret=$?

test -f x && test -f y \
  || skip_ "internal test failure: maybe LD_PRELOAD doesn't work?"

# After ftruncate fails, we use fstat to get the file type.
echo "dd: cannot fstat 'out': $EPERM" > exp
compare exp err || fail=1

# coreutils 9.1 to 9.9 would mistakenly continue copying after ftruncate
# failed and exit successfully.
test "$ret" = 1 || fail=1
compare exp-out out || fail=1

Exit $fail