read.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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
  17. // 1) read of every file in /proc
  18. // 2) readlink of every symlink in /proc
  19. // 3) recursively (1) + (2) for every directory in /proc
  20. // 4) write to /proc/*/clear_refs and /proc/*/task/*/clear_refs
  21. // 5) write to /proc/sysrq-trigger
  22. #undef NDEBUG
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <sys/types.h>
  26. #include <dirent.h>
  27. #include <stdbool.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. static inline bool streq(const char *s1, const char *s2)
  34. {
  35. return strcmp(s1, s2) == 0;
  36. }
  37. static struct dirent *xreaddir(DIR *d)
  38. {
  39. struct dirent *de;
  40. errno = 0;
  41. de = readdir(d);
  42. if (!de && errno != 0) {
  43. exit(1);
  44. }
  45. return de;
  46. }
  47. static void f_reg(DIR *d, const char *filename)
  48. {
  49. char buf[4096];
  50. int fd;
  51. ssize_t rv;
  52. /* read from /proc/kmsg can block */
  53. fd = openat(dirfd(d), filename, O_RDONLY|O_NONBLOCK);
  54. if (fd == -1)
  55. return;
  56. rv = read(fd, buf, sizeof(buf));
  57. assert((0 <= rv && rv <= sizeof(buf)) || rv == -1);
  58. close(fd);
  59. }
  60. static void f_reg_write(DIR *d, const char *filename, const char *buf, size_t len)
  61. {
  62. int fd;
  63. ssize_t rv;
  64. fd = openat(dirfd(d), filename, O_WRONLY);
  65. if (fd == -1)
  66. return;
  67. rv = write(fd, buf, len);
  68. assert((0 <= rv && rv <= len) || rv == -1);
  69. close(fd);
  70. }
  71. static void f_lnk(DIR *d, const char *filename)
  72. {
  73. char buf[4096];
  74. ssize_t rv;
  75. rv = readlinkat(dirfd(d), filename, buf, sizeof(buf));
  76. assert((0 <= rv && rv <= sizeof(buf)) || rv == -1);
  77. }
  78. static void f(DIR *d, unsigned int level)
  79. {
  80. struct dirent *de;
  81. de = xreaddir(d);
  82. assert(de->d_type == DT_DIR);
  83. assert(streq(de->d_name, "."));
  84. de = xreaddir(d);
  85. assert(de->d_type == DT_DIR);
  86. assert(streq(de->d_name, ".."));
  87. while ((de = xreaddir(d))) {
  88. assert(!streq(de->d_name, "."));
  89. assert(!streq(de->d_name, ".."));
  90. switch (de->d_type) {
  91. DIR *dd;
  92. int fd;
  93. case DT_REG:
  94. if (level == 0 && streq(de->d_name, "sysrq-trigger")) {
  95. f_reg_write(d, de->d_name, "h", 1);
  96. } else if (level == 1 && streq(de->d_name, "clear_refs")) {
  97. f_reg_write(d, de->d_name, "1", 1);
  98. } else if (level == 3 && streq(de->d_name, "clear_refs")) {
  99. f_reg_write(d, de->d_name, "1", 1);
  100. } else {
  101. f_reg(d, de->d_name);
  102. }
  103. break;
  104. case DT_DIR:
  105. fd = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY);
  106. if (fd == -1)
  107. continue;
  108. dd = fdopendir(fd);
  109. if (!dd)
  110. continue;
  111. f(dd, level + 1);
  112. closedir(dd);
  113. break;
  114. case DT_LNK:
  115. f_lnk(d, de->d_name);
  116. break;
  117. default:
  118. assert(0);
  119. }
  120. }
  121. }
  122. int main(void)
  123. {
  124. DIR *d;
  125. d = opendir("/proc");
  126. if (!d)
  127. return 2;
  128. f(d, 0);
  129. return 0;
  130. }