• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  * http://www.sgi.com
5  *
6  * AUTHOR            : Richard Logan
7  * CO-PILOT          : William Roske
8  *
9  * 1.) select(2) to fd of regular file with no I/O and small timeout
10  * 2.) select(2) to fd of system pipe with no I/O and small timeout
11  * 3.) select(2) of fd of a named-pipe (FIFO) with no I/O and small timeout value
12  */
13 
14 #include <unistd.h>
15 #include <errno.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <fcntl.h>
19 #include "select_var.h"
20 
21 static fd_set readfds_reg, readfds_pipe, writefds_pipe, readfds_npipe, writefds_npipe;
22 static int fd_reg, fds_pipe[2], fd_npipe;
23 
24 static struct tcases {
25 	int *nfds;
26 	fd_set *readfds;
27 	fd_set *writefds;
28 	int *readfd;
29 	int *writefd;
30 	char *desc;
31 } tests[] = {
32 	{&fd_reg, &readfds_reg, NULL, &fd_reg, NULL, "with regular file"},
33 	{&fds_pipe[1], &readfds_pipe, &writefds_pipe, &fds_pipe[0], &fds_pipe[1], "with system pipe"},
34 	{&fd_npipe, &readfds_npipe, &writefds_npipe, &fd_npipe, &fd_npipe, "with named pipe (FIFO)"},
35 };
36 
run(unsigned int n)37 static void run(unsigned int n)
38 {
39 	struct tcases *tc = &tests[n];
40 	struct timeval timeout;
41 	char buf;
42 	int exp_ret = 1;
43 
44 	timeout.tv_sec = 0;
45 	timeout.tv_usec = 100000;
46 
47 	if (tc->writefd) {
48 		SAFE_WRITE(0, *tc->writefd, &buf, sizeof(buf));
49 		exp_ret++;
50 	}
51 
52 	TEST(do_select(*tc->nfds + 1, tc->readfds, tc->writefds, 0, &timeout));
53 
54 	if (TST_RET == -1) {
55 		tst_res(TFAIL | TTERRNO, "select() %s failed", tc->desc);
56 		return;
57 	}
58 
59 	if (!TST_RET) {
60 		tst_res(TFAIL, "select() %s timed out", tc->desc);
61 		return;
62 	}
63 
64 	if (TST_RET != exp_ret) {
65 		tst_res(TFAIL, "select() %s returned %lu expected %d",
66 		        tc->desc, TST_RET, exp_ret);
67 		return;
68 	}
69 
70 	tst_res(TPASS, "select() %s returned %i", tc->desc, exp_ret);
71 
72 	if (FD_ISSET(*tc->readfd, tc->readfds))
73 		tst_res(TPASS, "readfds bit %i is set", *tc->readfd);
74 	else
75 		tst_res(TFAIL, "readfds bit %i is not set", *tc->readfd);
76 
77 	if (!tc->writefd)
78 		return;
79 
80 	if (FD_ISSET(*tc->writefd, tc->writefds))
81 		tst_res(TPASS, "writefds bit %i is set", *tc->writefd);
82 	else
83 		tst_res(TPASS, "writefds bit %i is not set", *tc->writefd);
84 }
85 
setup(void)86 static void setup(void)
87 {
88 	select_info();
89 
90 	/* Regular file */
91 	fd_reg = SAFE_OPEN("tmpfile1", O_CREAT | O_RDWR, 0777);
92 	FD_ZERO(&readfds_reg);
93 	FD_SET(fd_reg, &readfds_reg);
94 
95 	/* System pipe*/
96 	SAFE_PIPE(fds_pipe);
97 	FD_ZERO(&readfds_pipe);
98 	FD_ZERO(&writefds_pipe);
99 	FD_SET(fds_pipe[0], &readfds_pipe);
100 	FD_SET(fds_pipe[1], &writefds_pipe);
101 
102 	/* Named pipe (FIFO) */
103 	SAFE_MKFIFO("tmpfile2", 0666);
104 	fd_npipe = SAFE_OPEN("tmpfile2", O_RDWR);
105 	FD_ZERO(&readfds_npipe);
106 	FD_ZERO(&writefds_npipe);
107 	FD_SET(fd_npipe, &readfds_npipe);
108 	FD_SET(fd_npipe, &writefds_npipe);
109 }
110 
cleanup(void)111 static void cleanup(void)
112 {
113 	SAFE_UNLINK("tmpfile2");
114 }
115 
116 static struct tst_test test = {
117 	.test = run,
118 	.tcnt = ARRAY_SIZE(tests),
119 	.test_variants = TEST_VARIANTS,
120 	.setup = setup,
121 	.cleanup = cleanup,
122 	.needs_tmpdir = 1,
123 };
124