Fix setrlimit_test:setrlimit_memlock when the system has exceeded vm.max_wired.

This uses the same fix as r294894 did for the mlock test.  The code from
that commit is moved into a common object file which PROGS supports
building first.

Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D8689
This commit is contained in:
Bryan Drewery 2016-12-01 22:12:58 +00:00
parent 86a6fcd4ff
commit 710542df20
4 changed files with 146 additions and 76 deletions

View File

@ -50,86 +50,13 @@ __RCSID("$NetBSD: t_mlock.c,v 1.6 2016/08/09 12:02:44 kre Exp $");
#include <limits.h>
#define _KMEMUSER
#include <machine/vmparam.h>
void set_vm_max_wired(int);
void restore_vm_max_wired(void);
#endif
static long page = 0;
#ifdef __FreeBSD__
#define VM_MAX_WIRED "vm.max_wired"
static void
vm_max_wired_sysctl(int *old_value, int *new_value)
{
size_t old_len;
size_t new_len = (new_value == NULL ? 0 : sizeof(int));
if (old_value == NULL)
printf("Setting the new value to %d\n", *new_value);
else {
ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, NULL, &old_len,
new_value, new_len) == 0,
"sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
}
ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, old_value, &old_len,
new_value, new_len) == 0,
"sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
if (old_value != NULL)
printf("Saved the old value (%d)\n", *old_value);
}
static void
set_vm_max_wired(int new_value)
{
FILE *fp;
int old_value;
fp = fopen(VM_MAX_WIRED, "w");
if (fp == NULL) {
atf_tc_skip("could not open %s for writing: %s",
VM_MAX_WIRED, strerror(errno));
return;
}
vm_max_wired_sysctl(&old_value, NULL);
ATF_REQUIRE_MSG(fprintf(fp, "%d", old_value) > 0,
"saving %s failed", VM_MAX_WIRED);
fclose(fp);
vm_max_wired_sysctl(NULL, &new_value);
}
static void
restore_vm_max_wired(void)
{
FILE *fp;
int saved_max_wired;
fp = fopen(VM_MAX_WIRED, "r");
if (fp == NULL) {
perror("fopen failed\n");
return;
}
if (fscanf(fp, "%d", &saved_max_wired) != 1) {
perror("fscanf failed\n");
fclose(fp);
return;
}
fclose(fp);
printf("old value in %s: %d\n", VM_MAX_WIRED, saved_max_wired);
if (saved_max_wired == 0) /* This will cripple the test host */
return;
vm_max_wired_sysctl(NULL, &saved_max_wired);
}
#endif
ATF_TC(mlock_clip);
ATF_TC_HEAD(mlock_clip, tc)
{

View File

@ -50,6 +50,11 @@ __RCSID("$NetBSD: t_setrlimit.c,v 1.5 2016/07/13 09:53:16 njoly Exp $");
#include <ucontext.h>
#include <unistd.h>
#ifdef __FreeBSD__
void set_vm_max_wired(int);
void restore_vm_max_wired(void);
#endif
static void sighandler(int);
static const char path[] = "setrlimit";
@ -235,10 +240,18 @@ sighandler(int signo)
_exit(EXIT_SUCCESS);
}
#ifdef __FreeBSD__
ATF_TC_WITH_CLEANUP(setrlimit_memlock);
#else
ATF_TC(setrlimit_memlock);
#endif
ATF_TC_HEAD(setrlimit_memlock, tc)
{
atf_tc_set_md_var(tc, "descr", "Test setrlimit(2), RLIMIT_MEMLOCK");
#ifdef __FreeBSD__
atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
atf_tc_set_md_var(tc, "require.user", "root");
#endif
}
ATF_TC_BODY(setrlimit_memlock, tc)
@ -249,6 +262,11 @@ ATF_TC_BODY(setrlimit_memlock, tc)
pid_t pid;
int sta;
#ifdef __FreeBSD__
/* Set max_wired really really high to avoid EAGAIN */
set_vm_max_wired(INT_MAX);
#endif
page = sysconf(_SC_PAGESIZE);
ATF_REQUIRE(page >= 0);
@ -292,6 +310,14 @@ ATF_TC_BODY(setrlimit_memlock, tc)
atf_tc_fail("RLIMIT_MEMLOCK not enforced");
}
#ifdef __FreeBSD__
ATF_TC_CLEANUP(setrlimit_memlock, tc)
{
restore_vm_max_wired();
}
#endif
ATF_TC(setrlimit_nofile_1);
ATF_TC_HEAD(setrlimit_nofile_1, tc)
{

View File

@ -68,6 +68,9 @@ LIBADD.timer_create_test+= rt
.include "../Makefile.netbsd-tests"
SRCS.mlock_test+= mlock_helper.c
SRCS.setrlimit_test+= mlock_helper.c
.if ${COMPILER_TYPE} == "gcc"
WARNS?= 3
.else

View File

@ -0,0 +1,114 @@
/*-
* Copyright (C) 2016 Bryan Drewery <bdrewery@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Helper for mlock(3) to avoid EAGAIN errors
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/sysctl.h>
#include <atf-c.h>
#include <errno.h>
#include <stdio.h>
#include <limits.h>
#define VM_MAX_WIRED "vm.max_wired"
static void
vm_max_wired_sysctl(int *old_value, int *new_value)
{
size_t old_len;
size_t new_len = (new_value == NULL ? 0 : sizeof(int));
if (old_value == NULL)
printf("Setting the new value to %d\n", *new_value);
else {
ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, NULL, &old_len,
new_value, new_len) == 0,
"sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
}
ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, old_value, &old_len,
new_value, new_len) == 0,
"sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
if (old_value != NULL)
printf("Saved the old value (%d)\n", *old_value);
}
void
set_vm_max_wired(int new_value)
{
FILE *fp;
int old_value;
fp = fopen(VM_MAX_WIRED, "w");
if (fp == NULL) {
atf_tc_skip("could not open %s for writing: %s",
VM_MAX_WIRED, strerror(errno));
return;
}
vm_max_wired_sysctl(&old_value, NULL);
ATF_REQUIRE_MSG(fprintf(fp, "%d", old_value) > 0,
"saving %s failed", VM_MAX_WIRED);
fclose(fp);
vm_max_wired_sysctl(NULL, &new_value);
}
void
restore_vm_max_wired(void)
{
FILE *fp;
int saved_max_wired;
fp = fopen(VM_MAX_WIRED, "r");
if (fp == NULL) {
perror("fopen failed\n");
return;
}
if (fscanf(fp, "%d", &saved_max_wired) != 1) {
perror("fscanf failed\n");
fclose(fp);
return;
}
fclose(fp);
printf("old value in %s: %d\n", VM_MAX_WIRED, saved_max_wired);
if (saved_max_wired == 0) /* This will cripple the test host */
return;
vm_max_wired_sysctl(NULL, &saved_max_wired);
}