• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
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 #ifndef OHOS_ABILITY_RUNTIME_APPEXECFWK_PROCESS_UTIL_H
17 #define OHOS_ABILITY_RUNTIME_APPEXECFWK_PROCESS_UTIL_H
18 
19 #include <list>
20 #include <sys/stat.h>
21 
22 #include "file_ex.h"
23 #include "hilog_tag_wrapper.h"
24 #include "securec.h"
25 #include "simple_process_info.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace ProcessUtil {
30 
ProcessExist(pid_t pid)31 static bool ProcessExist(pid_t pid)
32 {
33     char pid_path[128] = {0};
34     struct stat stat_buf;
35     if (!pid) {
36         return false;
37     }
38     if (snprintf_s(pid_path, sizeof(pid_path), sizeof(pid_path) - 1, "/proc/%d/status", pid) < 0) {
39         return false;
40     }
41     if (stat(pid_path, &stat_buf) == 0) {
42         return true;
43     }
44     return false;
45 }
46 
ReadProcessName(pid_t pid,std::string & pidProcessName)47 static bool ReadProcessName(pid_t pid, std::string &pidProcessName)
48 {
49     pidProcessName.clear();
50     char pid_path[128] = {0};
51     if (snprintf_s(pid_path, sizeof(pid_path), sizeof(pid_path) - 1, "/proc/%d/cmdline", pid) < 0) {
52         return false;
53     }
54     std::string processPath = pid_path;
55     std::string name;
56     OHOS::LoadStringFromFile(processPath, name);
57     if (name.empty()) {
58         return false;
59     }
60     for (char c : name) {
61         if (c == '\0') {
62             break;
63         }
64         pidProcessName += c;
65     }
66     return true;
67 }
68 
ProcessExist(pid_t pid,const std::string & processName)69 static bool ProcessExist(pid_t pid, const std::string &processName)
70 {
71     if (!ProcessExist(pid)) {
72         return false;
73     }
74     std::string pidProcessName;
75     ReadProcessName(pid, pidProcessName);
76     if (pidProcessName.empty()) {
77         return true;
78     }
79     return pidProcessName == processName;
80 }
81 
CheckAllProcessExit(std::list<SimpleProcessInfo> & processInfos)82 static bool CheckAllProcessExit(std::list<SimpleProcessInfo> &processInfos)
83 {
84     // use pid and process name to check if process exists
85     for (auto iter = processInfos.begin(); iter != processInfos.end();) {
86         if (!ProcessExist((*iter).pid, (*iter).processName)) {
87             iter = processInfos.erase(iter);
88         } else {
89             iter++;
90         }
91     }
92     return processInfos.empty();
93 }
94 
CheckAllProcessExit(std::list<pid_t> & pids)95 static bool CheckAllProcessExit(std::list<pid_t> &pids)
96 {
97     for (auto iter = pids.begin(); iter != pids.end();) {
98         if (!ProcessExist(*iter)) {
99             iter = pids.erase(iter);
100         } else {
101             iter++;
102         }
103     }
104     return pids.empty();
105 }
106 
IsAllProcessKilled(std::list<SimpleProcessInfo> & processInfos)107 static bool IsAllProcessKilled(std::list<SimpleProcessInfo> &processInfos)
108 {
109     bool processExists = false;
110     for (auto &item : processInfos) {
111         if (ProcessExist(item.pid, item.processName)) {
112             return false;
113         }
114     }
115     return true;
116 }
117 
UpdateProcessNameByProcFile(std::list<SimpleProcessInfo> & processInfos)118 static void UpdateProcessNameByProcFile(std::list<SimpleProcessInfo> &processInfos)
119 {
120     TAG_LOGI(AAFwkTag::APPMGR, "UpdateProcessNameByProcFile");
121     for (auto &item : processInfos) {
122         std::string processName = item.processName;
123         ReadProcessName(item.pid, item.processName);
124         if (item.processName.empty()) {
125             TAG_LOGI(AAFwkTag::APPMGR, "%{public}s proc empty", processName.c_str());
126         }
127     }
128 }
129 }  // namespace ProcessUtil
130 }  // namespace AppExecFwk
131 }  // namespace OHOS
132 
133 #endif  // OHOS_ABILITY_RUNTIME_APPEXECFWK_PROCESS_UTIL_H