-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathlimits.c
145 lines (124 loc) · 4.21 KB
/
limits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <glib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "flags.h"
#include "limits.h"
// This file is part of halfempty - a fast, parallel testcase minimization tool.
//
// Parse and setup rlimits for child processes.
//
static const gchar * limit_to_str(uint8_t limit)
{
static const gchar * limits[UINT8_MAX] = {
[RLIMIT_CPU] = "RLIMIT_CPU",
[RLIMIT_FSIZE] = "RLIMIT_FSIZE",
[RLIMIT_DATA] = "RLIMIT_DATA",
[RLIMIT_STACK] = "RLIMIT_STACK",
[RLIMIT_CORE] = "RLIMIT_CORE",
[RLIMIT_RSS] = "RLIMIT_RSS",
[RLIMIT_NOFILE] = "RLIMIT_NOFILE",
[RLIMIT_NPROC] = "RLIMIT_NPROC",
[RLIMIT_MEMLOCK] = "RLIMIT_MEMLOCK",
#ifdef __linux__
[RLIMIT_AS] = "RLIMIT_AS",
[RLIMIT_LOCKS] = "RLIMIT_LOCKS",
[RLIMIT_SIGPENDING] = "RLIMIT_SIGPENDING",
[RLIMIT_MSGQUEUE] = "RLIMIT_MSGQUEUE",
[RLIMIT_NICE] = "RLIMIT_NICE",
[RLIMIT_RTPRIO] = "RLIMIT_RTPRIO",
[RLIMIT_RTTIME] = "RLIMIT_RTTIME",
#endif
};
return limits[limit];
}
static gint str_to_limit(const gchar *limit)
{
guint i;
for (i = 0; i < RLIMIT_NLIMITS; i++) {
if (limit_to_str(i) && strcmp(limit, limit_to_str(i)) == 0)
return i;
}
return -1;
}
gboolean decode_proc_limit(const gchar *option_name,
const gchar *value,
gpointer data,
GError **error)
{
gint resource;
gint limit;
gchar **param;
g_assert_cmpstr(option_name, ==, "--limit");
g_assert_nonnull(value);
// value will be of them form RLIMIT_FOO=12345.
param = g_strsplit(value, "=", 2);
// Check we got two parameters.
if (param[0] == NULL || param[1] == NULL) {
g_warning("You passed the string %s to %s, but that is not a valid limit specification",
value,
option_name);
g_warning("See setrlimit(3) manual for a full list, for example, --limit RLIMIT_CPU=120");
g_strfreev(param);
return false;
}
// Decode those two options.
resource = str_to_limit(param[0]);
limit = g_ascii_strtoll(param[1], NULL, 0);
if (resource == -1) {
g_warning("You passed the string %s to %s, but `%s` is not recognized as a limit name",
value,
option_name,
*param);
g_warning("See setrlimit(3) manual for a full list, for example, --limit RLIMIT_CPU=120");
g_strfreev(param);
return false;
}
kChildLimits[resource].rlim_cur = limit;
kChildLimits[resource].rlim_max = limit;
g_strfreev(param);
return true;
}
static void __attribute__((constructor)) init_child_limits()
{
g_debug("configuring default rlimits for child process");
for (gint i = 0; i < RLIMIT_NLIMITS; i++) {
if (getrlimit(i, &kChildLimits[i]) != 0) {
g_warning("failed to getrlimit for %u, %s", i, strerror(errno));
}
g_debug("Configured rlimit %s => { %llu, %llu }",
limit_to_str(i),
(unsigned long long)kChildLimits[i].rlim_cur,
(unsigned long long)kChildLimits[i].rlim_max);
}
// OK, but lets set some sane defaults.
kChildLimits[RLIMIT_CORE].rlim_cur = 0;
kChildLimits[RLIMIT_CORE].rlim_max = 0;
}