Skip to content

Commit

Permalink
contain: use prlimit64 instead of setrlimit64 which seems to be glibc…
Browse files Browse the repository at this point in the history
…-specific, so it compiles with musl too
  • Loading branch information
robertswiecki committed Sep 29, 2023
1 parent fbeac46 commit f388cad
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ uint64_t parseRLimit(int res, const char *optarg, unsigned long mul) {
return RLIM64_INFINITY;
}
struct rlimit64 cur;
if (getrlimit64(res, &cur) == -1) {
if (util::getrlimit(res, &cur) == -1) {
PLOG_F("getrlimit(%d)", res);
}
if (strcasecmp(optarg, "def") == 0 || strcasecmp(optarg, "soft") == 0) {
Expand Down
40 changes: 20 additions & 20 deletions contain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,53 +141,53 @@ static bool containSetLimits(nsjconf_t* nsjconf) {

struct rlimit64 rl;
rl.rlim_cur = rl.rlim_max = nsjconf->rl_as;
if (setrlimit64(RLIMIT_AS, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_AS, %" PRIu64 ")", nsjconf->rl_as);
if (util::setrlimit(RLIMIT_AS, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_AS, %" PRIu64 ")", nsjconf->rl_as);
return false;
}
rl.rlim_cur = rl.rlim_max = nsjconf->rl_core;
if (setrlimit64(RLIMIT_CORE, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_CORE, %" PRIu64 ")", nsjconf->rl_core);
if (util::setrlimit(RLIMIT_CORE, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_CORE, %" PRIu64 ")", nsjconf->rl_core);
return false;
}
rl.rlim_cur = rl.rlim_max = nsjconf->rl_cpu;
if (setrlimit64(RLIMIT_CPU, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_CPU, %" PRIu64 ")", nsjconf->rl_cpu);
if (util::setrlimit(RLIMIT_CPU, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_CPU, %" PRIu64 ")", nsjconf->rl_cpu);
return false;
}
rl.rlim_cur = rl.rlim_max = nsjconf->rl_fsize;
if (setrlimit64(RLIMIT_FSIZE, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_FSIZE, %" PRIu64 ")", nsjconf->rl_fsize);
if (util::setrlimit(RLIMIT_FSIZE, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_FSIZE, %" PRIu64 ")", nsjconf->rl_fsize);
return false;
}
rl.rlim_cur = rl.rlim_max = nsjconf->rl_nofile;
if (setrlimit64(RLIMIT_NOFILE, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_NOFILE, %" PRIu64 ")", nsjconf->rl_nofile);
if (util::setrlimit(RLIMIT_NOFILE, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_NOFILE, %" PRIu64 ")", nsjconf->rl_nofile);
return false;
}
rl.rlim_cur = rl.rlim_max = nsjconf->rl_nproc;
if (setrlimit64(RLIMIT_NPROC, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_NPROC, %" PRIu64 ")", nsjconf->rl_nproc);
if (util::setrlimit(RLIMIT_NPROC, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_NPROC, %" PRIu64 ")", nsjconf->rl_nproc);
return false;
}
rl.rlim_cur = rl.rlim_max = nsjconf->rl_stack;
if (setrlimit64(RLIMIT_STACK, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_STACK, %" PRIu64 ")", nsjconf->rl_stack);
if (util::setrlimit(RLIMIT_STACK, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_STACK, %" PRIu64 ")", nsjconf->rl_stack);
return false;
}
rl.rlim_cur = rl.rlim_max = nsjconf->rl_mlock;
if (setrlimit64(RLIMIT_MEMLOCK, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_MEMLOCK, %" PRIu64 ")", nsjconf->rl_mlock);
if (util::setrlimit(RLIMIT_MEMLOCK, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_MEMLOCK, %" PRIu64 ")", nsjconf->rl_mlock);
return false;
}
rl.rlim_cur = rl.rlim_max = nsjconf->rl_rtpr;
if (setrlimit64(RLIMIT_RTPRIO, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_RTPRIO, %" PRIu64 ")", nsjconf->rl_rtpr);
if (util::setrlimit(RLIMIT_RTPRIO, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_RTPRIO, %" PRIu64 ")", nsjconf->rl_rtpr);
return false;
}
rl.rlim_cur = rl.rlim_max = nsjconf->rl_msgq;
if (setrlimit64(RLIMIT_MSGQUEUE, &rl) == -1) {
PLOG_E("setrlimit64(0, RLIMIT_MSGQUEUE , %" PRIu64 ")", nsjconf->rl_msgq);
if (util::setrlimit(RLIMIT_MSGQUEUE, rl) == -1) {
PLOG_E("util::setrlimit(0, RLIMIT_MSGQUEUE , %" PRIu64 ")", nsjconf->rl_msgq);
return false;
}
return true;
Expand Down
8 changes: 8 additions & 0 deletions util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,12 @@ long syscall(long sysno, uintptr_t a0, uintptr_t a1, uintptr_t a2, uintptr_t a3,
return ::syscall(sysno, a0, a1, a2, a3, a4, a5);
}

long setrlimit(int res, const struct rlimit64& newlim) {
return util::syscall(__NR_prlimit64, 0, res, (uintptr_t)&newlim, (uintptr_t) nullptr);
}

long getrlimit(int res, struct rlimit64* curlim) {
return util::syscall(__NR_prlimit64, 0, res, (uintptr_t) nullptr, (uintptr_t)curlim);
}

} // namespace util
11 changes: 11 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/resource.h>

#include <string>
#include <vector>
Expand All @@ -41,6 +42,14 @@

#define QC(x) (util::StrQuote(x).c_str())

#if !defined(RLIM64_INFINITY)
#define RLIM64_INFINITY (~0ULL)
struct rlimit64 {
uint64_t rlim_cur;
uint64_t rlim_max;
};
#endif /* !defined(RLIM64_INFINITY) */

namespace util {

ssize_t readFromFd(int fd, void* buf, size_t len);
Expand All @@ -60,6 +69,8 @@ const std::string timeToStr(time_t t);
std::vector<std::string> strSplit(const std::string str, char delim);
long syscall(long sysno, uintptr_t a0 = 0, uintptr_t a1 = 0, uintptr_t a2 = 0, uintptr_t a3 = 0,
uintptr_t a4 = 0, uintptr_t a5 = 0);
long setrlimit(int res, const struct rlimit64& newlim);
long getrlimit(int res, struct rlimit64* curlim);

} // namespace util

Expand Down

0 comments on commit f388cad

Please sign in to comment.