eventfd.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various eventfd 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 <poll.h>
  14. #include <sys/eventfd.h>
  15. #include "liburing.h"
  16. #include "helpers.h"
  17. int main(int argc, char *argv[])
  18. {
  19. struct io_uring_params p = {};
  20. struct io_uring_sqe *sqe;
  21. struct io_uring_cqe *cqe;
  22. struct io_uring ring;
  23. uint64_t ptr;
  24. struct iovec vec = {
  25. .iov_base = &ptr,
  26. .iov_len = sizeof(ptr)
  27. };
  28. int ret, evfd, i;
  29. if (argc > 1)
  30. return T_EXIT_SKIP;
  31. ret = io_uring_queue_init_params(8, &ring, &p);
  32. if (ret) {
  33. fprintf(stderr, "ring setup failed: %d\n", ret);
  34. return T_EXIT_FAIL;
  35. }
  36. if (!(p.features & IORING_FEAT_CUR_PERSONALITY)) {
  37. fprintf(stdout, "Skipping\n");
  38. return T_EXIT_SKIP;
  39. }
  40. evfd = eventfd(0, EFD_CLOEXEC);
  41. if (evfd < 0) {
  42. perror("eventfd");
  43. return T_EXIT_FAIL;
  44. }
  45. ret = io_uring_register_eventfd(&ring, evfd);
  46. if (ret) {
  47. fprintf(stderr, "failed to register evfd: %d\n", ret);
  48. return T_EXIT_FAIL;
  49. }
  50. sqe = io_uring_get_sqe(&ring);
  51. io_uring_prep_poll_add(sqe, evfd, POLLIN);
  52. sqe->flags |= IOSQE_IO_LINK;
  53. sqe->user_data = 1;
  54. sqe = io_uring_get_sqe(&ring);
  55. io_uring_prep_readv(sqe, evfd, &vec, 1, 0);
  56. sqe->user_data = 2;
  57. ret = io_uring_submit(&ring);
  58. if (ret != 2) {
  59. fprintf(stderr, "submit: %d\n", ret);
  60. return T_EXIT_FAIL;
  61. }
  62. sqe = io_uring_get_sqe(&ring);
  63. io_uring_prep_nop(sqe);
  64. sqe->user_data = 3;
  65. ret = io_uring_submit(&ring);
  66. if (ret != 1) {
  67. fprintf(stderr, "submit: %d\n", ret);
  68. return T_EXIT_FAIL;
  69. }
  70. for (i = 0; i < 3; i++) {
  71. ret = io_uring_wait_cqe(&ring, &cqe);
  72. if (ret) {
  73. fprintf(stderr, "wait: %d\n", ret);
  74. return T_EXIT_FAIL;
  75. }
  76. switch (cqe->user_data) {
  77. case 1:
  78. /* POLLIN */
  79. if (cqe->res != 1) {
  80. fprintf(stderr, "poll: %d\n", cqe->res);
  81. return T_EXIT_FAIL;
  82. }
  83. break;
  84. case 2:
  85. if (cqe->res != sizeof(ptr)) {
  86. fprintf(stderr, "read: %d\n", cqe->res);
  87. return T_EXIT_FAIL;
  88. }
  89. break;
  90. case 3:
  91. if (cqe->res) {
  92. fprintf(stderr, "nop: %d\n", cqe->res);
  93. return T_EXIT_FAIL;
  94. }
  95. break;
  96. }
  97. io_uring_cqe_seen(&ring, cqe);
  98. }
  99. return T_EXIT_PASS;
  100. }