reg-reg-ring.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Test io_uring_register with a registered ring (IORING_REGISTER_USE_REGISTERED_RING)
  5. *
  6. */
  7. #include <stdio.h>
  8. #include "helpers.h"
  9. int main(int argc, char *argv[])
  10. {
  11. struct io_uring ring;
  12. unsigned values[2];
  13. int ret;
  14. if (argc > 1)
  15. return T_EXIT_SKIP;
  16. ret = io_uring_queue_init(8, &ring, 0);
  17. if (ret) {
  18. fprintf(stderr, "ring setup failed\n");
  19. return T_EXIT_FAIL;
  20. }
  21. if (!(ring.features & IORING_FEAT_REG_REG_RING)) {
  22. fprintf(stderr, "IORING_FEAT_REG_REG_RING not available in kernel\n");
  23. io_uring_queue_exit(&ring);
  24. return T_EXIT_SKIP;
  25. }
  26. ret = io_uring_close_ring_fd(&ring);
  27. if (ret != -EINVAL) {
  28. fprintf(stderr, "closing ring fd should EINVAL before register\n");
  29. goto err;
  30. }
  31. ret = io_uring_unregister_ring_fd(&ring);
  32. if (ret != -EINVAL) {
  33. fprintf(stderr, "unregistering not-registered ring fd should fail\n");
  34. goto err;
  35. }
  36. ret = io_uring_register_ring_fd(&ring);
  37. if (ret != 1) {
  38. fprintf(stderr, "registering ring fd failed\n");
  39. goto err;
  40. }
  41. ret = io_uring_register_ring_fd(&ring);
  42. if (ret != -EEXIST) {
  43. fprintf(stderr, "registering already-registered ring fd should fail\n");
  44. goto err;
  45. }
  46. /* Test a simple io_uring_register operation expected to work.
  47. * io_uring_register_iowq_max_workers is arbitrary.
  48. */
  49. values[0] = values[1] = 0;
  50. ret = io_uring_register_iowq_max_workers(&ring, values);
  51. if (ret || (values[0] == 0 && values[1] == 0)) {
  52. fprintf(stderr, "io_uring_register operation failed before closing ring fd\n");
  53. goto err;
  54. }
  55. ret = io_uring_close_ring_fd(&ring);
  56. if (ret != 1) {
  57. fprintf(stderr, "closing ring fd failed\n");
  58. goto err;
  59. }
  60. values[0] = values[1] = 0;
  61. ret = io_uring_register_iowq_max_workers(&ring, values);
  62. if (ret || (values[0] == 0 && values[1] == 0)) {
  63. fprintf(stderr, "io_uring_register operation failed after closing ring fd\n");
  64. goto err;
  65. }
  66. ret = io_uring_close_ring_fd(&ring);
  67. if (ret != -EBADF) {
  68. fprintf(stderr, "closing already-closed ring fd should fail\n");
  69. goto err;
  70. }
  71. io_uring_queue_exit(&ring);
  72. return T_EXIT_PASS;
  73. err:
  74. io_uring_queue_exit(&ring);
  75. return T_EXIT_FAIL;
  76. }