fd-pass.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various fixed file fd passing 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 "liburing.h"
  14. #include "helpers.h"
  15. #define FSIZE 128
  16. #define PAT 0x9a
  17. #define USER_DATA 0x89
  18. static int no_fd_pass;
  19. static int verify_fixed_read(struct io_uring *ring, int fixed_fd, int fail)
  20. {
  21. struct io_uring_sqe *sqe;
  22. struct io_uring_cqe *cqe;
  23. unsigned char buf[FSIZE];
  24. int i;
  25. sqe = io_uring_get_sqe(ring);
  26. io_uring_prep_read(sqe, fixed_fd, buf, FSIZE, 0);
  27. sqe->flags |= IOSQE_FIXED_FILE;
  28. io_uring_submit(ring);
  29. io_uring_wait_cqe(ring, &cqe);
  30. if (cqe->res != FSIZE) {
  31. if (fail && cqe->res == -EBADF)
  32. return 0;
  33. fprintf(stderr, "Read: %d\n", cqe->res);
  34. return 1;
  35. }
  36. io_uring_cqe_seen(ring, cqe);
  37. for (i = 0; i < FSIZE; i++) {
  38. if (buf[i] != PAT) {
  39. fprintf(stderr, "got %x, wanted %x\n", buf[i], PAT);
  40. return 1;
  41. }
  42. }
  43. return 0;
  44. }
  45. static int test(const char *filename, int source_fd, int target_fd)
  46. {
  47. struct io_uring sring, dring;
  48. struct io_uring_sqe *sqe;
  49. struct io_uring_cqe *cqe;
  50. int ret;
  51. ret = io_uring_queue_init(8, &sring, 0);
  52. if (ret) {
  53. fprintf(stderr, "ring setup failed: %d\n", ret);
  54. return T_EXIT_FAIL;
  55. }
  56. ret = io_uring_queue_init(8, &dring, 0);
  57. if (ret) {
  58. fprintf(stderr, "ring setup failed: %d\n", ret);
  59. return T_EXIT_FAIL;
  60. }
  61. ret = io_uring_register_files_sparse(&sring, 8);
  62. if (ret) {
  63. if (ret == -EINVAL)
  64. return T_EXIT_SKIP;
  65. fprintf(stderr, "register files failed %d\n", ret);
  66. return T_EXIT_FAIL;
  67. }
  68. ret = io_uring_register_files_sparse(&dring, 8);
  69. if (ret) {
  70. fprintf(stderr, "register files failed %d\n", ret);
  71. return T_EXIT_FAIL;
  72. }
  73. if (target_fd == IORING_FILE_INDEX_ALLOC) {
  74. /* we want to test installing into a non-zero slot */
  75. ret = io_uring_register_file_alloc_range(&dring, 1, 1);
  76. if (ret) {
  77. fprintf(stderr, "io_uring_register_file_alloc_range %d\n", ret);
  78. return T_EXIT_FAIL;
  79. }
  80. }
  81. /* open direct descriptor */
  82. sqe = io_uring_get_sqe(&sring);
  83. io_uring_prep_openat_direct(sqe, AT_FDCWD, filename, 0, 0644, source_fd);
  84. io_uring_submit(&sring);
  85. ret = io_uring_wait_cqe(&sring, &cqe);
  86. if (ret) {
  87. fprintf(stderr, "wait cqe failed %d\n", ret);
  88. return T_EXIT_FAIL;
  89. }
  90. if (cqe->res) {
  91. fprintf(stderr, "cqe res %d\n", cqe->res);
  92. return T_EXIT_FAIL;
  93. }
  94. io_uring_cqe_seen(&sring, cqe);
  95. /* verify data is sane for source ring */
  96. if (verify_fixed_read(&sring, source_fd, 0))
  97. return T_EXIT_FAIL;
  98. /* send direct descriptor to destination ring */
  99. sqe = io_uring_get_sqe(&sring);
  100. if (target_fd == IORING_FILE_INDEX_ALLOC) {
  101. io_uring_prep_msg_ring_fd_alloc(sqe, dring.ring_fd, source_fd,
  102. USER_DATA, 0);
  103. } else {
  104. io_uring_prep_msg_ring_fd(sqe, dring.ring_fd, source_fd,
  105. target_fd, USER_DATA, 0);
  106. }
  107. io_uring_submit(&sring);
  108. ret = io_uring_wait_cqe(&sring, &cqe);
  109. if (ret) {
  110. fprintf(stderr, "wait cqe failed %d\n", ret);
  111. return T_EXIT_FAIL;
  112. }
  113. if (cqe->res < 0) {
  114. if (cqe->res == -EINVAL && !no_fd_pass) {
  115. no_fd_pass = 1;
  116. return T_EXIT_SKIP;
  117. }
  118. fprintf(stderr, "msg_ring failed %d\n", cqe->res);
  119. return T_EXIT_FAIL;
  120. }
  121. io_uring_cqe_seen(&sring, cqe);
  122. /* get posted completion for the passing */
  123. ret = io_uring_wait_cqe(&dring, &cqe);
  124. if (ret) {
  125. fprintf(stderr, "wait cqe failed %d\n", ret);
  126. return T_EXIT_FAIL;
  127. }
  128. if (cqe->user_data != USER_DATA) {
  129. fprintf(stderr, "bad user_data %ld\n", (long) cqe->res);
  130. return T_EXIT_FAIL;
  131. }
  132. if (cqe->res < 0) {
  133. fprintf(stderr, "bad result %i\n", cqe->res);
  134. return T_EXIT_FAIL;
  135. }
  136. if (target_fd == IORING_FILE_INDEX_ALLOC) {
  137. if (cqe->res != 1) {
  138. fprintf(stderr, "invalid allocated index %i\n", cqe->res);
  139. return T_EXIT_FAIL;
  140. }
  141. target_fd = cqe->res;
  142. }
  143. io_uring_cqe_seen(&dring, cqe);
  144. /* now verify we can read the sane data from the destination ring */
  145. if (verify_fixed_read(&dring, target_fd, 0))
  146. return T_EXIT_FAIL;
  147. /* close descriptor in source ring */
  148. sqe = io_uring_get_sqe(&sring);
  149. io_uring_prep_close_direct(sqe, source_fd);
  150. io_uring_submit(&sring);
  151. ret = io_uring_wait_cqe(&sring, &cqe);
  152. if (ret) {
  153. fprintf(stderr, "wait cqe failed %d\n", ret);
  154. return T_EXIT_FAIL;
  155. }
  156. if (cqe->res) {
  157. fprintf(stderr, "direct close failed %d\n", cqe->res);
  158. return T_EXIT_FAIL;
  159. }
  160. io_uring_cqe_seen(&sring, cqe);
  161. /* check that source ring fails after close */
  162. if (verify_fixed_read(&sring, source_fd, 1))
  163. return T_EXIT_FAIL;
  164. /* check we can still read from destination ring */
  165. if (verify_fixed_read(&dring, target_fd, 0))
  166. return T_EXIT_FAIL;
  167. io_uring_queue_exit(&sring);
  168. io_uring_queue_exit(&dring);
  169. return T_EXIT_PASS;
  170. }
  171. int main(int argc, char *argv[])
  172. {
  173. char fname[80];
  174. int ret;
  175. if (argc > 1)
  176. return T_EXIT_SKIP;
  177. sprintf(fname, ".fd-pass.%d", getpid());
  178. t_create_file_pattern(fname, FSIZE, PAT);
  179. ret = test(fname, 0, 1);
  180. if (ret == T_EXIT_FAIL) {
  181. fprintf(stderr, "test failed 0 1\n");
  182. ret = T_EXIT_FAIL;
  183. }
  184. ret = test(fname, 0, 2);
  185. if (ret == T_EXIT_FAIL) {
  186. fprintf(stderr, "test failed 0 2\n");
  187. ret = T_EXIT_FAIL;
  188. }
  189. ret = test(fname, 1, 1);
  190. if (ret == T_EXIT_FAIL) {
  191. fprintf(stderr, "test failed 1 1\n");
  192. ret = T_EXIT_FAIL;
  193. }
  194. ret = test(fname, 1, 0);
  195. if (ret == T_EXIT_FAIL) {
  196. fprintf(stderr, "test failed 1 0\n");
  197. ret = T_EXIT_FAIL;
  198. }
  199. ret = test(fname, 1, IORING_FILE_INDEX_ALLOC);
  200. if (ret == T_EXIT_FAIL) {
  201. fprintf(stderr, "test failed 1 ALLOC\n");
  202. ret = T_EXIT_FAIL;
  203. }
  204. unlink(fname);
  205. return ret;
  206. }