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);
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)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)
43 : isStartWithDebug_(isStartWithDebug), hostPid_(hostPid), childProcessCount_(childProcessCount),
44 childProcessType_(CHILD_PROCESS_TYPE_NATIVE), hostRecord_(hostRecord), mainProcessCb_(mainProcessCb),
45 srcEntry_(libName)
46 {
47 MakeProcessName(hostRecord);
48 }
49
~ChildProcessRecord()50 ChildProcessRecord::~ChildProcessRecord()
51 {
52 TAG_LOGD(AAFwkTag::APPMGR, "called");
53 }
54
CreateChildProcessRecord(pid_t hostPid,const ChildProcessRequest & request,const std::shared_ptr<AppRunningRecord> hostRecord)55 std::shared_ptr<ChildProcessRecord> ChildProcessRecord::CreateChildProcessRecord(pid_t hostPid,
56 const ChildProcessRequest &request, const std::shared_ptr<AppRunningRecord> hostRecord)
57 {
58 TAG_LOGD(AAFwkTag::APPMGR, "hostPid: %{public}d, srcEntry: %{priavte}s,", hostPid, request.srcEntry.c_str());
59 if (hostPid <= 0 || request.srcEntry.empty() || !hostRecord) {
60 TAG_LOGE(AAFwkTag::APPMGR, "invalid parameter");
61 return nullptr;
62 }
63 return std::make_shared<ChildProcessRecord>(hostPid, request, hostRecord);
64 }
65
CreateNativeChildProcessRecord(pid_t hostPid,const std::string & libName,const std::shared_ptr<AppRunningRecord> hostRecord,const sptr<IRemoteObject> & mainProcessCb,int32_t childProcessCount,bool isStartWithDebug)66 std::shared_ptr<ChildProcessRecord> ChildProcessRecord::CreateNativeChildProcessRecord(
67 pid_t hostPid, const std::string &libName, const std::shared_ptr<AppRunningRecord> hostRecord,
68 const sptr<IRemoteObject> &mainProcessCb, int32_t childProcessCount, bool isStartWithDebug)
69 {
70 TAG_LOGD(AAFwkTag::APPMGR, "hostPid: %{public}d, libName: %{public}s", hostPid, libName.c_str());
71 if (hostPid <= 0 || libName.empty() || !hostRecord || !mainProcessCb) {
72 TAG_LOGE(AAFwkTag::APPMGR, "invalid parameter");
73 return nullptr;
74 }
75 return std::make_shared<ChildProcessRecord>(hostPid, libName, hostRecord, mainProcessCb,
76 childProcessCount, isStartWithDebug);
77 }
78
SetPid(pid_t pid)79 void ChildProcessRecord::SetPid(pid_t pid)
80 {
81 pid_ = pid;
82 }
83
GetPid() const84 pid_t ChildProcessRecord::GetPid() const
85 {
86 return pid_;
87 }
88
GetHostPid() const89 pid_t ChildProcessRecord::GetHostPid() const
90 {
91 return hostPid_;
92 }
93
SetUid(int32_t uid)94 void ChildProcessRecord::SetUid(int32_t uid)
95 {
96 uid_ = uid;
97 }
98
GetUid() const99 int32_t ChildProcessRecord::GetUid() const
100 {
101 return uid_;
102 }
103
GetProcessName() const104 std::string ChildProcessRecord::GetProcessName() const
105 {
106 return processName_;
107 }
108
GetSrcEntry() const109 std::string ChildProcessRecord::GetSrcEntry() const
110 {
111 return srcEntry_;
112 }
113
GetProcessType() const114 ProcessType ChildProcessRecord::GetProcessType() const
115 {
116 return processType_;
117 }
118
GetEntryFunc() const119 std::string ChildProcessRecord::GetEntryFunc() const
120 {
121 return entryFunc_;
122 }
123
GetHostRecord() const124 std::shared_ptr<AppRunningRecord> ChildProcessRecord::GetHostRecord() const
125 {
126 return hostRecord_.lock();
127 }
128
SetScheduler(const sptr<IChildScheduler> & scheduler)129 void ChildProcessRecord::SetScheduler(const sptr<IChildScheduler> &scheduler)
130 {
131 scheduler_ = scheduler;
132 }
133
GetScheduler() const134 sptr<IChildScheduler> ChildProcessRecord::GetScheduler() const
135 {
136 return scheduler_;
137 }
138
SetDeathRecipient(const sptr<AppDeathRecipient> recipient)139 void ChildProcessRecord::SetDeathRecipient(const sptr<AppDeathRecipient> recipient)
140 {
141 deathRecipient_ = recipient;
142 }
143
RegisterDeathRecipient()144 void ChildProcessRecord::RegisterDeathRecipient()
145 {
146 if (scheduler_ == nullptr || deathRecipient_ == nullptr) {
147 TAG_LOGE(AAFwkTag::APPMGR, "null scheduler_ or deathRecipient_");
148 return;
149 }
150 auto obj = scheduler_->AsObject();
151 if (!obj || !obj->AddDeathRecipient(deathRecipient_)) {
152 TAG_LOGE(AAFwkTag::APPMGR, "AddDeathRecipient failed");
153 }
154 }
155
RemoveDeathRecipient()156 void ChildProcessRecord::RemoveDeathRecipient()
157 {
158 if (!scheduler_) {
159 TAG_LOGE(AAFwkTag::APPMGR, "null scheduler_");
160 return;
161 }
162 auto object = scheduler_->AsObject();
163 if (object) {
164 object->RemoveDeathRecipient(deathRecipient_);
165 }
166 }
167
ScheduleExitProcessSafely()168 void ChildProcessRecord::ScheduleExitProcessSafely()
169 {
170 if (!scheduler_) {
171 TAG_LOGE(AAFwkTag::APPMGR, "null scheduler_");
172 return;
173 }
174 scheduler_->ScheduleExitProcessSafely();
175 }
176
MakeProcessName(const std::shared_ptr<AppRunningRecord> hostRecord)177 void ChildProcessRecord::MakeProcessName(const std::shared_ptr<AppRunningRecord> hostRecord)
178 {
179 if (!hostRecord) {
180 TAG_LOGW(AAFwkTag::APPMGR, "hostRecord empty");
181 return;
182 }
183 processName_ = hostRecord->GetBundleName();
184 if (srcEntry_.empty()) {
185 TAG_LOGW(AAFwkTag::APPMGR, "srcEntry empty");
186 return;
187 }
188 std::string filename = std::filesystem::path(srcEntry_).stem();
189 if (!filename.empty()) {
190 processName_.append(":");
191 if (childProcessType_ == CHILD_PROCESS_TYPE_NATIVE || childProcessType_ == CHILD_PROCESS_TYPE_NATIVE_ARGS) {
192 processName_.append("Native_");
193 }
194
195 processName_.append(filename);
196 }
197 processName_.append(std::to_string(childProcessCount_));
198 TAG_LOGD(AAFwkTag::APPMGR, "MakeSpawnForkProcessName processName is %{public}s", processName_.c_str());
199 }
200
isStartWithDebug()201 bool ChildProcessRecord::isStartWithDebug()
202 {
203 return isStartWithDebug_;
204 }
205
GetChildProcessType() const206 int32_t ChildProcessRecord::GetChildProcessType() const
207 {
208 return childProcessType_;
209 }
210
GetMainProcessCallback() const211 sptr<IRemoteObject> ChildProcessRecord::GetMainProcessCallback() const
212 {
213 return mainProcessCb_;
214 }
215
ClearMainProcessCallback()216 void ChildProcessRecord::ClearMainProcessCallback()
217 {
218 mainProcessCb_.clear();
219 }
220
SetEntryParams(const std::string & entryParams)221 void ChildProcessRecord::SetEntryParams(const std::string &entryParams)
222 {
223 entryParams_ = entryParams;
224 }
225
GetEntryParams() const226 std::string ChildProcessRecord::GetEntryParams() const
227 {
228 return entryParams_;
229 }
230 } // namespace AppExecFwk
231 } // namespace OHOS
232