tests: fix skipping of mtab simulation tests
[coreutils.git] / tests / df / no-mtab-status.sh
blobf70c695f869db386275e9a1e818632a3dbe3fba4
1 #!/bin/sh
2 # Test df's behavior when the mount list cannot be read.
3 # This test is skipped on systems that lack LD_PRELOAD support; that's fine.
5 # Copyright (C) 2012-2024 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program 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
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21 print_ver_ df
22 require_gcc_shared_
24 # Protect against inaccessible remote mounts etc.
25 timeout 10 df || skip_ "df fails"
27 grep '^#define HAVE_GETMNTENT 1' $CONFIG_HEADER > /dev/null \
28 || skip_ "getmntent is not used on this system"
30 # Simulate "mtab" failure.
31 cat > k.c <<EOF || framework_failure_
32 #define _GNU_SOURCE
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <mntent.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <dlfcn.h>
42 #define STREQ(a, b) (strcmp (a, b) == 0)
44 int open(const char *path, int flags, ...)
46 static int (*open_func)(const char *, int, ...);
48 /* get reference to original (libc provided) open */
49 if (!open_func)
51 open_func = (int(*)(const char *, int, ...))
52 dlsym(RTLD_NEXT, "open");
53 if (!open_func)
55 fprintf (stderr, "Failed to find open()\n");
56 errno = ESRCH;
57 return -1;
61 va_list ap;
62 va_start (ap, flags);
63 mode_t mode = (sizeof (mode_t) < sizeof (int)
64 ? va_arg (ap, int)
65 : va_arg (ap, mode_t));
66 va_end (ap);
68 /* Returning ENOENT here will get read_file_system_list()
69 to fall back to using getmntent() below. */
70 if (STREQ (path, "/proc/self/mountinfo"))
72 errno = ENOENT;
73 return -1;
75 else
76 return open_func(path, flags, mode);
79 struct mntent *getmntent (FILE *fp)
81 /* Prove that LD_PRELOAD works. */
82 static int done = 0;
83 if (!done)
85 fclose (fopen ("x", "w"));
86 ++done;
88 /* Now simulate the failure. */
89 errno = ENOENT;
90 return NULL;
92 EOF
94 # Then compile/link it:
95 gcc_shared_ k.c k.so \
96 || framework_failure_ 'failed to build shared library'
98 cleanup_() { unset LD_PRELOAD; }
100 export LD_PRELOAD=$LD_PRELOAD:./k.so
102 # Test if LD_PRELOAD works:
103 df 2>/dev/null
104 test -f x || skip_ "internal test failure: maybe LD_PRELOAD doesn't work?"
106 # These tests are supposed to succeed:
107 df '.' || fail=1
108 df -i '.' || fail=1
109 df -T '.' || fail=1
110 df -Ti '.' || fail=1
111 df --total '.' || fail=1
113 # These tests are supposed to fail:
114 returns_ 1 df || fail=1
115 returns_ 1 df -i || fail=1
116 returns_ 1 df -T || fail=1
117 returns_ 1 df -Ti || fail=1
118 returns_ 1 df --total || fail=1
120 returns_ 1 df -a || fail=1
121 returns_ 1 df -a '.' || fail=1
123 returns_ 1 df -l || fail=1
124 returns_ 1 df -l '.' || fail=1
126 returns_ 1 df -t hello || fail=1
127 returns_ 1 df -t hello '.' || fail=1
129 returns_ 1 df -x hello || fail=1
130 returns_ 1 df -x hello '.' || fail=1
132 Exit $fail