From: Sangwan Kwon Date: Fri, 17 Jan 2020 02:29:29 +0000 (+0900) Subject: Get process executable path from pid X-Git-Tag: accepted/tizen/unified/20200810.122954~92 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9414659f27234357e5d0e231c02a1fc7cd49b51d;p=platform%2Fcore%2Fsecurity%2Fvist.git Get process executable path from pid Signed-off-by: Sangwan Kwon --- diff --git a/src/vist/common/tests/process.cpp b/src/vist/common/tests/process.cpp new file mode 100644 index 0000000..fea8f1f --- /dev/null +++ b/src/vist/common/tests/process.cpp @@ -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 + +#include + +#include + +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 index 0000000..b364587 --- /dev/null +++ b/src/vist/process.hpp @@ -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 + +#include +#include +#include + +#include +#include +#include + +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 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