Index: kern_descrip_test.c =================================================================== --- kern_descrip_test.c (revision 262885) +++ kern_descrip_test.c (working copy) @@ -29,14 +29,23 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include #include #include +#include #include #include +static volatile sig_atomic_t done; + +#define AFILE "afile" +#define PARALLEL 4 +#define RENDEZVOUS "rendezvous" +#define VALUE "value" + ATF_TC_WITHOUT_HEAD(dup2__simple); ATF_TC_BODY(dup2__simple, tc) { @@ -43,8 +52,7 @@ ATF_TC_BODY(dup2__simple, tc) int fd1, fd2; struct stat sb1, sb2; - fd1 = open("/etc/passwd", O_RDONLY); - ATF_REQUIRE(fd1 >= 0); + ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1); fd2 = 27; ATF_REQUIRE(dup2(fd1, fd2) != -1); ATF_REQUIRE(fstat(fd1, &sb1) != -1); @@ -57,11 +65,12 @@ ATF_TC_HEAD(dup2__ebadf_when_2nd_arg_out_of_range, { atf_tc_set_md_var(tc, "descr", "Regression test for r234131"); } + ATF_TC_BODY(dup2__ebadf_when_2nd_arg_out_of_range, tc) { int fd1, fd2, ret; - fd1 = open("/etc/passwd", O_RDONLY); + ATF_REQUIRE((fd1 = open(AFILE, O_CREAT, 0644)) != -1); fd2 = INT_MAX; ret = dup2(fd1, fd2); ATF_CHECK_EQ(-1, ret); @@ -68,11 +77,118 @@ ATF_TC_BODY(dup2__ebadf_when_2nd_arg_out_of_range, ATF_CHECK_EQ(EBADF, errno); } +static void +handler(int s __unused) +{ + done++; +} + +static void +openfiles2(size_t n) +{ + size_t i; + int r; + + errno = 0; + for (i = 0; i < n; i++) + ATF_REQUIRE((r = open(AFILE, O_RDONLY)) != -1); + kill(getppid(), SIGUSR1); + + if (r != -1) { + for (;;) { + if (access(RENDEZVOUS, R_OK) != 0) + break; + usleep(1000); + } + } + _exit(0); +} + +static void +openfiles(size_t n) +{ + int i, fd; + + signal(SIGUSR1, handler); + ATF_REQUIRE((fd = open(AFILE, O_CREAT, 0644)) != -1); + close(fd); + ATF_REQUIRE((fd = open(RENDEZVOUS, O_CREAT, 0644)) != -1); + close(fd); + done = 0; + for (i = 0; i < PARALLEL; i++) + if (fork() == 0) + openfiles2(n / PARALLEL); + while (done != PARALLEL) + usleep(1000); + unlink(RENDEZVOUS); + usleep(40000); +} + +ATF_TC_WITH_CLEANUP(kern_maxfiles__increase); +ATF_TC_HEAD(kern_maxfiles__increase, tc) +{ + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "descr", + "Check kern.maxfiles expansion"); +} + +ATF_TC_BODY(kern_maxfiles__increase, tc) +{ + size_t oldlen; + int maxfiles, oldmaxfiles, current; + char buf[80]; + + oldlen = sizeof(maxfiles); + if (sysctlbyname("kern.maxfiles", &maxfiles, &oldlen, NULL, 0) == -1) + atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles", + strerror(errno)); + if (sysctlbyname("kern.openfiles", ¤t, &oldlen, NULL, 0) == -1) + atf_tc_fail("getsysctlbyname(%s): %s", "kern.openfiles", + strerror(errno)); + + oldmaxfiles = maxfiles; + + snprintf(buf, sizeof(buf), "%d", oldmaxfiles); + if (symlink(buf, VALUE) == 1) + atf_tc_fail("symlink(%s, %s): %s", buf, VALUE, + strerror(errno)); + + maxfiles += 1000; + if (sysctlbyname("kern.maxfiles", NULL, 0, &maxfiles, oldlen) == -1) + atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles", + strerror(errno)); + + openfiles(oldmaxfiles - current + 50); + + if (sysctlbyname("kern.maxfiles", NULL, 0, &oldmaxfiles, + oldlen) == -1) + atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles", + strerror(errno)); + (void)unlink(VALUE); +} + +ATF_TC_CLEANUP(kern_maxfiles__increase, tc) +{ + size_t oldlen; + int n, oldmaxfiles; + char buf[80]; + + if ((n = readlink(VALUE, buf, sizeof(buf))) > 0) { + buf[n] = '\0'; + if (sscanf(buf, "%d", &oldmaxfiles) == 1) { + oldlen = sizeof(oldmaxfiles); + (void) sysctlbyname("kern.maxfiles", NULL, 0, &oldmaxfiles, + oldlen); + } + } +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, dup2__simple); ATF_TP_ADD_TC(tp, dup2__ebadf_when_2nd_arg_out_of_range); + ATF_TP_ADD_TC(tp, kern_maxfiles__increase); return atf_no_error(); }