1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2020 Linaro Limited. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Viresh Kumar <viresh.kumar@linaro.org>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Test to check if fd set bits are cleared by select().
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * [Algorithm]
13f08c3bdfSopenharmony_ci *  - Check that writefds flag is cleared on full pipe
14f08c3bdfSopenharmony_ci *  - Check that readfds flag is cleared on empty pipe
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include <unistd.h>
18f08c3bdfSopenharmony_ci#include <errno.h>
19f08c3bdfSopenharmony_ci#include <sys/time.h>
20f08c3bdfSopenharmony_ci#include <sys/types.h>
21f08c3bdfSopenharmony_ci#include <fcntl.h>
22f08c3bdfSopenharmony_ci#include "select_var.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic fd_set readfds_pipe, writefds_pipe;
25f08c3bdfSopenharmony_cistatic int fd_empty[2], fd_full[2];
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic struct tcases {
28f08c3bdfSopenharmony_ci	int *fd;
29f08c3bdfSopenharmony_ci	fd_set *readfds;
30f08c3bdfSopenharmony_ci	fd_set *writefds;
31f08c3bdfSopenharmony_ci	char *desc;
32f08c3bdfSopenharmony_ci} tests[] = {
33f08c3bdfSopenharmony_ci	{&fd_empty[0], &readfds_pipe, NULL, "No data to read"},
34f08c3bdfSopenharmony_ci	{&fd_full[1], NULL, &writefds_pipe, "No space to write"},
35f08c3bdfSopenharmony_ci};
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic void run(unsigned int n)
38f08c3bdfSopenharmony_ci{
39f08c3bdfSopenharmony_ci	struct tcases *tc = &tests[n];
40f08c3bdfSopenharmony_ci	struct timeval timeout = {.tv_sec = 0, .tv_usec = 1000};
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	FD_SET(fd_empty[0], &readfds_pipe);
43f08c3bdfSopenharmony_ci	FD_SET(fd_full[1], &writefds_pipe);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	TEST(do_select(*tc->fd + 1, tc->readfds, tc->writefds, NULL, &timeout));
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	if (TST_RET) {
48f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s: select() should have timed out", tc->desc);
49f08c3bdfSopenharmony_ci		return;
50f08c3bdfSopenharmony_ci	}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	if ((tc->readfds && FD_ISSET(*tc->fd, tc->readfds)) ||
53f08c3bdfSopenharmony_ci	    (tc->writefds && FD_ISSET(*tc->fd, tc->writefds))) {
54f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s: select() didn't clear the fd set", tc->desc);
55f08c3bdfSopenharmony_ci		return;
56f08c3bdfSopenharmony_ci	}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	tst_res(TPASS, "%s: select() cleared the fd set", tc->desc);
59f08c3bdfSopenharmony_ci}
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_cistatic void setup(void)
62f08c3bdfSopenharmony_ci{
63f08c3bdfSopenharmony_ci	int buf = 0;
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	select_info();
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	SAFE_PIPE(fd_empty);
68f08c3bdfSopenharmony_ci	FD_ZERO(&readfds_pipe);
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	SAFE_PIPE2(fd_full, O_NONBLOCK);
71f08c3bdfSopenharmony_ci	FD_ZERO(&writefds_pipe);
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	/* Make the write buffer full for fd_full */
74f08c3bdfSopenharmony_ci	do {
75f08c3bdfSopenharmony_ci		TEST(write(fd_full[1], &buf, sizeof(buf)));
76f08c3bdfSopenharmony_ci	} while (TST_RET != -1);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	if (TST_ERR != EAGAIN)
79f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "write() failed with unexpected error");
80f08c3bdfSopenharmony_ci}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_cistatic struct tst_test test = {
83f08c3bdfSopenharmony_ci	.test = run,
84f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tests),
85f08c3bdfSopenharmony_ci	.test_variants = TEST_VARIANTS,
86f08c3bdfSopenharmony_ci	.setup = setup,
87f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
88f08c3bdfSopenharmony_ci};
89