1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2001
4 *
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 * NAME
22 *	pipe06.c
23 *
24 * DESCRIPTION
25 *	Check what happens when the system runs out of pipes.
26 *
27 * ALGORITHM
28 *	Issue enough pipe calls to run the system out of pipes.
29 *	Check that we get EMFILE.
30 *
31 * USAGE:  <for command-line>
32 *  pipe06 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
33 *     where,  -c n : Run n copies concurrently.
34 *             -e   : Turn on errno logging.
35 *             -i n : Execute test n times.
36 *             -I x : Execute test for x seconds.
37 *             -P x : Pause for x seconds between iterations.
38 *             -t   : Turn on syscall timing.
39 *
40 * HISTORY
41 *	07/2001 Ported by Wayne Boyer
42 *
43 * RESTRICTIONS
44 *	None
45 */
46#include <fcntl.h>
47#include <errno.h>
48#include "test.h"
49
50char *TCID = "pipe06";
51int TST_TOTAL = 1;
52
53int pipe_ret, pipes[2];
54void setup(void);
55void cleanup(void);
56
57int main(int ac, char **av)
58{
59	int lc;
60
61	tst_parse_opts(ac, av, NULL, NULL);
62
63	setup();
64
65	for (lc = 0; TEST_LOOPING(lc); lc++) {
66
67		/* reset tst_count in case we are looping */
68		tst_count = 0;
69
70		TEST(pipe(pipes));
71
72		if (TEST_RETURN != -1) {
73			tst_resm(TFAIL, "call succeeded unexpectedly");
74		}
75
76		if (TEST_ERRNO != EMFILE) {
77			tst_resm(TFAIL | TTERRNO, "pipe failed unexpectedly");
78		} else {
79			tst_resm(TPASS, "failed with EMFILE");
80		}
81
82	}
83	cleanup();
84	tst_exit();
85
86}
87
88/*
89 * setup() - performs all ONE TIME setup for this test.
90 */
91void setup(void)
92{
93	int i, numb_fds;
94
95	tst_sig(NOFORK, DEF_HANDLER, cleanup);
96
97	TEST_PAUSE;
98
99	numb_fds = getdtablesize();
100
101	for (i = 0; i < numb_fds; i++) {
102		pipe_ret = pipe(pipes);
103		if (pipe_ret < 0) {
104			if (errno != EMFILE) {
105				tst_brkm(TBROK | TTERRNO, cleanup,
106					 "didn't get EMFILE");
107			}
108			break;
109		}
110	}
111}
112
113/*
114 * cleanup() - performs all ONE TIME cleanup for this test at
115 *	       completion or premature exit.
116 */
117void cleanup(void)
118{
119}
120