• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *	07/2001 Ported by Wayne Boyer
4  * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
5  *
6  * This program is free software;  you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  * the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program;  if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * Check that poll() works for POLLOUT and POLLIN and that revents is set
23  * correctly.
24  */
25 #include <unistd.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/wait.h>
29 #include <sys/poll.h>
30 
31 #include "tst_test.h"
32 
33 #define BUF_SIZE	512
34 
35 static int fildes[2];
36 
verify_pollout(void)37 static void verify_pollout(void)
38 {
39 	struct pollfd outfds[] = {
40 		{.fd = fildes[1], .events = POLLOUT},
41 	};
42 
43 	TEST(poll(outfds, 1, -1));
44 
45 	if (TST_RET == -1) {
46 		tst_res(TFAIL | TTERRNO, "poll() POLLOUT failed");
47 		return;
48 	}
49 
50 	if (outfds[0].revents != POLLOUT) {
51 		tst_res(TFAIL | TTERRNO, "poll() failed to set POLLOUT");
52 		return;
53 	}
54 
55 	tst_res(TPASS, "poll() POLLOUT");
56 }
57 
verify_pollin(void)58 static void verify_pollin(void)
59 {
60 	char write_buf[] = "Testing";
61 	char read_buf[BUF_SIZE];
62 
63 	struct pollfd infds[] = {
64 		{.fd = fildes[0], .events = POLLIN},
65 	};
66 
67 	SAFE_WRITE(1, fildes[1], write_buf, sizeof(write_buf));
68 
69 	TEST(poll(infds, 1, -1));
70 
71 	if (TST_RET == -1) {
72 		tst_res(TFAIL | TTERRNO, "poll() POLLIN failed");
73 		goto end;
74 	}
75 
76 	if (infds[0].revents != POLLIN) {
77 		tst_res(TFAIL, "poll() failed to set POLLIN");
78 		goto end;
79 	}
80 
81 
82 	tst_res(TPASS, "poll() POLLIN");
83 
84 end:
85 	SAFE_READ(1, fildes[0], read_buf, sizeof(write_buf));
86 }
87 
verify_poll(unsigned int n)88 void verify_poll(unsigned int n)
89 {
90 	switch (n) {
91 	case 0:
92 		verify_pollout();
93 	break;
94 	case 1:
95 		verify_pollin();
96 	break;
97 	}
98 }
99 
setup(void)100 static void setup(void)
101 {
102 	SAFE_PIPE(fildes);
103 }
104 
cleanup(void)105 static void cleanup(void)
106 {
107 	if (fildes[0] > 0)
108 		SAFE_CLOSE(fildes[0]);
109 
110 	if (fildes[1] > 0)
111 		SAFE_CLOSE(fildes[1]);
112 }
113 
114 static struct tst_test test = {
115 	.setup = setup,
116 	.cleanup = cleanup,
117 	.test = verify_poll,
118 	.tcnt = 2,
119 };
120