close-opath.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "../config-host.h"
  2. // SPDX-License-Identifier: MIT
  3. #define _GNU_SOURCE 1
  4. #define _FILE_OFFSET_BITS 64
  5. // Test program for io_uring IORING_OP_CLOSE with O_PATH file.
  6. // Author: Clayton Harris <bugs@claycon.org>, 2020-06-07
  7. // linux 5.6.14-300.fc32.x86_64
  8. // gcc 10.1.1-1.fc32
  9. // liburing.x86_64 0.5-1.fc32
  10. // gcc -O2 -Wall -Wextra -std=c11 -o close_opath close_opath.c -luring
  11. // ./close_opath testfilepath
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <sys/stat.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "liburing.h"
  19. typedef struct
  20. {
  21. const char *const flnames;
  22. const int oflags;
  23. } oflgs_t;
  24. static int test_io_uring_close(struct io_uring *ring, int fd)
  25. {
  26. struct io_uring_sqe *sqe;
  27. struct io_uring_cqe *cqe;
  28. int ret;
  29. sqe = io_uring_get_sqe(ring);
  30. if (!sqe) {
  31. fprintf(stderr, "io_uring_get_sqe() failed\n");
  32. return -ENOENT;
  33. }
  34. io_uring_prep_close(sqe, fd);
  35. ret = io_uring_submit(ring);
  36. if (ret < 0) {
  37. fprintf(stderr, "io_uring_submit() failed, errno %d: %s\n",
  38. -ret, strerror(-ret));
  39. return ret;
  40. }
  41. ret = io_uring_wait_cqe(ring, &cqe);
  42. if (ret < 0) {
  43. fprintf(stderr, "io_uring_wait_cqe() failed, errno %d: %s\n",
  44. -ret, strerror(-ret));
  45. return ret;
  46. }
  47. ret = cqe->res;
  48. io_uring_cqe_seen(ring, cqe);
  49. if (ret < 0 && ret != -EOPNOTSUPP && ret != -EINVAL && ret != -EBADF) {
  50. fprintf(stderr, "io_uring close() failed, errno %d: %s\n",
  51. -ret, strerror(-ret));
  52. return ret;
  53. }
  54. return 0;
  55. }
  56. static int open_file(const char *path, const oflgs_t *oflgs)
  57. {
  58. int fd;
  59. fd = openat(AT_FDCWD, path, oflgs->oflags, 0);
  60. if (fd < 0) {
  61. int err = errno;
  62. fprintf(stderr, "openat(%s, %s) failed, errno %d: %s\n",
  63. path, oflgs->flnames, err, strerror(err));
  64. return -err;
  65. }
  66. return fd;
  67. }
  68. int main(int argc, char *argv[])
  69. {
  70. const char *fname = ".";
  71. struct io_uring ring;
  72. int ret, i;
  73. static const oflgs_t oflgs[] = {
  74. { "O_RDONLY", O_RDONLY },
  75. { "O_PATH", O_PATH }
  76. };
  77. ret = io_uring_queue_init(2, &ring, 0);
  78. if (ret < 0) {
  79. fprintf(stderr, "io_uring_queue_init() failed, errno %d: %s\n",
  80. -ret, strerror(-ret));
  81. return 0x02;
  82. }
  83. #define OFLGS_SIZE (sizeof(oflgs) / sizeof(oflgs[0]))
  84. ret = 0;
  85. for (i = 0; i < OFLGS_SIZE; i++) {
  86. int fd;
  87. fd = open_file(fname, &oflgs[i]);
  88. if (fd < 0) {
  89. ret |= 0x02;
  90. break;
  91. }
  92. /* Should always succeed */
  93. if (test_io_uring_close(&ring, fd) < 0)
  94. ret |= 0x04 << i;
  95. }
  96. #undef OFLGS_SIZE
  97. io_uring_queue_exit(&ring);
  98. return ret;
  99. }