fifo-nonblock-read.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: Test O_NONBLOCK reading from fifo, should result in proper
  5. * retry and a positive read results. Buggy result would be
  6. * -EAGAIN being returned to the user.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.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. char buf[32];
  19. int fds[2];
  20. int flags;
  21. int ret;
  22. io_uring_queue_init(1, &ring, 0);
  23. if (pipe(fds) < 0) {
  24. perror("pipe");
  25. return T_EXIT_FAIL;
  26. }
  27. flags = fcntl(fds[0], F_GETFL, 0);
  28. if (flags < 0) {
  29. perror("fcntl get");
  30. return T_EXIT_FAIL;
  31. }
  32. flags |= O_NONBLOCK;
  33. ret = fcntl(fds[0], F_SETFL, flags);
  34. if (ret < 0) {
  35. perror("fcntl set");
  36. return T_EXIT_FAIL;
  37. }
  38. sqe = io_uring_get_sqe(&ring);
  39. io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0);
  40. io_uring_submit(&ring);
  41. usleep(10000);
  42. ret = write(fds[1], "Hello\n", 6);
  43. if (ret < 0) {
  44. perror("pipe write");
  45. return T_EXIT_FAIL;
  46. }
  47. ret = io_uring_wait_cqe(&ring, &cqe);
  48. if (ret < 0) {
  49. fprintf(stderr, "wait=%d\n", ret);
  50. return T_EXIT_FAIL;
  51. }
  52. if (cqe->res < 0) {
  53. fprintf(stderr, "cqe res %d\n", cqe->res);
  54. return T_EXIT_FAIL;
  55. }
  56. io_uring_cqe_seen(&ring, cqe);
  57. io_uring_queue_exit(&ring);
  58. return T_EXIT_PASS;
  59. }