1/*
2 * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like.  Any license provided herein, whether implied or
15 * otherwise, applies only to this software file.  Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA  94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
33/*
34 *    AUTHOR            : Richard Logan
35 *    CO-PILOT          : Glen Overby
36 *    DATE STARTED      : 02/24/93
37 *
38 *      1.) select(2) of fd of a named-pipe (FIFO) with no I/O and small timeout value
39 */
40
41#include <errno.h>
42#include <signal.h>
43#include <fcntl.h>
44#include <signal.h>
45#include <sys/param.h>
46#include <sys/types.h>
47#include <sys/time.h>
48#include <sys/stat.h>
49
50#include "test.h"
51
52#define FILENAME	"select03"
53
54static void setup(void);
55static void cleanup(void);
56
57char *TCID = "select03";
58int TST_TOTAL = 1;
59
60int Fd;
61fd_set saved_Readfds, saved_Writefds;
62fd_set Readfds, Writefds;
63
64int main(int ac, char **av)
65{
66	int lc;
67	struct timeval timeout;
68	long test_time = 0;	/* in usecs */
69
70	tst_parse_opts(ac, av, NULL, NULL);
71
72	setup();
73
74	for (lc = 0; TEST_LOOPING(lc); lc++) {
75
76		tst_count = 0;
77
78		test_time = ((lc % 2000) * 100000);	/* 100 milli-seconds */
79
80		if (test_time > 1000000 * 60)
81			test_time = test_time % (1000000 * 60);
82
83		timeout.tv_sec = test_time / 1000000;
84		timeout.tv_usec = test_time - (timeout.tv_sec * 1000000);
85
86		Readfds = saved_Readfds;
87		Writefds = saved_Writefds;
88
89		TEST(select(5, &Readfds, &Writefds, 0, &timeout));
90
91		if (TEST_RETURN == -1) {
92			tst_resm(TFAIL,
93				 "%d select(5, &Readfds, &Writefds, 0, &timeout) failed errno=%d\n",
94				 lc, errno);
95		} else {
96			tst_resm(TPASS,
97				 "select(5, &Readfds, &Writefds, 0, &timeout) timeout = %ld usecs",
98				 test_time);
99		}
100
101	}
102
103	cleanup();
104	tst_exit();
105}
106
107static void setup(void)
108{
109
110	tst_sig(FORK, DEF_HANDLER, cleanup);
111
112	TEST_PAUSE;
113
114	tst_tmpdir();
115
116	if (mkfifo(FILENAME, 0777) == -1) {
117		tst_brkm(TBROK, cleanup, "mkfifo(%s, 0777) failed, errno=%d",
118			 FILENAME, errno);
119	}
120
121	if ((Fd = open(FILENAME, O_RDWR)) == -1) {
122		tst_brkm(TBROK, cleanup, "open(%s, O_RDWR) failed, errno=%d",
123			 FILENAME, errno);
124	}
125
126	FD_ZERO(&saved_Readfds);
127	FD_ZERO(&saved_Writefds);
128	FD_SET(Fd, &saved_Readfds);
129	FD_SET(Fd, &saved_Writefds);
130}
131
132static void cleanup(void)
133{
134	close(Fd);
135	tst_rmdir();
136}
137