• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: MIT
2 
3 #define _GNU_SOURCE 1
4 #define _FILE_OFFSET_BITS 64
5 
6 // Test program for io_uring IORING_OP_CLOSE with O_PATH file.
7 // Author: Clayton Harris <bugs@claycon.org>, 2020-06-07
8 
9 // linux                5.6.14-300.fc32.x86_64
10 // gcc                  10.1.1-1.fc32
11 // liburing.x86_64      0.5-1.fc32
12 
13 // gcc -O2 -Wall -Wextra -std=c11 -o close_opath close_opath.c -luring
14 // ./close_opath testfilepath
15 
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include "liburing.h"
24 
25 typedef struct
26 {
27 	const char *const flnames;
28 	const int oflags;
29 } oflgs_t;
30 
test_io_uring_close(struct io_uring * ring,int fd)31 static int test_io_uring_close(struct io_uring *ring, int fd)
32 {
33 	struct io_uring_sqe *sqe;
34 	struct io_uring_cqe *cqe;
35 	int ret;
36 
37 	sqe = io_uring_get_sqe(ring);
38 	if (!sqe) {
39 		fprintf(stderr, "io_uring_get_sqe() failed\n");
40 		return -ENOENT;
41 	}
42 
43 	io_uring_prep_close(sqe, fd);
44 
45 	ret = io_uring_submit(ring);
46 	if (ret < 0) {
47 		fprintf(stderr, "io_uring_submit() failed, errno %d: %s\n",
48 			-ret, strerror(-ret));
49 		return ret;
50 	}
51 
52 	ret = io_uring_wait_cqe(ring, &cqe);
53 	if (ret < 0) {
54 		fprintf(stderr, "io_uring_wait_cqe() failed, errno %d: %s\n",
55 			-ret, strerror(-ret));
56 		return ret;
57 	}
58 
59 	ret = cqe->res;
60 	io_uring_cqe_seen(ring, cqe);
61 
62 	if (ret < 0 && ret != -EOPNOTSUPP && ret != -EINVAL && ret != -EBADF) {
63 		fprintf(stderr, "io_uring close() failed, errno %d: %s\n",
64 			-ret, strerror(-ret));
65 		return ret;
66 	}
67 
68 	return 0;
69 }
70 
open_file(const char * path,const oflgs_t * oflgs)71 static int open_file(const char *path, const oflgs_t *oflgs)
72 {
73 	int fd;
74 
75 	fd = openat(AT_FDCWD, path, oflgs->oflags, 0);
76 	if (fd < 0) {
77 		int err = errno;
78 		fprintf(stderr, "openat(%s, %s) failed, errno %d: %s\n",
79 			path, oflgs->flnames, err, strerror(err));
80 		return -err;
81 	}
82 
83 	return fd;
84 }
85 
main(int argc,char * argv[])86 int main(int argc, char *argv[])
87 {
88 	const char *fname = ".";
89 	struct io_uring ring;
90 	int ret, i;
91 	static const oflgs_t oflgs[] = {
92 		{ "O_RDONLY", O_RDONLY },
93 		{ "O_PATH", O_PATH }
94 	};
95 
96 	ret = io_uring_queue_init(2, &ring, 0);
97 	if (ret < 0) {
98 		fprintf(stderr, "io_uring_queue_init() failed, errno %d: %s\n",
99 			-ret, strerror(-ret));
100 		return 0x02;
101 	}
102 
103 #define OFLGS_SIZE (sizeof(oflgs) / sizeof(oflgs[0]))
104 
105 	ret = 0;
106 	for (i = 0; i < OFLGS_SIZE; i++) {
107 		int fd;
108 
109 		fd = open_file(fname, &oflgs[i]);
110 		if (fd < 0) {
111 			ret |= 0x02;
112 			break;
113 		}
114 
115 		/* Should always succeed */
116 		if (test_io_uring_close(&ring, fd) < 0)
117 			ret |= 0x04 << i;
118 	}
119 #undef OFLGS_SIZE
120 
121 	io_uring_queue_exit(&ring);
122 	return ret;
123 }
124