xref: /freebsd/lib/libc/tests/sys/mlock_helper.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1710542dfSBryan Drewery /*-
2710542dfSBryan Drewery  * Copyright (C) 2016 Bryan Drewery <bdrewery@FreeBSD.org>
3710542dfSBryan Drewery  * All rights reserved.
4710542dfSBryan Drewery  *
5710542dfSBryan Drewery  * Redistribution and use in source and binary forms, with or without
6710542dfSBryan Drewery  * modification, are permitted provided that the following conditions
7710542dfSBryan Drewery  * are met:
8710542dfSBryan Drewery  * 1. Redistributions of source code must retain the above copyright
9710542dfSBryan Drewery  *    notice, this list of conditions and the following disclaimer.
10710542dfSBryan Drewery  * 2. Redistributions in binary form must reproduce the above copyright
11710542dfSBryan Drewery  *    notice, this list of conditions and the following disclaimer in the
12710542dfSBryan Drewery  *    documentation and/or other materials provided with the distribution.
13710542dfSBryan Drewery  *
14710542dfSBryan Drewery  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15710542dfSBryan Drewery  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16710542dfSBryan Drewery  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17710542dfSBryan Drewery  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18710542dfSBryan Drewery  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19710542dfSBryan Drewery  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20710542dfSBryan Drewery  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21710542dfSBryan Drewery  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22710542dfSBryan Drewery  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23710542dfSBryan Drewery  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24710542dfSBryan Drewery  * SUCH DAMAGE.
25710542dfSBryan Drewery  */
26710542dfSBryan Drewery 
27710542dfSBryan Drewery /*
28710542dfSBryan Drewery  * Helper for mlock(3) to avoid EAGAIN errors
29710542dfSBryan Drewery  */
30710542dfSBryan Drewery 
31710542dfSBryan Drewery #include <sys/types.h>
32710542dfSBryan Drewery #include <sys/sysctl.h>
33710542dfSBryan Drewery 
34710542dfSBryan Drewery #include <atf-c.h>
35710542dfSBryan Drewery #include <errno.h>
36710542dfSBryan Drewery #include <limits.h>
3713876dc9SEnji Cooper #include <stdio.h>
38710542dfSBryan Drewery 
39*54a3a114SMark Johnston #define	VM_MAX_WIRED "vm.max_user_wired"
40710542dfSBryan Drewery 
41710542dfSBryan Drewery static void
vm_max_wired_sysctl(u_long * old_value,u_long * new_value)42*54a3a114SMark Johnston vm_max_wired_sysctl(u_long *old_value, u_long *new_value)
43710542dfSBryan Drewery {
44710542dfSBryan Drewery 	size_t old_len;
45*54a3a114SMark Johnston 	size_t new_len = (new_value == NULL ? 0 : sizeof(*new_value));
46710542dfSBryan Drewery 
47710542dfSBryan Drewery 	if (old_value == NULL)
48*54a3a114SMark Johnston 		printf("Setting the new value to %lu\n", *new_value);
49710542dfSBryan Drewery 	else {
50710542dfSBryan Drewery 		ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, NULL, &old_len,
51710542dfSBryan Drewery 		    new_value, new_len) == 0,
52710542dfSBryan Drewery 		    "sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
53710542dfSBryan Drewery 	}
54710542dfSBryan Drewery 
55710542dfSBryan Drewery 	ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, old_value, &old_len,
56710542dfSBryan Drewery 	    new_value, new_len) == 0,
57710542dfSBryan Drewery 	    "sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
58710542dfSBryan Drewery 
59710542dfSBryan Drewery 	if (old_value != NULL)
60*54a3a114SMark Johnston 		printf("Saved the old value (%lu)\n", *old_value);
61710542dfSBryan Drewery }
62710542dfSBryan Drewery 
63710542dfSBryan Drewery void
set_vm_max_wired(u_long new_value)64*54a3a114SMark Johnston set_vm_max_wired(u_long new_value)
65710542dfSBryan Drewery {
66710542dfSBryan Drewery 	FILE *fp;
67*54a3a114SMark Johnston 	u_long old_value;
68710542dfSBryan Drewery 
69710542dfSBryan Drewery 	fp = fopen(VM_MAX_WIRED, "w");
70710542dfSBryan Drewery 	if (fp == NULL) {
71710542dfSBryan Drewery 		atf_tc_skip("could not open %s for writing: %s",
72710542dfSBryan Drewery 		    VM_MAX_WIRED, strerror(errno));
73710542dfSBryan Drewery 		return;
74710542dfSBryan Drewery 	}
75710542dfSBryan Drewery 
76710542dfSBryan Drewery 	vm_max_wired_sysctl(&old_value, NULL);
77710542dfSBryan Drewery 
78*54a3a114SMark Johnston 	ATF_REQUIRE_MSG(fprintf(fp, "%lu", old_value) > 0,
79710542dfSBryan Drewery 	    "saving %s failed", VM_MAX_WIRED);
80710542dfSBryan Drewery 
81710542dfSBryan Drewery 	fclose(fp);
82710542dfSBryan Drewery 
83710542dfSBryan Drewery 	vm_max_wired_sysctl(NULL, &new_value);
84710542dfSBryan Drewery }
85710542dfSBryan Drewery 
86710542dfSBryan Drewery void
restore_vm_max_wired(void)87710542dfSBryan Drewery restore_vm_max_wired(void)
88710542dfSBryan Drewery {
89710542dfSBryan Drewery 	FILE *fp;
90*54a3a114SMark Johnston 	u_long saved_max_wired;
91710542dfSBryan Drewery 
92710542dfSBryan Drewery 	fp = fopen(VM_MAX_WIRED, "r");
93710542dfSBryan Drewery 	if (fp == NULL) {
94710542dfSBryan Drewery 		perror("fopen failed\n");
95710542dfSBryan Drewery 		return;
96710542dfSBryan Drewery 	}
97710542dfSBryan Drewery 
98*54a3a114SMark Johnston 	if (fscanf(fp, "%lu", &saved_max_wired) != 1) {
99710542dfSBryan Drewery 		perror("fscanf failed\n");
100710542dfSBryan Drewery 		fclose(fp);
101710542dfSBryan Drewery 		return;
102710542dfSBryan Drewery 	}
103710542dfSBryan Drewery 
104710542dfSBryan Drewery 	fclose(fp);
105*54a3a114SMark Johnston 	printf("old value in %s: %lu\n", VM_MAX_WIRED, saved_max_wired);
106710542dfSBryan Drewery 
107710542dfSBryan Drewery 	if (saved_max_wired == 0) /* This will cripple the test host */
108710542dfSBryan Drewery 		return;
109710542dfSBryan Drewery 
110710542dfSBryan Drewery 	vm_max_wired_sysctl(NULL, &saved_max_wired);
111710542dfSBryan Drewery }
112