xref: /aosp_15_r20/external/liburing/test/self.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * 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.
6  *
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 
io_openat2(struct io_uring * ring,const char * path,int dfd)17 static int io_openat2(struct io_uring *ring, const char *path, int dfd)
18 {
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;
47 err:
48 	return -1;
49 }
50 
main(int argc,char * argv[])51 int main(int argc, char *argv[])
52 {
53 	struct io_uring ring;
54 	char buf[64];
55 	int ret;
56 
57 	if (argc > 1)
58 		return 0;
59 
60 	ret = io_uring_queue_init(1, &ring, 0);
61 	if (ret) {
62 		fprintf(stderr, "ring setup failed\n");
63 		return 1;
64 	}
65 
66 	ret = io_openat2(&ring, "/proc/self/comm", -1);
67 	if (ret < 0) {
68 		if (ret == -EOPNOTSUPP)
69 			return 0;
70 		if (ret == -EINVAL) {
71 			fprintf(stdout, "openat2 not supported, skipping\n");
72 			return 0;
73 		}
74 		fprintf(stderr, "openat2 failed: %s\n", strerror(-ret));
75 		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 }
92