1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "common.h"
17 #include <fcntl.h>
18 #include <array>
19 #include <cinttypes>
20 #include <csignal>
21 #include <dirent.h>
22 #include <fstream>
23 #include <iostream>
24 #include <parameter.h>
25 #include <parameters.h>
26 #include <sstream>
27 #include <sys/file.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32
33 #include "application_info.h"
34 #include "bundle_mgr_proxy.h"
35 #include "file_ex.h"
36 #include "iservice_registry.h"
37 #include "logging.h"
38 #include "system_ability_definition.h"
39 #include "os_account_info.h"
40 #include "os_account_manager.h"
41
42 using namespace OHOS;
43 using namespace OHOS::AppExecFwk;
44 namespace COMMON {
45 namespace {
46 const std::map<std::string, clockid_t> clockIdMap = {
47 {"realtime", CLOCK_REALTIME},
48 {"mono", CLOCK_MONOTONIC},
49 {"process_cputime_id", CLOCK_PROCESS_CPUTIME_ID},
50 {"thread_cputime_id", CLOCK_THREAD_CPUTIME_ID},
51 {"mono_raw", CLOCK_MONOTONIC_RAW},
52 {"realtime_coarse", CLOCK_REALTIME_COARSE},
53 {"mono_coarse", CLOCK_MONOTONIC_COARSE},
54 {"boot", CLOCK_BOOTTIME},
55 {"realtime_alarm", CLOCK_REALTIME_ALARM},
56 {"boot_alarm", CLOCK_BOOTTIME_ALARM},
57 {"sgi_cycle", CLOCK_SGI_CYCLE},
58 {"tai", CLOCK_TAI},
59 };
60 constexpr int EXECVP_ERRNO = 2;
61 const int SHELL_UID = 2000;
62 const std::string DEFAULT_PATH = "/data/local/tmp/";
63 constexpr int READ = 0;
64 constexpr int WRITE = 1;
65 const int FILE_PATH_SIZE = 512;
66 const int BUFFER_SIZE = 1024;
67 const int INVALID_PID = -1;
68 constexpr int32_t EC_INVALID_VALUE = -2;
69 const std::string KEY_HIVIEW_USER_TYPE = "const.logsystem.versiontype";
70 }
71
IsProcessRunning(int & lockFileFd)72 bool IsProcessRunning(int& lockFileFd)
73 {
74 setgid(SHELL_UID);
75 char buffer[PATH_MAX + 1] = {0};
76 readlink("/proc/self/exe", buffer, PATH_MAX);
77 std::string processName = buffer;
78 int pos = static_cast<int>(processName.find_last_of('/'));
79 if (pos != 0) {
80 processName = processName.substr(pos + 1, processName.size());
81 }
82
83 std::string fileName = DEFAULT_PATH + processName + ".pid";
84 umask(S_IWOTH);
85 int fd = open(fileName.c_str(), O_WRONLY | O_CREAT, static_cast<mode_t>(0664)); // 0664: rw-rw-r--
86 if (fd < 0) {
87 const int bufSize = 256;
88 char buf[bufSize] = {0};
89 strerror_r(errno, buf, bufSize);
90 PROFILER_LOG_ERROR(LOG_CORE, "%s:failed to open(%s), errno(%d:%s)", __func__, fileName.c_str(), errno, buf);
91 return false;
92 }
93 int flags = fcntl(fd, F_GETFD);
94 if (flags == -1) {
95 close(fd);
96 PROFILER_LOG_ERROR(LOG_CORE, "%s: get fd flags failed!", __func__);
97 return false;
98 }
99 flags |= FD_CLOEXEC;
100 if (fcntl(fd, F_SETFD, flags) == -1) {
101 close(fd);
102 PROFILER_LOG_ERROR(LOG_CORE, "%s: set fd_cloexec failed!", __func__);
103 return false;
104 }
105 if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
106 // 进程正在运行,加锁失败
107 close(fd);
108 printf("%s is running, please don't start it again.\n", processName.c_str());
109 PROFILER_LOG_ERROR(LOG_CORE, "%s is running, please don't start it again.", processName.c_str());
110 return true;
111 }
112 std::string pidStr = std::to_string(getpid());
113 auto nbytes = write(fd, pidStr.data(), pidStr.size());
114 lockFileFd = fd;
115 CHECK_TRUE(static_cast<size_t>(nbytes) == pidStr.size(), false, "write pid FAILED!");
116 return false;
117 }
118
IsProcessExist(const std::string & processName,int & pid)119 bool IsProcessExist(const std::string& processName, int& pid)
120 {
121 DIR* dir = opendir("/proc");
122 CHECK_NOTNULL(dir, false, "open /proc dir failed");
123 struct dirent* ptr;
124 int pidValue = INVALID_PID;
125 while ((ptr = readdir(dir)) != nullptr) {
126 if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) {
127 continue;
128 }
129 if ((!isdigit(*ptr->d_name)) || ptr->d_type != DT_DIR) {
130 continue;
131 }
132 char filePath[FILE_PATH_SIZE] = {0};
133 int len = snprintf_s(filePath, FILE_PATH_SIZE, FILE_PATH_SIZE - 1, "/proc/%s/cmdline", ptr->d_name);
134 if (len < 0) {
135 PROFILER_LOG_WARN(LOG_CORE, "maybe, the contents of cmdline had be cut off");
136 continue;
137 }
138 FILE* fp = fopen(filePath, "r");
139 if (fp == nullptr) {
140 PROFILER_LOG_DEBUG(LOG_CORE, "open file failed! %s", filePath);
141 continue;
142 }
143 char buf[BUFFER_SIZE] = {0};
144 if (fgets(buf, sizeof(buf) - 1, fp) == nullptr) {
145 fclose(fp);
146 continue;
147 }
148 std::string str(buf);
149 size_t found = str.rfind("/");
150 std::string fullProcess;
151 if (found != std::string::npos) {
152 fullProcess = str.substr(found + 1);
153 } else {
154 fullProcess = str;
155 }
156 if (fullProcess == processName) {
157 pidValue = atoi(ptr->d_name);
158 fclose(fp);
159 break;
160 }
161 fclose(fp);
162 }
163 closedir(dir);
164 if (pidValue != INVALID_PID) {
165 pid = pidValue;
166 }
167 return pidValue != INVALID_PID;
168 }
169
CloseStdio()170 static void CloseStdio()
171 {
172 close(STDIN_FILENO);
173 close(STDOUT_FILENO);
174 close(STDERR_FILENO);
175 }
176
StartProcess(const std::string & processBin,std::vector<char * > & argv)177 int StartProcess(const std::string& processBin, std::vector<char*>& argv)
178 {
179 int pid = fork();
180 if (pid == 0) {
181 CloseStdio();
182 argv.push_back(nullptr); // last item in argv must be NULL
183 int retval = execvp(processBin.c_str(), argv.data());
184 if (retval == -1 && errno == EXECVP_ERRNO) {
185 printf("warning: %s does not exist!\n", processBin.c_str());
186 PROFILER_LOG_WARN(LOG_CORE, "warning: %s does not exist!", processBin.c_str());
187 }
188 _exit(EXIT_FAILURE);
189 }
190
191 return pid;
192 }
193
KillProcess(int pid)194 int KillProcess(int pid)
195 {
196 if (pid == -1) {
197 return -1;
198 }
199 int stat;
200 kill(pid, SIGTERM);
201 if (waitpid(pid, &stat, 0) == -1) {
202 if (errno != EINTR) {
203 stat = -1;
204 }
205 }
206 return stat;
207 }
208
PrintMallinfoLog(const std::string & mallInfoPrefix,const struct mallinfo2 & mi)209 void PrintMallinfoLog(const std::string& mallInfoPrefix, const struct mallinfo2& mi)
210 {
211 #ifdef HOOK_ENABLE
212 std::string mallinfoLog = mallInfoPrefix;
213 mallinfoLog += "arena = " + std::to_string(mi.arena) + ", ordblks = " + std::to_string(mi.ordblks);
214 mallinfoLog += ", smblks = " + std::to_string(mi.smblks) + ", hblks = " + std::to_string(mi.hblks);
215 mallinfoLog += ", hblkhd = " + std::to_string(mi.hblkhd) + ", usmblks = " + std::to_string(mi.usmblks);
216 mallinfoLog +=
217 ", fsmblks = " + std::to_string(mi.fsmblks) + ", uordblks = " + std::to_string(mi.uordblks);
218 mallinfoLog +=
219 ", fordblks = " + std::to_string(mi.fordblks) + ", keepcost = " + std::to_string(mi.keepcost);
220 PROFILER_LOG_INFO(LOG_CORE, "%s", mallinfoLog.c_str());
221 #endif // HOOK_ENABLE
222 }
223
CustomFdClose(int & fd)224 inline int CustomFdClose(int& fd)
225 {
226 int ret = close(fd);
227 if (ret == 0) {
228 fd = -1;
229 }
230 return ret;
231 }
232
CustomFdFclose(FILE ** fp)233 inline int CustomFdFclose(FILE** fp)
234 {
235 int ret = fclose(*fp);
236 if (ret == 0) {
237 *fp = nullptr;
238 }
239 return ret;
240 }
241
CustomPopen(const std::vector<std::string> & command,const char * type,int fds[],volatile pid_t & childPid,bool needUnblock)242 FILE* CustomPopen(const std::vector<std::string>& command, const char* type, int fds[],
243 volatile pid_t& childPid, bool needUnblock)
244 {
245 PROFILER_LOG_DEBUG(LOG_CORE, "BEGN %s: ready!", __func__);
246 if (command.empty() || type == nullptr) {
247 PROFILER_LOG_ERROR(LOG_CORE, "%s: param invalid", __func__);
248 return nullptr;
249 }
250
251 // only allow "r" or "w"
252 if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) {
253 errno = EINVAL;
254 return nullptr;
255 }
256
257 CHECK_TRUE(pipe(fds) == 0, nullptr, "Pipe open failed!");
258
259 pid_t pid = fork();
260 if (pid == -1) {
261 perror("fork");
262 exit(1);
263 }
264
265 if (pid == 0) {
266 char* const envp[] = {nullptr};
267 // execve : the last argv must be nullptr.
268 std::vector<char*> argv(command.size() + 1, nullptr);
269 for (size_t i = 0, cmdSize = command.size(); i < cmdSize; i++) {
270 argv[i] = const_cast<char*>(command[i].data());
271 }
272
273 if (strncmp(type, "r", strlen(type)) == 0) {
274 CHECK_TRUE(CustomFdClose(fds[READ]) == 0, nullptr, "CustomFdClose failed!");
275 dup2(fds[WRITE], STDOUT_FILENO); // Redirect stdout to pipe
276 CHECK_TRUE(CustomFdClose(fds[WRITE]) == 0, nullptr, "CustomFdClose failed!");
277 } else {
278 CHECK_TRUE(CustomFdClose(fds[WRITE]) == 0, nullptr, "CustomFdClose failed!");
279 dup2(fds[READ], STDIN_FILENO); // Redirect stdin to pipe
280 CHECK_TRUE(CustomFdClose(fds[READ]) == 0, nullptr, "CustomFdClose failed!");
281 }
282
283 setpgid(pid, pid);
284 // exe path = argv[0]; exe name = argv[1]
285 if (execve(argv[0], &argv[1], envp) == -1) {
286 PROFILER_LOG_ERROR(LOG_CORE, "execve failed {%s:%s}", __func__, strerror(errno));
287 exit(EXIT_FAILURE);
288 }
289 }
290
291 if (!needUnblock) {
292 if (strncmp(type, "r", strlen(type)) == 0) {
293 // Close the WRITE end of the pipe since parent's fd is read-only
294 CHECK_TRUE(CustomFdClose(fds[WRITE]) == 0, nullptr, "%s %d CustomFdClose failed! errno(%s)\n",
295 __func__, __LINE__, strerror(errno));
296 } else {
297 // Close the READ end of the pipe since parent's fd is write-only
298 CHECK_TRUE(CustomFdClose(fds[READ]) == 0, nullptr, "%s %d CustomFdClose failed! errno(%s)\n",
299 __func__, __LINE__, strerror(errno));
300 }
301 }
302
303 // Make sure the parent pipe reads and writes exist;CustomPUnblock will use.
304 childPid = pid;
305 if (strncmp(type, "r", strlen(type)) == 0) {
306 PROFILER_LOG_DEBUG(LOG_CORE, "END %s fds[READ]: success!", __func__);
307 return fdopen(fds[READ], "r");
308 }
309
310 PROFILER_LOG_DEBUG(LOG_CORE, "END %s fds[WRITE]: success!", __func__);
311 return fdopen(fds[WRITE], "w");
312 }
313
CustomPclose(FILE * fp,int fds[],volatile pid_t & childPid,bool needUnblock)314 int CustomPclose(FILE* fp, int fds[], volatile pid_t& childPid, bool needUnblock)
315 {
316 PROFILER_LOG_DEBUG(LOG_CORE, "BEGN %s: ready!", __func__);
317 CHECK_NOTNULL(fp, -1, "NOTE %s: fp is null", __func__);
318
319 int stat = 0;
320
321 if (needUnblock) {
322 PROFILER_LOG_DEBUG(LOG_CORE, "NOTE Kill Endless Loop Child %d.", childPid);
323 kill(childPid, SIGKILL);
324 }
325
326 while (waitpid(childPid, &stat, 0) == -1) {
327 PROFILER_LOG_ERROR(LOG_CORE, "%s: %s.", __func__, strerror(errno));
328 if (errno == EINTR) {
329 continue;
330 }
331 break;
332 }
333
334 if (needUnblock) {
335 if (fileno(fp) == fds[READ]) {
336 fds[READ] = -1;
337 CHECK_TRUE(CustomFdClose(fds[WRITE]) == 0, -1, "CustomFdClose failed!");
338 } else if (fileno(fp) == fds[WRITE]) {
339 fds[WRITE] = -1;
340 CHECK_TRUE(CustomFdClose(fds[READ]) == 0, -1, "CustomFdClose failed!");
341 } else {
342 PROFILER_LOG_INFO(LOG_CORE, "%s: Can't find fp in fds[READ/WRITE].", __func__);
343 }
344 }
345
346 CHECK_TRUE(CustomFdFclose(&fp) == 0, -1, "CustomFdFclose failed!");
347
348 PROFILER_LOG_DEBUG(LOG_CORE, "END %s: success!", __func__);
349 return stat;
350 }
351
352 // IF pipe fds is block, before release other threads, you need call CustomPUnblock
CustomPUnblock(int fds[])353 int CustomPUnblock(int fds[])
354 {
355 PROFILER_LOG_DEBUG(LOG_CORE, "BEGN %s: ready!", __func__);
356
357 CHECK_TRUE(fds[READ] != -1 && fds[WRITE] != -1, -1, "END fds[READ/WRITE]=-1");
358
359 int stat = fcntl(fds[READ], F_GETFL);
360 CHECK_TRUE(stat != -1, -1, "END fcntl(F_GETFL) failed!");
361
362 if (!(stat & O_NONBLOCK)) {
363 PROFILER_LOG_DEBUG(LOG_CORE, "NOTE %s: ready!Unblock r_fd and close all", __func__);
364 const char* eof = "\n\0";
365 write(fds[WRITE], eof, strlen(eof) + 1);
366 fcntl(fds[READ], F_SETFL, O_NONBLOCK);
367 }
368 PROFILER_LOG_DEBUG(LOG_CORE, "END %s: success!", __func__);
369 return 0;
370 }
371
GetServicePort()372 int GetServicePort()
373 {
374 const std::string portRangePath = "/proc/sys/net/ipv4/ip_local_port_range";
375 std::ifstream file(portRangePath.c_str());
376 CHECK_TRUE(file.is_open(), -1, "Open file failed! filePath:%s", portRangePath.c_str());
377
378 std::string rangeStr;
379 copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), std::back_inserter(rangeStr));
380
381 int minPort;
382 int maxPort;
383 std::istringstream istr(rangeStr);
384 istr >> minPort >> maxPort;
385 const int offset = 3168; // To be compatible with previously used port 50051;
386 int port = (minPort + maxPort) / 2 + offset;
387 PROFILER_LOG_DEBUG(LOG_CORE, "Service port is: %d", port);
388 return port;
389 }
390
SplitString(const std::string & str,const std::string & sep,std::vector<std::string> & ret)391 void SplitString(const std::string& str, const std::string &sep, std::vector<std::string>& ret)
392 {
393 if (str.empty()) {
394 PROFILER_LOG_ERROR(LOG_CORE, "The string splited is empty!");
395 return;
396 }
397 std::string::size_type beginPos = str.find_first_not_of(sep);
398 std::string::size_type findPos = 0;
399 while (beginPos != std::string::npos) {
400 findPos = str.find(sep, beginPos);
401 std::string tmp;
402 if (findPos != std::string::npos) {
403 tmp = str.substr(beginPos, findPos - beginPos);
404 beginPos = findPos + sep.length();
405 } else {
406 tmp = str.substr(beginPos);
407 beginPos = findPos;
408 }
409 if (!tmp.empty()) {
410 ret.push_back(tmp);
411 tmp.clear();
412 }
413 }
414 }
415
CheckApplicationPermission(int pid,const std::string & processName)416 bool CheckApplicationPermission(int pid, const std::string& processName)
417 {
418 std::string bundleName;
419 if (pid > 0) {
420 std::string filePath = "/proc/" + std::to_string(pid) + "/cmdline";
421 if (!LoadStringFromFile(filePath, bundleName)) {
422 PROFILER_LOG_ERROR(LOG_CORE, "Get process name by pid failed!");
423 return false;
424 }
425 bundleName.resize(strlen(bundleName.c_str()));
426 } else {
427 bundleName = processName;
428 }
429 CHECK_TRUE(!bundleName.empty(), false, "Pid or process name is illegal!");
430
431 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
432 CHECK_NOTNULL(sam, false, "GetSystemAbilityManager failed!");
433 sptr<IRemoteObject> remoteObject = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
434 CHECK_NOTNULL(remoteObject, false, "Get BundleMgr SA failed!");
435 sptr<BundleMgrProxy> proxy = iface_cast<BundleMgrProxy>(remoteObject);
436 int uid = proxy->GetUidByDebugBundleName(bundleName, Constants::ANY_USERID);
437 CHECK_TRUE(uid >= 0, false, "Get %s uid = %d", bundleName.c_str(), uid);
438 return true;
439 }
440
VerifyPath(const std::string & filePath,const std::vector<std::string> & validPaths)441 bool VerifyPath(const std::string& filePath, const std::vector<std::string>& validPaths)
442 {
443 if (validPaths.size() == 0) {
444 return true;
445 }
446
447 for (const std::string& path : validPaths) {
448 if (filePath.rfind(path, 0) == 0) {
449 return true;
450 }
451 }
452 return false;
453 }
454
ReadFile(const std::string & filePath,const std::vector<std::string> & validPaths,std::string & fileContent)455 bool ReadFile(const std::string &filePath, const std::vector<std::string>& validPaths, std::string& fileContent)
456 {
457 char* realFilePath = realpath(filePath.c_str(), nullptr);
458 CHECK_NOTNULL(realFilePath, false, "Fail to realPath: %s", filePath.c_str());
459
460 std::string realFilePathStr(realFilePath);
461 free(realFilePath);
462 CHECK_TRUE(VerifyPath(realFilePathStr, validPaths), false, "Fail to VerifyPath: %s", realFilePathStr.c_str());
463
464 std::ifstream fileStream(realFilePathStr, std::ios::in);
465 CHECK_TRUE(fileStream.is_open(), false, "Fail to open file %s", realFilePathStr.c_str());
466
467 std::istreambuf_iterator<char> firstIt = { fileStream };
468 std::string content(firstIt, {});
469 fileContent = content;
470 return true;
471 }
472
GetErrorMsg()473 std::string GetErrorMsg()
474 {
475 const int bufSize = 256;
476 char buffer[bufSize] = { 0 };
477 strerror_r(errno, buffer, bufSize);
478 std::string errorMsg(buffer);
479 return errorMsg;
480 }
481
GetTimeStr()482 std::string GetTimeStr()
483 {
484 time_t now = time(nullptr);
485 struct tm tmTime;
486 localtime_r(&now, &tmTime);
487
488 char buffer[32] = {0};
489 // 1900: count of years
490 (void)sprintf_s(buffer, sizeof(buffer), "%04d%02d%02d_%02d%02d%02d", tmTime.tm_year + 1900, tmTime.tm_mon + 1,
491 tmTime.tm_mday, tmTime.tm_hour, tmTime.tm_min, tmTime.tm_sec);
492 std::string timeStr(buffer);
493 return timeStr;
494 }
495
496 // get clockid by str, return CLOCK_REALTIME as default
GetClockId(const std::string & clockIdStr)497 clockid_t GetClockId(const std::string& clockIdStr)
498 {
499 clockid_t clockId = CLOCK_REALTIME;
500 auto iter = clockIdMap.find(clockIdStr);
501 if (iter != clockIdMap.end()) {
502 clockId = iter->second;
503 }
504 return clockId;
505 }
506
GetClockStr(const int32_t clockId)507 std::string GetClockStr(const int32_t clockId)
508 {
509 std::string ret = "realtime";
510 for (const auto& [str, id] : clockIdMap) {
511 if (id == clockId) {
512 ret = str;
513 break;
514 }
515 }
516 return ret;
517 }
518
AdaptSandboxPath(std::string & filePath,int pid)519 void AdaptSandboxPath(std::string& filePath, int pid)
520 {
521 if (filePath.find("/data/storage") == 0 && access(filePath.c_str(), F_OK) != 0) {
522 filePath = "/proc/" + std::to_string(pid) + "/root" + filePath;
523 }
524 }
525
GetCurrentUserId(int32_t & userId)526 bool GetCurrentUserId(int32_t& userId)
527 {
528 std::vector<int32_t> activeIds;
529 int32_t ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds);
530 if (ret != 0) {
531 PROFILER_LOG_ERROR(LOG_CORE, "QueryActiveOsAccountIds failed ret:%d", ret);
532 return false;
533 }
534
535 if (activeIds.empty()) {
536 PROFILER_LOG_ERROR(LOG_CORE, "QueryActiveOsAccountIds activeIds empty");
537 return false;
538 }
539 userId = activeIds[0];
540 PROFILER_LOG_INFO(LOG_CORE, "QueryActiveOsAccountIds userId[0]:%d", userId);
541 return true;
542 }
543
GetPackageUid(const std::string & name)544 int32_t GetPackageUid(const std::string& name)
545 {
546 int32_t userId = 0;
547 if (!GetCurrentUserId(userId)) {
548 PROFILER_LOG_ERROR(LOG_CORE, "Failed to get current user id");
549 return EC_INVALID_VALUE;
550 }
551 auto manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
552 if (manager == nullptr) {
553 PROFILER_LOG_ERROR(LOG_CORE, "systemAbilityManager is nullptr");
554 return EC_INVALID_VALUE;
555 }
556 sptr<IRemoteObject> remoteObject = manager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
557 if (remoteObject == nullptr) {
558 PROFILER_LOG_ERROR(LOG_CORE, "failed to get service id");
559 return EC_INVALID_VALUE;
560 }
561 sptr<AppExecFwk::IBundleMgr> mgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
562 if (mgr == nullptr) {
563 PROFILER_LOG_ERROR(LOG_CORE, "mgr is nullptr");
564 return EC_INVALID_VALUE;
565 }
566 int32_t uid = mgr->GetUidByBundleName(name, userId);
567 PROFILER_LOG_INFO(LOG_CORE, "pkgname is: %s, uid is : %d", name.c_str(), uid);
568 return uid;
569 }
570
IsBetaVersion()571 bool IsBetaVersion()
572 {
573 int ret = static_cast<int>(FindParameter(KEY_HIVIEW_USER_TYPE.c_str()));
574 if (ret == -1) {
575 PROFILER_LOG_INFO(LOG_CORE, "user type is not exist");
576 return true;
577 }
578 std::string userType = OHOS::system::GetParameter(KEY_HIVIEW_USER_TYPE, "");
579 PROFILER_LOG_INFO(LOG_CORE, "user type is:%s", userType.c_str());
580 return userType == "beta";
581 }
582 } // namespace COMMON
583