xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/io_setup/io_setup02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 <[email protected]>
5  * Copyright (c) 2011 Cyril Hrubis <[email protected]>
6  * Copyright (c) 2017 Xiao Yang <[email protected]>
7  * Copyright (c) 2021 Xie Ziyao <[email protected]>
8  */
9 
10 /*\
11  * [Description]
12  *
13  * Test io_setup invoked via syscall(2):
14  *
15  * - io_setup fails and returns EFAULT if ctxp is NULL.
16  * - io_setup fails and returns EINVAL if ctxp is not initialized to 0.
17  * - io_setup fails and returns EINVAL if nr_events is -1.
18  * - io_setup fails and returns EAGAIN if nr_events exceeds the limit
19  *   of available events.
20  * - io_setup succeeds if both nr_events and ctxp are valid.
21  */
22 
23 #include <linux/aio_abi.h>
24 
25 #include "config.h"
26 #include "tst_test.h"
27 #include "lapi/syscalls.h"
28 
run(void)29 static void run(void)
30 {
31 	aio_context_t ctx;
32 
33 	TST_EXP_FAIL(tst_syscall(__NR_io_setup, 1, NULL), EFAULT,
34 		     "io_setup() when ctxp is NULL");
35 
36 	memset(&ctx, 1, sizeof(ctx));
37 	TST_EXP_FAIL(tst_syscall(__NR_io_setup, 1, &ctx), EINVAL,
38 		     "io_setup() when ctxp is not initialized to 0");
39 
40 	memset(&ctx, 0, sizeof(ctx));
41 	TST_EXP_FAIL(tst_syscall(__NR_io_setup, -1, &ctx), EINVAL,
42 		     "io_setup() when nr_events is -1");
43 
44 	unsigned aio_max = 0;
45 	if (!access("/proc/sys/fs/aio-max-nr", F_OK)) {
46 		SAFE_FILE_SCANF("/proc/sys/fs/aio-max-nr", "%u", &aio_max);
47 		TST_EXP_FAIL(tst_syscall(__NR_io_setup, aio_max + 1, &ctx), EAGAIN,
48 			     "io_setup() when nr_events exceeds the limit");
49 	} else {
50 		tst_res(TCONF, "the aio-max-nr file did not exist");
51 	}
52 
53 	TST_EXP_PASS(tst_syscall(__NR_io_setup, 1, &ctx),
54 		     "io_setup() when both nr_events and ctxp are valid");
55 	TST_EXP_PASS_SILENT(tst_syscall(__NR_io_destroy, ctx));
56 }
57 
58 static struct tst_test test = {
59 	.needs_kconfigs = (const char *[]) {
60 		"CONFIG_AIO=y",
61 		NULL
62 	},
63 	.test_all = run,
64 };
65