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