1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * Block on a futex and wait for wakeup.
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci * This tests uses shared memory page to store the mutex variable.
8f08c3bdfSopenharmony_ci */
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#include <sys/mman.h>
11f08c3bdfSopenharmony_ci#include <sys/wait.h>
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#include "futextest.h"
14f08c3bdfSopenharmony_ci#include "futex_utils.h"
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic futex_t *futex;
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_cistatic struct futex_test_variants variants[] = {
19f08c3bdfSopenharmony_ci#if (__NR_futex != __LTP__NR_INVALID_SYSCALL)
20f08c3bdfSopenharmony_ci	{ .fntype = FUTEX_FN_FUTEX, .desc = "syscall with old kernel spec"},
21f08c3bdfSopenharmony_ci#endif
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL)
24f08c3bdfSopenharmony_ci	{ .fntype = FUTEX_FN_FUTEX64, .desc = "syscall time64 with kernel spec"},
25f08c3bdfSopenharmony_ci#endif
26f08c3bdfSopenharmony_ci};
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void do_child(void)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	struct futex_test_variants *tv = &variants[tst_variant];
31f08c3bdfSopenharmony_ci	int ret;
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	TST_PROCESS_STATE_WAIT(getppid(), 'S', 1000);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	ret = futex_wake(tv->fntype, futex, 1, 0);
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	if (ret != 1)
38f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "futex_wake() failed");
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	exit(0);
41f08c3bdfSopenharmony_ci}
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_cistatic void run(void)
44f08c3bdfSopenharmony_ci{
45f08c3bdfSopenharmony_ci	struct futex_test_variants *tv = &variants[tst_variant];
46f08c3bdfSopenharmony_ci	int res, pid;
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
49f08c3bdfSopenharmony_ci	if (!pid)
50f08c3bdfSopenharmony_ci		do_child();
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	res = futex_wait(tv->fntype, futex, *futex, NULL, 0);
53f08c3bdfSopenharmony_ci	if (res) {
54f08c3bdfSopenharmony_ci		tst_res(TFAIL | TERRNO, "futex_wait() failed");
55f08c3bdfSopenharmony_ci		return;
56f08c3bdfSopenharmony_ci	}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	SAFE_WAIT(NULL);
59f08c3bdfSopenharmony_ci	tst_res(TPASS, "futex_wait() woken up");
60f08c3bdfSopenharmony_ci}
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_cistatic void setup(void)
63f08c3bdfSopenharmony_ci{
64f08c3bdfSopenharmony_ci	struct futex_test_variants *tv = &variants[tst_variant];
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	tst_res(TINFO, "Testing variant: %s", tv->desc);
67f08c3bdfSopenharmony_ci	futex_supported_by_kernel(tv->fntype);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	futex = SAFE_MMAP(NULL, sizeof(*futex), PROT_READ | PROT_WRITE,
70f08c3bdfSopenharmony_ci			  MAP_ANONYMOUS | MAP_SHARED, -1, 0);
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	*futex = FUTEX_INITIALIZER;
73f08c3bdfSopenharmony_ci}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_cistatic struct tst_test test = {
76f08c3bdfSopenharmony_ci	.setup = setup,
77f08c3bdfSopenharmony_ci	.test_all = run,
78f08c3bdfSopenharmony_ci	.test_variants = ARRAY_SIZE(variants),
79f08c3bdfSopenharmony_ci	.forks_child = 1,
80f08c3bdfSopenharmony_ci};
81