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 * $FreeBSD$ 39*e30a6200SEnji Cooper * This program tests how sys_pipe.c handles the case where there 40*e30a6200SEnji Cooper * is ample memory to allocate a pipe, but the file descriptor 41*e30a6200SEnji Cooper * limit for that user has been exceeded. 42*e30a6200SEnji Cooper */ 43*e30a6200SEnji Cooper 44*e30a6200SEnji Cooper int 45*e30a6200SEnji Cooper main(void) 46*e30a6200SEnji Cooper { 47*e30a6200SEnji Cooper char template[] = "pipe.XXXXXXXXXX"; 48*e30a6200SEnji Cooper int lastfd, pipes[10000], returnval; 49*e30a6200SEnji Cooper unsigned int i; 50*e30a6200SEnji Cooper 51*e30a6200SEnji Cooper lastfd = -1; 52*e30a6200SEnji Cooper 53*e30a6200SEnji Cooper if (mkstemp(template) == -1) 54*e30a6200SEnji Cooper err(1, "mkstemp failed"); 55*e30a6200SEnji Cooper 56*e30a6200SEnji Cooper for (i = 0; i < nitems(pipes); i++) { 57*e30a6200SEnji Cooper returnval = open(template, O_RDONLY); 58*e30a6200SEnji Cooper if (returnval == -1 && (errno == ENFILE || errno == EMFILE)) 59*e30a6200SEnji Cooper break; /* All descriptors exhausted. */ 60*e30a6200SEnji Cooper else 61*e30a6200SEnji Cooper lastfd = returnval; 62*e30a6200SEnji Cooper } 63*e30a6200SEnji Cooper 64*e30a6200SEnji Cooper /* First falloc failure case in sys_pipe.c:pipe() */ 65*e30a6200SEnji Cooper for (i = 0; i < 1000; i++) { 66*e30a6200SEnji Cooper returnval = pipe(&pipes[i]); 67*e30a6200SEnji Cooper } 68*e30a6200SEnji Cooper 69*e30a6200SEnji Cooper /* 70*e30a6200SEnji Cooper * Free just one FD so that the second falloc failure 71*e30a6200SEnji Cooper * case will occur. 72*e30a6200SEnji Cooper */ 73*e30a6200SEnji Cooper close(lastfd); 74*e30a6200SEnji Cooper 75*e30a6200SEnji Cooper for (i = 0; i < 1000; i++) { 76*e30a6200SEnji Cooper returnval = pipe(&pipes[i]); 77*e30a6200SEnji Cooper } 78*e30a6200SEnji Cooper printf("PASS\n"); 79*e30a6200SEnji Cooper 80*e30a6200SEnji Cooper unlink(template); 81*e30a6200SEnji Cooper 82*e30a6200SEnji Cooper exit(0); 83*e30a6200SEnji Cooper } 84