open-close.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 inline int try_close(struct io_uring *ring, int fd, int slot)
  35. {
  36. struct io_uring_sqe *sqe;
  37. sqe = io_uring_get_sqe(ring);
  38. io_uring_prep_close(sqe, fd);
  39. __io_uring_set_target_fixed_file(sqe, slot);
  40. return submit_wait(ring);
  41. }
  42. static int test_close_fixed(void)
  43. {
  44. struct io_uring ring;
  45. struct io_uring_sqe *sqe;
  46. int ret, fds[2];
  47. char buf[1];
  48. ret = io_uring_queue_init(8, &ring, 0);
  49. if (ret) {
  50. fprintf(stderr, "ring setup failed\n");
  51. return -1;
  52. }
  53. if (pipe(fds)) {
  54. perror("pipe");
  55. return -1;
  56. }
  57. ret = try_close(&ring, 0, 0);
  58. if (ret == -EINVAL) {
  59. fprintf(stderr, "close for fixed files is not supported\n");
  60. return 0;
  61. } else if (ret != -ENXIO) {
  62. fprintf(stderr, "no table failed %i\n", ret);
  63. return -1;
  64. }
  65. ret = try_close(&ring, 1, 0);
  66. if (ret != -EINVAL) {
  67. fprintf(stderr, "set fd failed %i\n", ret);
  68. return -1;
  69. }
  70. ret = io_uring_register_files(&ring, fds, 2);
  71. if (ret) {
  72. fprintf(stderr, "file_register: %d\n", ret);
  73. return ret;
  74. }
  75. ret = try_close(&ring, 0, 2);
  76. if (ret != -EINVAL) {
  77. fprintf(stderr, "out of table failed %i\n", ret);
  78. return -1;
  79. }
  80. ret = try_close(&ring, 0, 0);
  81. if (ret != 0) {
  82. fprintf(stderr, "close failed %i\n", ret);
  83. return -1;
  84. }
  85. sqe = io_uring_get_sqe(&ring);
  86. io_uring_prep_read(sqe, 0, buf, sizeof(buf), 0);
  87. sqe->flags |= IOSQE_FIXED_FILE;
  88. ret = submit_wait(&ring);
  89. if (ret != -EBADF) {
  90. fprintf(stderr, "read failed %i\n", ret);
  91. return -1;
  92. }
  93. ret = try_close(&ring, 0, 1);
  94. if (ret != 0) {
  95. fprintf(stderr, "close 2 failed %i\n", ret);
  96. return -1;
  97. }
  98. ret = try_close(&ring, 0, 0);
  99. if (ret != -EBADF) {
  100. fprintf(stderr, "empty slot failed %i\n", ret);
  101. return -1;
  102. }
  103. close(fds[0]);
  104. close(fds[1]);
  105. io_uring_queue_exit(&ring);
  106. return 0;
  107. }
  108. static int test_close(struct io_uring *ring, int fd, int is_ring_fd)
  109. {
  110. struct io_uring_cqe *cqe;
  111. struct io_uring_sqe *sqe;
  112. int ret;
  113. sqe = io_uring_get_sqe(ring);
  114. if (!sqe) {
  115. fprintf(stderr, "get sqe failed\n");
  116. goto err;
  117. }
  118. io_uring_prep_close(sqe, fd);
  119. ret = io_uring_submit(ring);
  120. if (ret <= 0) {
  121. fprintf(stderr, "sqe submit failed: %d\n", ret);
  122. goto err;
  123. }
  124. ret = io_uring_wait_cqe(ring, &cqe);
  125. if (ret < 0) {
  126. if (!(is_ring_fd && ret == -EBADF)) {
  127. fprintf(stderr, "wait completion %d\n", ret);
  128. goto err;
  129. }
  130. return ret;
  131. }
  132. ret = cqe->res;
  133. io_uring_cqe_seen(ring, cqe);
  134. return ret;
  135. err:
  136. return -1;
  137. }
  138. static int test_openat(struct io_uring *ring, const char *path, int dfd)
  139. {
  140. struct io_uring_cqe *cqe;
  141. struct io_uring_sqe *sqe;
  142. int ret;
  143. sqe = io_uring_get_sqe(ring);
  144. if (!sqe) {
  145. fprintf(stderr, "get sqe failed\n");
  146. goto err;
  147. }
  148. io_uring_prep_openat(sqe, dfd, path, O_RDONLY, 0);
  149. ret = io_uring_submit(ring);
  150. if (ret <= 0) {
  151. fprintf(stderr, "sqe submit failed: %d\n", ret);
  152. goto err;
  153. }
  154. ret = io_uring_wait_cqe(ring, &cqe);
  155. if (ret < 0) {
  156. fprintf(stderr, "wait completion %d\n", ret);
  157. goto err;
  158. }
  159. ret = cqe->res;
  160. io_uring_cqe_seen(ring, cqe);
  161. return ret;
  162. err:
  163. return -1;
  164. }
  165. int main(int argc, char *argv[])
  166. {
  167. struct io_uring ring;
  168. const char *path, *path_rel;
  169. int ret, do_unlink;
  170. ret = io_uring_queue_init(8, &ring, 0);
  171. if (ret) {
  172. fprintf(stderr, "ring setup failed\n");
  173. return 1;
  174. }
  175. if (argc > 1) {
  176. path = "/tmp/.open.close";
  177. path_rel = argv[1];
  178. do_unlink = 0;
  179. } else {
  180. path = "/tmp/.open.close";
  181. path_rel = ".open.close";
  182. do_unlink = 1;
  183. }
  184. t_create_file(path, 4096);
  185. if (do_unlink)
  186. t_create_file(path_rel, 4096);
  187. ret = test_openat(&ring, path, -1);
  188. if (ret < 0) {
  189. if (ret == -EINVAL) {
  190. fprintf(stdout, "Open not supported, skipping\n");
  191. goto done;
  192. }
  193. fprintf(stderr, "test_openat absolute failed: %d\n", ret);
  194. goto err;
  195. }
  196. ret = test_openat(&ring, path_rel, AT_FDCWD);
  197. if (ret < 0) {
  198. fprintf(stderr, "test_openat relative failed: %d\n", ret);
  199. goto err;
  200. }
  201. ret = test_close(&ring, ret, 0);
  202. if (ret) {
  203. fprintf(stderr, "test_close normal failed\n");
  204. goto err;
  205. }
  206. ret = test_close(&ring, ring.ring_fd, 1);
  207. if (ret != -EBADF) {
  208. fprintf(stderr, "test_close ring_fd failed\n");
  209. goto err;
  210. }
  211. ret = test_close_fixed();
  212. if (ret) {
  213. fprintf(stderr, "test_close_fixed failed\n");
  214. goto err;
  215. }
  216. done:
  217. unlink(path);
  218. if (do_unlink)
  219. unlink(path_rel);
  220. return 0;
  221. err:
  222. unlink(path);
  223. if (do_unlink)
  224. unlink(path_rel);
  225. return 1;
  226. }