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