1 /*- 2 * Copyright (c) 2014 EMC Corp. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <sys/param.h> 29 #include <sys/limits.h> 30 #include <sys/resource.h> 31 #include <sys/stat.h> 32 #include <sys/sysctl.h> 33 #include <sys/time.h> 34 #include <sys/wait.h> 35 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <signal.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <strings.h> 43 #include <unistd.h> 44 45 #include <atf-c.h> 46 47 static volatile sig_atomic_t done; 48 49 #define AFILE "afile" 50 #define EXPANDBY 1000 51 #define PARALLEL 4 52 #define RENDEZVOUS "rendezvous" 53 #define VALUE "value" 54 55 ATF_TC_WITHOUT_HEAD(dup2__simple); 56 ATF_TC_BODY(dup2__simple, tc) 57 { 58 int fd1, fd2; 59 struct stat sb1, sb2; 60 61 ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1); 62 fd2 = 27; 63 ATF_REQUIRE(dup2(fd1, fd2) != -1); 64 ATF_REQUIRE(fstat(fd1, &sb1) != -1); 65 ATF_REQUIRE(fstat(fd2, &sb2) != -1); 66 ATF_REQUIRE(bcmp(&sb1, &sb2, sizeof(sb1)) == 0); 67 } 68 69 ATF_TC(dup2__ebadf_when_2nd_arg_out_of_range); 70 ATF_TC_HEAD(dup2__ebadf_when_2nd_arg_out_of_range, tc) 71 { 72 atf_tc_set_md_var(tc, "descr", "Regression test for r234131"); 73 } 74 75 ATF_TC_BODY(dup2__ebadf_when_2nd_arg_out_of_range, tc) 76 { 77 int fd1, fd2, ret; 78 79 ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1); 80 fd2 = INT_MAX; 81 ret = dup2(fd1, fd2); 82 ATF_CHECK_EQ(-1, ret); 83 ATF_CHECK_EQ(EBADF, errno); 84 } 85 86 static void 87 handler(int s __unused) 88 { 89 done++; 90 } 91 92 static void 93 openfiles2(size_t n) 94 { 95 size_t i; 96 int r; 97 98 errno = 0; 99 for (i = 0; i < n; i++) { 100 r = open(AFILE, O_RDONLY); 101 if (r < 0) { 102 fprintf(stderr, "open: %s\n", strerror(errno)); 103 _exit(1); 104 } 105 } 106 kill(getppid(), SIGUSR1); 107 108 for (;;) { 109 if (access(RENDEZVOUS, R_OK) != 0) 110 break; 111 usleep(1000); 112 } 113 _exit(0); 114 } 115 116 static void 117 openfiles(size_t n) 118 { 119 int i, fd; 120 121 signal(SIGUSR1, handler); 122 ATF_REQUIRE((fd = open(AFILE, O_CREAT, 0644)) != -1); 123 close(fd); 124 ATF_REQUIRE((fd = open(RENDEZVOUS, O_CREAT, 0644)) != -1); 125 close(fd); 126 done = 0; 127 for (i = 0; i < PARALLEL; i++) 128 if (fork() == 0) 129 openfiles2(n / PARALLEL); 130 while (done != PARALLEL) { 131 usleep(1000); 132 ATF_REQUIRE_EQ_MSG(0, waitpid(-1, NULL, WNOHANG), 133 "a child exited unexpectedly"); 134 } 135 unlink(RENDEZVOUS); 136 for (i = 0; i < PARALLEL; i++) 137 ATF_CHECK_MSG(wait(NULL) > 0, "wait: %s", strerror(errno)); 138 } 139 140 ATF_TC_WITH_CLEANUP(kern_maxfiles__increase); 141 ATF_TC_HEAD(kern_maxfiles__increase, tc) 142 { 143 atf_tc_set_md_var(tc, "require.user", "root"); 144 atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects"); 145 atf_tc_set_md_var(tc, "descr", 146 "Check kern.maxfiles expansion"); 147 } 148 149 ATF_TC_BODY(kern_maxfiles__increase, tc) 150 { 151 size_t oldlen; 152 int maxfiles, oldmaxfiles, current; 153 char buf[80]; 154 struct rlimit rl; 155 156 oldlen = sizeof(maxfiles); 157 if (sysctlbyname("kern.maxfiles", &maxfiles, &oldlen, NULL, 0) == -1) 158 atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles", 159 strerror(errno)); 160 if (sysctlbyname("kern.openfiles", ¤t, &oldlen, NULL, 0) == -1) 161 atf_tc_fail("getsysctlbyname(%s): %s", "kern.openfiles", 162 strerror(errno)); 163 164 oldmaxfiles = maxfiles; 165 166 /* Store old kern.maxfiles in a symlink for cleanup */ 167 snprintf(buf, sizeof(buf), "%d", oldmaxfiles); 168 if (symlink(buf, VALUE) == 1) 169 atf_tc_fail("symlink(%s, %s): %s", buf, VALUE, 170 strerror(errno)); 171 172 maxfiles += EXPANDBY; 173 if (sysctlbyname("kern.maxfiles", NULL, 0, &maxfiles, oldlen) == -1) 174 atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles", 175 strerror(errno)); 176 177 rl.rlim_cur = rl.rlim_max = maxfiles; 178 ATF_REQUIRE_EQ_MSG(0, setrlimit(RLIMIT_NOFILE, &rl), 179 "setrlimit(RLIMIT_NOFILE, %d): %s", maxfiles, strerror(errno)); 180 181 openfiles(oldmaxfiles - current + EXPANDBY / 2); 182 } 183 184 ATF_TC_CLEANUP(kern_maxfiles__increase, tc) 185 { 186 size_t oldlen; 187 int n, oldmaxfiles; 188 char buf[80]; 189 190 if ((n = readlink(VALUE, buf, sizeof(buf))) > 0) { 191 buf[MIN((size_t)n, sizeof(buf) - 1)] = '\0'; 192 if (sscanf(buf, "%d", &oldmaxfiles) == 1) { 193 oldlen = sizeof(oldmaxfiles); 194 (void) sysctlbyname("kern.maxfiles", NULL, 0, 195 &oldmaxfiles, oldlen); 196 } 197 } 198 (void)unlink(VALUE); 199 (void)unlink(AFILE); 200 } 201 202 ATF_TP_ADD_TCS(tp) 203 { 204 205 ATF_TP_ADD_TC(tp, dup2__simple); 206 ATF_TP_ADD_TC(tp, dup2__ebadf_when_2nd_arg_out_of_range); 207 ATF_TP_ADD_TC(tp, kern_maxfiles__increase); 208 209 return (atf_no_error()); 210 } 211