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 several processes on a mutex, then wake them up.
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci#include <sys/types.h>
9f08c3bdfSopenharmony_ci#include <sys/wait.h>
10f08c3bdfSopenharmony_ci
11f08c3bdfSopenharmony_ci#include "futextest.h"
12f08c3bdfSopenharmony_ci#include "futex_utils.h"
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_cistatic futex_t *futex;
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic struct futex_test_variants variants[] = {
17f08c3bdfSopenharmony_ci#if (__NR_futex != __LTP__NR_INVALID_SYSCALL)
18f08c3bdfSopenharmony_ci	{ .fntype = FUTEX_FN_FUTEX, .desc = "syscall with old kernel spec"},
19f08c3bdfSopenharmony_ci#endif
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL)
22f08c3bdfSopenharmony_ci	{ .fntype = FUTEX_FN_FUTEX64, .desc = "syscall time64 with kernel spec"},
23f08c3bdfSopenharmony_ci#endif
24f08c3bdfSopenharmony_ci};
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic void do_child(void)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	struct futex_test_variants *tv = &variants[tst_variant];
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	futex_wait(tv->fntype, futex, *futex, NULL, 0);
31f08c3bdfSopenharmony_ci	exit(0);
32f08c3bdfSopenharmony_ci}
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_cistatic void do_wake(int nr_children)
35f08c3bdfSopenharmony_ci{
36f08c3bdfSopenharmony_ci	struct futex_test_variants *tv = &variants[tst_variant];
37f08c3bdfSopenharmony_ci	int res, i, cnt;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	TEST(futex_wake(tv->fntype, futex, nr_children, 0));
40f08c3bdfSopenharmony_ci	if (TST_RET != nr_children) {
41f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
42f08c3bdfSopenharmony_ci		        "futex_wake() woken up %li children, expected %i",
43f08c3bdfSopenharmony_ci			TST_RET, nr_children);
44f08c3bdfSopenharmony_ci		return;
45f08c3bdfSopenharmony_ci	}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	for (cnt = 0, i = 0; i < 100000; i++) {
48f08c3bdfSopenharmony_ci		while (waitpid(-1, &res, WNOHANG) > 0)
49f08c3bdfSopenharmony_ci			cnt++;
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci		if (cnt == nr_children)
52f08c3bdfSopenharmony_ci			break;
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci		usleep(100);
55f08c3bdfSopenharmony_ci	}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	if (cnt != nr_children) {
58f08c3bdfSopenharmony_ci		tst_res(TFAIL, "reaped only %i childs, expected %i",
59f08c3bdfSopenharmony_ci		        cnt, nr_children);
60f08c3bdfSopenharmony_ci	} else {
61f08c3bdfSopenharmony_ci		tst_res(TPASS, "futex_wake() woken up %i childs", cnt);
62f08c3bdfSopenharmony_ci	}
63f08c3bdfSopenharmony_ci}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic void run(void)
66f08c3bdfSopenharmony_ci{
67f08c3bdfSopenharmony_ci	struct futex_test_variants *tv = &variants[tst_variant];
68f08c3bdfSopenharmony_ci	pid_t pids[55];
69f08c3bdfSopenharmony_ci	unsigned int i;
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(pids); i++) {
72f08c3bdfSopenharmony_ci		pids[i] = SAFE_FORK();
73f08c3bdfSopenharmony_ci		if (!pids[i])
74f08c3bdfSopenharmony_ci			do_child();
75f08c3bdfSopenharmony_ci	}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	for (i = 0; i < ARRAY_SIZE(pids); i++)
78f08c3bdfSopenharmony_ci		TST_PROCESS_STATE_WAIT(pids[i], 'S', 0);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	for (i = 1; i <= 10; i++)
81f08c3bdfSopenharmony_ci		do_wake(i);
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	TEST(futex_wake(tv->fntype, futex, 1, 0));
84f08c3bdfSopenharmony_ci	if (TST_RET) {
85f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
86f08c3bdfSopenharmony_ci			"futex_wake() woken up %li, none were waiting",
87f08c3bdfSopenharmony_ci		        TST_RET);
88f08c3bdfSopenharmony_ci	} else {
89f08c3bdfSopenharmony_ci		tst_res(TPASS, "futex_wake() woken up 0 children");
90f08c3bdfSopenharmony_ci	}
91f08c3bdfSopenharmony_ci}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_cistatic void setup(void)
94f08c3bdfSopenharmony_ci{
95f08c3bdfSopenharmony_ci	struct futex_test_variants *tv = &variants[tst_variant];
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	tst_res(TINFO, "Testing variant: %s", tv->desc);
98f08c3bdfSopenharmony_ci	futex_supported_by_kernel(tv->fntype);
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	futex = SAFE_MMAP(NULL, sizeof(*futex), PROT_READ | PROT_WRITE,
101f08c3bdfSopenharmony_ci			  MAP_ANONYMOUS | MAP_SHARED, -1, 0);
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	*futex = FUTEX_INITIALIZER;
104f08c3bdfSopenharmony_ci}
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_cistatic struct tst_test test = {
107f08c3bdfSopenharmony_ci	.setup = setup,
108f08c3bdfSopenharmony_ci	.test_all = run,
109f08c3bdfSopenharmony_ci	.test_variants = ARRAY_SIZE(variants),
110f08c3bdfSopenharmony_ci	.forks_child = 1,
111f08c3bdfSopenharmony_ci};
112