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. 4 5# Copyright (C) 2012-2023 Free Software Foundation, Inc. 6 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. 11 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. 16 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/>. 19 20. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src 21print_ver_ df 22require_gcc_shared_ 23 24# Protect against inaccessible remote mounts etc. 25timeout 10 df || skip_ "df fails" 26 27grep '^#define HAVE_MNTENT_H 1' $CONFIG_HEADER > /dev/null \ 28 || skip_ "no mntent.h available to confirm the interface" 29 30grep '^#define HAVE_GETMNTENT 1' $CONFIG_HEADER > /dev/null \ 31 || skip_ "getmntent is not used on this system" 32 33# Simulate "mtab" failure. 34cat > k.c <<EOF || framework_failure_ 35#define _GNU_SOURCE 36#include <stdio.h> 37#include <errno.h> 38#include <mntent.h> 39#include <string.h> 40#include <dlfcn.h> 41 42#define STREQ(a, b) (strcmp (a, b) == 0) 43 44FILE* fopen(const char *path, const char *mode) 45{ 46 static FILE* (*fopen_func)(char const *, char const *); 47 48 /* get reference to original (libc provided) fopen */ 49 if (!fopen_func) 50 { 51 fopen_func = (FILE*(*)(char const *, char const *)) 52 dlsym(RTLD_NEXT, "fopen"); 53 if (!fopen_func) 54 { 55 fprintf (stderr, "Failed to find fopen()\n"); 56 errno = ESRCH; 57 return NULL; 58 } 59 } 60 61 /* Returning ENOENT here will get read_file_system_list() 62 to fall back to using getmntent() below. */ 63 if (STREQ (path, "/proc/self/mountinfo")) 64 { 65 errno = ENOENT; 66 return NULL; 67 } 68 else 69 return fopen_func(path, mode); 70} 71 72struct mntent *getmntent (FILE *fp) 73{ 74 /* Prove that LD_PRELOAD works. */ 75 static int done = 0; 76 if (!done) 77 { 78 fclose (fopen ("x", "w")); 79 ++done; 80 } 81 /* Now simulate the failure. */ 82 errno = ENOENT; 83 return NULL; 84} 85EOF 86 87# Then compile/link it: 88gcc_shared_ k.c k.so \ 89 || framework_failure_ 'failed to build shared library' 90 91cleanup_() { unset LD_PRELOAD; } 92 93export LD_PRELOAD=$LD_PRELOAD:./k.so 94 95# Test if LD_PRELOAD works: 96df 2>/dev/null 97test -f x || skip_ "internal test failure: maybe LD_PRELOAD doesn't work?" 98 99# These tests are supposed to succeed: 100df '.' || fail=1 101df -i '.' || fail=1 102df -T '.' || fail=1 103df -Ti '.' || fail=1 104df --total '.' || fail=1 105 106# These tests are supposed to fail: 107returns_ 1 df || fail=1 108returns_ 1 df -i || fail=1 109returns_ 1 df -T || fail=1 110returns_ 1 df -Ti || fail=1 111returns_ 1 df --total || fail=1 112 113returns_ 1 df -a || fail=1 114returns_ 1 df -a '.' || fail=1 115 116returns_ 1 df -l || fail=1 117returns_ 1 df -l '.' || fail=1 118 119returns_ 1 df -t hello || fail=1 120returns_ 1 df -t hello '.' || fail=1 121 122returns_ 1 df -x hello || fail=1 123returns_ 1 df -x hello '.' || fail=1 124 125Exit $fail 126