shutdown.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Check that writev on a socket that has been shutdown(2) fails
  5. *
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <sys/socket.h>
  16. #include <sys/un.h>
  17. #include <netinet/tcp.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. #include "liburing.h"
  21. #include "helpers.h"
  22. static void sig_pipe(int sig)
  23. {
  24. }
  25. int main(int argc, char *argv[])
  26. {
  27. int p_fd[2], ret;
  28. int32_t recv_s0;
  29. int32_t val = 1;
  30. struct sockaddr_in addr = { };
  31. if (argc > 1)
  32. return 0;
  33. srand(getpid());
  34. recv_s0 = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
  35. ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
  36. assert(ret != -1);
  37. ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  38. assert(ret != -1);
  39. addr.sin_family = AF_INET;
  40. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  41. ret = t_bind_ephemeral_port(recv_s0, &addr);
  42. assert(!ret);
  43. ret = listen(recv_s0, 128);
  44. assert(ret != -1);
  45. p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
  46. val = 1;
  47. ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
  48. assert(ret != -1);
  49. int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
  50. assert(flags != -1);
  51. flags |= O_NONBLOCK;
  52. ret = fcntl(p_fd[1], F_SETFL, flags);
  53. assert(ret != -1);
  54. ret = connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr));
  55. assert(ret == -1);
  56. flags = fcntl(p_fd[1], F_GETFL, 0);
  57. assert(flags != -1);
  58. flags &= ~O_NONBLOCK;
  59. ret = fcntl(p_fd[1], F_SETFL, flags);
  60. assert(ret != -1);
  61. p_fd[0] = accept(recv_s0, NULL, NULL);
  62. assert(p_fd[0] != -1);
  63. signal(SIGPIPE, sig_pipe);
  64. while (1) {
  65. int32_t code;
  66. socklen_t code_len = sizeof(code);
  67. ret = getsockopt(p_fd[1], SOL_SOCKET, SO_ERROR, &code, &code_len);
  68. assert(ret != -1);
  69. if (!code)
  70. break;
  71. }
  72. struct io_uring m_io_uring;
  73. ret = io_uring_queue_init(32, &m_io_uring, 0);
  74. assert(ret >= 0);
  75. {
  76. struct io_uring_cqe *cqe;
  77. struct io_uring_sqe *sqe;
  78. int res;
  79. sqe = io_uring_get_sqe(&m_io_uring);
  80. io_uring_prep_shutdown(sqe, p_fd[1], SHUT_WR);
  81. sqe->user_data = 1;
  82. res = io_uring_submit_and_wait(&m_io_uring, 1);
  83. assert(res != -1);
  84. res = io_uring_wait_cqe(&m_io_uring, &cqe);
  85. if (res < 0) {
  86. fprintf(stderr, "wait: %s\n", strerror(-ret));
  87. goto err;
  88. }
  89. if (cqe->res) {
  90. if (cqe->res == -EINVAL) {
  91. fprintf(stdout, "Shutdown not supported, skipping\n");
  92. goto done;
  93. }
  94. fprintf(stderr, "writev: %d\n", cqe->res);
  95. goto err;
  96. }
  97. io_uring_cqe_seen(&m_io_uring, cqe);
  98. }
  99. {
  100. struct io_uring_cqe *cqe;
  101. struct io_uring_sqe *sqe;
  102. struct iovec iov[1];
  103. char send_buff[128];
  104. int res;
  105. iov[0].iov_base = send_buff;
  106. iov[0].iov_len = sizeof(send_buff);
  107. sqe = io_uring_get_sqe(&m_io_uring);
  108. assert(sqe != NULL);
  109. io_uring_prep_writev(sqe, p_fd[1], iov, 1, 0);
  110. res = io_uring_submit_and_wait(&m_io_uring, 1);
  111. assert(res != -1);
  112. res = io_uring_wait_cqe(&m_io_uring, &cqe);
  113. if (res < 0) {
  114. fprintf(stderr, "wait: %s\n", strerror(-ret));
  115. goto err;
  116. }
  117. if (cqe->res != -EPIPE) {
  118. fprintf(stderr, "writev: %d\n", cqe->res);
  119. goto err;
  120. }
  121. io_uring_cqe_seen(&m_io_uring, cqe);
  122. }
  123. done:
  124. io_uring_queue_exit(&m_io_uring);
  125. return 0;
  126. err:
  127. io_uring_queue_exit(&m_io_uring);
  128. return 1;
  129. }