1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci * Copyright (c) 2008 Vijay Kumar B. <vijaykumar@bravegnu.org>
5f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2008-2022
6f08c3bdfSopenharmony_ci * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci/*\
10f08c3bdfSopenharmony_ci * [Description]
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Test whether writefd is set by select() when eventfd() counter value is
13f08c3bdfSopenharmony_ci * not the maximum value, then check if writefd is not set when eventfd()
14f08c3bdfSopenharmony_ci * counter value is maximum value.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <stdlib.h>
18f08c3bdfSopenharmony_ci#include <sys/eventfd.h>
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic void run(void)
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	int fd;
24f08c3bdfSopenharmony_ci	fd_set writefds;
25f08c3bdfSopenharmony_ci	uint64_t val;
26f08c3bdfSopenharmony_ci	uint64_t non_max = 10;
27f08c3bdfSopenharmony_ci	uint64_t max = UINT64_MAX - 1;
28f08c3bdfSopenharmony_ci	struct timeval timeout = { 0, 0 };
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	fd = TST_EXP_FD(eventfd(0, EFD_NONBLOCK));
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	FD_ZERO(&writefds);
33f08c3bdfSopenharmony_ci	FD_SET(fd, &writefds);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	SAFE_WRITE(0, fd, &non_max, sizeof(non_max));
36f08c3bdfSopenharmony_ci	TEST(select(fd + 1, NULL, &writefds, NULL, &timeout));
37f08c3bdfSopenharmony_ci	if (TST_RET == -1)
38f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "select");
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(FD_ISSET(fd, &writefds), 1);
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	SAFE_READ(0, fd, &val, sizeof(val));
43f08c3bdfSopenharmony_ci	SAFE_WRITE(0, fd, &max, sizeof(max));
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	TEST(select(fd + 1, NULL, &writefds, NULL, &timeout));
46f08c3bdfSopenharmony_ci	if (TST_RET == -1)
47f08c3bdfSopenharmony_ci		tst_brk(TBROK | TERRNO, "select");
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(FD_ISSET(fd, &writefds), 0);
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
52f08c3bdfSopenharmony_ci}
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cistatic struct tst_test test = {
55f08c3bdfSopenharmony_ci	.test_all = run,
56f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
57f08c3bdfSopenharmony_ci		"CONFIG_EVENTFD",
58f08c3bdfSopenharmony_ci		NULL
59f08c3bdfSopenharmony_ci	},
60f08c3bdfSopenharmony_ci};
61