self.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test that pathname resolution works from async context when
  5. * using /proc/self/ which should be the original submitting task, not the
  6. * async worker.
  7. *
  8. */
  9. #include <errno.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <fcntl.h>
  15. #include "liburing.h"
  16. static int io_openat2(struct io_uring *ring, const char *path, int dfd)
  17. {
  18. struct io_uring_cqe *cqe;
  19. struct io_uring_sqe *sqe;
  20. struct open_how how;
  21. int ret;
  22. sqe = io_uring_get_sqe(ring);
  23. if (!sqe) {
  24. fprintf(stderr, "get sqe failed\n");
  25. goto err;
  26. }
  27. memset(&how, 0, sizeof(how));
  28. how.flags = O_RDONLY;
  29. io_uring_prep_openat2(sqe, dfd, path, &how);
  30. ret = io_uring_submit(ring);
  31. if (ret <= 0) {
  32. fprintf(stderr, "sqe submit failed: %d\n", ret);
  33. goto err;
  34. }
  35. ret = io_uring_wait_cqe(ring, &cqe);
  36. if (ret < 0) {
  37. fprintf(stderr, "wait completion %d\n", ret);
  38. goto err;
  39. }
  40. ret = cqe->res;
  41. io_uring_cqe_seen(ring, cqe);
  42. return ret;
  43. err:
  44. return -1;
  45. }
  46. int main(int argc, char *argv[])
  47. {
  48. struct io_uring ring;
  49. char buf[64];
  50. int ret;
  51. if (argc > 1)
  52. return 0;
  53. ret = io_uring_queue_init(1, &ring, 0);
  54. if (ret) {
  55. fprintf(stderr, "ring setup failed\n");
  56. return 1;
  57. }
  58. ret = io_openat2(&ring, "/proc/self/comm", -1);
  59. if (ret < 0) {
  60. if (ret == -EOPNOTSUPP)
  61. return 0;
  62. if (ret == -EINVAL) {
  63. fprintf(stdout, "openat2 not supported, skipping\n");
  64. return 0;
  65. }
  66. fprintf(stderr, "openat2 failed: %s\n", strerror(-ret));
  67. return 1;
  68. }
  69. memset(buf, 0, sizeof(buf));
  70. ret = read(ret, buf, sizeof(buf));
  71. if (ret < 0) {
  72. perror("read");
  73. return 1;
  74. }
  75. if (strncmp(buf, "self", 4)) {
  76. fprintf(stderr, "got comm=<%s>, wanted <self>\n", buf);
  77. return 1;
  78. }
  79. return 0;
  80. }