diff --git a/base/BUILD.gn b/base/BUILD.gn index 75383b1c3f45184207e2605daa74df7cd73edf4e..713d1a4e977fb3aa62d390e7936437379b434f87 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 0000000000000000000000000000000000000000..1a7f5d4df2cd28e4f60e41fa5d8e04c679055680 --- /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 0000000000000000000000000000000000000000..cd83871b07fa6dbe72ce06a0c75b557b0e498ded --- /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