From 8678af23d0d09f3171de0c86ea5c552948878f04 Mon Sep 17 00:00:00 2001 From: Wiktor Garbacz Date: Fri, 17 May 2019 01:55:13 -0700 Subject: [PATCH] Extract GetRlimitName into util PiperOrigin-RevId: 248682931 Change-Id: I702533a8d36465de956a1a90a40c634434b7a671 --- sandboxed_api/sandbox2/monitor.cc | 33 +++++++------------------------ sandboxed_api/sandbox2/util.cc | 18 +++++++++++++++++ sandboxed_api/sandbox2/util.h | 3 +++ 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/sandboxed_api/sandbox2/monitor.cc b/sandboxed_api/sandbox2/monitor.cc index c3bf9ca..9122dc8 100644 --- a/sandboxed_api/sandbox2/monitor.cc +++ b/sandboxed_api/sandbox2/monitor.cc @@ -480,41 +480,22 @@ bool Monitor::InitSendCwd() { bool Monitor::InitApplyLimit(pid_t pid, __rlimit_resource resource, const rlimit64& rlim) const { - std::string rlim_name = absl::StrCat("UNKNOWN: ", resource); - switch (resource) { - case RLIMIT_AS: - rlim_name = "RLIMIT_AS"; - break; - case RLIMIT_FSIZE: - rlim_name = "RLIMIT_FSIZE"; - break; - case RLIMIT_NOFILE: - rlim_name = "RLIMIT_NOFILE"; - break; - case RLIMIT_CPU: - rlim_name = "RLIMIT_CPU"; - break; - case RLIMIT_CORE: - rlim_name = "RLIMIT_CORE"; - break; - default: - break; - } - rlimit64 curr_limit; if (prlimit64(pid, resource, nullptr, &curr_limit) == -1) { - PLOG(ERROR) << "prlimit64(" << pid << ", " << rlim_name << ")"; + PLOG(ERROR) << "prlimit64(" << pid << ", " << util::GetRlimitName(resource) + << ")"; } else if (rlim.rlim_cur > curr_limit.rlim_max) { // In such case, don't update the limits, as it will fail. Just stick to the // current ones (which are already lower than intended). - LOG(ERROR) << rlim_name << ": new.current > current.max (" << rlim.rlim_cur - << " > " << curr_limit.rlim_max << "), skipping"; + LOG(ERROR) << util::GetRlimitName(resource) + << ": new.current > current.max (" << rlim.rlim_cur << " > " + << curr_limit.rlim_max << "), skipping"; return true; } if (prlimit64(pid, resource, &rlim, nullptr) == -1) { - PLOG(ERROR) << "prlimit64(" << pid << ", " << rlim_name << ", " - << rlim.rlim_cur << ")"; + PLOG(ERROR) << "prlimit64(" << pid << ", " << util::GetRlimitName(resource) + << ", " << rlim.rlim_cur << ")"; return false; } diff --git a/sandboxed_api/sandbox2/util.cc b/sandboxed_api/sandbox2/util.cc index d58783c..3b74bfa 100644 --- a/sandboxed_api/sandbox2/util.cc +++ b/sandboxed_api/sandbox2/util.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -261,6 +262,23 @@ std::string GetSignalName(int signo) { return absl::StrFormat("%s [%d]", kSignalNames[signo], signo); } +std::string GetRlimitName(int resource) { + switch (resource) { + case RLIMIT_AS: + return "RLIMIT_AS"; + case RLIMIT_FSIZE: + return "RLIMIT_FSIZE"; + case RLIMIT_NOFILE: + return "RLIMIT_NOFILE"; + case RLIMIT_CPU: + return "RLIMIT_CPU"; + case RLIMIT_CORE: + return "RLIMIT_CORE"; + default: + return absl::StrCat("UNKNOWN: ", resource); + } +} + ::sapi::StatusOr ReadCPathFromPid(pid_t pid, uintptr_t ptr) { std::string path(PATH_MAX, '\0'); iovec local_iov[] = {{&path[0], path.size()}}; diff --git a/sandboxed_api/sandbox2/util.h b/sandboxed_api/sandbox2/util.h index 3e0439c..e574882 100644 --- a/sandboxed_api/sandbox2/util.h +++ b/sandboxed_api/sandbox2/util.h @@ -69,6 +69,9 @@ bool CreateMemFd(int* fd, const char* name = "buffer_file"); // Returns signal description. std::string GetSignalName(int signo); +// Returns rlimit resource name +std::string GetRlimitName(int resource); + // Reads a path string (NUL-terminated, shorter than PATH_MAX) from another // process memory ::sapi::StatusOr ReadCPathFromPid(pid_t pid, uintptr_t ptr);