xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/eventfd/eventfd03.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 readfd is set by select() when eventfd() counter value is
13  * non-zero, then check if readfd is not set when eventfd() counter value is
14  * zero.
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 	uint64_t val;
25 	fd_set readfds;
26 	uint64_t non_zero = 10;
27 	struct timeval timeout = { 0, 0 };
28 
29 	fd = TST_EXP_FD(eventfd(0, EFD_NONBLOCK));
30 
31 	FD_ZERO(&readfds);
32 	FD_SET(fd, &readfds);
33 
34 	SAFE_WRITE(0, fd, &non_zero, sizeof(non_zero));
35 	TEST(select(fd + 1, &readfds, NULL, NULL, &timeout));
36 	if (TST_RET == -1)
37 		tst_brk(TBROK | TERRNO, "select");
38 
39 	TST_EXP_EQ_LI(FD_ISSET(fd, &readfds), 1);
40 
41 	SAFE_READ(0, fd, &val, sizeof(val));
42 	TEST(select(fd + 1, &readfds, NULL, NULL, &timeout));
43 	if (TST_RET == -1)
44 		tst_brk(TBROK | TERRNO, "select");
45 
46 	TST_EXP_EQ_LI(FD_ISSET(fd, &readfds), 0);
47 
48 	SAFE_CLOSE(fd);
49 }
50 
51 static struct tst_test test = {
52 	.test_all = run,
53 	.needs_kconfigs = (const char *[]) {
54 		"CONFIG_EVENTFD",
55 		NULL
56 	},
57 };
58