1 /* SPDX-License-Identifier: MIT */
2 /*
3 * io_uring_setup.c
4 *
5 * Description: Unit tests for the io_uring_setup system call.
6 *
7 * Copyright 2019, Red Hat, Inc.
8 * Author: Jeff Moyer <jmoyer@redhat.com>
9 */
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <sys/sysinfo.h>
17 #include "liburing.h"
18 #include "helpers.h"
19
20 #include "../src/syscall.h"
21
22 /* bogus: setup returns a valid fd on success... expect can't predict the
23 fd we'll get, so this really only takes 1 parameter: error */
try_io_uring_setup(unsigned entries,struct io_uring_params * p,int expect)24 static int try_io_uring_setup(unsigned entries, struct io_uring_params *p,
25 int expect)
26 {
27 int ret;
28
29 ret = io_uring_setup(entries, p);
30 if (ret != expect) {
31 fprintf(stderr, "expected %d, got %d\n", expect, ret);
32 /* if we got a valid uring, close it */
33 if (ret > 0)
34 close(ret);
35 return 1;
36 }
37
38 if (expect < 0 && expect != ret) {
39 if (ret == -EPERM && geteuid() != 0) {
40 printf("Needs root, not flagging as an error\n");
41 return 0;
42 }
43 fprintf(stderr, "expected errno %d, got %d\n", expect, ret);
44 return 1;
45 }
46
47 return 0;
48 }
49
main(int argc,char ** argv)50 int main(int argc, char **argv)
51 {
52 int fd;
53 unsigned int status = 0;
54 struct io_uring_params p;
55
56 if (argc > 1)
57 return T_EXIT_SKIP;
58
59 memset(&p, 0, sizeof(p));
60 status |= try_io_uring_setup(0, &p, -EINVAL);
61 status |= try_io_uring_setup(1, NULL, -EFAULT);
62
63 /* resv array is non-zero */
64 memset(&p, 0, sizeof(p));
65 p.resv[0] = p.resv[1] = p.resv[2] = 1;
66 status |= try_io_uring_setup(1, &p, -EINVAL);
67
68 /* invalid flags */
69 memset(&p, 0, sizeof(p));
70 p.flags = ~0U;
71 status |= try_io_uring_setup(1, &p, -EINVAL);
72
73 /* IORING_SETUP_SQ_AFF set but not IORING_SETUP_SQPOLL */
74 memset(&p, 0, sizeof(p));
75 p.flags = IORING_SETUP_SQ_AFF;
76 status |= try_io_uring_setup(1, &p, -EINVAL);
77
78 /* attempt to bind to invalid cpu */
79 memset(&p, 0, sizeof(p));
80 p.flags = IORING_SETUP_SQPOLL | IORING_SETUP_SQ_AFF;
81 p.sq_thread_cpu = get_nprocs_conf();
82 status |= try_io_uring_setup(1, &p, -EINVAL);
83
84 /* I think we can limit a process to a set of cpus. I assume
85 * we shouldn't be able to setup a kernel thread outside of that.
86 * try to do that. (task->cpus_allowed) */
87
88 /* read/write on io_uring_fd */
89 memset(&p, 0, sizeof(p));
90 fd = io_uring_setup(1, &p);
91 if (fd < 0) {
92 fprintf(stderr, "io_uring_setup failed with %d, expected success\n",
93 -fd);
94 status = 1;
95 } else {
96 char buf[4096];
97 int ret;
98 ret = read(fd, buf, 4096);
99 if (ret >= 0) {
100 fprintf(stderr, "read from io_uring fd succeeded. expected fail\n");
101 status = 1;
102 }
103 }
104
105 if (!status)
106 return T_EXIT_PASS;
107
108 fprintf(stderr, "FAIL\n");
109 return T_EXIT_FAIL;
110 }
111