1 /*- 2 * Copyright (C) 2016 Bryan Drewery <bdrewery@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Helper for mlock(3) to avoid EAGAIN errors 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/types.h> 35 #include <sys/sysctl.h> 36 37 #include <atf-c.h> 38 #include <errno.h> 39 #include <limits.h> 40 #include <stdio.h> 41 42 #define VM_MAX_WIRED "vm.max_user_wired" 43 44 static void 45 vm_max_wired_sysctl(u_long *old_value, u_long *new_value) 46 { 47 size_t old_len; 48 size_t new_len = (new_value == NULL ? 0 : sizeof(*new_value)); 49 50 if (old_value == NULL) 51 printf("Setting the new value to %lu\n", *new_value); 52 else { 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)); 56 } 57 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)); 61 62 if (old_value != NULL) 63 printf("Saved the old value (%lu)\n", *old_value); 64 } 65 66 void 67 set_vm_max_wired(u_long new_value) 68 { 69 FILE *fp; 70 u_long old_value; 71 72 fp = fopen(VM_MAX_WIRED, "w"); 73 if (fp == NULL) { 74 atf_tc_skip("could not open %s for writing: %s", 75 VM_MAX_WIRED, strerror(errno)); 76 return; 77 } 78 79 vm_max_wired_sysctl(&old_value, NULL); 80 81 ATF_REQUIRE_MSG(fprintf(fp, "%lu", old_value) > 0, 82 "saving %s failed", VM_MAX_WIRED); 83 84 fclose(fp); 85 86 vm_max_wired_sysctl(NULL, &new_value); 87 } 88 89 void 90 restore_vm_max_wired(void) 91 { 92 FILE *fp; 93 u_long saved_max_wired; 94 95 fp = fopen(VM_MAX_WIRED, "r"); 96 if (fp == NULL) { 97 perror("fopen failed\n"); 98 return; 99 } 100 101 if (fscanf(fp, "%lu", &saved_max_wired) != 1) { 102 perror("fscanf failed\n"); 103 fclose(fp); 104 return; 105 } 106 107 fclose(fp); 108 printf("old value in %s: %lu\n", VM_MAX_WIRED, saved_max_wired); 109 110 if (saved_max_wired == 0) /* This will cripple the test host */ 111 return; 112 113 vm_max_wired_sysctl(NULL, &saved_max_wired); 114 } 115