1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: test a mem leak with IOPOLL
4 */
5 #include <errno.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <fcntl.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include "helpers.h"
14 #include "liburing.h"
15
16 #define FILE_SIZE (128 * 1024)
17 #define BS 4096
18 #define BUFFERS (FILE_SIZE / BS)
19
do_iopoll(const char * fname)20 static int do_iopoll(const char *fname)
21 {
22 struct io_uring_sqe *sqe;
23 struct io_uring ring;
24 struct iovec *iov;
25 int fd;
26
27 fd = open(fname, O_RDONLY | O_DIRECT);
28 if (fd < 0) {
29 perror("open");
30 return T_EXIT_SKIP;
31 }
32
33 iov = t_create_buffers(1, 4096);
34
35 t_create_ring(2, &ring, IORING_SETUP_IOPOLL);
36
37 sqe = io_uring_get_sqe(&ring);
38 io_uring_prep_read(sqe, fd, iov->iov_base, iov->iov_len, 0);
39 io_uring_submit(&ring);
40
41 close(fd);
42 return T_EXIT_PASS;
43 }
44
test(const char * fname)45 static int test(const char *fname)
46 {
47 if (fork()) {
48 int stat;
49
50 wait(&stat);
51 return WEXITSTATUS(stat);
52 } else {
53 int ret;
54
55 ret = do_iopoll(fname);
56 exit(ret);
57 }
58 }
59
main(int argc,char * argv[])60 int main(int argc, char *argv[])
61 {
62 char buf[256];
63 char *fname;
64 int i, ret;
65
66 if (argc > 1) {
67 fname = argv[1];
68 } else {
69 srand((unsigned)time(NULL));
70 snprintf(buf, sizeof(buf), ".iopoll-leak-%u-%u",
71 (unsigned)rand(), (unsigned)getpid());
72 fname = buf;
73 t_create_file(fname, FILE_SIZE);
74 }
75
76 for (i = 0; i < 16; i++) {
77 ret = test(fname);
78 if (ret == T_EXIT_SKIP || ret == T_EXIT_FAIL)
79 break;
80 }
81
82 if (fname != argv[1])
83 unlink(fname);
84 return ret;
85 }
86