symlink.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test io_uring symlinkat 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. static int do_symlinkat(struct io_uring *ring, const char *oldname, const char *newname)
  14. {
  15. int ret;
  16. struct io_uring_sqe *sqe;
  17. struct io_uring_cqe *cqe;
  18. sqe = io_uring_get_sqe(ring);
  19. if (!sqe) {
  20. fprintf(stderr, "sqe get failed\n");
  21. goto err;
  22. }
  23. io_uring_prep_symlinkat(sqe, oldname, AT_FDCWD, newname);
  24. ret = io_uring_submit(ring);
  25. if (ret != 1) {
  26. fprintf(stderr, "submit failed: %d\n", ret);
  27. goto err;
  28. }
  29. ret = io_uring_wait_cqes(ring, &cqe, 1, 0, 0);
  30. if (ret) {
  31. fprintf(stderr, "wait_cqe failed: %d\n", ret);
  32. goto err;
  33. }
  34. ret = cqe->res;
  35. io_uring_cqe_seen(ring, cqe);
  36. return ret;
  37. err:
  38. return 1;
  39. }
  40. static int test_link_contents(const char* linkname,
  41. const char *expected_contents)
  42. {
  43. char buf[128];
  44. int ret = readlink(linkname, buf, 127);
  45. if (ret < 0) {
  46. perror("readlink");
  47. return ret;
  48. }
  49. buf[ret] = 0;
  50. if (strncmp(buf, expected_contents, 128)) {
  51. fprintf(stderr, "link contents differs from expected: '%s' vs '%s'",
  52. buf, expected_contents);
  53. return -1;
  54. }
  55. return 0;
  56. }
  57. int main(int argc, char *argv[])
  58. {
  59. static const char target[] = "io_uring-symlinkat-test-target";
  60. static const char linkname[] = "io_uring-symlinkat-test-link";
  61. int ret;
  62. struct io_uring ring;
  63. if (argc > 1)
  64. return 0;
  65. ret = io_uring_queue_init(8, &ring, 0);
  66. if (ret) {
  67. fprintf(stderr, "queue init failed: %d\n", ret);
  68. return ret;
  69. }
  70. ret = do_symlinkat(&ring, target, linkname);
  71. if (ret < 0) {
  72. if (ret == -EBADF || ret == -EINVAL) {
  73. fprintf(stdout, "symlinkat not supported, skipping\n");
  74. goto out;
  75. }
  76. fprintf(stderr, "symlinkat: %s\n", strerror(-ret));
  77. goto err;
  78. } else if (ret) {
  79. goto err;
  80. }
  81. ret = test_link_contents(linkname, target);
  82. if (ret < 0)
  83. goto err1;
  84. ret = do_symlinkat(&ring, target, linkname);
  85. if (ret != -EEXIST) {
  86. fprintf(stderr, "test_symlinkat linkname already exists failed: %d\n", ret);
  87. goto err1;
  88. }
  89. ret = do_symlinkat(&ring, target, "surely/this/does/not/exist");
  90. if (ret != -ENOENT) {
  91. fprintf(stderr, "test_symlinkat no parent failed: %d\n", ret);
  92. goto err1;
  93. }
  94. ret = do_symlinkat(&ring, target, (const char *) (uintptr_t) 0x1234);
  95. if (ret != -EFAULT) {
  96. fprintf(stderr, "test_symlinkat bad target failed: %d\n", ret);
  97. goto err1;
  98. }
  99. ret = do_symlinkat(&ring, (const char *) (uintptr_t) 0x1234, target);
  100. if (ret != -EFAULT) {
  101. fprintf(stderr, "test_symlinkat bad source failed: %d\n", ret);
  102. goto err1;
  103. }
  104. out:
  105. unlinkat(AT_FDCWD, linkname, 0);
  106. io_uring_queue_exit(&ring);
  107. return 0;
  108. err1:
  109. unlinkat(AT_FDCWD, linkname, 0);
  110. err:
  111. io_uring_queue_exit(&ring);
  112. return 1;
  113. }