55 lines
2.0 KiB
C
55 lines
2.0 KiB
C
#include "resource.h"
|
|
#include "err.h"
|
|
#include "utils/log.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <sys/resource.h>
|
|
|
|
#define SET_LIMIT(resource, limit, name, unit) \
|
|
do { \
|
|
if (limit) { \
|
|
LOG_INFO("Setting " name " limit to %ld " unit, limit); \
|
|
if (setrlimit(resource, &(struct rlimit){limit, limit})) { \
|
|
LOG_ERR("Failed to set " name " limit"); \
|
|
exit(ERR_RLIMIT_SET); \
|
|
} \
|
|
} \
|
|
} while (0)
|
|
|
|
void setup_rlimit(char *config[CFG_IS_VALID + 1]) {
|
|
LOG_INFO("Setting resource limit");
|
|
|
|
char *mem_limit_str = config[CFG_MEMORY_LIMIT]; // in mb
|
|
char *nproc_limit_str = config[CFG_NPROC_LIMIT];
|
|
char *time_limit_str = config[CFG_TIME_LIMIT]; // in ms
|
|
char *fsize_limit_str = config[CFG_FSIZE_LIMIT]; // in mb
|
|
|
|
long mem_limit = 0, nproc_limit = 0, time_limit = 0, fsize_limit = 0;
|
|
|
|
if (mem_limit_str) {
|
|
// convert to bytes
|
|
mem_limit = strtol(mem_limit_str, NULL, 10) * 1024 * 1024;
|
|
}
|
|
|
|
if (nproc_limit_str) {
|
|
nproc_limit = strtol(nproc_limit_str, NULL, 10);
|
|
}
|
|
|
|
if (time_limit_str) {
|
|
long limit = strtol(time_limit_str, NULL, 10);
|
|
time_limit = limit ? (limit + 1000) / 1000 : 0;
|
|
}
|
|
|
|
if (fsize_limit_str) {
|
|
// convert to bytes
|
|
fsize_limit = strtol(fsize_limit_str, NULL, 10) * 1024 * 1024;
|
|
}
|
|
|
|
SET_LIMIT(RLIMIT_AS, mem_limit, "memory", "bytes");
|
|
SET_LIMIT(RLIMIT_NPROC, nproc_limit, "nproc", "processes"); // per user, **not** subprocess
|
|
SET_LIMIT(RLIMIT_CPU, time_limit, "time", "seconds"); // except blocked time
|
|
SET_LIMIT(RLIMIT_FSIZE, fsize_limit, "fsize", "bytes");
|
|
|
|
SET_LIMIT(RLIMIT_CORE, 0, "coredump", "bytes"); // disable core dump
|
|
}
|