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 read 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
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include <stdlib.h>
19f08c3bdfSopenharmony_ci#include <sys/eventfd.h>
20f08c3bdfSopenharmony_ci#include "tst_test.h"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#define EVENT_COUNT 10
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic void run(void)
25f08c3bdfSopenharmony_ci{
26f08c3bdfSopenharmony_ci	int fd;
27f08c3bdfSopenharmony_ci	uint64_t val;
28f08c3bdfSopenharmony_ci	uint32_t invalid;
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	fd = TST_EXP_FD(eventfd(EVENT_COUNT, EFD_NONBLOCK));
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	SAFE_READ(0, fd, &val, sizeof(val));
33f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(val, EVENT_COUNT);
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	TST_EXP_FAIL(read(fd, &val, sizeof(val)), EAGAIN);
36f08c3bdfSopenharmony_ci	TST_EXP_FAIL(read(fd, &invalid, sizeof(invalid)), EINVAL);
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic struct tst_test test = {
42f08c3bdfSopenharmony_ci	.test_all = run,
43f08c3bdfSopenharmony_ci	.needs_kconfigs = (const char *[]) {
44f08c3bdfSopenharmony_ci		"CONFIG_EVENTFD",
45f08c3bdfSopenharmony_ci		NULL
46f08c3bdfSopenharmony_ci	},
47f08c3bdfSopenharmony_ci};
48