-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrlimit.c
More file actions
195 lines (157 loc) · 4.9 KB
/
rlimit.c
File metadata and controls
195 lines (157 loc) · 4.9 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "rlimit.h"
#include "fail.h"
#include "logging.h"
#include "util.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#ifndef RLIMIT_DEFAULT_CPU
#define RLIMIT_DEFAULT_CPU (0)
#endif
#ifndef RLIMIT_DEFAULT_FSIZE
#define RLIMIT_DEFAULT_FSIZE (0)
#endif
#ifndef RLIMIT_DEFAULT_DATA
#define RLIMIT_DEFAULT_DATA (0)
#endif
#ifndef RLIMIT_DEFAULT_STACK
#define RLIMIT_DEFAULT_STACK (0)
#endif
#ifndef RLIMIT_DEFAULT_CORE
#define RLIMIT_DEFAULT_CORE (0)
#endif
#ifndef RLIMIT_DEFAULT_RSS
#define RLIMIT_DEFAULT_RSS (0)
#endif
#ifndef RLIMIT_DEFAULT_NPROC
#define RLIMIT_DEFAULT_NPROC (0)
#endif
#ifndef RLIMIT_DEFAULT_NOFILE
#define RLIMIT_DEFAULT_NOFILE (0),
#endif
#ifndef RLIMIT_DEFAULT_MEMLOCK
#define RLIMIT_DEFAULT_MEMLOCK (0)
#endif
#ifndef RLIMIT_DEFAULT_AS
#define RLIMIT_DEFAULT_AS (0),
#endif
#ifndef RLIMIT_DEFAULT_LOCKS
#define RLIMIT_DEFAULT_LOCKS (0)
#endif
#ifndef RLIMIT_DEFAULT_SIGPENDING
#define RLIMIT_DEFAULT_SIGPENDING (0)
#endif
#ifndef RLIMIT_DEFAULT_MSGQUEUE
#define RLIMIT_DEFAULT_MSGQUEUE (0)
#endif
#ifndef RLIMIT_DEFAULT_NICE
#define RLIMIT_DEFAULT_NICE (0)
#endif
#ifndef RLIMIT_DEFAULT_RTPRIO
#define RLIMIT_DEFAULT_RTPRIO (0)
#endif
#ifndef RLIMIT_DEFAULT_RTTIME
#define RLIMIT_DEFAULT_RTTIME (0)
#endif
#define RLIMIT_SPEC(rl, v) (struct rlimit_spec) { .name = #rl, .resource = RLIMIT_##rl, .action = RLIMIT_ACTION_ABS, .value = v }
API void LIBR(rlimit_default)(struct rlimit_spec rlimits[], size_t len)
{
struct rlimit_spec defaults[] = {
RLIMIT_SPEC(CPU, RLIMIT_DEFAULT_CPU),
RLIMIT_SPEC(FSIZE, RLIMIT_DEFAULT_FSIZE),
RLIMIT_SPEC(DATA, RLIMIT_DEFAULT_DATA),
RLIMIT_SPEC(STACK, RLIMIT_DEFAULT_STACK),
RLIMIT_SPEC(CORE, RLIMIT_DEFAULT_CORE),
RLIMIT_SPEC(RSS, RLIMIT_DEFAULT_RSS),
RLIMIT_SPEC(NPROC, RLIMIT_DEFAULT_NPROC),
RLIMIT_SPEC(NOFILE, RLIMIT_DEFAULT_NOFILE),
RLIMIT_SPEC(MEMLOCK, RLIMIT_DEFAULT_MEMLOCK),
RLIMIT_SPEC(AS, RLIMIT_DEFAULT_AS),
RLIMIT_SPEC(LOCKS, RLIMIT_DEFAULT_LOCKS),
RLIMIT_SPEC(SIGPENDING, RLIMIT_DEFAULT_SIGPENDING),
RLIMIT_SPEC(MSGQUEUE, RLIMIT_DEFAULT_MSGQUEUE),
RLIMIT_SPEC(NICE, RLIMIT_DEFAULT_NICE),
RLIMIT_SPEC(RTPRIO, RLIMIT_DEFAULT_RTPRIO),
RLIMIT_SPEC(RTTIME, RLIMIT_DEFAULT_RTTIME),
};
assert(RLIMIT_NLIMITS == LENGTH(defaults));
for(int i = 0; i < (int)len; i++) {
assert(i == defaults[i].resource);
memcpy(&rlimits[i], &defaults[i], sizeof(struct rlimit_spec));
}
if(len < LENGTH(defaults)) {
debug("not copying all rlimits: %zu < %zu", len, LENGTH(defaults));
}
}
API void LIBR(rlimit_inherit)(struct rlimit_spec rlimits[], size_t len)
{
for(size_t i = 0; i < len; i++) {
rlimits[i].action = RLIMIT_ACTION_INHERIT;
}
}
API int LIBR(rlimit_parse)(struct rlimit_spec rlimits[], size_t len, const char* str)
{
debug("parsing: %s", str);
size_t L = strlen(str);
size_t l = 0;
while(str[l] != '=' && l < L) {
l += 1;
}
if(l == L) {
info("unable to parse: %s (unexpected end)", str);
return 1;
}
for(size_t i = 0; i < len; i++) {
if(strlen(rlimits[i].name) != l) {
continue;
}
if(0 == strncasecmp(str, rlimits[i].name, l)) {
unsigned long v;
int r = sscanf(str + l + 1, "%lu", &v);
if(r != 1) {
info("unable to parse: %s (value not an unsigned int)", str + l + 1);
return 1;
}
info("rlimit %s: %lu", rlimits[i].name, v);
rlimits[i].action = RLIMIT_ACTION_ABS;
rlimits[i].value = v;
return 0;
}
}
info("unable to parse: %s (no such limit found)", str);
return 1;
}
API void LIBR(rlimit_apply)(const struct rlimit_spec rlimits[], size_t len)
{
debug("applying rlimits");
for(size_t i = 0; i < len; i++) {
struct rlimit rlp;
int r = getrlimit(rlimits[i].resource, &rlp);
CHECK(r, "getrlimit(%s)", rlimits[i].name);
debug("get rlimit %s: soft=%lu hard=%lu",
rlimits[i].name, rlp.rlim_cur, rlp.rlim_max);
if(rlimits[i].action == RLIMIT_ACTION_INHERIT) {
continue;
}
switch(rlimits[i].action) {
case RLIMIT_ACTION_ZERO:
rlp.rlim_cur = 0;
rlp.rlim_max = 0;
break;
case RLIMIT_ACTION_ABS:
rlp.rlim_cur = rlimits[i].value;
rlp.rlim_max = rlimits[i].value;
break;
case RLIMIT_ACTION_EQUAL:
rlp.rlim_max = rlp.rlim_cur;
break;
default:
failwith("unexpected action: %d", rlimits[i].action);
}
debug("set rlimit %s: soft=%lu hard=%lu",
rlimits[i].name, rlp.rlim_cur, rlp.rlim_max);
r = setrlimit(rlimits[i].resource, &rlp);
CHECK(r, "setrlimit(%s)", rlimits[i].name);
}
}