pipe-eof.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Test that closed pipe reads returns 0, instead of waiting for more
  5. * data.
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <pthread.h>
  12. #include <string.h>
  13. #include "liburing.h"
  14. #define BUFSIZE 512
  15. struct data {
  16. char *str;
  17. int fds[2];
  18. };
  19. static void *t(void *data)
  20. {
  21. struct data *d = data;
  22. int ret;
  23. strcpy(d->str, "This is a test string");
  24. ret = write(d->fds[1], d->str, strlen(d->str));
  25. close(d->fds[1]);
  26. if (ret < 0)
  27. perror("write");
  28. return NULL;
  29. }
  30. int main(int argc, char *argv[])
  31. {
  32. static char buf[BUFSIZE];
  33. struct io_uring ring;
  34. pthread_t thread;
  35. struct data d;
  36. int ret;
  37. if (pipe(d.fds) < 0) {
  38. perror("pipe");
  39. return 1;
  40. }
  41. d.str = buf;
  42. io_uring_queue_init(8, &ring, 0);
  43. pthread_create(&thread, NULL, t, &d);
  44. while (1) {
  45. struct io_uring_sqe *sqe;
  46. struct io_uring_cqe *cqe;
  47. sqe = io_uring_get_sqe(&ring);
  48. io_uring_prep_read(sqe, d.fds[0], buf, BUFSIZE, 0);
  49. ret = io_uring_submit(&ring);
  50. if (ret != 1) {
  51. fprintf(stderr, "submit: %d\n", ret);
  52. return 1;
  53. }
  54. ret = io_uring_wait_cqe(&ring, &cqe);
  55. if (ret) {
  56. fprintf(stderr, "wait: %d\n", ret);
  57. return 1;
  58. }
  59. if (cqe->res < 0) {
  60. fprintf(stderr, "Read error: %s\n", strerror(-cqe->res));
  61. return 1;
  62. }
  63. if (cqe->res == 0)
  64. break;
  65. io_uring_cqe_seen(&ring, cqe);
  66. }
  67. pthread_join(thread, NULL);
  68. io_uring_queue_exit(&ring);
  69. return 0;
  70. }