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