eeed8b54e0df.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: -EAGAIN handling
  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 "helpers.h"
  14. #include "liburing.h"
  15. #define BLOCK 4096
  16. #ifndef RWF_NOWAIT
  17. #define RWF_NOWAIT 8
  18. #endif
  19. static int get_file_fd(void)
  20. {
  21. ssize_t ret;
  22. char *buf;
  23. int fd;
  24. fd = open("testfile", O_RDWR | O_CREAT, 0644);
  25. unlink("testfile");
  26. if (fd < 0) {
  27. perror("open file");
  28. return -1;
  29. }
  30. buf = t_malloc(BLOCK);
  31. memset(buf, 0, BLOCK);
  32. ret = write(fd, buf, BLOCK);
  33. if (ret != BLOCK) {
  34. if (ret < 0)
  35. perror("write");
  36. else
  37. printf("Short write\n");
  38. goto err;
  39. }
  40. fsync(fd);
  41. if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) {
  42. perror("fadvise");
  43. err:
  44. close(fd);
  45. free(buf);
  46. return -1;
  47. }
  48. free(buf);
  49. return fd;
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. struct io_uring ring;
  54. struct io_uring_sqe *sqe;
  55. struct io_uring_cqe *cqe;
  56. struct iovec iov;
  57. int ret, fd;
  58. if (argc > 1)
  59. return T_EXIT_SKIP;
  60. iov.iov_base = t_malloc(4096);
  61. iov.iov_len = 4096;
  62. ret = io_uring_queue_init(2, &ring, 0);
  63. if (ret) {
  64. printf("ring setup failed\n");
  65. return T_EXIT_FAIL;
  66. }
  67. sqe = io_uring_get_sqe(&ring);
  68. if (!sqe) {
  69. printf("get sqe failed\n");
  70. return T_EXIT_FAIL;
  71. }
  72. fd = get_file_fd();
  73. if (fd < 0)
  74. return T_EXIT_FAIL;
  75. io_uring_prep_readv(sqe, fd, &iov, 1, 0);
  76. sqe->rw_flags = RWF_NOWAIT;
  77. ret = io_uring_submit(&ring);
  78. if (ret != 1) {
  79. printf("Got submit %d, expected 1\n", ret);
  80. goto err;
  81. }
  82. ret = io_uring_peek_cqe(&ring, &cqe);
  83. if (ret) {
  84. printf("Ring peek got %d\n", ret);
  85. goto err;
  86. }
  87. ret = T_EXIT_PASS;
  88. if (cqe->res != -EAGAIN && cqe->res != 4096) {
  89. if (cqe->res == -EOPNOTSUPP) {
  90. ret = T_EXIT_SKIP;
  91. } else {
  92. printf("cqe error: %d\n", cqe->res);
  93. goto err;
  94. }
  95. }
  96. close(fd);
  97. free(iov.iov_base);
  98. return ret;
  99. err:
  100. close(fd);
  101. free(iov.iov_base);
  102. return T_EXIT_FAIL;
  103. }