xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/eventfd/eventfd02.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  * Verify write operation for eventfd fail with:
13  *
14  * - EAGAIN when counter is zero on non blocking fd
15  * - EINVAL when buffer size is less than 8 bytes, or if an attempt is made to
16  *	write the value 0xffffffffffffffff
17  */
18 
19 #include <stdlib.h>
20 #include <sys/eventfd.h>
21 #include "tst_test.h"
22 
run(void)23 static void run(void)
24 {
25 	int fd;
26 	uint64_t val = 12;
27 	uint64_t buf;
28 	uint32_t invalid;
29 
30 	fd = TST_EXP_FD(eventfd(0, EFD_NONBLOCK));
31 
32 	SAFE_WRITE(0, fd, &val, sizeof(val));
33 	SAFE_READ(0, fd, &buf, sizeof(buf));
34 	TST_EXP_EQ_LI(buf, val);
35 
36 	val = UINT64_MAX - 1;
37 	SAFE_WRITE(0, fd, &val, sizeof(val));
38 	TST_EXP_FAIL(write(fd, &val, sizeof(val)), EAGAIN);
39 	TST_EXP_FAIL(write(fd, &invalid, sizeof(invalid)), EINVAL);
40 
41 	val = 0xffffffffffffffffLL;
42 	TST_EXP_FAIL(write(fd, &val, sizeof(val)), EINVAL);
43 
44 	SAFE_CLOSE(fd);
45 }
46 
47 static struct tst_test test = {
48 	.test_all = run,
49 	.needs_kconfigs = (const char *[]) {
50 		"CONFIG_EVENTFD",
51 		NULL
52 	},
53 };
54