blob: 422c9e3baf3ae1606fd74a437958718f123bebab [file] [log] [blame]
Jens Axboed7e4f942020-10-30 21:33:22 -06001/* SPDX-License-Identifier: MIT */
2/*
Jens Axboebd62cfb2020-10-30 21:42:33 -06003 * Description: test that pathname resolution works from async context when
4 * using /proc/self/ which should be the original submitting task, not the
5 * async worker.
Jens Axboed7e4f942020-10-30 21:33:22 -06006 *
7 */
8#include <errno.h>
9#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <string.h>
13#include <fcntl.h>
14
15#include "liburing.h"
16
Jens Axboebd62cfb2020-10-30 21:42:33 -060017static int io_openat2(struct io_uring *ring, const char *path, int dfd)
Jens Axboed7e4f942020-10-30 21:33:22 -060018{
19 struct io_uring_cqe *cqe;
20 struct io_uring_sqe *sqe;
21 struct open_how how;
22 int ret;
23
24 sqe = io_uring_get_sqe(ring);
25 if (!sqe) {
26 fprintf(stderr, "get sqe failed\n");
27 goto err;
28 }
29 memset(&how, 0, sizeof(how));
30 how.flags = O_RDONLY;
31 io_uring_prep_openat2(sqe, dfd, path, &how);
32
33 ret = io_uring_submit(ring);
34 if (ret <= 0) {
35 fprintf(stderr, "sqe submit failed: %d\n", ret);
36 goto err;
37 }
38
39 ret = io_uring_wait_cqe(ring, &cqe);
40 if (ret < 0) {
41 fprintf(stderr, "wait completion %d\n", ret);
42 goto err;
43 }
44 ret = cqe->res;
45 io_uring_cqe_seen(ring, cqe);
46 return ret;
47err:
48 return -1;
49}
50
51int main(int argc, char *argv[])
52{
53 struct io_uring ring;
54 char buf[64];
55 int ret;
56
Jens Axboed2471092021-01-21 06:05:01 -070057 if (argc > 1)
58 return 0;
59
Jens Axboebd62cfb2020-10-30 21:42:33 -060060 ret = io_uring_queue_init(1, &ring, 0);
Jens Axboed7e4f942020-10-30 21:33:22 -060061 if (ret) {
62 fprintf(stderr, "ring setup failed\n");
63 return 1;
64 }
65
Jens Axboebd62cfb2020-10-30 21:42:33 -060066 ret = io_openat2(&ring, "/proc/self/comm", -1);
Jens Axboed7e4f942020-10-30 21:33:22 -060067 if (ret < 0) {
Jens Axboed2471092021-01-21 06:05:01 -070068 if (ret == -EOPNOTSUPP)
69 return 0;
Jens Axboed7e4f942020-10-30 21:33:22 -060070 if (ret == -EINVAL) {
71 fprintf(stdout, "openat2 not supported, skipping\n");
72 return 0;
73 }
Jens Axboebd62cfb2020-10-30 21:42:33 -060074 fprintf(stderr, "openat2 failed: %s\n", strerror(-ret));
Jens Axboed7e4f942020-10-30 21:33:22 -060075 return 1;
76 }
77
78 memset(buf, 0, sizeof(buf));
79 ret = read(ret, buf, sizeof(buf));
80 if (ret < 0) {
81 perror("read");
82 return 1;
83 }
84
85 if (strncmp(buf, "self", 4)) {
86 fprintf(stderr, "got comm=<%s>, wanted <self>\n", buf);
87 return 1;
88 }
89
90 return 0;
91}