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 pidPath[128] = {0};
34 struct stat statBuf;
35 if (!pid) {
36 return false;
37 }
38 if (snprintf_s(pidPath, sizeof(pidPath), sizeof(pidPath) - 1, "/proc/%d/status", pid) < 0) {
39 return false;
40 }
41 if (stat(pidPath, &statBuf) == 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 pidPath[128] = {0};
51 if (snprintf_s(pidPath, sizeof(pidPath), sizeof(pidPath) - 1, "/proc/%d/cmdline", pid) < 0) {
52 return false;
53 }
54 std::string processPath = pidPath;
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 TAG_LOGI(AAFwkTag::APPMGR, "process not exit %{public}d, %{public}s",
113 static_cast<int32_t>(item.pid), item.processName.c_str());
114 return false;
115 }
116 }
117 return true;
118 }
119
UpdateProcessNameByProcFile(std::list<SimpleProcessInfo> & processInfos)120 static void UpdateProcessNameByProcFile(std::list<SimpleProcessInfo> &processInfos)
121 {
122 TAG_LOGI(AAFwkTag::APPMGR, "UpdateProcessNameByProcFile");
123 for (auto &item : processInfos) {
124 std::string processName = item.processName;
125 ReadProcessName(item.pid, item.processName);
126 if (item.processName.empty()) {
127 TAG_LOGI(AAFwkTag::APPMGR, "%{public}s proc empty", processName.c_str());
128 }
129 }
130 }
131 } // namespace ProcessUtil
132 } // namespace AppExecFwk
133 } // namespace OHOS
134
135 #endif // OHOS_ABILITY_RUNTIME_APPEXECFWK_PROCESS_UTIL_H