xref: /freebsd/lib/libc/tests/sys/mlock_helper.c (revision 315ee00f)
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 #include <sys/types.h>
33 #include <sys/sysctl.h>
34 
35 #include <atf-c.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <stdio.h>
39 
40 #define	VM_MAX_WIRED "vm.max_user_wired"
41 
42 static void
43 vm_max_wired_sysctl(u_long *old_value, u_long *new_value)
44 {
45 	size_t old_len;
46 	size_t new_len = (new_value == NULL ? 0 : sizeof(*new_value));
47 
48 	if (old_value == NULL)
49 		printf("Setting the new value to %lu\n", *new_value);
50 	else {
51 		ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, NULL, &old_len,
52 		    new_value, new_len) == 0,
53 		    "sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
54 	}
55 
56 	ATF_REQUIRE_MSG(sysctlbyname(VM_MAX_WIRED, old_value, &old_len,
57 	    new_value, new_len) == 0,
58 	    "sysctlbyname(%s) failed: %s", VM_MAX_WIRED, strerror(errno));
59 
60 	if (old_value != NULL)
61 		printf("Saved the old value (%lu)\n", *old_value);
62 }
63 
64 void
65 set_vm_max_wired(u_long new_value)
66 {
67 	FILE *fp;
68 	u_long old_value;
69 
70 	fp = fopen(VM_MAX_WIRED, "w");
71 	if (fp == NULL) {
72 		atf_tc_skip("could not open %s for writing: %s",
73 		    VM_MAX_WIRED, strerror(errno));
74 		return;
75 	}
76 
77 	vm_max_wired_sysctl(&old_value, NULL);
78 
79 	ATF_REQUIRE_MSG(fprintf(fp, "%lu", old_value) > 0,
80 	    "saving %s failed", VM_MAX_WIRED);
81 
82 	fclose(fp);
83 
84 	vm_max_wired_sysctl(NULL, &new_value);
85 }
86 
87 void
88 restore_vm_max_wired(void)
89 {
90 	FILE *fp;
91 	u_long saved_max_wired;
92 
93 	fp = fopen(VM_MAX_WIRED, "r");
94 	if (fp == NULL) {
95 		perror("fopen failed\n");
96 		return;
97 	}
98 
99 	if (fscanf(fp, "%lu", &saved_max_wired) != 1) {
100 		perror("fscanf failed\n");
101 		fclose(fp);
102 		return;
103 	}
104 
105 	fclose(fp);
106 	printf("old value in %s: %lu\n", VM_MAX_WIRED, saved_max_wired);
107 
108 	if (saved_max_wired == 0) /* This will cripple the test host */
109 		return;
110 
111 	vm_max_wired_sysctl(NULL, &saved_max_wired);
112 }
113