Home | History | Annotate | Download | only in poll
      1 /*
      2  * Copyright (c) International Business Machines  Corp., 2001
      3  *	07/2001 Ported by Wayne Boyer
      4  * Copyright (C) 2015 Cyril Hrubis <chrubis (at) 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 "test.h"
     32 #include "safe_macros.h"
     33 
     34 #define BUF_SIZE	512
     35 
     36 char *TCID = "poll01";
     37 int TST_TOTAL = 2;
     38 
     39 static int fildes[2];
     40 
     41 static void setup(void);
     42 static void cleanup(void);
     43 
     44 static void verify_pollout(void)
     45 {
     46 	struct pollfd outfds[] = {
     47 		{.fd = fildes[1], .events = POLLOUT},
     48 	};
     49 
     50 	TEST(poll(outfds, 1, -1));
     51 
     52 	if (TEST_RETURN == -1) {
     53 		tst_resm(TFAIL | TTERRNO, "poll() POLLOUT failed");
     54 		return;
     55 	}
     56 
     57 	if (outfds[0].revents != POLLOUT) {
     58 		tst_resm(TFAIL | TTERRNO, "poll() failed to set POLLOUT");
     59 		return;
     60 	}
     61 
     62 	tst_resm(TPASS, "poll() POLLOUT");
     63 }
     64 
     65 static void verify_pollin(void)
     66 {
     67 	char write_buf[] = "Testing";
     68 	char read_buf[BUF_SIZE];
     69 
     70 	struct pollfd infds[] = {
     71 		{.fd = fildes[0], .events = POLLIN},
     72 	};
     73 
     74 	SAFE_WRITE(cleanup, 1, fildes[1], write_buf, sizeof(write_buf));
     75 
     76 	TEST(poll(infds, 1, -1));
     77 
     78 	if (TEST_RETURN == -1) {
     79 		tst_resm(TFAIL | TTERRNO, "poll() POLLIN failed");
     80 		goto end;
     81 	}
     82 
     83 	if (infds[0].revents != POLLIN) {
     84 		tst_resm(TFAIL, "poll() failed to set POLLIN");
     85 		goto end;
     86 	}
     87 
     88 
     89 	tst_resm(TPASS, "poll() POLLIN");
     90 
     91 end:
     92 	SAFE_READ(cleanup, 1, fildes[0], read_buf, sizeof(write_buf));
     93 }
     94 
     95 int main(int ac, char **av)
     96 {
     97 	int lc;
     98 
     99 	tst_parse_opts(ac, av, NULL, NULL);
    100 
    101 	setup();
    102 
    103 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    104 		verify_pollout();
    105 		verify_pollin();
    106 	}
    107 
    108 	cleanup();
    109 	tst_exit();
    110 }
    111 
    112 static void setup(void)
    113 {
    114 	tst_sig(FORK, DEF_HANDLER, cleanup);
    115 
    116 	TEST_PAUSE;
    117 
    118 	SAFE_PIPE(NULL, fildes);
    119 }
    120 
    121 static void cleanup(void)
    122 {
    123 	if (close(fildes[0]))
    124 		tst_resm(TWARN | TERRNO, "failed to close fildes[0]");
    125 
    126 	if (close(fildes[1]))
    127 		tst_resm(TWARN | TERRNO, "failed to close fildes[1]");
    128 }
    129