lfs-openat-write.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <sys/resource.h>
  11. #include <unistd.h>
  12. #include "liburing.h"
  13. #include "helpers.h"
  14. static const int RSIZE = 2;
  15. static const int OPEN_FLAGS = O_RDWR | O_CREAT | O_LARGEFILE;
  16. static const mode_t OPEN_MODE = S_IRUSR | S_IWUSR;
  17. #define DIE(...) \
  18. do { \
  19. fprintf(stderr, __VA_ARGS__); \
  20. abort(); \
  21. } while(0)
  22. static int do_write(struct io_uring *ring, int fd, off_t offset)
  23. {
  24. char buf[] = "some test write buf";
  25. struct io_uring_sqe *sqe;
  26. struct io_uring_cqe *cqe;
  27. int res, ret;
  28. sqe = io_uring_get_sqe(ring);
  29. if (!sqe) {
  30. fprintf(stderr, "failed to get sqe\n");
  31. return 1;
  32. }
  33. io_uring_prep_write(sqe, fd, buf, sizeof(buf), offset);
  34. ret = io_uring_submit(ring);
  35. if (ret < 0) {
  36. fprintf(stderr, "failed to submit write: %s\n", strerror(-ret));
  37. return 1;
  38. }
  39. ret = io_uring_wait_cqe(ring, &cqe);
  40. if (ret < 0) {
  41. fprintf(stderr, "wait_cqe failed: %s\n", strerror(-ret));
  42. return 1;
  43. }
  44. res = cqe->res;
  45. io_uring_cqe_seen(ring, cqe);
  46. if (res < 0) {
  47. fprintf(stderr, "write failed: %s\n", strerror(-res));
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. static int test_open_write(struct io_uring *ring, int dfd, const char *fn)
  53. {
  54. struct io_uring_sqe *sqe;
  55. struct io_uring_cqe *cqe;
  56. int ret, fd = -1;
  57. sqe = io_uring_get_sqe(ring);
  58. if (!sqe) {
  59. fprintf(stderr, "failed to get sqe\n");
  60. return 1;
  61. }
  62. io_uring_prep_openat(sqe, dfd, fn, OPEN_FLAGS, OPEN_MODE);
  63. ret = io_uring_submit(ring);
  64. if (ret < 0) {
  65. fprintf(stderr, "failed to submit openat: %s\n", strerror(-ret));
  66. return 1;
  67. }
  68. ret = io_uring_wait_cqe(ring, &cqe);
  69. if (ret < 0) {
  70. fprintf(stderr, "wait_cqe failed: %s\n", strerror(-ret));
  71. return 1;
  72. }
  73. fd = cqe->res;
  74. io_uring_cqe_seen(ring, cqe);
  75. if (fd < 0) {
  76. fprintf(stderr, "openat failed: %s\n", strerror(-fd));
  77. return 1;
  78. }
  79. return do_write(ring, fd, 1ULL << 32);
  80. }
  81. int main(int argc, char *argv[])
  82. {
  83. struct io_uring ring;
  84. int dfd, ret;
  85. if (argc > 1)
  86. return T_EXIT_SKIP;
  87. dfd = open("/tmp", O_RDONLY | O_DIRECTORY);
  88. if (dfd < 0)
  89. DIE("open /tmp: %s\n", strerror(errno));
  90. ret = io_uring_queue_init(RSIZE, &ring, 0);
  91. if (ret < 0)
  92. DIE("failed to init io_uring: %s\n", strerror(-ret));
  93. ret = test_open_write(&ring, dfd, "io_uring_openat_write_test1");
  94. io_uring_queue_exit(&ring);
  95. close(dfd);
  96. unlink("/tmp/io_uring_openat_write_test1");
  97. return ret;
  98. }