From 25d24023d37a4e6c6d01d87998180dce9d79777c Mon Sep 17 00:00:00 2001 From: zhushengle Date: Fri, 10 Nov 2023 11:41:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87proc?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=BF=9B=E7=A8=8BID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhushengle Change-Id: Id79621b5b00ae470772bc04180ee8046c65f86d8 --- base/BUILD.gn | 1 + base/include/process_ex.h | 35 +++++++ base/src/process_ex.cpp | 91 +++++++++++++++++++ base/test/unittest/common/BUILD.gn | 14 +++ .../unittest/common/utils_process_test.cpp | 66 ++++++++++++++ 5 files changed, 207 insertions(+) create mode 100644 base/include/process_ex.h create mode 100644 base/src/process_ex.cpp create mode 100644 base/test/unittest/common/utils_process_test.cpp diff --git a/base/BUILD.gn b/base/BUILD.gn index fb3a04c..16648d4 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -83,6 +83,7 @@ sources_utils = [ "src/timer_event_handler.cpp", "src/ashmem.cpp", "src/rwlock.cpp", + "src/process_ex.cpp", ] if (current_os == "ios") { diff --git a/base/include/process_ex.h b/base/include/process_ex.h new file mode 100644 index 0000000..5fc63fa --- /dev/null +++ b/base/include/process_ex.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef UTILS_PROCESS_EX_H +#define UTILS_PROCESS_EX_H + +#include +#include +#include + +namespace OHOS { + +class Process { +public: + static pid_t GetProcPid(void); + +private: + static pid_t procPid_; +}; + +} // OHOS +#endif + diff --git a/base/src/process_ex.cpp b/base/src/process_ex.cpp new file mode 100644 index 0000000..498a1a8 --- /dev/null +++ b/base/src/process_ex.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2023-2023 Huawei Device Co., Ltd. + * 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 "process_ex.h" + +#include +#include +#include + +namespace OHOS { + +pid_t Process::procPid_ = -1; +const int PID_MAX_LEN = 11; +const int PID_STR_LEN = 5; + +static int32_t FindAndConvertPid(char *buf, int len) +{ + int count = 0; + char pidBuf[PID_MAX_LEN] = {0}; + char *str = buf; + + if (len <= PID_STR_LEN) { + return -1; + } + + while (*str != '\0') { + if ((*str >= '0') && (*str <= '9') && (count < PID_MAX_LEN)) { + pidBuf[count] = *str; + count++; + str++; + continue; + } + + if (count > 0) { + break; + } + str++; + } + return atoi(pidBuf); +} + +static int32_t GetProcessProcPid(void) +{ + const int STATUS_LINE_SIZE = 255; + const char *path = "/proc/self/status"; + char buf[STATUS_LINE_SIZE] = {0}; + + FILE *fp = fopen(path, "r"); + if (fp == nullptr) { + return -errno; + } + + while (!feof(fp)) { + if (fgets(buf, STATUS_LINE_SIZE, fp) == nullptr) { + fclose(fp); + return -errno; + } + if (strncmp(buf, "Tgid:", PID_STR_LEN) == 0) { + break; + } + } + (void)fclose(fp); + + return FindAndConvertPid(buf, strlen(buf)); +} + +pid_t Process::GetProcPid(void) +{ + if (Process::procPid_ < 0) { + int32_t pid = GetProcessProcPid(); + if (pid < 1) { + return -1; + } + Process::procPid_ = static_cast(pid); + } + return Process::procPid_; +} + +} // OHOS diff --git a/base/test/unittest/common/BUILD.gn b/base/test/unittest/common/BUILD.gn index b7c2f52..86ec2a7 100644 --- a/base/test/unittest/common/BUILD.gn +++ b/base/test/unittest/common/BUILD.gn @@ -318,6 +318,19 @@ ohos_unittest("UtilsRWLockTest") { ] } +############################################################################### +ohos_unittest("UtilsProcessTest") { + module_out_path = module_output_path + sources = [ "utils_process_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//commonlibrary/c_utils/base:utils", + "//third_party/googletest:gtest_main", + ] +} + ############################################################################### group("unittest") { @@ -334,6 +347,7 @@ group("unittest") { ":UtilsMappedFileTest", ":UtilsObserverTest", ":UtilsParcelTest", + ":UtilsProcessTest", ":UtilsRWLockTest", ":UtilsRefbaseTest", ":UtilsSafeBlockQueueTest", diff --git a/base/test/unittest/common/utils_process_test.cpp b/base/test/unittest/common/utils_process_test.cpp new file mode 100644 index 0000000..d3ae2a2 --- /dev/null +++ b/base/test/unittest/common/utils_process_test.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * 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 "process_ex.h" +#include + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace { + +class UtilsProcessTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void UtilsProcessTest::SetUpTestCase(void) +{ + // input testsuit setup step +} + +void UtilsProcessTest::TearDownTestCase(void) +{ + // input testsuit teardown step +} + +void UtilsProcessTest::SetUp(void) +{ + // recover times +} + +void UtilsProcessTest::TearDown(void) +{ + // recover times +} + +/* + * @tc.name: testProcess001 + * @tc.desc: ThreadTest + */ +HWTEST_F(UtilsProcessTest, testProcess001, TestSize.Level0) +{ + pid_t pid1 = Process::GetProcPid(); + EXPECT_EQ(pid1 > 1, true); + pid_t pid2 = Process::GetProcPid(); + EXPECT_EQ(pid2 > 1, true); + EXPECT_EQ(pid1 == pid2, true); +} +} // namespace +} // namespace OHOS -- Gitee