/*-1* Copyright (C) 2016 Bryan Drewery <[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, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Helper for mlock(3) to avoid EAGAIN errors28*/2930#include <sys/types.h>31#include <sys/sysctl.h>3233#include <atf-c.h>34#include <errno.h>35#include <limits.h>36#include <stdio.h>3738#define VM_MAX_WIRED "vm.max_user_wired"3940static void41vm_max_wired_sysctl(u_long *old_value, u_long *new_value)42{43size_t old_len;44size_t new_len = (new_value == NULL ? 0 : sizeof(*new_value));4546if (old_value == NULL)47printf("Setting the new value to %lu\n", *new_value);48else {49ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, NULL, &old_len,50new_value, new_len) == 0,51"sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));52}5354ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, old_value, &old_len,55new_value, new_len) == 0,56"sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));5758if (old_value != NULL)59printf("Saved the old value (%lu)\n", *old_value);60}6162void63set_vm_max_wired(u_long new_value)64{65FILE *fp;66u_long old_value;6768fp = fopen(VM_MAX_WIRED, "w");69if (fp == NULL) {70atf_tc_skip("could not open %s for writing: %s",71VM_MAX_WIRED, strerror(errno));72return;73}7475vm_max_wired_sysctl(&old_value, NULL);7677ATF_REQUIRE_MSG(fprintf(fp, "%lu", old_value) > 0,78"saving %s failed", VM_MAX_WIRED);7980fclose(fp);8182vm_max_wired_sysctl(NULL, &new_value);83}8485void86restore_vm_max_wired(void)87{88FILE *fp;89u_long saved_max_wired;9091fp = fopen(VM_MAX_WIRED, "r");92if (fp == NULL) {93perror("fopen failed\n");94return;95}9697if (fscanf(fp, "%lu", &saved_max_wired) != 1) {98perror("fscanf failed\n");99fclose(fp);100return;101}102103fclose(fp);104printf("old value in %s: %lu\n", VM_MAX_WIRED, saved_max_wired);105106if (saved_max_wired == 0) /* This will cripple the test host */107return;108109vm_max_wired_sysctl(NULL, &saved_max_wired);110}111112113