hardlink.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test io_uring linkat handling
  5. */
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12. #include "liburing.h"
  13. #include "helpers.h"
  14. static int do_linkat(struct io_uring *ring, int olddirfd, const char *oldname,
  15. const char *newname, int flags)
  16. {
  17. struct io_uring_sqe *sqe;
  18. struct io_uring_cqe *cqe;
  19. int ret;
  20. sqe = io_uring_get_sqe(ring);
  21. if (!sqe) {
  22. fprintf(stderr, "sqe get failed\n");
  23. return 1;
  24. }
  25. io_uring_prep_linkat(sqe, olddirfd, oldname, AT_FDCWD, newname, flags);
  26. ret = io_uring_submit(ring);
  27. if (ret != 1) {
  28. fprintf(stderr, "submit failed: %d\n", ret);
  29. return 1;
  30. }
  31. ret = io_uring_wait_cqes(ring, &cqe, 1, 0, 0);
  32. if (ret) {
  33. fprintf(stderr, "wait_cqe failed: %d\n", ret);
  34. return 1;
  35. }
  36. ret = cqe->res;
  37. io_uring_cqe_seen(ring, cqe);
  38. return ret;
  39. }
  40. static int files_linked_ok(const char* fn1, const char *fn2)
  41. {
  42. struct stat s1, s2;
  43. if (stat(fn1, &s1)) {
  44. fprintf(stderr, "stat(%s): %s\n", fn1, strerror(errno));
  45. return 0;
  46. }
  47. if (stat(fn2, &s2)) {
  48. fprintf(stderr, "stat(%s): %s\n", fn2, strerror(errno));
  49. return 0;
  50. }
  51. if (s1.st_dev != s2.st_dev || s1.st_ino != s2.st_ino) {
  52. fprintf(stderr, "linked files have different device / inode numbers\n");
  53. return 0;
  54. }
  55. if (s1.st_nlink != 2 || s2.st_nlink != 2) {
  56. fprintf(stderr, "linked files have unexpected links count\n");
  57. return 0;
  58. }
  59. return 1;
  60. }
  61. int main(int argc, char *argv[])
  62. {
  63. static const char target[] = "io_uring-linkat-test-target";
  64. static const char emptyname[] = "io_uring-linkat-test-empty";
  65. static const char linkname[] = "io_uring-linkat-test-link";
  66. static const char symlinkname[] = "io_uring-linkat-test-symlink";
  67. struct io_uring ring;
  68. int ret, fd, exit_status = T_EXIT_FAIL;
  69. if (argc > 1)
  70. return T_EXIT_SKIP;
  71. ret = io_uring_queue_init(8, &ring, 0);
  72. if (ret) {
  73. fprintf(stderr, "queue init failed: %d\n", ret);
  74. return ret;
  75. }
  76. ret = fd = open(target, O_CREAT | O_RDWR | O_EXCL, 0600);
  77. if (ret < 0) {
  78. perror("open");
  79. goto out;
  80. }
  81. if (write(fd, "linktest", 8) != 8) {
  82. close(fd);
  83. goto out;
  84. }
  85. if(geteuid()) {
  86. fprintf(stdout, "not root, skipping AT_EMPTY_PATH test\n");
  87. } else {
  88. ret = do_linkat(&ring, fd, "", emptyname, AT_EMPTY_PATH);
  89. if (ret < 0) {
  90. if (ret == -EBADF || ret == -EINVAL) {
  91. fprintf(stdout, "linkat not supported, skipping\n");
  92. exit_status = T_EXIT_SKIP;
  93. goto out;
  94. }
  95. fprintf(stderr, "linkat: %s\n", strerror(-ret));
  96. goto out;
  97. } else if (ret) {
  98. goto out;
  99. }
  100. if (!files_linked_ok(emptyname, target))
  101. goto out;
  102. unlinkat(AT_FDCWD, emptyname, 0);
  103. }
  104. close(fd);
  105. ret = symlink(target, symlinkname);
  106. if (ret < 0) {
  107. perror("open");
  108. goto out;
  109. }
  110. ret = do_linkat(&ring, AT_FDCWD, target, linkname, 0);
  111. if (ret < 0) {
  112. if (ret == -EBADF || ret == -EINVAL) {
  113. fprintf(stdout, "linkat not supported, skipping\n");
  114. exit_status = T_EXIT_SKIP;
  115. goto out;
  116. }
  117. fprintf(stderr, "linkat: %s\n", strerror(-ret));
  118. goto out;
  119. } else if (ret) {
  120. goto out;
  121. }
  122. if (!files_linked_ok(linkname, target))
  123. goto out;
  124. unlinkat(AT_FDCWD, linkname, 0);
  125. ret = do_linkat(&ring, AT_FDCWD, symlinkname, linkname, AT_SYMLINK_FOLLOW);
  126. if (ret < 0) {
  127. fprintf(stderr, "linkat: %s\n", strerror(-ret));
  128. goto out;
  129. } else if (ret) {
  130. goto out;
  131. }
  132. if (!files_linked_ok(symlinkname, target))
  133. goto out;
  134. ret = do_linkat(&ring, AT_FDCWD, target, linkname, 0);
  135. if (ret != -EEXIST) {
  136. fprintf(stderr, "test_linkat linkname already exists failed: %d\n", ret);
  137. goto out;
  138. }
  139. ret = do_linkat(&ring, AT_FDCWD, target, "surely/this/does/not/exist", 0);
  140. if (ret != -ENOENT) {
  141. fprintf(stderr, "test_linkat no parent failed: %d\n", ret);
  142. goto out;
  143. }
  144. exit_status = T_EXIT_PASS;
  145. out:
  146. unlinkat(AT_FDCWD, symlinkname, 0);
  147. unlinkat(AT_FDCWD, linkname, 0);
  148. unlinkat(AT_FDCWD, emptyname, 0);
  149. unlinkat(AT_FDCWD, target, 0);
  150. io_uring_queue_exit(&ring);
  151. return exit_status;
  152. }