Skip to content

Commit

Permalink
util: implement rLimName() to use in cmdline/config
Browse files Browse the repository at this point in the history
  • Loading branch information
robertswiecki committed Oct 9, 2023
1 parent e7c7583 commit a5c0d71
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
9 changes: 4 additions & 5 deletions cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,10 @@ uint64_t parseRLimit(int res, const char *optarg, unsigned long mul) {
return cur.rlim_max;
}
if (!util::isANumber(optarg)) {
LOG_F("RLIMIT %d needs a numeric or 'max'/'hard'/'def'/'soft'/'inf' "
"value "
"('%s' "
"provided)",
res, optarg);
LOG_F(
"RLIMIT %s (%d) needs a numeric value or 'max'/'hard'/'def'/'soft'/'inf' value "
"(%s provided)",
util::rLimName(res).c_str(), res, QC(optarg));
}
errno = 0;
uint64_t val = strtoull(optarg, NULL, 0);
Expand Down
2 changes: 1 addition & 1 deletion subproc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ static void newProc(nsjconf_t* nsjconf, int netfd, int fd_in, int fd_out, int fd
execv(nsjconf->exec_file.c_str(), (char* const*)argv.data());
}

PLOG_E("execve('%s') failed", nsjconf->exec_file.c_str());
PLOG_E("execve(%s) failed", QC(nsjconf->exec_file));
}

static void addProc(nsjconf_t* nsjconf, pid_t pid, int sock) {
Expand Down
40 changes: 40 additions & 0 deletions util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,46 @@ const std::string sigName(int signo) {
return res;
}

const std::string rLimName(int res) {
std::string ret;

struct {
const int res;
const char* const name;
} static const rLimNames[] = {
NS_VALSTR_STRUCT(RLIMIT_CPU),
NS_VALSTR_STRUCT(RLIMIT_FSIZE),
NS_VALSTR_STRUCT(RLIMIT_DATA),
NS_VALSTR_STRUCT(RLIMIT_STACK),
NS_VALSTR_STRUCT(RLIMIT_CORE),
NS_VALSTR_STRUCT(RLIMIT_RSS),
NS_VALSTR_STRUCT(RLIMIT_NOFILE),
NS_VALSTR_STRUCT(RLIMIT_OFILE),
NS_VALSTR_STRUCT(RLIMIT_AS),
NS_VALSTR_STRUCT(RLIMIT_NPROC),
NS_VALSTR_STRUCT(RLIMIT_MEMLOCK),
NS_VALSTR_STRUCT(RLIMIT_LOCKS),
NS_VALSTR_STRUCT(RLIMIT_SIGPENDING),
NS_VALSTR_STRUCT(RLIMIT_MSGQUEUE),
NS_VALSTR_STRUCT(RLIMIT_NICE),
NS_VALSTR_STRUCT(RLIMIT_RTPRIO),
NS_VALSTR_STRUCT(RLIMIT_RTTIME),
NS_VALSTR_STRUCT(RLIMIT_NLIMITS),
};

for (const auto& i : rLimNames) {
if (res == i.res) {
ret.append(i.name);
return ret;
}
}

ret.append("SIGUNKNOWN(");
ret.append(std::to_string(res));
ret.append(")");
return ret;
}

const std::string timeToStr(time_t t) {
char timestr[128];
struct tm utctime;
Expand Down
1 change: 1 addition & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const std::string StrQuote(const std::string& str);
bool isANumber(const char* s);
uint64_t rnd64(void);
const std::string sigName(int signo);
const std::string rLimName(int res);
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,
Expand Down

0 comments on commit a5c0d71

Please sign in to comment.