xref: /freebsd/tests/sys/kern/pipe/pipe_overcommit2_test.c (revision 42b388439bd3795e09258c57a74ce9eec3651c7b)
1*e30a6200SEnji Cooper /*-
2*e30a6200SEnji Cooper  * Copyright (C) 2005 Michael J. Silbersack <silby@freebsd.org>
3*e30a6200SEnji Cooper  *  All rights reserved.
4*e30a6200SEnji Cooper  *
5*e30a6200SEnji Cooper  * Redistribution and use in source and binary forms, with or without
6*e30a6200SEnji Cooper  * modification, are permitted provided that the following conditions
7*e30a6200SEnji Cooper  * are met:
8*e30a6200SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
9*e30a6200SEnji Cooper  *    notice(s), this list of conditions and the following disclaimer as
10*e30a6200SEnji Cooper  *    the first lines of this file unmodified other than the possible
11*e30a6200SEnji Cooper  *    addition of one or more copyright notices.
12*e30a6200SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
13*e30a6200SEnji Cooper  *    notice(s), this list of conditions and the following disclaimer in the
14*e30a6200SEnji Cooper  *    documentation and/or other materials provided with the distribution.
15*e30a6200SEnji Cooper  *
16*e30a6200SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
17*e30a6200SEnji Cooper  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18*e30a6200SEnji Cooper  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19*e30a6200SEnji Cooper  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
20*e30a6200SEnji Cooper  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21*e30a6200SEnji Cooper  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22*e30a6200SEnji Cooper  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23*e30a6200SEnji Cooper  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*e30a6200SEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*e30a6200SEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26*e30a6200SEnji Cooper  * DAMAGE.
27*e30a6200SEnji Cooper  */
28*e30a6200SEnji Cooper 
29*e30a6200SEnji Cooper #include <sys/param.h>
30*e30a6200SEnji Cooper #include <err.h>
31*e30a6200SEnji Cooper #include <errno.h>
32*e30a6200SEnji Cooper #include <fcntl.h>
33*e30a6200SEnji Cooper #include <stdio.h>
34*e30a6200SEnji Cooper #include <stdlib.h>
35*e30a6200SEnji Cooper #include <unistd.h>
36*e30a6200SEnji Cooper 
37*e30a6200SEnji Cooper /*
38*e30a6200SEnji Cooper  * This program tests how sys_pipe.c handles the case where there
39*e30a6200SEnji Cooper  * is ample memory to allocate a pipe, but the file descriptor
40*e30a6200SEnji Cooper  * limit for that user has been exceeded.
41*e30a6200SEnji Cooper  */
42*e30a6200SEnji Cooper 
43*e30a6200SEnji Cooper int
main(void)44*e30a6200SEnji Cooper main(void)
45*e30a6200SEnji Cooper {
46*e30a6200SEnji Cooper 	char template[] = "pipe.XXXXXXXXXX";
47*e30a6200SEnji Cooper 	int lastfd, pipes[10000], returnval;
48*e30a6200SEnji Cooper 	unsigned int i;
49*e30a6200SEnji Cooper 
50*e30a6200SEnji Cooper 	lastfd = -1;
51*e30a6200SEnji Cooper 
52*e30a6200SEnji Cooper 	if (mkstemp(template) == -1)
53*e30a6200SEnji Cooper 		err(1, "mkstemp failed");
54*e30a6200SEnji Cooper 
55*e30a6200SEnji Cooper 	for (i = 0; i < nitems(pipes); i++) {
56*e30a6200SEnji Cooper 		returnval = open(template, O_RDONLY);
57*e30a6200SEnji Cooper 		if (returnval == -1 && (errno == ENFILE || errno == EMFILE))
58*e30a6200SEnji Cooper 			break; /* All descriptors exhausted. */
59*e30a6200SEnji Cooper 		else
60*e30a6200SEnji Cooper 			lastfd = returnval;
61*e30a6200SEnji Cooper 	}
62*e30a6200SEnji Cooper 
63*e30a6200SEnji Cooper 	/* First falloc failure case in sys_pipe.c:pipe() */
64*e30a6200SEnji Cooper 	for (i = 0; i < 1000; i++) {
65*e30a6200SEnji Cooper 		returnval = pipe(&pipes[i]);
66*e30a6200SEnji Cooper 	}
67*e30a6200SEnji Cooper 
68*e30a6200SEnji Cooper 	/*
69*e30a6200SEnji Cooper 	 * Free just one FD so that the second falloc failure
70*e30a6200SEnji Cooper 	 * case will occur.
71*e30a6200SEnji Cooper 	 */
72*e30a6200SEnji Cooper 	close(lastfd);
73*e30a6200SEnji Cooper 
74*e30a6200SEnji Cooper 	for (i = 0; i < 1000; i++) {
75*e30a6200SEnji Cooper 		returnval = pipe(&pipes[i]);
76*e30a6200SEnji Cooper 	}
77*e30a6200SEnji Cooper 	printf("PASS\n");
78*e30a6200SEnji Cooper 
79*e30a6200SEnji Cooper 	unlink(template);
80*e30a6200SEnji Cooper 
81*e30a6200SEnji Cooper 	exit(0);
82*e30a6200SEnji Cooper }
83