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     : hostPid_(hostPid), childProcessCount_(request.childProcessCount), childProcessType_(request.childProcessType),
27     hostRecord_(hostRecord), isStartWithDebug_(request.isStartWithDebug)
28 {
29     srcEntry_ = request.srcEntry;
30     fds_ = request.args.fds;
31     if (childProcessType_ == CHILD_PROCESS_TYPE_NATIVE_ARGS) {
32         auto pos = request.srcEntry.rfind(":");
33         if (pos != std::string::npos) {
34             srcEntry_ = request.srcEntry.substr(0, pos);
35             entryFunc_ = request.srcEntry.substr(pos + 1);
36         }
37     }
38     MakeProcessName(hostRecord);
39 }
40 
ChildProcessRecord(pid_t hostPid,const std::string & libName,const std::shared_ptr<AppRunningRecord> hostRecord,const sptr<IRemoteObject> & mainProcessCb,int32_t childProcessCount,bool isStartWithDebug)41 ChildProcessRecord::ChildProcessRecord(pid_t hostPid, const std::string &libName,
42     const std::shared_ptr<AppRunningRecord> hostRecord, const sptr<IRemoteObject> &mainProcessCb,
43     int32_t childProcessCount, bool isStartWithDebug)
44     : hostPid_(hostPid), childProcessCount_(childProcessCount), childProcessType_(CHILD_PROCESS_TYPE_NATIVE),
45     srcEntry_(libName), hostRecord_(hostRecord), mainProcessCb_(mainProcessCb), isStartWithDebug_(isStartWithDebug)
46 {
47     MakeProcessName(hostRecord);
48 }
49 
~ChildProcessRecord()50 ChildProcessRecord::~ChildProcessRecord()
51 {
52     TAG_LOGD(AAFwkTag::APPMGR, "called");
53     for (const auto &item : fds_) {
54         close(item.second);
55     }
56 }
57 
CreateChildProcessRecord(pid_t hostPid,const ChildProcessRequest & request,const std::shared_ptr<AppRunningRecord> hostRecord)58 std::shared_ptr<ChildProcessRecord> ChildProcessRecord::CreateChildProcessRecord(pid_t hostPid,
59     const ChildProcessRequest &request, const std::shared_ptr<AppRunningRecord> hostRecord)
60 {
61     TAG_LOGD(AAFwkTag::APPMGR, "hostPid: %{public}d, srcEntry: %{priavte}s,", hostPid, request.srcEntry.c_str());
62     if (hostPid <= 0 || request.srcEntry.empty() || !hostRecord) {
63         TAG_LOGE(AAFwkTag::APPMGR, "Invalid parameter.");
64         return nullptr;
65     }
66     return std::make_shared<ChildProcessRecord>(hostPid, request, hostRecord);
67 }
68 
CreateNativeChildProcessRecord(pid_t hostPid,const std::string & libName,const std::shared_ptr<AppRunningRecord> hostRecord,const sptr<IRemoteObject> & mainProcessCb,int32_t childProcessCount,bool isStartWithDebug)69 std::shared_ptr<ChildProcessRecord> ChildProcessRecord::CreateNativeChildProcessRecord(
70     pid_t hostPid, const std::string &libName, const std::shared_ptr<AppRunningRecord> hostRecord,
71     const sptr<IRemoteObject> &mainProcessCb, int32_t childProcessCount, bool isStartWithDebug)
72 {
73     TAG_LOGD(AAFwkTag::APPMGR, "hostPid: %{public}d, libName: %{public}s", hostPid, libName.c_str());
74     if (hostPid <= 0 || libName.empty() || !hostRecord || !mainProcessCb) {
75         TAG_LOGE(AAFwkTag::APPMGR, "Invalid parameter.");
76         return nullptr;
77     }
78     return std::make_shared<ChildProcessRecord>(hostPid, libName, hostRecord, mainProcessCb,
79         childProcessCount, isStartWithDebug);
80 }
81 
SetPid(pid_t pid)82 void ChildProcessRecord::SetPid(pid_t pid)
83 {
84     pid_ = pid;
85 }
86 
GetPid() const87 pid_t ChildProcessRecord::GetPid() const
88 {
89     return pid_;
90 }
91 
GetHostPid() const92 pid_t ChildProcessRecord::GetHostPid() const
93 {
94     return hostPid_;
95 }
96 
SetUid(int32_t uid)97 void ChildProcessRecord::SetUid(int32_t uid)
98 {
99     uid_ = uid;
100 }
101 
GetUid() const102 int32_t ChildProcessRecord::GetUid() const
103 {
104     return uid_;
105 }
106 
GetProcessName() const107 std::string ChildProcessRecord::GetProcessName() const
108 {
109     return processName_;
110 }
111 
GetSrcEntry() const112 std::string ChildProcessRecord::GetSrcEntry() const
113 {
114     return srcEntry_;
115 }
116 
GetProcessType() const117 ProcessType ChildProcessRecord::GetProcessType() const
118 {
119     return processType_;
120 }
121 
GetEntryFunc() const122 std::string ChildProcessRecord::GetEntryFunc() const
123 {
124     return entryFunc_;
125 }
126 
GetHostRecord() const127 std::shared_ptr<AppRunningRecord> ChildProcessRecord::GetHostRecord() const
128 {
129     return hostRecord_.lock();
130 }
131 
SetScheduler(const sptr<IChildScheduler> & scheduler)132 void ChildProcessRecord::SetScheduler(const sptr<IChildScheduler> &scheduler)
133 {
134     scheduler_ = scheduler;
135 }
136 
GetScheduler() const137 sptr<IChildScheduler> ChildProcessRecord::GetScheduler() const
138 {
139     return scheduler_;
140 }
141 
SetDeathRecipient(const sptr<AppDeathRecipient> recipient)142 void ChildProcessRecord::SetDeathRecipient(const sptr<AppDeathRecipient> recipient)
143 {
144     deathRecipient_ = recipient;
145 }
146 
RegisterDeathRecipient()147 void ChildProcessRecord::RegisterDeathRecipient()
148 {
149     if (scheduler_ == nullptr || deathRecipient_ == nullptr) {
150         TAG_LOGE(AAFwkTag::APPMGR, "scheduler_ or deathRecipient_ is null.");
151         return;
152     }
153     auto obj = scheduler_->AsObject();
154     if (!obj || !obj->AddDeathRecipient(deathRecipient_)) {
155         TAG_LOGE(AAFwkTag::APPMGR, "AddDeathRecipient failed.");
156     }
157 }
158 
RemoveDeathRecipient()159 void ChildProcessRecord::RemoveDeathRecipient()
160 {
161     if (!scheduler_) {
162         TAG_LOGE(AAFwkTag::APPMGR, "scheduler_ is null.");
163         return;
164     }
165     auto object = scheduler_->AsObject();
166     if (object) {
167         object->RemoveDeathRecipient(deathRecipient_);
168     }
169 }
170 
ScheduleExitProcessSafely()171 void ChildProcessRecord::ScheduleExitProcessSafely()
172 {
173     if (!scheduler_) {
174         TAG_LOGE(AAFwkTag::APPMGR, "scheduler_ is null.");
175         return;
176     }
177     scheduler_->ScheduleExitProcessSafely();
178 }
179 
MakeProcessName(const std::shared_ptr<AppRunningRecord> hostRecord)180 void ChildProcessRecord::MakeProcessName(const std::shared_ptr<AppRunningRecord> hostRecord)
181 {
182     if (!hostRecord) {
183         TAG_LOGW(AAFwkTag::APPMGR, "hostRecord empty.");
184         return;
185     }
186     processName_ = hostRecord->GetBundleName();
187     if (srcEntry_.empty()) {
188         TAG_LOGW(AAFwkTag::APPMGR, "srcEntry empty.");
189         return;
190     }
191     std::string filename = std::filesystem::path(srcEntry_).stem();
192     if (!filename.empty()) {
193         processName_.append(":");
194         if (childProcessType_ == CHILD_PROCESS_TYPE_NATIVE || childProcessType_ == CHILD_PROCESS_TYPE_NATIVE_ARGS) {
195             processName_.append("Native_");
196         }
197 
198         processName_.append(filename);
199     }
200     processName_.append(std::to_string(childProcessCount_));
201     TAG_LOGD(AAFwkTag::APPMGR, "MakeSpawnForkProcessName processName is %{public}s", processName_.c_str());
202 }
203 
isStartWithDebug()204 bool ChildProcessRecord::isStartWithDebug()
205 {
206     return isStartWithDebug_;
207 }
208 
GetChildProcessType() const209 int32_t ChildProcessRecord::GetChildProcessType() const
210 {
211     return childProcessType_;
212 }
213 
GetMainProcessCallback() const214 sptr<IRemoteObject> ChildProcessRecord::GetMainProcessCallback() const
215 {
216     return mainProcessCb_;
217 }
218 
ClearMainProcessCallback()219 void ChildProcessRecord::ClearMainProcessCallback()
220 {
221     mainProcessCb_.clear();
222 }
223 
SetEntryParams(const std::string & entryParams)224 void ChildProcessRecord::SetEntryParams(const std::string &entryParams)
225 {
226     entryParams_ = entryParams;
227 }
228 
GetEntryParams() const229 std::string ChildProcessRecord::GetEntryParams() const
230 {
231     return entryParams_;
232 }
233 }  // namespace AppExecFwk
234 }  // namespace OHOS
235