xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/futex/futex_wait01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Cyril Hrubis <[email protected]>
4  *
5  * Based on futextest (futext_wait_timeout.c and futex_wait_ewouldblock.c)
6  * written by Darren Hart <[email protected]>
7  *            Gowrishankar <[email protected]>
8  *
9  * 1. Block on a futex and wait for timeout.
10  * 2. Test if FUTEX_WAIT op returns -EWOULDBLOCK if the futex value differs
11  *    from the expected one.
12  */
13 
14 #include "futextest.h"
15 
16 struct testcase {
17 	futex_t *f_addr;
18 	futex_t f_val;
19 	int opflags;
20 	int exp_errno;
21 };
22 
23 static futex_t futex = FUTEX_INITIALIZER;
24 
25 static struct testcase testcases[] = {
26 	{&futex, FUTEX_INITIALIZER, 0, ETIMEDOUT},
27 	{&futex, FUTEX_INITIALIZER+1, 0, EWOULDBLOCK},
28 	{&futex, FUTEX_INITIALIZER, FUTEX_PRIVATE_FLAG, ETIMEDOUT},
29 	{&futex, FUTEX_INITIALIZER+1, FUTEX_PRIVATE_FLAG, EWOULDBLOCK},
30 };
31 
32 static struct futex_test_variants variants[] = {
33 #if (__NR_futex != __LTP__NR_INVALID_SYSCALL)
34 	{ .fntype = FUTEX_FN_FUTEX, .tstype = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
35 #endif
36 
37 #if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL)
38 	{ .fntype = FUTEX_FN_FUTEX64, .tstype = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
39 #endif
40 };
41 
run(unsigned int n)42 static void run(unsigned int n)
43 {
44 	struct futex_test_variants *tv = &variants[tst_variant];
45 	struct testcase *tc = &testcases[n];
46 	struct tst_ts to = tst_ts_from_ns(tv->tstype, 10000);
47 	int res;
48 
49 	res = futex_wait(tv->fntype, tc->f_addr, tc->f_val, &to, tc->opflags);
50 
51 	if (res != -1) {
52 		tst_res(TFAIL, "futex_wait() succeeded unexpectedly");
53 		return;
54 	}
55 
56 	if (errno != tc->exp_errno) {
57 		tst_res(TFAIL | TERRNO, "futex_wait() failed with incorrect error, expected errno=%s",
58 		         tst_strerrno(tc->exp_errno));
59 		return;
60 	}
61 
62 	tst_res(TPASS | TERRNO, "futex_wait() passed");
63 }
64 
setup(void)65 static void setup(void)
66 {
67 	struct futex_test_variants *tv = &variants[tst_variant];
68 
69 	tst_res(TINFO, "Testing variant: %s", tv->desc);
70 	futex_supported_by_kernel(tv->fntype);
71 }
72 
73 static struct tst_test test = {
74 	.setup = setup,
75 	.test = run,
76 	.tcnt = ARRAY_SIZE(testcases),
77 	.test_variants = ARRAY_SIZE(variants),
78 };
79