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