From 7966a722064e04f3986fd72f4ead66e7cd07b0ec Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Tue, 25 Feb 2020 15:53:41 +0900 Subject: [PATCH] Fix to get process identifier from cmdline Signed-off-by: Sangwan Kwon --- src/vist/common/tests/process.cpp | 6 ++--- src/vist/policy/api.cpp | 4 +-- src/vist/process.hpp | 51 +++++++++++++++++++-------------------- src/vist/service/tests/core.cpp | 4 +-- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/vist/common/tests/process.cpp b/src/vist/common/tests/process.cpp index fea8f1f..a7b79d4 100644 --- a/src/vist/common/tests/process.cpp +++ b/src/vist/common/tests/process.cpp @@ -24,15 +24,15 @@ using namespace vist; TEST(ProcessTests, path_positive) { - auto name = Process::GetPath(Process::GetPid()); - EXPECT_EQ("/usr/bin/vist-test", name); + auto name = Process::GetIdentifier(Process::GetPid()); + EXPECT_EQ("vist-test", name); } TEST(ProcessTests, path_negative) { bool raised = false; try { - Process::GetPath(-1); + Process::GetIdentifier(-1); } catch(...) { raised = true; } diff --git a/src/vist/policy/api.cpp b/src/vist/policy/api.cpp index 61ff676..98e6b6b 100644 --- a/src/vist/policy/api.cpp +++ b/src/vist/policy/api.cpp @@ -39,9 +39,9 @@ void API::Admin::Set(const std::string& policy, const PolicyValue& value) std::string admin; auto peer = rmi::Gateway::GetPeerCredentials(); if (peer == nullptr) - admin = Process::GetPath(Process::GetPid()); + admin = Process::GetIdentifier(Process::GetPid()); else - admin = Process::GetPath(peer->pid); + admin = Process::GetIdentifier(peer->pid); PolicyManager::Instance().set(policy, value, admin); } diff --git a/src/vist/process.hpp b/src/vist/process.hpp index 29e4ebc..ff7548d 100644 --- a/src/vist/process.hpp +++ b/src/vist/process.hpp @@ -39,34 +39,24 @@ struct Process { return ::getpid(); } - /// TODO(Sangwan): Unify the method which get process identifier - static std::string GetPath(pid_t pid) + static std::string GetIdentifier(pid_t pid) { - std::string exe = "/proc/" + std::to_string(pid) + "/exe"; - - /// c++17 std::filesystem::read_symlink - std::vector buf(1024); errno = 0; - auto size = ::readlink(exe.c_str(), buf.data(), buf.size()); - if (size == -1) { - WARN(VIST) << "Failed to get process path by exe: " << exe - << ", errno: " << errno; - - std::string cmdline = "/proc/" + std::to_string(pid) + "/cmdline"; - int fd = ::open(cmdline.c_str(), O_RDONLY); - if (fd == -1) - THROW(ErrCode::RuntimeError) << "Failed to get process path: " << cmdline; + std::string cmdline = "/proc/" + std::to_string(pid) + "/cmdline"; + int fd = ::open(cmdline.c_str(), O_RDONLY); + if (fd == -1) + THROW(ErrCode::RuntimeError) << "Failed to get process path: " << cmdline; - errno = 0; - size = ::read(fd, buf.data(), buf.size()); - ::close(fd); + errno = 0; + std::vector buf(1024); + auto size = ::read(fd, buf.data(), buf.size()); + ::close(fd); - if (size == -1) - THROW(ErrCode::RuntimeError) << "Failed to get process path: " << cmdline - << ", errno: " << errno; + if (size == -1) + THROW(ErrCode::RuntimeError) << "Failed to get process path: " << cmdline + << ", errno: " << errno; - buf[size - 1] = '\0'; - } + buf[size - 1] = '\0'; return canonicalize(std::string(buf.begin(), buf.begin() + size)); } @@ -74,9 +64,18 @@ struct Process { private: static std::string canonicalize(std::string&& s) { - auto predicate = [](unsigned char c){ return std::isspace(c) || c == '\0'; }; - auto base = std::find_if(s.begin(), s.end(), predicate); - s.erase(base, s.end()); + { /// rtrim + auto predicate = [](unsigned char c){ return std::isspace(c) || c == '\0'; }; + auto base = std::find_if(s.begin(), s.end(), predicate); + s.erase(base, s.end()); + } + + { /// ltrim + auto predicate = [](unsigned char c){ return c == '/'; }; + auto base = std::find_if(s.rbegin(), s.rend(), predicate).base(); + s.erase(s.begin(), base); + } + return s; } }; diff --git a/src/vist/service/tests/core.cpp b/src/vist/service/tests/core.cpp index c7fc564..83d6f73 100644 --- a/src/vist/service/tests/core.cpp +++ b/src/vist/service/tests/core.cpp @@ -41,7 +41,7 @@ TEST_F(CoreTests, query_select) TEST_F(CoreTests, query_update) { - policy::API::Admin::Enroll("/usr/bin/vist-test"); + policy::API::Admin::Enroll("vist-test"); std::string statement = "SELECT * FROM policy WHERE name = 'sample-int-policy'"; auto rows = Vistd::Query(statement); @@ -56,5 +56,5 @@ TEST_F(CoreTests, query_update) rows = Vistd::Query(statement); EXPECT_EQ(rows[0]["value"], "I/10"); - policy::API::Admin::Disenroll("/usr/bin/vist-test"); + policy::API::Admin::Disenroll("vist-test"); } -- 2.7.4