woj-sandbox/resource.c

46 lines
1.7 KiB
C
Raw Normal View History

2022-10-02 16:06:27 +08:00
#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(void) {
LOG_INFO("Setting resource limit");
char *mem_limit_str = getenv(LIMIT_MEMORY); // in mb
char *nproc_limit_str = getenv(LIMIT_NPROC);
char *time_limit_str = getenv(LIMIT_TIME); // in ms
long mem_limit = 0, nproc_limit = 0, time_limit = 0;
if (mem_limit_str) {
// convert to bytes and double the memory limit
mem_limit = strtol(mem_limit_str, NULL, 10) * 1024 * 1024 * 2;
}
if (nproc_limit_str) {
nproc_limit = strtol(nproc_limit_str, NULL, 10);
}
if (time_limit_str) {
long limit = strtol(time_limit_str, NULL, 10);
2022-10-09 00:06:07 +08:00
time_limit = limit ? (limit + 1000) / 1000 : 0;
2022-10-02 16:06:27 +08:00
}
SET_LIMIT(RLIMIT_AS, mem_limit, "memory", "bytes");
SET_LIMIT(RLIMIT_NPROC, nproc_limit, "nproc", "processes"); // per user
2022-10-09 00:06:07 +08:00
SET_LIMIT(RLIMIT_CPU, time_limit, "time", "seconds"); // except blocked time
2022-10-02 16:06:27 +08:00
}