forked from msantos/librlimit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
librlimit.c
144 lines (121 loc) · 3.11 KB
/
librlimit.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 (c) 2019-2022, Michael Santos <michael.santos@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <err.h>
#define LRL_RESOURCE(x) \
{ x, #x }
typedef struct {
int resource;
char *name;
} lrl_resource_t;
static const lrl_resource_t limits[] = {
#ifdef RLIMIT_AS
LRL_RESOURCE(RLIMIT_AS),
#endif
#ifdef RLIMIT_CORE
LRL_RESOURCE(RLIMIT_CORE),
#endif
#ifdef RLIMIT_CPU
LRL_RESOURCE(RLIMIT_CPU),
#endif
#ifdef RLIMIT_DATA
LRL_RESOURCE(RLIMIT_DATA),
#endif
#ifdef RLIMIT_FSIZE
LRL_RESOURCE(RLIMIT_FSIZE),
#endif
#ifdef RLIMIT_LOCKS
LRL_RESOURCE(RLIMIT_LOCKS),
#endif
#ifdef RLIMIT_MEMLOCK
LRL_RESOURCE(RLIMIT_MEMLOCK),
#endif
#ifdef RLIMIT_MSGQUEUE
LRL_RESOURCE(RLIMIT_MSGQUEUE),
#endif
#ifdef RLIMIT_NICE
LRL_RESOURCE(RLIMIT_NICE),
#endif
#ifdef RLIMIT_NOFILE
LRL_RESOURCE(RLIMIT_NOFILE),
#endif
#ifdef RLIMIT_NPROC
LRL_RESOURCE(RLIMIT_NPROC),
#endif
#ifdef RLIMIT_RSS
LRL_RESOURCE(RLIMIT_RSS),
#endif
#ifdef RLIMIT_RTPRIO
LRL_RESOURCE(RLIMIT_RTPRIO),
#endif
#ifdef RLIMIT_RTTIME
LRL_RESOURCE(RLIMIT_RTTIME),
#endif
#ifdef RLIMIT_SIGPENDING
LRL_RESOURCE(RLIMIT_SIGPENDING),
#endif
#ifdef RLIMIT_STACK
LRL_RESOURCE(RLIMIT_STACK),
#endif
{0, NULL}};
enum { LRL_OPT_EXIT = 1, LRL_OPT_DEBUG = 2 };
void _init(void);
int lrl_setrlimit(int resource, char *rlim);
void lrl_error(int opt, char *msg);
void _init(void) {
char *rlim;
char *env_opt;
int opt = LRL_OPT_EXIT;
const lrl_resource_t *p;
env_opt = getenv("LIBRLIMIT_OPT");
if (env_opt != NULL) {
opt = atoi(env_opt);
}
for (p = limits; p->name != NULL; p++) {
rlim = getenv(p->name);
if (lrl_setrlimit(p->resource, rlim) < 0)
lrl_error(opt, p->name);
}
}
int lrl_setrlimit(int resource, char *rlim) {
struct rlimit rl = {0};
char *endptr;
rlim_t cur = 0;
if (rlim == NULL)
return 0;
errno = 0;
cur = (rlim_t)strtoul(rlim, &endptr, 10);
if (errno != 0)
return -1;
if ((endptr == rlim) || *endptr != '\0') {
errno = EINVAL;
return -1;
}
rl.rlim_cur = cur;
rl.rlim_max = cur;
return setrlimit(resource, &rl);
}
void lrl_error(int opt, char *msg) {
if (opt & LRL_OPT_DEBUG)
warn("librlimit: %s", msg);
if (opt & LRL_OPT_EXIT)
_exit(111);
}