Get process executable path from pid
authorSangwan Kwon <sangwan.kwon@samsung.com>
Fri, 17 Jan 2020 02:29:29 +0000 (11:29 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Fri, 17 Jan 2020 06:24:01 +0000 (15:24 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/common/tests/process.cpp [new file with mode: 0644]
src/vist/process.hpp [new file with mode: 0644]

diff --git a/src/vist/common/tests/process.cpp b/src/vist/common/tests/process.cpp
new file mode 100644 (file)
index 0000000..fea8f1f
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ *  Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#include <gtest/gtest.h>
+
+#include <vist/process.hpp>
+
+#include <string>
+
+using namespace vist;
+
+TEST(ProcessTests, path_positive)
+{
+       auto name = Process::GetPath(Process::GetPid());
+       EXPECT_EQ("/usr/bin/vist-test", name);
+}
+
+TEST(ProcessTests, path_negative)
+{
+       bool raised = false;
+       try {
+               Process::GetPath(-1);
+       } catch(...) {
+               raised = true;
+       }
+
+       EXPECT_TRUE(raised);
+}
diff --git a/src/vist/process.hpp b/src/vist/process.hpp
new file mode 100644 (file)
index 0000000..b364587
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ *  Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#pragma once
+
+#include <vist/exception.hpp>
+
+#include <cstdio>
+#include <memory>
+#include <string>
+
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+namespace vist {
+
+struct Process {
+       static pid_t GetPid()
+       {
+               return ::getpid();
+       }
+
+       static std::string GetPath(pid_t pid)
+       {
+               std::string cmdline = "/proc/" + std::to_string(pid) + "/exe";
+
+               /// c++17 std::filesystem::read_symlink
+               std::vector<char> buf(1024);
+               auto size = ::readlink(cmdline.c_str(), buf.data(), buf.size());
+               if (size == -1)
+                       THROW(ErrCode::RuntimeError) << "Failed to get process path: " << errno;
+
+               return std::string(buf.begin(), buf.begin() + size);
+       }
+};
+
+} // namespace vist