2 * Copyright (C) 2016 Bryan Drewery <bdrewery@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Helper for mlock(3) to avoid EAGAIN errors
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #include <sys/sysctl.h>
42 #define VM_MAX_WIRED "vm.max_wired"
45 vm_max_wired_sysctl(int *old_value, int *new_value)
48 size_t new_len = (new_value == NULL ? 0 : sizeof(int));
50 if (old_value == NULL)
51 printf("Setting the new value to %d\n", *new_value);
53 ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, NULL, &old_len,
54 new_value, new_len) == 0,
55 "sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
58 ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, old_value, &old_len,
59 new_value, new_len) == 0,
60 "sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
62 if (old_value != NULL)
63 printf("Saved the old value (%d)\n", *old_value);
67 set_vm_max_wired(int new_value)
72 fp = fopen(VM_MAX_WIRED, "w");
74 atf_tc_skip("could not open %s for writing: %s",
75 VM_MAX_WIRED, strerror(errno));
79 vm_max_wired_sysctl(&old_value, NULL);
81 ATF_REQUIRE_MSG(fprintf(fp, "%d", old_value) > 0,
82 "saving %s failed", VM_MAX_WIRED);
86 vm_max_wired_sysctl(NULL, &new_value);
90 restore_vm_max_wired(void)
95 fp = fopen(VM_MAX_WIRED, "r");
97 perror("fopen failed\n");
101 if (fscanf(fp, "%d", &saved_max_wired) != 1) {
102 perror("fscanf failed\n");
108 printf("old value in %s: %d\n", VM_MAX_WIRED, saved_max_wired);
110 if (saved_max_wired == 0) /* This will cripple the test host */
113 vm_max_wired_sysctl(NULL, &saved_max_wired);