fd-001-lookup.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. // Test /proc/*/fd lookup.
  17. #define _GNU_SOURCE
  18. #undef NDEBUG
  19. #include <assert.h>
  20. #include <dirent.h>
  21. #include <errno.h>
  22. #include <limits.h>
  23. #include <sched.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include "proc.h"
  30. /* lstat(2) has more "coverage" in case non-symlink pops up somehow. */
  31. static void test_lookup_pass(const char *pathname)
  32. {
  33. struct stat st;
  34. ssize_t rv;
  35. memset(&st, 0, sizeof(struct stat));
  36. rv = lstat(pathname, &st);
  37. assert(rv == 0);
  38. assert(S_ISLNK(st.st_mode));
  39. }
  40. static void test_lookup_fail(const char *pathname)
  41. {
  42. struct stat st;
  43. ssize_t rv;
  44. rv = lstat(pathname, &st);
  45. assert(rv == -1 && errno == ENOENT);
  46. }
  47. static void test_lookup(unsigned int fd)
  48. {
  49. char buf[64];
  50. unsigned int c;
  51. unsigned int u;
  52. int i;
  53. snprintf(buf, sizeof(buf), "/proc/self/fd/%u", fd);
  54. test_lookup_pass(buf);
  55. /* leading junk */
  56. for (c = 1; c <= 255; c++) {
  57. if (c == '/')
  58. continue;
  59. snprintf(buf, sizeof(buf), "/proc/self/fd/%c%u", c, fd);
  60. test_lookup_fail(buf);
  61. }
  62. /* trailing junk */
  63. for (c = 1; c <= 255; c++) {
  64. if (c == '/')
  65. continue;
  66. snprintf(buf, sizeof(buf), "/proc/self/fd/%u%c", fd, c);
  67. test_lookup_fail(buf);
  68. }
  69. for (i = INT_MIN; i < INT_MIN + 1024; i++) {
  70. snprintf(buf, sizeof(buf), "/proc/self/fd/%d", i);
  71. test_lookup_fail(buf);
  72. }
  73. for (i = -1024; i < 0; i++) {
  74. snprintf(buf, sizeof(buf), "/proc/self/fd/%d", i);
  75. test_lookup_fail(buf);
  76. }
  77. for (u = INT_MAX - 1024; u <= (unsigned int)INT_MAX + 1024; u++) {
  78. snprintf(buf, sizeof(buf), "/proc/self/fd/%u", u);
  79. test_lookup_fail(buf);
  80. }
  81. for (u = UINT_MAX - 1024; u != 0; u++) {
  82. snprintf(buf, sizeof(buf), "/proc/self/fd/%u", u);
  83. test_lookup_fail(buf);
  84. }
  85. }
  86. int main(void)
  87. {
  88. struct dirent *de;
  89. unsigned int fd, target_fd;
  90. if (unshare(CLONE_FILES) == -1)
  91. return 1;
  92. /* Wipe fdtable. */
  93. do {
  94. DIR *d;
  95. d = opendir("/proc/self/fd");
  96. if (!d)
  97. return 1;
  98. de = xreaddir(d);
  99. assert(de->d_type == DT_DIR);
  100. assert(streq(de->d_name, "."));
  101. de = xreaddir(d);
  102. assert(de->d_type == DT_DIR);
  103. assert(streq(de->d_name, ".."));
  104. next:
  105. de = xreaddir(d);
  106. if (de) {
  107. unsigned long long fd_ull;
  108. unsigned int fd;
  109. char *end;
  110. assert(de->d_type == DT_LNK);
  111. fd_ull = xstrtoull(de->d_name, &end);
  112. assert(*end == '\0');
  113. assert(fd_ull == (unsigned int)fd_ull);
  114. fd = fd_ull;
  115. if (fd == dirfd(d))
  116. goto next;
  117. close(fd);
  118. }
  119. closedir(d);
  120. } while (de);
  121. /* Now fdtable is clean. */
  122. fd = open("/", O_PATH|O_DIRECTORY);
  123. assert(fd == 0);
  124. test_lookup(fd);
  125. close(fd);
  126. /* Clean again! */
  127. fd = open("/", O_PATH|O_DIRECTORY);
  128. assert(fd == 0);
  129. /* Default RLIMIT_NOFILE-1 */
  130. target_fd = 1023;
  131. while (target_fd > 0) {
  132. if (dup2(fd, target_fd) == target_fd)
  133. break;
  134. target_fd /= 2;
  135. }
  136. assert(target_fd > 0);
  137. close(fd);
  138. test_lookup(target_fd);
  139. close(target_fd);
  140. return 0;
  141. }