xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/io_pgetevents/io_pgetevents01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2020 Viresh Kumar <[email protected]>
4  *
5  * Description:
6  * Basic io_pgetevents() test to receive 1 event successfully.
7  */
8 #include "time64_variants.h"
9 #include "tst_test.h"
10 #include "tst_timer.h"
11 #include "lapi/io_pgetevents.h"
12 
13 #ifdef HAVE_LIBAIO
14 static int fd;
15 
16 static struct time64_variants variants[] = {
17 #if (__NR_io_pgetevents != __LTP__NR_INVALID_SYSCALL)
18 	{ .io_pgetevents = sys_io_pgetevents, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
19 #endif
20 
21 #if (__NR_io_pgetevents_time64 != __LTP__NR_INVALID_SYSCALL)
22 	{ .io_pgetevents = sys_io_pgetevents_time64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
23 #endif
24 };
25 
setup(void)26 static void setup(void)
27 {
28 	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
29 }
30 
run(void)31 static void run(void)
32 {
33 	struct time64_variants *tv = &variants[tst_variant];
34 	struct io_event events[1];
35 	struct iocb cb, *cbs[1];
36 	io_context_t ctx = 0;
37 	struct tst_ts to = tst_ts_from_ns(tv->ts_type, 10000);
38 	sigset_t sigmask;
39 	char data[4096];
40 	int ret;
41 
42 	cbs[0] = &cb;
43 	sigemptyset(&sigmask);
44 
45 	fd = SAFE_OPEN("io_pgetevents_file", O_RDWR | O_CREAT, 0644);
46 	io_prep_pwrite(&cb, fd, data, 4096, 0);
47 
48 	TEST(io_setup(1, &ctx));
49 	if (TST_RET == -ENOSYS)
50 		tst_brk(TCONF | TRERRNO, "io_setup(): AIO not supported by kernel");
51 	if (TST_RET < 0)
52 		tst_brk(TBROK | TRERRNO, "io_setup() failed");
53 
54 	ret = io_submit(ctx, 1, cbs);
55 	if (ret != 1)
56 		tst_brk(TBROK | TERRNO, "io_submit() failed");
57 
58 	/* get the reply */
59 	ret = tv->io_pgetevents(ctx, 1, 1, events, tst_ts_get(&to), &sigmask);
60 
61 	if (ret == 1)
62 		tst_res(TPASS, "io_pgetevents() works as expected");
63 	else
64 		tst_res(TFAIL | TERRNO, "io_pgetevents() failed");
65 
66 	if (io_destroy(ctx) < 0)
67 		tst_brk(TBROK | TERRNO, "io_destroy() failed");
68 
69 	SAFE_CLOSE(fd);
70 }
71 
72 static struct tst_test test = {
73 	.min_kver = "4.18",
74 	.test_all = run,
75 	.test_variants = ARRAY_SIZE(variants),
76 	.needs_tmpdir = 1,
77 	.setup = setup,
78 };
79 
80 #else
81 TST_TEST_TCONF("test requires libaio and it's development packages");
82 #endif
83