accept-test.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: Check to see if accept handles addr and addrlen
  5. */
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <sys/socket.h>
  9. #include <sys/un.h>
  10. #include <assert.h>
  11. #include "liburing.h"
  12. #include "helpers.h"
  13. int main(int argc, char *argv[])
  14. {
  15. struct io_uring_sqe *sqe;
  16. struct io_uring_cqe *cqe;
  17. struct io_uring ring;
  18. struct sockaddr_un addr;
  19. socklen_t addrlen = sizeof(addr);
  20. int ret, fd;
  21. struct __kernel_timespec ts = {
  22. .tv_sec = 0,
  23. .tv_nsec = 1000000
  24. };
  25. if (argc > 1)
  26. return T_EXIT_SKIP;
  27. if (io_uring_queue_init(4, &ring, 0) != 0) {
  28. fprintf(stderr, "ring setup failed\n");
  29. return T_EXIT_FAIL;
  30. }
  31. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  32. assert(fd != -1);
  33. memset(&addr, 0, sizeof(addr));
  34. addr.sun_family = AF_UNIX;
  35. memcpy(addr.sun_path, "\0sock2", 7);
  36. ret = bind(fd, (struct sockaddr *)&addr, addrlen);
  37. assert(ret != -1);
  38. ret = listen(fd, 128);
  39. assert(ret != -1);
  40. sqe = io_uring_get_sqe(&ring);
  41. if (!sqe) {
  42. fprintf(stderr, "get sqe failed\n");
  43. return T_EXIT_FAIL;
  44. }
  45. io_uring_prep_accept(sqe, fd, (struct sockaddr*)&addr, &addrlen, 0);
  46. sqe->user_data = 1;
  47. ret = io_uring_submit(&ring);
  48. if (ret != 1) {
  49. fprintf(stderr, "Got submit %d, expected 1\n", ret);
  50. return T_EXIT_FAIL;
  51. }
  52. ret = io_uring_wait_cqe_timeout(&ring, &cqe, &ts);
  53. if (!ret) {
  54. if (cqe->res == -EBADF || cqe->res == -EINVAL) {
  55. fprintf(stdout, "Accept not supported, skipping\n");
  56. goto skip;
  57. } else if (cqe->res < 0) {
  58. fprintf(stderr, "cqe error %d\n", cqe->res);
  59. goto err;
  60. }
  61. } else if (ret != -ETIME) {
  62. fprintf(stderr, "accept() failed to use addr & addrlen parameters!\n");
  63. return T_EXIT_FAIL;
  64. }
  65. io_uring_queue_exit(&ring);
  66. return T_EXIT_PASS;
  67. skip:
  68. io_uring_queue_exit(&ring);
  69. return T_EXIT_SKIP;
  70. err:
  71. io_uring_queue_exit(&ring);
  72. return T_EXIT_FAIL;
  73. }