read.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "proc.h"
  34. static void f_reg(DIR *d, const char *filename)
  35. {
  36. char buf[4096];
  37. int fd;
  38. ssize_t rv;
  39. /* read from /proc/kmsg can block */
  40. fd = openat(dirfd(d), filename, O_RDONLY|O_NONBLOCK);
  41. if (fd == -1)
  42. return;
  43. rv = read(fd, buf, sizeof(buf));
  44. assert((0 <= rv && rv <= sizeof(buf)) || rv == -1);
  45. close(fd);
  46. }
  47. static void f_reg_write(DIR *d, const char *filename, const char *buf, size_t len)
  48. {
  49. int fd;
  50. ssize_t rv;
  51. fd = openat(dirfd(d), filename, O_WRONLY);
  52. if (fd == -1)
  53. return;
  54. rv = write(fd, buf, len);
  55. assert((0 <= rv && rv <= len) || rv == -1);
  56. close(fd);
  57. }
  58. static void f_lnk(DIR *d, const char *filename)
  59. {
  60. char buf[4096];
  61. ssize_t rv;
  62. rv = readlinkat(dirfd(d), filename, buf, sizeof(buf));
  63. assert((0 <= rv && rv <= sizeof(buf)) || rv == -1);
  64. }
  65. static void f(DIR *d, unsigned int level)
  66. {
  67. struct dirent *de;
  68. de = xreaddir(d);
  69. assert(de->d_type == DT_DIR);
  70. assert(streq(de->d_name, "."));
  71. de = xreaddir(d);
  72. assert(de->d_type == DT_DIR);
  73. assert(streq(de->d_name, ".."));
  74. while ((de = xreaddir(d))) {
  75. assert(!streq(de->d_name, "."));
  76. assert(!streq(de->d_name, ".."));
  77. switch (de->d_type) {
  78. DIR *dd;
  79. int fd;
  80. case DT_REG:
  81. if (level == 0 && streq(de->d_name, "sysrq-trigger")) {
  82. f_reg_write(d, de->d_name, "h", 1);
  83. } else if (level == 1 && streq(de->d_name, "clear_refs")) {
  84. f_reg_write(d, de->d_name, "1", 1);
  85. } else if (level == 3 && streq(de->d_name, "clear_refs")) {
  86. f_reg_write(d, de->d_name, "1", 1);
  87. } else {
  88. f_reg(d, de->d_name);
  89. }
  90. break;
  91. case DT_DIR:
  92. fd = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY);
  93. if (fd == -1)
  94. continue;
  95. dd = fdopendir(fd);
  96. if (!dd)
  97. continue;
  98. f(dd, level + 1);
  99. closedir(dd);
  100. break;
  101. case DT_LNK:
  102. f_lnk(d, de->d_name);
  103. break;
  104. default:
  105. assert(0);
  106. }
  107. }
  108. }
  109. int main(void)
  110. {
  111. DIR *d;
  112. d = opendir("/proc");
  113. if (!d)
  114. return 2;
  115. f(d, 0);
  116. return 0;
  117. }