TestProcFS.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <cstring>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. TEST_CASE(test_process_fd_readlink)
  11. {
  12. // Make sure that stdin, stdout and stderr are actually symlinks that point somewhere interesting
  13. // Sadly we can't assume that they all point to the same file.
  14. struct stat stat_buf = {};
  15. struct stat lstat_buf = {};
  16. auto rc = stat("/proc/self/fd/0", &stat_buf);
  17. EXPECT_EQ(rc, 0);
  18. rc = lstat("/proc/self/fd/0", &lstat_buf);
  19. EXPECT_EQ(rc, 0);
  20. EXPECT_NE(0, memcmp(&stat_buf, &lstat_buf, sizeof(struct stat)));
  21. stat_buf = {};
  22. lstat_buf = {};
  23. rc = stat("/proc/self/fd/1", &stat_buf);
  24. EXPECT_EQ(rc, 0);
  25. rc = lstat("/proc/self/fd/1", &lstat_buf);
  26. EXPECT_EQ(rc, 0);
  27. EXPECT_NE(0, memcmp(&stat_buf, &lstat_buf, sizeof(struct stat)));
  28. stat_buf = {};
  29. lstat_buf = {};
  30. rc = stat("/proc/self/fd/2", &stat_buf);
  31. EXPECT_EQ(rc, 0);
  32. rc = lstat("/proc/self/fd/2", &lstat_buf);
  33. EXPECT_EQ(rc, 0);
  34. EXPECT_NE(0, memcmp(&stat_buf, &lstat_buf, sizeof(struct stat)));
  35. // Create a new file descriptor that is a dup of 0 with various big values in order to reproduce issue #7820.
  36. // We should get the same link value for each fd that was duplicated.
  37. char expected_link[MAXPATHLEN];
  38. char buf[MAXPATHLEN];
  39. // Read the symlink for stdin, stdout and stderr
  40. auto link_length = readlink("/proc/self/fd/0", expected_link, sizeof(expected_link));
  41. expected_link[link_length] = '\0';
  42. // 255 is the first broken file descriptor that was discovered and might be used by other software (e.g. bash)
  43. auto new_fd = dup2(0, 255);
  44. EXPECT_EQ(new_fd, 255);
  45. link_length = readlink("/proc/self/fd/255", buf, sizeof(buf));
  46. buf[link_length] = '\0';
  47. EXPECT_EQ(0, strcmp(buf, expected_link));
  48. // 215 is the last fd before we have to encode the fd using more than one byte (due to the offset by FI_MaxStaticFileIndex)
  49. new_fd = dup2(0, 215);
  50. EXPECT_EQ(new_fd, 215);
  51. link_length = readlink("/proc/self/fd/215", buf, sizeof(buf));
  52. buf[link_length] = '\0';
  53. EXPECT_EQ(0, strcmp(buf, expected_link));
  54. // 216 is the first fd that is encoded using more than one byte
  55. new_fd = dup2(0, 216);
  56. EXPECT_EQ(new_fd, 216);
  57. link_length = readlink("/proc/self/fd/216", buf, sizeof(buf));
  58. buf[link_length] = '\0';
  59. EXPECT_EQ(0, strcmp(buf, expected_link));
  60. // 1023 is the largest possible file descriptor
  61. new_fd = dup2(0, 1023);
  62. EXPECT_EQ(new_fd, 1023);
  63. link_length = readlink("/proc/self/fd/1023", buf, sizeof(buf));
  64. buf[link_length] = '\0';
  65. EXPECT_EQ(0, strcmp(buf, expected_link));
  66. }