1 /*
2 * Copyright (c) 2023-2024 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 #include "child_process_record.h"
16
17 #include <filesystem>
18
19 #include "app_running_record.h"
20 #include "hilog_tag_wrapper.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
ChildProcessRecord(pid_t hostPid,const ChildProcessRequest & request,const std::shared_ptr<AppRunningRecord> hostRecord)24 ChildProcessRecord::ChildProcessRecord(pid_t hostPid, const ChildProcessRequest &request,
25 const std::shared_ptr<AppRunningRecord> hostRecord)
26 : isStartWithDebug_(request.isStartWithDebug), hostPid_(hostPid), childProcessCount_(request.childProcessCount),
27 childProcessType_(request.childProcessType), hostRecord_(hostRecord)
28 {
29 srcEntry_ = request.srcEntry;
30 if (childProcessType_ == CHILD_PROCESS_TYPE_NATIVE_ARGS) {
31 auto pos = request.srcEntry.rfind(":");
32 if (pos != std::string::npos) {
33 srcEntry_ = request.srcEntry.substr(0, pos);
34 entryFunc_ = request.srcEntry.substr(pos + 1);
35 }
36 }
37 MakeProcessName(hostRecord, request.options.customProcessName);
38 }
39
ChildProcessRecord(pid_t hostPid,const std::string & libName,const std::shared_ptr<AppRunningRecord> hostRecord,const sptr<IRemoteObject> & mainProcessCb,int32_t childProcessCount,bool isStartWithDebug,const std::string & customProcessName)40 ChildProcessRecord::ChildProcessRecord(pid_t hostPid, const std::string &libName,
41 const std::shared_ptr<AppRunningRecord> hostRecord, const sptr<IRemoteObject> &mainProcessCb,
42 int32_t childProcessCount, bool isStartWithDebug, const std::string &customProcessName)
43 : isStartWithDebug_(isStartWithDebug), hostPid_(hostPid), childProcessCount_(childProcessCount),
44 childProcessType_(CHILD_PROCESS_TYPE_NATIVE), hostRecord_(hostRecord), mainProcessCb_(mainProcessCb),
45 srcEntry_(libName)
46 {
47 MakeProcessName(hostRecord, customProcessName);
48 }
49
~ChildProcessRecord()50 ChildProcessRecord::~ChildProcessRecord()
51 {
52 TAG_LOGD(AAFwkTag::APPMGR, "called");
53 }
54
IsNativeSpawnStarted() const55 bool ChildProcessRecord::IsNativeSpawnStarted() const
56 {
57 return childProcessType_ == CHILD_PROCESS_TYPE_NATIVE_ARGS;
58 }
59
CreateChildProcessRecord(pid_t hostPid,const ChildProcessRequest & request,const std::shared_ptr<AppRunningRecord> hostRecord)60 std::shared_ptr<ChildProcessRecord> ChildProcessRecord::CreateChildProcessRecord(pid_t hostPid,
61 const ChildProcessRequest &request, const std::shared_ptr<AppRunningRecord> hostRecord)
62 {
63 TAG_LOGD(AAFwkTag::APPMGR, "hostPid: %{public}d, srcEntry: %{priavte}s,", hostPid, request.srcEntry.c_str());
64 if (hostPid <= 0 || request.srcEntry.empty() || !hostRecord) {
65 TAG_LOGE(AAFwkTag::APPMGR, "invalid parameter");
66 return nullptr;
67 }
68 return std::make_shared<ChildProcessRecord>(hostPid, request, hostRecord);
69 }
70
CreateNativeChildProcessRecord(pid_t hostPid,const std::string & libName,const std::shared_ptr<AppRunningRecord> hostRecord,const sptr<IRemoteObject> & mainProcessCb,int32_t childProcessCount,bool isStartWithDebug,const std::string & customProcessName)71 std::shared_ptr<ChildProcessRecord> ChildProcessRecord::CreateNativeChildProcessRecord(
72 pid_t hostPid, const std::string &libName, const std::shared_ptr<AppRunningRecord> hostRecord,
73 const sptr<IRemoteObject> &mainProcessCb, int32_t childProcessCount, bool isStartWithDebug,
74 const std::string &customProcessName)
75 {
76 TAG_LOGD(AAFwkTag::APPMGR, "hostPid: %{public}d, libName: %{public}s", hostPid, libName.c_str());
77 if (hostPid <= 0 || libName.empty() || !hostRecord || !mainProcessCb) {
78 TAG_LOGE(AAFwkTag::APPMGR, "invalid parameter");
79 return nullptr;
80 }
81 return std::make_shared<ChildProcessRecord>(hostPid, libName, hostRecord, mainProcessCb,
82 childProcessCount, isStartWithDebug, customProcessName);
83 }
84
SetPid(pid_t pid)85 void ChildProcessRecord::SetPid(pid_t pid)
86 {
87 pid_ = pid;
88 }
89
GetPid() const90 pid_t ChildProcessRecord::GetPid() const
91 {
92 return pid_;
93 }
94
GetHostPid() const95 pid_t ChildProcessRecord::GetHostPid() const
96 {
97 return hostPid_;
98 }
99
SetUid(int32_t uid)100 void ChildProcessRecord::SetUid(int32_t uid)
101 {
102 uid_ = uid;
103 }
104
GetUid() const105 int32_t ChildProcessRecord::GetUid() const
106 {
107 return uid_;
108 }
109
GetProcessName() const110 std::string ChildProcessRecord::GetProcessName() const
111 {
112 return processName_;
113 }
114
GetSrcEntry() const115 std::string ChildProcessRecord::GetSrcEntry() const
116 {
117 return srcEntry_;
118 }
119
GetProcessType() const120 ProcessType ChildProcessRecord::GetProcessType() const
121 {
122 return processType_;
123 }
124
GetEntryFunc() const125 std::string ChildProcessRecord::GetEntryFunc() const
126 {
127 return entryFunc_;
128 }
129
GetHostRecord() const130 std::shared_ptr<AppRunningRecord> ChildProcessRecord::GetHostRecord() const
131 {
132 return hostRecord_.lock();
133 }
134
SetScheduler(const sptr<IChildScheduler> & scheduler)135 void ChildProcessRecord::SetScheduler(const sptr<IChildScheduler> &scheduler)
136 {
137 scheduler_ = scheduler;
138 }
139
GetScheduler() const140 sptr<IChildScheduler> ChildProcessRecord::GetScheduler() const
141 {
142 return scheduler_;
143 }
144
SetDeathRecipient(const sptr<AppDeathRecipient> recipient)145 void ChildProcessRecord::SetDeathRecipient(const sptr<AppDeathRecipient> recipient)
146 {
147 deathRecipient_ = recipient;
148 }
149
RegisterDeathRecipient()150 void ChildProcessRecord::RegisterDeathRecipient()
151 {
152 if (scheduler_ == nullptr || deathRecipient_ == nullptr) {
153 TAG_LOGE(AAFwkTag::APPMGR, "null scheduler_ or deathRecipient_");
154 return;
155 }
156 auto obj = scheduler_->AsObject();
157 if (!obj || !obj->AddDeathRecipient(deathRecipient_)) {
158 TAG_LOGE(AAFwkTag::APPMGR, "AddDeathRecipient failed");
159 }
160 }
161
RemoveDeathRecipient()162 void ChildProcessRecord::RemoveDeathRecipient()
163 {
164 if (!scheduler_) {
165 TAG_LOGE(AAFwkTag::APPMGR, "null scheduler_");
166 return;
167 }
168 auto object = scheduler_->AsObject();
169 if (object) {
170 object->RemoveDeathRecipient(deathRecipient_);
171 }
172 }
173
ScheduleExitProcessSafely()174 void ChildProcessRecord::ScheduleExitProcessSafely()
175 {
176 if (!scheduler_) {
177 TAG_LOGE(AAFwkTag::APPMGR, "null scheduler_");
178 return;
179 }
180 scheduler_->ScheduleExitProcessSafely();
181 }
182
MakeProcessName(const std::shared_ptr<AppRunningRecord> hostRecord,const std::string & customProcessName)183 void ChildProcessRecord::MakeProcessName(const std::shared_ptr<AppRunningRecord> hostRecord,
184 const std::string &customProcessName)
185 {
186 if (!hostRecord) {
187 TAG_LOGW(AAFwkTag::APPMGR, "hostRecord empty");
188 return;
189 }
190 processName_ = hostRecord->GetBundleName();
191 if (!customProcessName.empty()) {
192 processName_.append(":");
193 processName_.append(customProcessName);
194 return;
195 }
196 if (srcEntry_.empty()) {
197 TAG_LOGW(AAFwkTag::APPMGR, "srcEntry empty");
198 return;
199 }
200 std::string filename = std::filesystem::path(srcEntry_).stem();
201 if (!filename.empty()) {
202 processName_.append(":");
203 if (childProcessType_ == CHILD_PROCESS_TYPE_NATIVE || childProcessType_ == CHILD_PROCESS_TYPE_NATIVE_ARGS) {
204 processName_.append("Native_");
205 }
206
207 processName_.append(filename);
208 }
209 processName_.append(std::to_string(childProcessCount_));
210 TAG_LOGD(AAFwkTag::APPMGR, "MakeSpawnForkProcessName processName is %{public}s", processName_.c_str());
211 }
212
isStartWithDebug()213 bool ChildProcessRecord::isStartWithDebug()
214 {
215 return isStartWithDebug_;
216 }
217
GetChildProcessType() const218 int32_t ChildProcessRecord::GetChildProcessType() const
219 {
220 return childProcessType_;
221 }
222
GetMainProcessCallback() const223 sptr<IRemoteObject> ChildProcessRecord::GetMainProcessCallback() const
224 {
225 return mainProcessCb_;
226 }
227
ClearMainProcessCallback()228 void ChildProcessRecord::ClearMainProcessCallback()
229 {
230 mainProcessCb_.clear();
231 }
232
SetEntryParams(const std::string & entryParams)233 void ChildProcessRecord::SetEntryParams(const std::string &entryParams)
234 {
235 entryParams_ = entryParams;
236 }
237
GetEntryParams() const238 std::string ChildProcessRecord::GetEntryParams() const
239 {
240 return entryParams_;
241 }
242 } // namespace AppExecFwk
243 } // namespace OHOS
244