eventfd_test.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <asm/unistd.h>
  6. #include <linux/time_types.h>
  7. #include <unistd.h>
  8. #include <assert.h>
  9. #include <signal.h>
  10. #include <pthread.h>
  11. #include <sys/epoll.h>
  12. #include <sys/eventfd.h>
  13. #include "kselftest_harness.h"
  14. #define EVENTFD_TEST_ITERATIONS 100000UL
  15. struct error {
  16. int code;
  17. char msg[512];
  18. };
  19. static int error_set(struct error *err, int code, const char *fmt, ...)
  20. {
  21. va_list args;
  22. int r;
  23. if (code == 0 || !err || err->code != 0)
  24. return code;
  25. err->code = code;
  26. va_start(args, fmt);
  27. r = vsnprintf(err->msg, sizeof(err->msg), fmt, args);
  28. assert((size_t)r < sizeof(err->msg));
  29. va_end(args);
  30. return code;
  31. }
  32. static inline int sys_eventfd2(unsigned int count, int flags)
  33. {
  34. return syscall(__NR_eventfd2, count, flags);
  35. }
  36. TEST(eventfd_check_flag_rdwr)
  37. {
  38. int fd, flags;
  39. fd = sys_eventfd2(0, 0);
  40. ASSERT_GE(fd, 0);
  41. flags = fcntl(fd, F_GETFL);
  42. // The kernel automatically adds the O_RDWR flag.
  43. EXPECT_EQ(flags, O_RDWR);
  44. close(fd);
  45. }
  46. TEST(eventfd_check_flag_cloexec)
  47. {
  48. int fd, flags;
  49. fd = sys_eventfd2(0, EFD_CLOEXEC);
  50. ASSERT_GE(fd, 0);
  51. flags = fcntl(fd, F_GETFD);
  52. ASSERT_GT(flags, -1);
  53. EXPECT_EQ(flags, FD_CLOEXEC);
  54. close(fd);
  55. }
  56. TEST(eventfd_check_flag_nonblock)
  57. {
  58. int fd, flags;
  59. fd = sys_eventfd2(0, EFD_NONBLOCK);
  60. ASSERT_GE(fd, 0);
  61. flags = fcntl(fd, F_GETFL);
  62. ASSERT_GT(flags, -1);
  63. EXPECT_EQ(flags & EFD_NONBLOCK, EFD_NONBLOCK);
  64. EXPECT_EQ(flags & O_RDWR, O_RDWR);
  65. close(fd);
  66. }
  67. TEST(eventfd_check_flag_cloexec_and_nonblock)
  68. {
  69. int fd, flags;
  70. fd = sys_eventfd2(0, EFD_CLOEXEC|EFD_NONBLOCK);
  71. ASSERT_GE(fd, 0);
  72. flags = fcntl(fd, F_GETFL);
  73. ASSERT_GT(flags, -1);
  74. EXPECT_EQ(flags & EFD_NONBLOCK, EFD_NONBLOCK);
  75. EXPECT_EQ(flags & O_RDWR, O_RDWR);
  76. flags = fcntl(fd, F_GETFD);
  77. ASSERT_GT(flags, -1);
  78. EXPECT_EQ(flags, FD_CLOEXEC);
  79. close(fd);
  80. }
  81. static inline void trim_newline(char *str)
  82. {
  83. char *pos = strrchr(str, '\n');
  84. if (pos)
  85. *pos = '\0';
  86. }
  87. static int verify_fdinfo(int fd, struct error *err, const char *prefix,
  88. size_t prefix_len, const char *expect, ...)
  89. {
  90. char buffer[512] = {0, };
  91. char path[512] = {0, };
  92. va_list args;
  93. FILE *f;
  94. char *line = NULL;
  95. size_t n = 0;
  96. int found = 0;
  97. int r;
  98. va_start(args, expect);
  99. r = vsnprintf(buffer, sizeof(buffer), expect, args);
  100. assert((size_t)r < sizeof(buffer));
  101. va_end(args);
  102. snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
  103. f = fopen(path, "re");
  104. if (!f)
  105. return error_set(err, -1, "fdinfo open failed for %d", fd);
  106. while (getline(&line, &n, f) != -1) {
  107. char *val;
  108. if (strncmp(line, prefix, prefix_len))
  109. continue;
  110. found = 1;
  111. val = line + prefix_len;
  112. r = strcmp(val, buffer);
  113. if (r != 0) {
  114. trim_newline(line);
  115. trim_newline(buffer);
  116. error_set(err, -1, "%s '%s' != '%s'",
  117. prefix, val, buffer);
  118. }
  119. break;
  120. }
  121. free(line);
  122. fclose(f);
  123. if (found == 0)
  124. return error_set(err, -1, "%s not found for fd %d",
  125. prefix, fd);
  126. return 0;
  127. }
  128. TEST(eventfd_check_flag_semaphore)
  129. {
  130. struct error err = {0};
  131. int fd, ret;
  132. fd = sys_eventfd2(0, EFD_SEMAPHORE);
  133. ASSERT_GE(fd, 0);
  134. ret = fcntl(fd, F_GETFL);
  135. ASSERT_GT(ret, -1);
  136. EXPECT_EQ(ret & O_RDWR, O_RDWR);
  137. // The semaphore could only be obtained from fdinfo.
  138. ret = verify_fdinfo(fd, &err, "eventfd-semaphore: ", 19, "1\n");
  139. if (ret != 0)
  140. ksft_print_msg("eventfd semaphore flag check failed: %s\n", err.msg);
  141. EXPECT_EQ(ret, 0);
  142. close(fd);
  143. }
  144. /*
  145. * A write(2) fails with the error EINVAL if the size of the supplied buffer
  146. * is less than 8 bytes, or if an attempt is made to write the value
  147. * 0xffffffffffffffff.
  148. */
  149. TEST(eventfd_check_write)
  150. {
  151. uint64_t value = 1;
  152. ssize_t size;
  153. int fd;
  154. fd = sys_eventfd2(0, 0);
  155. ASSERT_GE(fd, 0);
  156. size = write(fd, &value, sizeof(int));
  157. EXPECT_EQ(size, -1);
  158. EXPECT_EQ(errno, EINVAL);
  159. size = write(fd, &value, sizeof(value));
  160. EXPECT_EQ(size, sizeof(value));
  161. value = (uint64_t)-1;
  162. size = write(fd, &value, sizeof(value));
  163. EXPECT_EQ(size, -1);
  164. EXPECT_EQ(errno, EINVAL);
  165. close(fd);
  166. }
  167. /*
  168. * A read(2) fails with the error EINVAL if the size of the supplied buffer is
  169. * less than 8 bytes.
  170. */
  171. TEST(eventfd_check_read)
  172. {
  173. uint64_t value;
  174. ssize_t size;
  175. int fd;
  176. fd = sys_eventfd2(1, 0);
  177. ASSERT_GE(fd, 0);
  178. size = read(fd, &value, sizeof(int));
  179. EXPECT_EQ(size, -1);
  180. EXPECT_EQ(errno, EINVAL);
  181. size = read(fd, &value, sizeof(value));
  182. EXPECT_EQ(size, sizeof(value));
  183. EXPECT_EQ(value, 1);
  184. close(fd);
  185. }
  186. /*
  187. * If EFD_SEMAPHORE was not specified and the eventfd counter has a nonzero
  188. * value, then a read(2) returns 8 bytes containing that value, and the
  189. * counter's value is reset to zero.
  190. * If the eventfd counter is zero at the time of the call to read(2), then the
  191. * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
  192. */
  193. TEST(eventfd_check_read_with_nonsemaphore)
  194. {
  195. uint64_t value;
  196. ssize_t size;
  197. int fd;
  198. int i;
  199. fd = sys_eventfd2(0, EFD_NONBLOCK);
  200. ASSERT_GE(fd, 0);
  201. value = 1;
  202. for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) {
  203. size = write(fd, &value, sizeof(value));
  204. EXPECT_EQ(size, sizeof(value));
  205. }
  206. size = read(fd, &value, sizeof(value));
  207. EXPECT_EQ(size, sizeof(uint64_t));
  208. EXPECT_EQ(value, EVENTFD_TEST_ITERATIONS);
  209. size = read(fd, &value, sizeof(value));
  210. EXPECT_EQ(size, -1);
  211. EXPECT_EQ(errno, EAGAIN);
  212. close(fd);
  213. }
  214. /*
  215. * If EFD_SEMAPHORE was specified and the eventfd counter has a nonzero value,
  216. * then a read(2) returns 8 bytes containing the value 1, and the counter's
  217. * value is decremented by 1.
  218. * If the eventfd counter is zero at the time of the call to read(2), then the
  219. * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
  220. */
  221. TEST(eventfd_check_read_with_semaphore)
  222. {
  223. uint64_t value;
  224. ssize_t size;
  225. int fd;
  226. int i;
  227. fd = sys_eventfd2(0, EFD_SEMAPHORE|EFD_NONBLOCK);
  228. ASSERT_GE(fd, 0);
  229. value = 1;
  230. for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) {
  231. size = write(fd, &value, sizeof(value));
  232. EXPECT_EQ(size, sizeof(value));
  233. }
  234. for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) {
  235. size = read(fd, &value, sizeof(value));
  236. EXPECT_EQ(size, sizeof(value));
  237. EXPECT_EQ(value, 1);
  238. }
  239. size = read(fd, &value, sizeof(value));
  240. EXPECT_EQ(size, -1);
  241. EXPECT_EQ(errno, EAGAIN);
  242. close(fd);
  243. }
  244. TEST_HARNESS_MAIN