Path: blob/main/tests/sys/kern/pipe/pipe_overcommit2_test.c
39488 views
/*-1* Copyright (C) 2005 Michael J. Silbersack <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice(s), this list of conditions and the following disclaimer as9* the first lines of this file unmodified other than the possible10* addition of one or more copyright notices.11* 2. Redistributions in binary form must reproduce the above copyright12* notice(s), this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY16* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED17* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE18* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY19* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES20* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR21* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER22* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH25* DAMAGE.26*/2728#include <sys/param.h>29#include <err.h>30#include <errno.h>31#include <fcntl.h>32#include <stdio.h>33#include <stdlib.h>34#include <unistd.h>3536/*37* This program tests how sys_pipe.c handles the case where there38* is ample memory to allocate a pipe, but the file descriptor39* limit for that user has been exceeded.40*/4142int43main(void)44{45char template[] = "pipe.XXXXXXXXXX";46int lastfd, pipes[10000], returnval;47unsigned int i;4849lastfd = -1;5051if (mkstemp(template) == -1)52err(1, "mkstemp failed");5354for (i = 0; i < nitems(pipes); i++) {55returnval = open(template, O_RDONLY);56if (returnval == -1 && (errno == ENFILE || errno == EMFILE))57break; /* All descriptors exhausted. */58else59lastfd = returnval;60}6162/* First falloc failure case in sys_pipe.c:pipe() */63for (i = 0; i < 1000; i++) {64returnval = pipe(&pipes[i]);65}6667/*68* Free just one FD so that the second falloc failure69* case will occur.70*/71close(lastfd);7273for (i = 0; i < 1000; i++) {74returnval = pipe(&pipes[i]);75}76printf("PASS\n");7778unlink(template);7980exit(0);81}828384