eventfd-reg.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test eventfd registration+unregistration
  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 ring;
  21. int ret, evfd[2], i;
  22. if (argc > 1)
  23. return T_EXIT_SKIP;
  24. ret = io_uring_queue_init_params(8, &ring, &p);
  25. if (ret) {
  26. fprintf(stderr, "ring setup failed: %d\n", ret);
  27. return T_EXIT_FAIL;
  28. }
  29. evfd[0] = eventfd(0, EFD_CLOEXEC);
  30. evfd[1] = eventfd(0, EFD_CLOEXEC);
  31. if (evfd[0] < 0 || evfd[1] < 0) {
  32. perror("eventfd");
  33. return T_EXIT_FAIL;
  34. }
  35. ret = io_uring_register_eventfd(&ring, evfd[0]);
  36. if (ret) {
  37. fprintf(stderr, "failed to register evfd: %d\n", ret);
  38. return T_EXIT_FAIL;
  39. }
  40. /* Check that registrering again will get -EBUSY */
  41. ret = io_uring_register_eventfd(&ring, evfd[1]);
  42. if (ret != -EBUSY) {
  43. fprintf(stderr, "unexpected 2nd register: %d\n", ret);
  44. return T_EXIT_FAIL;
  45. }
  46. close(evfd[1]);
  47. ret = io_uring_unregister_eventfd(&ring);
  48. if (ret) {
  49. fprintf(stderr, "unexpected unregister: %d\n", ret);
  50. return T_EXIT_FAIL;
  51. }
  52. /* loop 100 registers/unregister */
  53. for (i = 0; i < 100; i++) {
  54. ret = io_uring_register_eventfd(&ring, evfd[0]);
  55. if (ret) {
  56. fprintf(stderr, "failed to register evfd: %d\n", ret);
  57. return T_EXIT_FAIL;
  58. }
  59. ret = io_uring_unregister_eventfd(&ring);
  60. if (ret) {
  61. fprintf(stderr, "unexpected unregister: %d\n", ret);
  62. return T_EXIT_FAIL;
  63. }
  64. }
  65. close(evfd[0]);
  66. return T_EXIT_PASS;
  67. }