1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: Check that closing a file with SQPOLL has it immediately closed
4 * upon receiving the CQE for the close. The 6.9 kernel had a bug
5 * where SQPOLL would not run kernel wide task_work when running the
6 * private task_work, which would defer the close if this was the
7 * final close of the file.
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 <sys/time.h>
16 #include <sys/wait.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19
20 #include "helpers.h"
21 #include "liburing.h"
22
fill_exec_target(char * dst,char * path)23 static int fill_exec_target(char *dst, char *path)
24 {
25 struct stat sb;
26
27 /*
28 * Should either be ./exec-target.t or test/exec-target.t
29 */
30 sprintf(dst, "%s", path);
31 return stat(dst, &sb);
32 }
33
test_exec(struct io_uring * ring,char * const argv[])34 static int test_exec(struct io_uring *ring, char * const argv[])
35 {
36 char prog_path[PATH_MAX];
37 struct io_uring_sqe *sqe;
38 struct io_uring_cqe *cqe;
39 int ret, wstatus, fd;
40 pid_t p;
41
42 if (fill_exec_target(prog_path, "./exec-target.t") &&
43 fill_exec_target(prog_path, "test/exec-target.t")) {
44 fprintf(stdout, "Can't find exec-target, skipping\n");
45 return 0;
46 }
47
48 sqe = io_uring_get_sqe(ring);
49 io_uring_prep_openat(sqe, AT_FDCWD, prog_path, O_WRONLY, 0);
50 sqe->user_data = 0;
51
52 io_uring_submit(ring);
53
54 ret = io_uring_wait_cqe(ring, &cqe);
55 if (ret) {
56 fprintf(stderr, "wait cqe %d\n", ret);
57 return 1;
58 }
59 if (cqe->res < 0) {
60 fprintf(stderr, "open: %d\n", cqe->res);
61 return 1;
62 }
63 fd = cqe->res;
64 io_uring_cqe_seen(ring, cqe);
65
66 sqe = io_uring_get_sqe(ring);
67 io_uring_prep_close(sqe, fd);
68 sqe->user_data = 1;
69
70 io_uring_submit(ring);
71
72 ret = io_uring_wait_cqe(ring, &cqe);
73 if (ret) {
74 fprintf(stderr, "wait cqe %d\n", ret);
75 return 1;
76 }
77 if (cqe->res < 0) {
78 fprintf(stderr, "close: %d\n", cqe->res);
79 return 1;
80 }
81 io_uring_cqe_seen(ring, cqe);
82
83 p = fork();
84 if (p == -1) {
85 fprintf(stderr, "fork() failed\n");
86 return 1;
87 }
88
89 if (p == 0) {
90 /* file should be closed, try exec'ing it */
91 ret = execve(prog_path, argv, NULL);
92 if (ret) {
93 fprintf(stderr, "exec failed: %s\n", strerror(errno));
94 exit(1);
95 }
96 }
97
98 if (waitpid(p, &wstatus, 0) == (pid_t)-1) {
99 perror("waitpid()");
100 return 1;
101 }
102 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus))
103 return 1;
104
105 return 0;
106 }
107
main(int argc,char * const argv[])108 int main(int argc, char * const argv[])
109 {
110 struct io_uring_params p = { .flags = IORING_SETUP_SQPOLL, };
111 struct io_uring ring;
112 int ret, i;
113
114 if (argc > 1)
115 return T_EXIT_SKIP;
116
117 ret = t_create_ring_params(8, &ring, &p);
118 if (ret == T_SETUP_SKIP)
119 return T_EXIT_SKIP;
120 else if (ret != T_SETUP_OK)
121 return T_EXIT_FAIL;
122
123 for (i = 0; i < 20; i++) {
124 ret = test_exec(&ring, argv);
125 if (ret) {
126 fprintf(stderr, "test_exec failed\n");
127 return ret;
128 }
129 }
130
131 return T_EXIT_PASS;
132 }
133