1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci * Ported to LTP: Wayne Boyer
5f08c3bdfSopenharmony_ci * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*
9f08c3bdfSopenharmony_ci * Check that poll() works for POLLOUT and POLLIN and that revents is set
10f08c3bdfSopenharmony_ci * correctly.
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci#include <unistd.h>
13f08c3bdfSopenharmony_ci#include <errno.h>
14f08c3bdfSopenharmony_ci#include <fcntl.h>
15f08c3bdfSopenharmony_ci#include <sys/wait.h>
16f08c3bdfSopenharmony_ci#include <sys/poll.h>
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#define BUF_SIZE	512
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic int fildes[2];
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic void verify_pollout(void)
25f08c3bdfSopenharmony_ci{
26f08c3bdfSopenharmony_ci	struct pollfd outfds[] = {
27f08c3bdfSopenharmony_ci		{.fd = fildes[1], .events = POLLOUT},
28f08c3bdfSopenharmony_ci	};
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	TEST(poll(outfds, 1, -1));
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
33f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "poll() POLLOUT failed");
34f08c3bdfSopenharmony_ci		return;
35f08c3bdfSopenharmony_ci	}
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	if (outfds[0].revents != POLLOUT) {
38f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "poll() failed to set POLLOUT");
39f08c3bdfSopenharmony_ci		return;
40f08c3bdfSopenharmony_ci	}
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	tst_res(TPASS, "poll() POLLOUT");
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_cistatic void verify_pollin(void)
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci	char write_buf[] = "Testing";
48f08c3bdfSopenharmony_ci	char read_buf[BUF_SIZE];
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	struct pollfd infds[] = {
51f08c3bdfSopenharmony_ci		{.fd = fildes[0], .events = POLLIN},
52f08c3bdfSopenharmony_ci	};
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, fildes[1], write_buf, sizeof(write_buf));
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	TEST(poll(infds, 1, -1));
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
59f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "poll() POLLIN failed");
60f08c3bdfSopenharmony_ci		goto end;
61f08c3bdfSopenharmony_ci	}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	if (infds[0].revents != POLLIN) {
64f08c3bdfSopenharmony_ci		tst_res(TFAIL, "poll() failed to set POLLIN");
65f08c3bdfSopenharmony_ci		goto end;
66f08c3bdfSopenharmony_ci	}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	tst_res(TPASS, "poll() POLLIN");
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ciend:
72f08c3bdfSopenharmony_ci	SAFE_READ(1, fildes[0], read_buf, sizeof(write_buf));
73f08c3bdfSopenharmony_ci}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_civoid verify_poll(unsigned int n)
76f08c3bdfSopenharmony_ci{
77f08c3bdfSopenharmony_ci	switch (n) {
78f08c3bdfSopenharmony_ci	case 0:
79f08c3bdfSopenharmony_ci		verify_pollout();
80f08c3bdfSopenharmony_ci	break;
81f08c3bdfSopenharmony_ci	case 1:
82f08c3bdfSopenharmony_ci		verify_pollin();
83f08c3bdfSopenharmony_ci	break;
84f08c3bdfSopenharmony_ci	}
85f08c3bdfSopenharmony_ci}
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_cistatic void setup(void)
88f08c3bdfSopenharmony_ci{
89f08c3bdfSopenharmony_ci	SAFE_PIPE(fildes);
90f08c3bdfSopenharmony_ci}
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_cistatic void cleanup(void)
93f08c3bdfSopenharmony_ci{
94f08c3bdfSopenharmony_ci	if (fildes[0] > 0)
95f08c3bdfSopenharmony_ci		SAFE_CLOSE(fildes[0]);
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	if (fildes[1] > 0)
98f08c3bdfSopenharmony_ci		SAFE_CLOSE(fildes[1]);
99f08c3bdfSopenharmony_ci}
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_cistatic struct tst_test test = {
102f08c3bdfSopenharmony_ci	.setup = setup,
103f08c3bdfSopenharmony_ci	.cleanup = cleanup,
104f08c3bdfSopenharmony_ci	.test = verify_poll,
105f08c3bdfSopenharmony_ci	.tcnt = 2,
106f08c3bdfSopenharmony_ci};
107