1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Crackerjack Project., 2007
4f08c3bdfSopenharmony_ci * Ported from Crackerjack to LTP by Masatake YAMATO <yamato@redhat.com>
5f08c3bdfSopenharmony_ci * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
6f08c3bdfSopenharmony_ci * Copyright (c) 2017 Xiao Yang <yangx.jy@cn.fujitsu.com>
7f08c3bdfSopenharmony_ci * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
8f08c3bdfSopenharmony_ci */
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci/*\
11f08c3bdfSopenharmony_ci * [Description]
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * Test io_setup invoked via syscall(2):
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * - io_setup fails and returns EFAULT if ctxp is NULL.
16f08c3bdfSopenharmony_ci * - io_setup fails and returns EINVAL if ctxp is not initialized to 0.
17f08c3bdfSopenharmony_ci * - io_setup fails and returns EINVAL if nr_events is -1.
18f08c3bdfSopenharmony_ci * - io_setup fails and returns EAGAIN if nr_events exceeds the limit
19f08c3bdfSopenharmony_ci *   of available events.
20f08c3bdfSopenharmony_ci * - io_setup succeeds if both nr_events and ctxp are valid.
21f08c3bdfSopenharmony_ci */
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#include <linux/aio_abi.h>
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#include "config.h"
26f08c3bdfSopenharmony_ci#include "tst_test.h"
27f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic void run(void)
30f08c3bdfSopenharmony_ci{
31f08c3bdfSopenharmony_ci	aio_context_t ctx;
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	TST_EXP_FAIL(tst_syscall(__NR_io_setup, 1, NULL), EFAULT,
34f08c3bdfSopenharmony_ci		     "io_setup() when ctxp is NULL");
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	memset(&ctx, 1, sizeof(ctx));
37f08c3bdfSopenharmony_ci	TST_EXP_FAIL(tst_syscall(__NR_io_setup, 1, &ctx), EINVAL,
38f08c3bdfSopenharmony_ci		     "io_setup() when ctxp is not initialized to 0");
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	memset(&ctx, 0, sizeof(ctx));
41f08c3bdfSopenharmony_ci	TST_EXP_FAIL(tst_syscall(__NR_io_setup, -1, &ctx), EINVAL,
42f08c3bdfSopenharmony_ci		     "io_setup() when nr_events is -1");
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	unsigned aio_max = 0;
45f08c3bdfSopenharmony_ci	if (!access("/proc/sys/fs/aio-max-nr", F_OK)) {
46f08c3bdfSopenharmony_ci		SAFE_FILE_SCANF("/proc/sys/fs/aio-max-nr", "%u", &aio_max);
47f08c3bdfSopenharmony_ci		TST_EXP_FAIL(tst_syscall(__NR_io_setup, aio_max + 1, &ctx), EAGAIN,
48f08c3bdfSopenharmony_ci			     "io_setup() when nr_events exceeds the limit");
49f08c3bdfSopenharmony_ci	} else {
50f08c3bdfSopenharmony_ci		tst_res(TCONF, "the aio-max-nr file did not exist");
51f08c3bdfSopenharmony_ci	}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	TST_EXP_PASS(tst_syscall(__NR_io_setup, 1, &ctx),
54f08c3bdfSopenharmony_ci		     "io_setup() when both nr_events and ctxp are valid");
55f08c3bdfSopenharmony_ci	TST_EXP_PASS_SILENT(tst_syscall(__NR_io_destroy, ctx));
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic struct tst_test test = {
59f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
60f08c3bdfSopenharmony_ci		"CONFIG_AIO=y",
61f08c3bdfSopenharmony_ci		NULL
62f08c3bdfSopenharmony_ci	},
63f08c3bdfSopenharmony_ci	.test_all = run,
64f08c3bdfSopenharmony_ci};
65