no-mtab-status.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/sh
  2. # Test df's behaviour when the mount list cannot be read.
  3. # This test is skipped on systems that lack LD_PRELOAD support; that's fine.
  4. # Copyright (C) 2012-2018 Free Software Foundation, Inc.
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
  16. print_ver_ df
  17. require_gcc_shared_
  18. # Protect against inaccessible remote mounts etc.
  19. timeout 10 df || skip_ "df fails"
  20. grep '^#define HAVE_MNTENT_H 1' $CONFIG_HEADER > /dev/null \
  21. || skip_ "no mntent.h available to confirm the interface"
  22. grep '^#define HAVE_GETMNTENT 1' $CONFIG_HEADER > /dev/null \
  23. || skip_ "getmntent is not used on this system"
  24. # Simulate "mtab" failure.
  25. cat > k.c <<EOF || framework_failure_
  26. #define _GNU_SOURCE
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <mntent.h>
  30. #include <string.h>
  31. #include <dlfcn.h>
  32. #define STREQ(a, b) (strcmp (a, b) == 0)
  33. FILE* fopen(const char *path, const char *mode)
  34. {
  35. static FILE* (*fopen_func)(char const *, char const *);
  36. /* get reference to original (libc provided) fopen */
  37. if (!fopen_func)
  38. {
  39. fopen_func = (FILE*(*)(char const *, char const *))
  40. dlsym(RTLD_NEXT, "fopen");
  41. if (!fopen_func)
  42. {
  43. fprintf (stderr, "Failed to find fopen()\n");
  44. errno = ESRCH;
  45. return NULL;
  46. }
  47. }
  48. /* Returning ENOENT here will get read_file_system_list()
  49. to fall back to using getmntent() below. */
  50. if (STREQ (path, "/proc/self/mountinfo"))
  51. {
  52. errno = ENOENT;
  53. return NULL;
  54. }
  55. else
  56. return fopen_func(path, mode);
  57. }
  58. struct mntent *getmntent (FILE *fp)
  59. {
  60. /* Prove that LD_PRELOAD works. */
  61. static int done = 0;
  62. if (!done)
  63. {
  64. fclose (fopen ("x", "w"));
  65. ++done;
  66. }
  67. /* Now simulate the failure. */
  68. errno = ENOENT;
  69. return NULL;
  70. }
  71. EOF
  72. # Then compile/link it:
  73. gcc_shared_ k.c k.so \
  74. || framework_failure_ 'failed to build shared library'
  75. cleanup_() { unset LD_PRELOAD; }
  76. export LD_PRELOAD=$LD_PRELOAD:./k.so
  77. # Test if LD_PRELOAD works:
  78. df 2>/dev/null
  79. test -f x || skip_ "internal test failure: maybe LD_PRELOAD doesn't work?"
  80. # These tests are supposed to succeed:
  81. df '.' || fail=1
  82. df -i '.' || fail=1
  83. df -T '.' || fail=1
  84. df -Ti '.' || fail=1
  85. df --total '.' || fail=1
  86. # These tests are supposed to fail:
  87. returns_ 1 df || fail=1
  88. returns_ 1 df -i || fail=1
  89. returns_ 1 df -T || fail=1
  90. returns_ 1 df -Ti || fail=1
  91. returns_ 1 df --total || fail=1
  92. returns_ 1 df -a || fail=1
  93. returns_ 1 df -a '.' || fail=1
  94. returns_ 1 df -l || fail=1
  95. returns_ 1 df -l '.' || fail=1
  96. returns_ 1 df -t hello || fail=1
  97. returns_ 1 df -t hello '.' || fail=1
  98. returns_ 1 df -x hello || fail=1
  99. returns_ 1 df -x hello '.' || fail=1
  100. Exit $fail