open-close.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various openat(2) tests
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <assert.h>
  14. #include "helpers.h"
  15. #include "liburing.h"
  16. static int submit_wait(struct io_uring *ring)
  17. {
  18. struct io_uring_cqe *cqe;
  19. int ret;
  20. ret = io_uring_submit(ring);
  21. if (ret <= 0) {
  22. fprintf(stderr, "sqe submit failed: %d\n", ret);
  23. return 1;
  24. }
  25. ret = io_uring_wait_cqe(ring, &cqe);
  26. if (ret < 0) {
  27. fprintf(stderr, "wait completion %d\n", ret);
  28. return 1;
  29. }
  30. ret = cqe->res;
  31. io_uring_cqe_seen(ring, cqe);
  32. return ret;
  33. }
  34. static int test_close_flush(void)
  35. {
  36. struct io_uring ring;
  37. struct io_uring_sqe *sqe;
  38. char buf[128];
  39. int ret, fd;
  40. sprintf(buf, "/sys/kernel/debug/tracing/per_cpu/cpu0/trace_pipe_raw");
  41. fd = open(buf, O_RDONLY);
  42. if (fd < 0)
  43. return 0;
  44. ret = io_uring_queue_init(8, &ring, 0);
  45. if (ret) {
  46. fprintf(stderr, "ring setup failed\n");
  47. return -1;
  48. }
  49. sqe = io_uring_get_sqe(&ring);
  50. io_uring_prep_close(sqe, fd);
  51. ret = submit_wait(&ring);
  52. if (ret) {
  53. fprintf(stderr, "closefailed %i\n", ret);
  54. return -1;
  55. }
  56. io_uring_queue_exit(&ring);
  57. return 0;
  58. }
  59. static inline int try_close(struct io_uring *ring, int fd, int slot)
  60. {
  61. struct io_uring_sqe *sqe;
  62. sqe = io_uring_get_sqe(ring);
  63. io_uring_prep_close(sqe, fd);
  64. __io_uring_set_target_fixed_file(sqe, slot);
  65. return submit_wait(ring);
  66. }
  67. static int test_close_fixed(void)
  68. {
  69. struct io_uring ring;
  70. struct io_uring_sqe *sqe;
  71. int ret, fds[2];
  72. char buf[1];
  73. ret = io_uring_queue_init(8, &ring, 0);
  74. if (ret) {
  75. fprintf(stderr, "ring setup failed\n");
  76. return -1;
  77. }
  78. if (pipe(fds)) {
  79. perror("pipe");
  80. return -1;
  81. }
  82. ret = try_close(&ring, 0, 0);
  83. if (ret == -EINVAL) {
  84. fprintf(stderr, "close for fixed files is not supported\n");
  85. return 0;
  86. } else if (ret != -ENXIO) {
  87. fprintf(stderr, "no table failed %i\n", ret);
  88. return -1;
  89. }
  90. ret = try_close(&ring, 1, 0);
  91. if (ret != -EINVAL) {
  92. fprintf(stderr, "set fd failed %i\n", ret);
  93. return -1;
  94. }
  95. ret = io_uring_register_files(&ring, fds, 2);
  96. if (ret) {
  97. fprintf(stderr, "file_register: %d\n", ret);
  98. return ret;
  99. }
  100. ret = try_close(&ring, 0, 2);
  101. if (ret != -EINVAL) {
  102. fprintf(stderr, "out of table failed %i\n", ret);
  103. return -1;
  104. }
  105. ret = try_close(&ring, 0, 0);
  106. if (ret != 0) {
  107. fprintf(stderr, "close failed %i\n", ret);
  108. return -1;
  109. }
  110. sqe = io_uring_get_sqe(&ring);
  111. io_uring_prep_read(sqe, 0, buf, sizeof(buf), 0);
  112. sqe->flags |= IOSQE_FIXED_FILE;
  113. ret = submit_wait(&ring);
  114. if (ret != -EBADF) {
  115. fprintf(stderr, "read failed %i\n", ret);
  116. return -1;
  117. }
  118. ret = try_close(&ring, 0, 1);
  119. if (ret != 0) {
  120. fprintf(stderr, "close 2 failed %i\n", ret);
  121. return -1;
  122. }
  123. ret = try_close(&ring, 0, 0);
  124. if (ret != -EBADF) {
  125. fprintf(stderr, "empty slot failed %i\n", ret);
  126. return -1;
  127. }
  128. close(fds[0]);
  129. close(fds[1]);
  130. io_uring_queue_exit(&ring);
  131. return 0;
  132. }
  133. static int test_close(struct io_uring *ring, int fd, int is_ring_fd)
  134. {
  135. struct io_uring_cqe *cqe;
  136. struct io_uring_sqe *sqe;
  137. int ret;
  138. sqe = io_uring_get_sqe(ring);
  139. if (!sqe) {
  140. fprintf(stderr, "get sqe failed\n");
  141. goto err;
  142. }
  143. io_uring_prep_close(sqe, fd);
  144. ret = io_uring_submit(ring);
  145. if (ret <= 0) {
  146. fprintf(stderr, "sqe submit failed: %d\n", ret);
  147. goto err;
  148. }
  149. ret = io_uring_wait_cqe(ring, &cqe);
  150. if (ret < 0) {
  151. if (!(is_ring_fd && ret == -EBADF)) {
  152. fprintf(stderr, "wait completion %d\n", ret);
  153. goto err;
  154. }
  155. return ret;
  156. }
  157. ret = cqe->res;
  158. io_uring_cqe_seen(ring, cqe);
  159. return ret;
  160. err:
  161. return -1;
  162. }
  163. static int test_openat(struct io_uring *ring, const char *path, int dfd)
  164. {
  165. struct io_uring_cqe *cqe;
  166. struct io_uring_sqe *sqe;
  167. int ret;
  168. sqe = io_uring_get_sqe(ring);
  169. if (!sqe) {
  170. fprintf(stderr, "get sqe failed\n");
  171. goto err;
  172. }
  173. io_uring_prep_openat(sqe, dfd, path, O_RDONLY, 0);
  174. ret = io_uring_submit(ring);
  175. if (ret <= 0) {
  176. fprintf(stderr, "sqe submit failed: %d\n", ret);
  177. goto err;
  178. }
  179. ret = io_uring_wait_cqe(ring, &cqe);
  180. if (ret < 0) {
  181. fprintf(stderr, "wait completion %d\n", ret);
  182. goto err;
  183. }
  184. ret = cqe->res;
  185. io_uring_cqe_seen(ring, cqe);
  186. return ret;
  187. err:
  188. return -1;
  189. }
  190. int main(int argc, char *argv[])
  191. {
  192. struct io_uring ring;
  193. const char *path, *path_rel;
  194. int ret, do_unlink;
  195. ret = io_uring_queue_init(8, &ring, 0);
  196. if (ret) {
  197. fprintf(stderr, "ring setup failed\n");
  198. return 1;
  199. }
  200. if (argc > 1) {
  201. path = "/tmp/.open.close";
  202. path_rel = argv[1];
  203. do_unlink = 0;
  204. } else {
  205. path = "/tmp/.open.close";
  206. path_rel = ".open.close";
  207. do_unlink = 1;
  208. }
  209. t_create_file(path, 4096);
  210. if (do_unlink)
  211. t_create_file(path_rel, 4096);
  212. ret = test_openat(&ring, path, -1);
  213. if (ret < 0) {
  214. if (ret == -EINVAL) {
  215. fprintf(stdout, "Open not supported, skipping\n");
  216. goto done;
  217. }
  218. if (ret == -EPERM || ret == -EACCES)
  219. return T_EXIT_SKIP;
  220. fprintf(stderr, "test_openat absolute failed: %d\n", ret);
  221. goto err;
  222. }
  223. ret = test_openat(&ring, path_rel, AT_FDCWD);
  224. if (ret < 0) {
  225. if (ret == -EPERM || ret == -EACCES)
  226. return T_EXIT_SKIP;
  227. fprintf(stderr, "test_openat relative failed: %d\n", ret);
  228. goto err;
  229. }
  230. ret = test_close(&ring, ret, 0);
  231. if (ret) {
  232. fprintf(stderr, "test_close normal failed\n");
  233. goto err;
  234. }
  235. ret = test_close(&ring, ring.ring_fd, 1);
  236. if (ret != -EBADF) {
  237. fprintf(stderr, "test_close ring_fd failed\n");
  238. goto err;
  239. }
  240. ret = test_close_fixed();
  241. if (ret) {
  242. fprintf(stderr, "test_close_fixed failed\n");
  243. goto err;
  244. }
  245. ret = test_close_flush();
  246. if (ret) {
  247. fprintf(stderr, "test_close_flush failed\n");
  248. goto err;
  249. }
  250. done:
  251. unlink(path);
  252. if (do_unlink)
  253. unlink(path_rel);
  254. return 0;
  255. err:
  256. unlink(path);
  257. if (do_unlink)
  258. unlink(path_rel);
  259. return 1;
  260. }