fixed-buf-iter.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Test fixed buffers with non-iterators.
  5. *
  6. * Taken from: https://github.com/axboe/liburing/issues/549
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <stdlib.h>
  12. #include "liburing.h"
  13. #include "helpers.h"
  14. #define BUF_SIZE 4096
  15. #define BUFFERS 1
  16. #define IN_FD "/dev/urandom"
  17. #define OUT_FD "/dev/zero"
  18. static int test(struct io_uring *ring)
  19. {
  20. struct iovec iov[BUFFERS];
  21. struct io_uring_sqe *sqe;
  22. struct io_uring_cqe *cqe;
  23. int ret, fd_in, fd_out, i;
  24. fd_in = open(IN_FD, O_RDONLY, 0644);
  25. if (fd_in < 0) {
  26. perror("open in");
  27. return 1;
  28. }
  29. fd_out = open(OUT_FD, O_RDWR, 0644);
  30. if (fd_out < 0) {
  31. perror("open out");
  32. return 1;
  33. }
  34. for (i = 0; i < BUFFERS; i++) {
  35. iov[i].iov_base = malloc(BUF_SIZE);
  36. iov[i].iov_len = BUF_SIZE;
  37. memset(iov[i].iov_base, 0, BUF_SIZE);
  38. }
  39. ret = io_uring_register_buffers(ring, iov, BUFFERS);
  40. if (ret) {
  41. fprintf(stderr, "Error registering buffers: %s", strerror(-ret));
  42. return 1;
  43. }
  44. sqe = io_uring_get_sqe(ring);
  45. if (!sqe) {
  46. fprintf(stderr, "Could not get SQE.\n");
  47. return 1;
  48. }
  49. io_uring_prep_read_fixed(sqe, fd_in, iov[0].iov_base, BUF_SIZE, 0, 0);
  50. io_uring_submit(ring);
  51. ret = io_uring_wait_cqe(ring, &cqe);
  52. if (ret < 0) {
  53. fprintf(stderr, "Error waiting for completion: %s\n", strerror(-ret));
  54. return 1;
  55. }
  56. if (cqe->res < 0) {
  57. fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res));
  58. return 1;
  59. }
  60. io_uring_cqe_seen(ring, cqe);
  61. sqe = io_uring_get_sqe(ring);
  62. if (!sqe) {
  63. fprintf(stderr, "Could not get SQE.\n");
  64. return 1;
  65. }
  66. io_uring_prep_write_fixed(sqe, fd_out, iov[0].iov_base, BUF_SIZE, 0, 0);
  67. io_uring_submit(ring);
  68. ret = io_uring_wait_cqe(ring, &cqe);
  69. if (ret < 0) {
  70. fprintf(stderr, "Error waiting for completion: %s\n", strerror(-ret));
  71. return 1;
  72. }
  73. if (cqe->res < 0) {
  74. fprintf(stderr, "Error in async operation: %s\n", strerror(-cqe->res));
  75. return 1;
  76. }
  77. io_uring_cqe_seen(ring, cqe);
  78. for (i = 0; i < BUFFERS; i++)
  79. free(iov[i].iov_base);
  80. return 0;
  81. }
  82. int main(int argc, char *argv[])
  83. {
  84. struct io_uring ring;
  85. int ret;
  86. if (argc > 1)
  87. return T_EXIT_SKIP;
  88. ret = t_create_ring(8, &ring, 0);
  89. if (ret == T_SETUP_SKIP)
  90. return T_EXIT_SKIP;
  91. else if (ret < 0)
  92. return T_EXIT_FAIL;
  93. ret = test(&ring);
  94. if (ret) {
  95. fprintf(stderr, "Test failed\n");
  96. return T_EXIT_FAIL;
  97. }
  98. io_uring_queue_exit(&ring);
  99. return T_EXIT_PASS;
  100. }