shutdown.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. assert(!t_bind_ephemeral_port(recv_s0, &addr));
  42. ret = listen(recv_s0, 128);
  43. assert(ret != -1);
  44. p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
  45. val = 1;
  46. ret = setsockopt(p_fd[1], IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
  47. assert(ret != -1);
  48. int32_t flags = fcntl(p_fd[1], F_GETFL, 0);
  49. assert(flags != -1);
  50. flags |= O_NONBLOCK;
  51. ret = fcntl(p_fd[1], F_SETFL, flags);
  52. assert(ret != -1);
  53. ret = connect(p_fd[1], (struct sockaddr*)&addr, sizeof(addr));
  54. assert(ret == -1);
  55. flags = fcntl(p_fd[1], F_GETFL, 0);
  56. assert(flags != -1);
  57. flags &= ~O_NONBLOCK;
  58. ret = fcntl(p_fd[1], F_SETFL, flags);
  59. assert(ret != -1);
  60. p_fd[0] = accept(recv_s0, NULL, NULL);
  61. assert(p_fd[0] != -1);
  62. signal(SIGPIPE, sig_pipe);
  63. while (1) {
  64. int32_t code;
  65. socklen_t code_len = sizeof(code);
  66. ret = getsockopt(p_fd[1], SOL_SOCKET, SO_ERROR, &code, &code_len);
  67. assert(ret != -1);
  68. if (!code)
  69. break;
  70. }
  71. struct io_uring m_io_uring;
  72. ret = io_uring_queue_init(32, &m_io_uring, 0);
  73. assert(ret >= 0);
  74. {
  75. struct io_uring_cqe *cqe;
  76. struct io_uring_sqe *sqe;
  77. int res;
  78. sqe = io_uring_get_sqe(&m_io_uring);
  79. io_uring_prep_shutdown(sqe, p_fd[1], SHUT_WR);
  80. sqe->user_data = 1;
  81. res = io_uring_submit_and_wait(&m_io_uring, 1);
  82. assert(res != -1);
  83. res = io_uring_wait_cqe(&m_io_uring, &cqe);
  84. if (res < 0) {
  85. fprintf(stderr, "wait: %s\n", strerror(-ret));
  86. goto err;
  87. }
  88. if (cqe->res) {
  89. if (cqe->res == -EINVAL) {
  90. fprintf(stdout, "Shutdown not supported, skipping\n");
  91. goto done;
  92. }
  93. fprintf(stderr, "writev: %d\n", cqe->res);
  94. goto err;
  95. }
  96. io_uring_cqe_seen(&m_io_uring, cqe);
  97. }
  98. {
  99. struct io_uring_cqe *cqe;
  100. struct io_uring_sqe *sqe;
  101. struct iovec iov[1];
  102. char send_buff[128];
  103. int res;
  104. iov[0].iov_base = send_buff;
  105. iov[0].iov_len = sizeof(send_buff);
  106. sqe = io_uring_get_sqe(&m_io_uring);
  107. assert(sqe != NULL);
  108. io_uring_prep_writev(sqe, p_fd[1], iov, 1, 0);
  109. res = io_uring_submit_and_wait(&m_io_uring, 1);
  110. assert(res != -1);
  111. res = io_uring_wait_cqe(&m_io_uring, &cqe);
  112. if (res < 0) {
  113. fprintf(stderr, "wait: %s\n", strerror(-ret));
  114. goto err;
  115. }
  116. if (cqe->res != -EPIPE) {
  117. fprintf(stderr, "writev: %d\n", cqe->res);
  118. goto err;
  119. }
  120. io_uring_cqe_seen(&m_io_uring, cqe);
  121. }
  122. done:
  123. io_uring_queue_exit(&m_io_uring);
  124. return 0;
  125. err:
  126. io_uring_queue_exit(&m_io_uring);
  127. return 1;
  128. }