fixed-link.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <sys/types.h>
  10. #include "helpers.h"
  11. #include "liburing.h"
  12. #define IOVECS_LEN 2
  13. int main(int argc, char *argv[])
  14. {
  15. struct iovec iovecs[IOVECS_LEN];
  16. struct io_uring ring;
  17. int i, fd, ret;
  18. if (argc > 1)
  19. return T_EXIT_SKIP;
  20. fd = open("/dev/zero", O_RDONLY);
  21. if (fd < 0) {
  22. fprintf(stderr, "Failed to open /dev/zero\n");
  23. return T_EXIT_FAIL;
  24. }
  25. if (io_uring_queue_init(32, &ring, 0) < 0) {
  26. fprintf(stderr, "Failed to init io_uring\n");
  27. close(fd);
  28. return T_EXIT_FAIL;
  29. }
  30. for (i = 0; i < IOVECS_LEN; ++i) {
  31. iovecs[i].iov_base = t_malloc(64);
  32. iovecs[i].iov_len = 64;
  33. }
  34. ret = io_uring_register_buffers(&ring, iovecs, IOVECS_LEN);
  35. if (ret) {
  36. fprintf(stderr, "Failed to register buffers\n");
  37. return T_EXIT_FAIL;
  38. }
  39. for (i = 0; i < IOVECS_LEN; ++i) {
  40. struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
  41. const char *str = "#include <errno.h>";
  42. iovecs[i].iov_len = strlen(str);
  43. io_uring_prep_read_fixed(sqe, fd, iovecs[i].iov_base, strlen(str), 0, i);
  44. if (i == 0)
  45. io_uring_sqe_set_flags(sqe, IOSQE_IO_LINK);
  46. io_uring_sqe_set_data(sqe, (void *)str);
  47. }
  48. ret = io_uring_submit_and_wait(&ring, IOVECS_LEN);
  49. if (ret < 0) {
  50. fprintf(stderr, "Failed to submit IO\n");
  51. return T_EXIT_FAIL;
  52. } else if (ret < 2) {
  53. fprintf(stderr, "Submitted %d, wanted %d\n", ret, IOVECS_LEN);
  54. return T_EXIT_FAIL;
  55. }
  56. for (i = 0; i < IOVECS_LEN; i++) {
  57. struct io_uring_cqe *cqe;
  58. ret = io_uring_wait_cqe(&ring, &cqe);
  59. if (ret) {
  60. fprintf(stderr, "wait_cqe=%d\n", ret);
  61. return T_EXIT_FAIL;
  62. }
  63. if (cqe->res != iovecs[i].iov_len) {
  64. fprintf(stderr, "read: wanted %ld, got %d\n",
  65. (long) iovecs[i].iov_len, cqe->res);
  66. return T_EXIT_FAIL;
  67. }
  68. io_uring_cqe_seen(&ring, cqe);
  69. }
  70. close(fd);
  71. io_uring_queue_exit(&ring);
  72. for (i = 0; i < IOVECS_LEN; ++i)
  73. free(iovecs[i].iov_base);
  74. return T_EXIT_PASS;
  75. }