1cf69771bSopenharmony_ci/* 2cf69771bSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3cf69771bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cf69771bSopenharmony_ci * you may not use this file except in compliance with the License. 5cf69771bSopenharmony_ci * You may obtain a copy of the License at 6cf69771bSopenharmony_ci * 7cf69771bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cf69771bSopenharmony_ci * 9cf69771bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cf69771bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cf69771bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cf69771bSopenharmony_ci * See the License for the specific language governing permissions and 13cf69771bSopenharmony_ci * limitations under the License. 14cf69771bSopenharmony_ci */ 15cf69771bSopenharmony_ci 16cf69771bSopenharmony_ci#include "time_file_utils.h" 17cf69771bSopenharmony_ci 18cf69771bSopenharmony_ci#include <fcntl.h> 19cf69771bSopenharmony_ci#include <sys/stat.h> 20cf69771bSopenharmony_ci 21cf69771bSopenharmony_ci#include "accesstoken_kit.h" 22cf69771bSopenharmony_ci#include "ipc_skeleton.h" 23cf69771bSopenharmony_ci#include "securec.h" 24cf69771bSopenharmony_ci#include "time_hilog.h" 25cf69771bSopenharmony_ci#include "parameters.h" 26cf69771bSopenharmony_ci 27cf69771bSopenharmony_ciconstexpr int CMDLINE_PATH_LEN = 32; 28cf69771bSopenharmony_ciconstexpr int CMDLINE_LEN = 128; 29cf69771bSopenharmony_ci 30cf69771bSopenharmony_cinamespace OHOS { 31cf69771bSopenharmony_cinamespace MiscServices { 32cf69771bSopenharmony_ciconst std::string AUTO_RESTORE_TIMER_APPS = "persist.time.auto_restore_timer_apps"; 33cf69771bSopenharmony_ciusing AccessTokenKit = OHOS::Security::AccessToken::AccessTokenKit; 34cf69771bSopenharmony_ciusing HapTokenInfo = OHOS::Security::AccessToken::HapTokenInfo; 35cf69771bSopenharmony_ciusing TypeATokenTypeEnum = OHOS::Security::AccessToken::TypeATokenTypeEnum; 36cf69771bSopenharmony_ci 37cf69771bSopenharmony_cistd::string TimeFileUtils::GetBundleNameByTokenID(uint32_t tokenID) 38cf69771bSopenharmony_ci{ 39cf69771bSopenharmony_ci auto tokenType = AccessTokenKit::GetTokenTypeFlag(tokenID); 40cf69771bSopenharmony_ci if (tokenType != TypeATokenTypeEnum::TOKEN_HAP) { 41cf69771bSopenharmony_ci return ""; 42cf69771bSopenharmony_ci } 43cf69771bSopenharmony_ci HapTokenInfo hapTokenInfo; 44cf69771bSopenharmony_ci int result = AccessTokenKit::GetHapTokenInfo(tokenID, hapTokenInfo); 45cf69771bSopenharmony_ci if (result != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) { 46cf69771bSopenharmony_ci TIME_HILOGE(TIME_MODULE_SERVICE, "failed to get hap token info, result = %{public}d", result); 47cf69771bSopenharmony_ci return ""; 48cf69771bSopenharmony_ci } 49cf69771bSopenharmony_ci return hapTokenInfo.bundleName; 50cf69771bSopenharmony_ci} 51cf69771bSopenharmony_ci 52cf69771bSopenharmony_cistd::string TimeFileUtils::GetNameByPid(uint32_t pid) 53cf69771bSopenharmony_ci{ 54cf69771bSopenharmony_ci char path[CMDLINE_PATH_LEN] = { 0 }; 55cf69771bSopenharmony_ci if (snprintf_s(path, CMDLINE_PATH_LEN, CMDLINE_PATH_LEN - 1, "/proc/%u/cmdline", pid) <= 0) { 56cf69771bSopenharmony_ci return ""; 57cf69771bSopenharmony_ci } 58cf69771bSopenharmony_ci char cmdline[CMDLINE_LEN] = { 0 }; 59cf69771bSopenharmony_ci int i = 0; 60cf69771bSopenharmony_ci FILE *fp = fopen(path, "r"); 61cf69771bSopenharmony_ci if (fp == nullptr) { 62cf69771bSopenharmony_ci return ""; 63cf69771bSopenharmony_ci } 64cf69771bSopenharmony_ci while (i < (CMDLINE_LEN - 1)) { 65cf69771bSopenharmony_ci char c = static_cast<char>(fgetc(fp)); 66cf69771bSopenharmony_ci // 0. don't need args of cmdline 67cf69771bSopenharmony_ci // 1. ignore unvisible character 68cf69771bSopenharmony_ci if (!isgraph(c)) { 69cf69771bSopenharmony_ci break; 70cf69771bSopenharmony_ci } 71cf69771bSopenharmony_ci cmdline[i] = c; 72cf69771bSopenharmony_ci i++; 73cf69771bSopenharmony_ci } 74cf69771bSopenharmony_ci (void)fclose(fp); 75cf69771bSopenharmony_ci return cmdline; 76cf69771bSopenharmony_ci} 77cf69771bSopenharmony_ci 78cf69771bSopenharmony_cistd::vector<std::string> TimeFileUtils::GetBundleList() 79cf69771bSopenharmony_ci{ 80cf69771bSopenharmony_ci std::vector<std::string> bundleList; 81cf69771bSopenharmony_ci std::string bundleStr = system::GetParameter(AUTO_RESTORE_TIMER_APPS, ""); 82cf69771bSopenharmony_ci size_t start = 0; 83cf69771bSopenharmony_ci do { 84cf69771bSopenharmony_ci size_t end = bundleStr.find(',', start); 85cf69771bSopenharmony_ci if (end < start) { 86cf69771bSopenharmony_ci break; 87cf69771bSopenharmony_ci } 88cf69771bSopenharmony_ci std::string temp = bundleStr.substr(start, end - start); 89cf69771bSopenharmony_ci if (temp.empty()) { 90cf69771bSopenharmony_ci ++start; 91cf69771bSopenharmony_ci continue; 92cf69771bSopenharmony_ci } 93cf69771bSopenharmony_ci bundleList.emplace_back(temp); 94cf69771bSopenharmony_ci if (end == std::string::npos) { 95cf69771bSopenharmony_ci break; 96cf69771bSopenharmony_ci } 97cf69771bSopenharmony_ci start = end + 1; 98cf69771bSopenharmony_ci } while (start < bundleStr.size()); 99cf69771bSopenharmony_ci return bundleList; 100cf69771bSopenharmony_ci} 101cf69771bSopenharmony_ci} // namespace MiscServices 102cf69771bSopenharmony_ci} // namespace OHOS