#include "resource.h" #include "err.h" #include "utils/log.h" #include #include #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 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; } 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; } 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 }