From b4cdbedeffeb207c3258bd38a22d37838551c8aa Mon Sep 17 00:00:00 2001 From: zhushengle Date: Mon, 29 Aug 2022 19:10:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=B2=99=E7=AE=B1?= =?UTF-8?q?=E5=85=AC=E5=85=B1API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close #I5OE8Q Signed-off-by: zhushengle Change-Id: Ib68e98ad33043d25110c4a0ded28f0042cd5cc7f --- base/BUILD.gn | 1 + base/include/sandbox_utils.h | 25 +++++++++++++ base/src/sandbox_utils.cpp | 72 ++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 base/include/sandbox_utils.h create mode 100644 base/src/sandbox_utils.cpp diff --git a/base/BUILD.gn b/base/BUILD.gn index 75383b1..713d1a4 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -56,6 +56,7 @@ sources_utils = [ "src/timer_event_handler.cpp", "src/ashmem.cpp", "src/rwlock.cpp", + "src/sandbox_utils.cpp", ] securec_sources = [ diff --git a/base/include/sandbox_utils.h b/base/include/sandbox_utils.h new file mode 100644 index 0000000..1a7f5d4 --- /dev/null +++ b/base/include/sandbox_utils.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 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 SANDBOX_UTILS_H +#define SANDBOX_UTILS_H + +#include +#include + +namespace OHOS { +pid_t GetRealPid(void); +} // namespace OHOS +#endif diff --git a/base/src/sandbox_utils.cpp b/base/src/sandbox_utils.cpp new file mode 100644 index 0000000..cd83871 --- /dev/null +++ b/base/src/sandbox_utils.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2022 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 "sandbox_utils.h" +#include + +namespace OHOS { +#ifndef OHOS_LITE +#define PID_STR_SIZE 4 +#define STATUS_LINE_SIZE 1024 + +static int FindAndConvertPid(char *buf) +{ + int count = 0; + char pidBuf[11] = {0}; /* 11: 32 bits to the maximum length of a string */ + char *str = buf; + while (*str != '\0') { + if ((*str >= '0') && (*str <= '9')) { + pidBuf[count] = *str; + count++; + str++; + continue; + } + + if (count > 0) { + break; + } + str++; + } + return atoi(pidBuf); +} +#endif + +pid_t GetRealPid(void) +{ +#ifdef OHOS_LITE + return getpid(); +#else + const char *path = "/proc/self/status"; + char buf[STATUS_LINE_SIZE] = {0}; + FILE *fp = fopen(path, "r"); + if (fp == nullptr) { + return -1; + } + + while (!feof(fp)) { + if (fgets(buf, STATUS_LINE_SIZE, fp) == nullptr) { + fclose(fp); + return -1; + } + if (strncmp(buf, "Pid:", PID_STR_SIZE) == 0) { + break; + } + } + fclose(fp); + + return (pid_t)FindAndConvertPid(buf); +#endif +} +} // namespace OHOS -- Gitee