From 807e2f21fd8580eb45719c1e74d4f69cb83c799f Mon Sep 17 00:00:00 2001 From: ngie Date: Wed, 13 May 2015 12:17:01 +0000 Subject: MFC r262781,r263336: r262781 (by pho): Preserve naming consistency for test cases. Pointed out by: jmmv Sponsored by: EMC / Isilon storage division r263336 (by pho): Added sysctl kern.maxfiles increase test, do not use /etc/passwd for tests and use volatile sig_atomic_t for signal handler variable. Reviewed by: asomers (previous version) Sponsored by: EMC / Isilon storage division --- tests/sys/kern/kern_descrip_test.c | 128 ++++++++++++++++++++++++++++++++++--- 1 file changed, 120 insertions(+), 8 deletions(-) (limited to 'tests/sys') diff --git a/tests/sys/kern/kern_descrip_test.c b/tests/sys/kern/kern_descrip_test.c index 59a8163..6953b00 100644 --- a/tests/sys/kern/kern_descrip_test.c +++ b/tests/sys/kern/kern_descrip_test.c @@ -29,22 +29,31 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include #include #include +#include #include #include -ATF_TC_WITHOUT_HEAD(dup2_simple); -ATF_TC_BODY(dup2_simple, tc) +static volatile sig_atomic_t done; + +#define AFILE "afile" +#define EXPANDBY 1000 +#define PARALLEL 4 +#define RENDEZVOUS "rendezvous" +#define VALUE "value" + +ATF_TC_WITHOUT_HEAD(dup2__simple); +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,22 +66,125 @@ ATF_TC_HEAD(dup2__ebadf_when_2nd_arg_out_of_range, tc) { 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); 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); + + 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, "require.config", "allow_sysctl_side_effects"); + 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; + + /* Store old kern.maxfiles in a symlink for cleanup */ + snprintf(buf, sizeof(buf), "%d", oldmaxfiles); + if (symlink(buf, VALUE) == 1) + atf_tc_fail("symlink(%s, %s): %s", buf, VALUE, + strerror(errno)); + + maxfiles += EXPANDBY; + if (sysctlbyname("kern.maxfiles", NULL, 0, &maxfiles, oldlen) == -1) + atf_tc_fail("getsysctlbyname(%s): %s", "kern.maxfiles", + strerror(errno)); + + openfiles(oldmaxfiles - current + 1); + (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, 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(); + return (atf_no_error()); } -- cgit v1.1