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