files-exit-hang-poll.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Based on a test case from Josef Grieb - test that we can exit without
  5. * hanging if we have the task file table pinned by a request that is linked
  6. * to another request that doesn't finish.
  7. */
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <netinet/in.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <strings.h>
  15. #include <sys/socket.h>
  16. #include <unistd.h>
  17. #include <poll.h>
  18. #include "liburing.h"
  19. #include "helpers.h"
  20. #define BACKLOG 512
  21. static struct io_uring ring;
  22. static void add_poll(struct io_uring *ring, int fd)
  23. {
  24. struct io_uring_sqe *sqe;
  25. sqe = io_uring_get_sqe(ring);
  26. io_uring_prep_poll_add(sqe, fd, POLLIN);
  27. sqe->flags |= IOSQE_IO_LINK;
  28. }
  29. static void add_accept(struct io_uring *ring, int fd)
  30. {
  31. struct io_uring_sqe *sqe;
  32. sqe = io_uring_get_sqe(ring);
  33. io_uring_prep_accept(sqe, fd, 0, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
  34. }
  35. static int setup_io_uring(void)
  36. {
  37. int ret;
  38. ret = io_uring_queue_init(16, &ring, 0);
  39. if (ret) {
  40. fprintf(stderr, "Unable to setup io_uring: %s\n", strerror(-ret));
  41. return 1;
  42. }
  43. return 0;
  44. }
  45. static void alarm_sig(int sig)
  46. {
  47. exit(0);
  48. }
  49. int main(int argc, char *argv[])
  50. {
  51. struct sockaddr_in serv_addr;
  52. struct io_uring_cqe *cqe;
  53. int ret, sock_listen_fd;
  54. const int val = 1;
  55. if (argc > 1)
  56. return T_EXIT_SKIP;
  57. sock_listen_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
  58. if (sock_listen_fd < 0) {
  59. perror("socket");
  60. return T_EXIT_FAIL;
  61. }
  62. setsockopt(sock_listen_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  63. memset(&serv_addr, 0, sizeof(serv_addr));
  64. serv_addr.sin_family = AF_INET;
  65. serv_addr.sin_addr.s_addr = INADDR_ANY;
  66. if (t_bind_ephemeral_port(sock_listen_fd, &serv_addr)) {
  67. perror("bind");
  68. return T_EXIT_FAIL;
  69. }
  70. if (listen(sock_listen_fd, BACKLOG) < 0) {
  71. perror("Error listening on socket\n");
  72. return T_EXIT_FAIL;
  73. }
  74. if (setup_io_uring())
  75. return T_EXIT_FAIL;
  76. add_poll(&ring, sock_listen_fd);
  77. add_accept(&ring, sock_listen_fd);
  78. ret = io_uring_submit(&ring);
  79. if (ret != 2) {
  80. fprintf(stderr, "submit=%d\n", ret);
  81. return T_EXIT_FAIL;
  82. }
  83. signal(SIGALRM, alarm_sig);
  84. alarm(1);
  85. ret = io_uring_wait_cqe(&ring, &cqe);
  86. if (ret) {
  87. fprintf(stderr, "wait_cqe=%d\n", ret);
  88. return T_EXIT_FAIL;
  89. }
  90. io_uring_queue_exit(&ring);
  91. return T_EXIT_PASS;
  92. }