shared-wq.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test wq sharing
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <fcntl.h>
  12. #include "liburing.h"
  13. static int test_attach_invalid(int ringfd)
  14. {
  15. struct io_uring_params p;
  16. struct io_uring ring;
  17. int ret;
  18. memset(&p, 0, sizeof(p));
  19. p.flags = IORING_SETUP_ATTACH_WQ;
  20. p.wq_fd = ringfd;
  21. ret = io_uring_queue_init_params(1, &ring, &p);
  22. if (ret != -EINVAL) {
  23. fprintf(stderr, "Attach to zero: %d\n", ret);
  24. goto err;
  25. }
  26. return 0;
  27. err:
  28. return 1;
  29. }
  30. static int test_attach(int ringfd)
  31. {
  32. struct io_uring_params p;
  33. struct io_uring ring2;
  34. int ret;
  35. memset(&p, 0, sizeof(p));
  36. p.flags = IORING_SETUP_ATTACH_WQ;
  37. p.wq_fd = ringfd;
  38. ret = io_uring_queue_init_params(1, &ring2, &p);
  39. if (ret == -EINVAL) {
  40. fprintf(stdout, "Sharing not supported, skipping\n");
  41. return 0;
  42. } else if (ret) {
  43. fprintf(stderr, "Attach to id: %d\n", ret);
  44. goto err;
  45. }
  46. io_uring_queue_exit(&ring2);
  47. return 0;
  48. err:
  49. return 1;
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. struct io_uring ring;
  54. int ret;
  55. if (argc > 1)
  56. return 0;
  57. ret = io_uring_queue_init(8, &ring, 0);
  58. if (ret) {
  59. fprintf(stderr, "ring setup failed\n");
  60. return 1;
  61. }
  62. /* stdout is definitely not an io_uring descriptor */
  63. ret = test_attach_invalid(2);
  64. if (ret) {
  65. fprintf(stderr, "test_attach_invalid failed\n");
  66. return ret;
  67. }
  68. ret = test_attach(ring.ring_fd);
  69. if (ret) {
  70. fprintf(stderr, "test_attach failed\n");
  71. return ret;
  72. }
  73. return 0;
  74. }