1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Crackerjack Project., 2007
4 * Ported from Crackerjack to LTP by Masatake YAMATO <yamato@redhat.com>
5 * Copyright (c) 2011-2017 Cyril Hrubis <chrubis@suse.cz>
6 * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Test io_submit invoked via syscall(2):
13 *
14 * 1. io_submit fails and returns EINVAL if ctx is invalid.
15 * 2. io_submit fails and returns EINVAL if nr is invalid.
16 * 3. io_submit fails and returns EFAULT if iocbpp pointer is invalid.
17 * 4. io_submit fails and returns EBADF if fd is invalid.
18 */
19
20 #include <linux/aio_abi.h>
21
22 #include "config.h"
23 #include "tst_test.h"
24 #include "lapi/syscalls.h"
25
26 #define RDONLY_FILE "rdonly_file"
27 #define WRONLY_FILE "wronly_file"
28 #define MODE 0777
29
30 static char buf[100];
31 static aio_context_t ctx;
32 static aio_context_t invalid_ctx;
33
34 static struct iocb iocb;
35 static struct iocb *iocbs[] = {&iocb};
36
37 static struct iocb inv_fd_iocb;
38 static struct iocb *inv_fd_iocbs[] = {&inv_fd_iocb};
39
40 static int rdonly_fd;
41 static struct iocb rdonly_fd_iocb;
42 static struct iocb *rdonly_fd_iocbs[] = {&rdonly_fd_iocb};
43
44 static int wronly_fd;
45 static struct iocb wronly_fd_iocb;
46 static struct iocb *wronly_fd_iocbs[] = {&wronly_fd_iocb};
47
48 static struct iocb *zero_iocbs[1];
49
50 static struct tcase {
51 aio_context_t *ctx;
52 long nr;
53 struct iocb **iocbs;
54 int exp_errno;
55 const char *desc;
56 } tc[] = {
57 /* Invalid ctx */
58 {&invalid_ctx, 1, iocbs, EINVAL, "invalid ctx"},
59 /* Invalid nr */
60 {&ctx, -1, iocbs, EINVAL, "invalid nr"},
61 /* Invalid pointer */
62 {&ctx, 1, (void*)-1, EFAULT, "invalid iocbpp pointer"},
63 {&ctx, 1, zero_iocbs, EFAULT, "NULL iocb pointers"},
64 /* Invalid fd */
65 {&ctx, 1, inv_fd_iocbs, EBADF, "invalid fd"},
66 {&ctx, 1, rdonly_fd_iocbs, EBADF, "readonly fd for write"},
67 {&ctx, 1, wronly_fd_iocbs, EBADF, "writeonly fd for read"},
68 };
69
io_prep_option(struct iocb * cb,int fd,void * buf,size_t count,long long offset,unsigned opcode)70 static inline void io_prep_option(struct iocb *cb, int fd, void *buf,
71 size_t count, long long offset, unsigned opcode)
72 {
73 memset(cb, 0, sizeof(*cb));
74 cb->aio_fildes = fd;
75 cb->aio_lio_opcode = opcode;
76 cb->aio_buf = (uint64_t)buf;
77 cb->aio_offset = offset;
78 cb->aio_nbytes = count;
79 }
80
setup(void)81 static void setup(void)
82 {
83 TST_EXP_PASS_SILENT(tst_syscall(__NR_io_setup, 1, &ctx));
84 io_prep_option(&inv_fd_iocb, -1, buf, sizeof(buf), 0, IOCB_CMD_PREAD);
85
86 rdonly_fd = SAFE_OPEN(RDONLY_FILE, O_RDONLY | O_CREAT, MODE);
87 io_prep_option(&rdonly_fd_iocb, rdonly_fd, buf, sizeof(buf), 0, IOCB_CMD_PWRITE);
88
89 wronly_fd = SAFE_OPEN(WRONLY_FILE, O_WRONLY | O_CREAT, MODE);
90 io_prep_option(&wronly_fd_iocb, wronly_fd, buf, sizeof(buf), 0, IOCB_CMD_PREAD);
91 }
92
cleanup(void)93 static void cleanup(void)
94 {
95 if (rdonly_fd > 0)
96 SAFE_CLOSE(rdonly_fd);
97 if (wronly_fd > 0)
98 SAFE_CLOSE(wronly_fd);
99
100 if (tst_syscall(__NR_io_destroy, ctx))
101 tst_brk(TBROK | TERRNO, "io_destroy() failed");
102 }
103
run(unsigned int i)104 static void run(unsigned int i)
105 {
106 TST_EXP_FAIL2(tst_syscall(__NR_io_submit, *tc[i].ctx, tc[i].nr, tc[i].iocbs),
107 tc[i].exp_errno, "io_submit() with %s", tc[i].desc);
108 }
109
110 static struct tst_test test = {
111 .tcnt = ARRAY_SIZE(tc),
112 .needs_tmpdir = 1,
113 .needs_kconfigs = (const char *[]) {
114 "CONFIG_AIO=y",
115 NULL
116 },
117 .setup = setup,
118 .cleanup = cleanup,
119 .test = run,
120 };
121