2 # Test df's behavior when the mount list contains duplicate entries.
3 # This test is skipped on systems that lack LD_PRELOAD support; that's fine.
5 # Copyright (C) 2012-2023 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
24 # We use --local here so as to not activate
25 # potentially very many remote mounts.
26 df
--local --output=target
>LOCAL_FS || skip_
'df fails'
27 grep '^/$' LOCAL_FS || skip_
'no root file system found'
29 # Get real targets to substitute for /NONROOT and /REMOTE below.
30 export CU_NONROOT_FS
=$
(grep /. LOCAL_FS |
head -n1)
31 export CU_REMOTE_FS
=$
(grep /. LOCAL_FS |
tail -n+2 |
head -n1)
34 test -z "$CU_NONROOT_FS" || unique_entries
=$
(expr $unique_entries + 1)
35 test -z "$CU_REMOTE_FS" || unique_entries
=$
(expr $unique_entries + 2)
37 grep '^#define HAVE_MNTENT_H 1' $CONFIG_HEADER > /dev
/null \
38 || skip_
"no mntent.h available to confirm the interface"
40 grep '^#define HAVE_GETMNTENT 1' $CONFIG_HEADER > /dev
/null \
41 || skip_
"getmntent is not used on this system"
43 # Simulate an mtab file to test various cases.
44 cat > k.c
<<EOF || framework_failure_
53 #define STREQ(a, b) (strcmp (a, b) == 0)
55 FILE* fopen(const char *path, const char *mode)
57 static FILE* (*fopen_func)(char const *, char const *);
59 /* get reference to original (libc provided) fopen */
62 fopen_func = (FILE*(*)(char const *, char const *))
63 dlsym(RTLD_NEXT, "fopen");
66 fprintf (stderr, "Failed to find fopen()\n");
72 /* Returning ENOENT here will get read_file_system_list()
73 to fall back to using getmntent() below. */
74 if (STREQ (path, "/proc/self/mountinfo"))
80 return fopen_func(path, mode);
83 #define STREQ(a, b) (strcmp (a, b) == 0)
85 struct mntent *getmntent (FILE *fp)
87 static char *nonroot_fs;
88 static char *remote_fs;
91 /* Prove that LD_PRELOAD works. */
94 fclose (fopen ("x", "w"));
98 static struct mntent mntents[] = {
99 {.mnt_fsname="/short", .mnt_dir="/invalid/mount/dir", .mnt_opts=""},
100 {.mnt_fsname="fsname", .mnt_dir="/", .mnt_opts=""},
101 {.mnt_fsname="/fsname", .mnt_dir="/.", .mnt_opts=""},
102 {.mnt_fsname="/fsname", .mnt_dir="/", .mnt_opts=""},
103 {.mnt_fsname="virtfs", .mnt_dir="/NONROOT", .mnt_type="t1", .mnt_opts=""},
104 {.mnt_fsname="virtfs2", .mnt_dir="/NONROOT", .mnt_type="t2", .mnt_opts=""},
105 {.mnt_fsname="netns", .mnt_dir="net:[1234567]", .mnt_opts=""},
106 {.mnt_fsname="rem:ote1",.mnt_dir="/REMOTE", .mnt_opts=""},
107 {.mnt_fsname="rem:ote1",.mnt_dir="/REMOTE", .mnt_opts=""},
108 {.mnt_fsname="rem:ote2",.mnt_dir="/REMOTE", .mnt_opts=""},
113 nonroot_fs = getenv ("CU_NONROOT_FS");
114 if (!nonroot_fs || !*nonroot_fs)
115 nonroot_fs = "/"; /* merge into / entries. */
117 remote_fs = getenv ("CU_REMOTE_FS");
120 if (done == 1 && !getenv ("CU_TEST_DUPE_INVALID"))
121 done++; /* skip the first entry. */
125 if (!mntents[done-2].mnt_type)
126 mntents[done-2].mnt_type = "-";
127 if (!mntents[done-2].mnt_opts)
128 mntents[done-2].mnt_opts = "-";
129 if (STREQ (mntents[done-2].mnt_dir, "/NONROOT"))
130 mntents[done-2].mnt_dir = nonroot_fs;
131 if (STREQ (mntents[done-2].mnt_dir, "/REMOTE"))
133 if (!remote_fs || !*remote_fs)
136 mntents[done-2].mnt_dir = remote_fs;
138 return &mntents[done-2];
145 # Then compile/link it:
146 gcc_shared_ k.c k.so \
147 || framework_failure_
'failed to build shared library'
149 # Test if LD_PRELOAD works:
150 LD_PRELOAD
=$LD_PRELOAD:.
/k.so df
151 test -f x || skip_
"internal test failure: maybe LD_PRELOAD doesn't work?"
153 # The fake mtab file should only contain entries
154 # having the same device number; thus the output should
155 # consist of a header and unique entries.
156 LD_PRELOAD
=$LD_PRELOAD:.
/k.so df
-T >out || fail
=1
157 test $
(wc -l <out
) -eq $
(expr 1 + $unique_entries) ||
{ fail
=1; cat out
; }
159 # With --total we should suppress the duplicate but separate remote file system
160 LD_PRELOAD
=$LD_PRELOAD:.
/k.so df
--total >out || fail
=1
161 test "$CU_REMOTE_FS" && elide_remote
=1 || elide_remote
=0
162 test $
(wc -l <out
) -eq $
(expr 2 + $unique_entries - $elide_remote) ||
165 # Ensure we don't fail when unable to stat (currently) unavailable entries
166 LD_PRELOAD
=$LD_PRELOAD:.
/k.so CU_TEST_DUPE_INVALID
=1 df
-T >out || fail
=1
167 test $
(wc -l <out
) -eq $
(expr 1 + $unique_entries) ||
{ fail
=1; cat out
; }
169 # df should also prefer "/fsname" over "fsname"
170 if test "$unique_entries" = 2; then
171 test $
(grep -c '/fsname' <out
) -eq 1 ||
{ fail
=1; cat out
; }
172 # ... and "/fsname" with '/' as Mounted on over '/.'
173 test $
(grep -cF '/.' <out
) -eq 0 ||
{ fail
=1; cat out
; }
176 # df should use the last seen devname (mnt_fsname) and devtype (mnt_type)
177 test $
(grep -c 'virtfs2.*t2' <out
) -eq 1 ||
{ fail
=1; cat out
; }
179 # Ensure that filtering duplicates does not affect -a processing.
180 LD_PRELOAD
=$LD_PRELOAD:.
/k.so df
-a >out || fail
=1
181 total_fs
=6; test "$CU_REMOTE_FS" && total_fs
=$
(expr $total_fs + 3)
182 test $
(wc -l <out
) -eq $total_fs ||
{ fail
=1; cat out
; }
183 # Ensure placeholder "-" values used for the eclipsed "virtfs"
184 test $
(grep -c 'virtfs *-' <out
) -eq 1 ||
{ fail
=1; cat out
; }
186 # Ensure that filtering duplicates does not affect
187 # argument processing (now without the fake getmntent()).
188 df
'.' '.' >out || fail
=1
189 test $
(wc -l <out
) -eq 3 ||
{ fail
=1; cat out
; }