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 */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci/*\
10f08c3bdfSopenharmony_ci * [Description]
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Test io_setup invoked via libaio:
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * - io_setup succeeds if both nr_events and ctxp are valid.
15f08c3bdfSopenharmony_ci * - io_setup fails and returns -EINVAL if ctxp is not initialized to 0.
16f08c3bdfSopenharmony_ci * - io_setup fails and returns -EINVAL if nr_events is invalid.
17f08c3bdfSopenharmony_ci * - io_setup fails and returns -EFAULT if ctxp is NULL.
18f08c3bdfSopenharmony_ci * - io_setup fails and returns -EAGAIN if nr_events exceeds the limit
19f08c3bdfSopenharmony_ci *   1of available events.
20f08c3bdfSopenharmony_ci */
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include <errno.h>
23f08c3bdfSopenharmony_ci#include <string.h>
24f08c3bdfSopenharmony_ci#include <unistd.h>
25f08c3bdfSopenharmony_ci#include "config.h"
26f08c3bdfSopenharmony_ci#include "tst_test.h"
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#ifdef HAVE_LIBAIO
29f08c3bdfSopenharmony_ci#include <libaio.h>
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic void verify_failure(unsigned int nr, io_context_t *ctx, int init_val, long exp_err)
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	if (ctx)
34f08c3bdfSopenharmony_ci		memset(ctx, init_val, sizeof(*ctx));
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	TEST(io_setup(nr, ctx));
37f08c3bdfSopenharmony_ci	if (TST_RET == 0) {
38f08c3bdfSopenharmony_ci		tst_res(TFAIL, "io_setup() passed unexpectedly");
39f08c3bdfSopenharmony_ci		io_destroy(*ctx);
40f08c3bdfSopenharmony_ci		return;
41f08c3bdfSopenharmony_ci	}
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	if (TST_RET == -exp_err) {
44f08c3bdfSopenharmony_ci		tst_res(TPASS, "io_setup() failed as expected, returned -%s",
45f08c3bdfSopenharmony_ci			tst_strerrno(exp_err));
46f08c3bdfSopenharmony_ci	} else {
47f08c3bdfSopenharmony_ci		tst_res(TFAIL, "io_setup() failed unexpectedly, returned -%s "
48f08c3bdfSopenharmony_ci			"expected -%s", tst_strerrno(-TST_RET),
49f08c3bdfSopenharmony_ci			tst_strerrno(exp_err));
50f08c3bdfSopenharmony_ci	}
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic void verify_success(unsigned int nr, io_context_t *ctx, int init_val)
54f08c3bdfSopenharmony_ci{
55f08c3bdfSopenharmony_ci	memset(ctx, init_val, sizeof(*ctx));
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	TEST(io_setup(nr, ctx));
58f08c3bdfSopenharmony_ci	if (TST_RET == -ENOSYS)
59f08c3bdfSopenharmony_ci		tst_brk(TCONF | TRERRNO, "io_setup(): AIO not supported by kernel");
60f08c3bdfSopenharmony_ci	if (TST_RET != 0) {
61f08c3bdfSopenharmony_ci		tst_res(TFAIL, "io_setup() failed unexpectedly with %li (%s)",
62f08c3bdfSopenharmony_ci			TST_RET, tst_strerrno(-TST_RET));
63f08c3bdfSopenharmony_ci		return;
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	tst_res(TPASS, "io_setup() passed as expected");
67f08c3bdfSopenharmony_ci	io_destroy(*ctx);
68f08c3bdfSopenharmony_ci}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic void verify_io_setup(void)
71f08c3bdfSopenharmony_ci{
72f08c3bdfSopenharmony_ci	io_context_t ctx;
73f08c3bdfSopenharmony_ci	unsigned int aio_max = 0;
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	verify_success(1, &ctx, 0);
76f08c3bdfSopenharmony_ci	verify_failure(1, &ctx, 1, EINVAL);
77f08c3bdfSopenharmony_ci	verify_failure(-1, &ctx, 0, EINVAL);
78f08c3bdfSopenharmony_ci	verify_failure(1, NULL, 0, EFAULT);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	if (!access("/proc/sys/fs/aio-max-nr", F_OK)) {
81f08c3bdfSopenharmony_ci		SAFE_FILE_SCANF("/proc/sys/fs/aio-max-nr", "%u", &aio_max);
82f08c3bdfSopenharmony_ci		verify_failure(aio_max + 1, &ctx, 0, EAGAIN);
83f08c3bdfSopenharmony_ci	} else {
84f08c3bdfSopenharmony_ci		tst_res(TCONF, "the aio-max-nr file did not exist");
85f08c3bdfSopenharmony_ci	}
86f08c3bdfSopenharmony_ci}
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_cistatic struct tst_test test = {
89f08c3bdfSopenharmony_ci	.test_all = verify_io_setup,
90f08c3bdfSopenharmony_ci};
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci#else
93f08c3bdfSopenharmony_ci	TST_TEST_TCONF("test requires libaio and it's development packages");
94f08c3bdfSopenharmony_ci#endif
95