• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *   Copyright (c) Crackerjack Project., 2007
3  *   Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
4  *   Copyright (c) 2017 Xiao Yang <yangx.jy@cn.fujitsu.com>
5  *
6  *   This program is free software;  you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  *   the GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program;  if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /* Porting from Crackerjack to LTP is done
22  * by Masatake YAMATO <yamato@redhat.com>
23  *
24  * Description:
25  * 1) io_setup(2) succeeds if both nr_events and ctxp are valid.
26  * 2) io_setup(2) fails and returns -EINVAL if ctxp is not initialized to 0.
27  * 3) io_setup(2) fails and returns -EINVAL if nr_events is invalid.
28  * 4) io_setup(2) fails and returns -EFAULT if ctxp is NULL.
29  * 5) io_setup(2) fails and returns -EAGAIN if nr_events exceeds the limit
30  *    of available events.
31  */
32 
33 #include <errno.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include "config.h"
37 #include "tst_test.h"
38 
39 #ifdef HAVE_LIBAIO
40 #include <libaio.h>
41 
verify_failure(unsigned int nr,io_context_t * ctx,int init_val,long exp_err)42 static void verify_failure(unsigned int nr, io_context_t *ctx, int init_val, long exp_err)
43 {
44 	if (ctx)
45 		memset(ctx, init_val, sizeof(*ctx));
46 
47 	TEST(io_setup(nr, ctx));
48 	if (TST_RET == 0) {
49 		tst_res(TFAIL, "io_setup() passed unexpectedly");
50 		io_destroy(*ctx);
51 		return;
52 	}
53 
54 	if (TST_RET == -exp_err) {
55 		tst_res(TPASS, "io_setup() failed as expected, returned -%s",
56 			tst_strerrno(exp_err));
57 	} else {
58 		tst_res(TFAIL, "io_setup() failed unexpectedly, returned -%s "
59 			"expected -%s", tst_strerrno(-TST_RET),
60 			tst_strerrno(exp_err));
61 	}
62 }
63 
verify_success(unsigned int nr,io_context_t * ctx,int init_val)64 static void verify_success(unsigned int nr, io_context_t *ctx, int init_val)
65 {
66 	memset(ctx, init_val, sizeof(*ctx));
67 
68 	TEST(io_setup(nr, ctx));
69 	if (TST_RET != 0) {
70 		tst_res(TFAIL, "io_setup() failed unexpectedly with %li (%s)",
71 			TST_RET, tst_strerrno(-TST_RET));
72 		return;
73 	}
74 
75 	tst_res(TPASS, "io_setup() passed as expected");
76 	io_destroy(*ctx);
77 }
78 
verify_io_setup(void)79 static void verify_io_setup(void)
80 {
81 	io_context_t ctx;
82 	unsigned int aio_max = 0;
83 
84 	verify_success(1, &ctx, 0);
85 	verify_failure(1, &ctx, 1, EINVAL);
86 	verify_failure(-1, &ctx, 0, EINVAL);
87 	verify_failure(1, NULL, 0, EFAULT);
88 
89 	if (!access("/proc/sys/fs/aio-max-nr", F_OK)) {
90 		SAFE_FILE_SCANF("/proc/sys/fs/aio-max-nr", "%u", &aio_max);
91 		verify_failure(aio_max + 1, &ctx, 0, EAGAIN);
92 	} else {
93 		tst_res(TCONF, "the aio-max-nr file did not exist");
94 	}
95 }
96 
97 static struct tst_test test = {
98 	.test_all = verify_io_setup,
99 };
100 
101 #else
102 	TST_TEST_TCONF("test requires libaio and it's development packages");
103 #endif
104