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 
16 #include "child_process_manager.h"
17 
18 #include <csignal>
19 #include <filesystem>
20 #include <string>
21 #include <sys/prctl.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25 
26 #include "app_utils.h"
27 #include "application_info.h"
28 #include "app_mgr_interface.h"
29 #include "bundle_info.h"
30 #include "bundle_mgr_interface.h"
31 #include "child_process.h"
32 #include "native_args_child_process.h"
33 #include "native_child_ipc_process.h"
34 #include "child_process_manager_error_utils.h"
35 #include "child_process_request.h"
36 #include "child_process_start_info.h"
37 #include "constants.h"
38 #include "event_runner.h"
39 #include "errors.h"
40 #include "hap_module_info.h"
41 #include "hilog_tag_wrapper.h"
42 #include "parameters.h"
43 #include "runtime.h"
44 #include "sys_mgr_client.h"
45 #include "system_ability_definition.h"
46 
47 namespace OHOS {
48 namespace AbilityRuntime {
49 namespace {
50     bool g_jitEnabled = false;
51     AbilityRuntime::Runtime::DebugOption g_debugOption;
52 }
53 bool ChildProcessManager::signalRegistered_ = false;
54 
GetInstance()55 ChildProcessManager &ChildProcessManager::GetInstance()
56 {
57     static ChildProcessManager instance;
58     return instance;
59 }
60 
ChildProcessManager()61 ChildProcessManager::ChildProcessManager()
62 {
63     TAG_LOGD(AAFwkTag::PROCESSMGR, "called");
64 }
65 
~ChildProcessManager()66 ChildProcessManager::~ChildProcessManager()
67 {
68     TAG_LOGD(AAFwkTag::PROCESSMGR, "called");
69 }
70 
StartChildProcessBySelfFork(const std::string & srcEntry,pid_t & pid)71 ChildProcessManagerErrorCode ChildProcessManager::StartChildProcessBySelfFork(const std::string &srcEntry, pid_t &pid)
72 {
73     TAG_LOGI(AAFwkTag::PROCESSMGR, "called");
74     ChildProcessManagerErrorCode errorCode = PreCheck();
75     if (errorCode != ChildProcessManagerErrorCode::ERR_OK) {
76         return errorCode;
77     }
78 
79     AppExecFwk::BundleInfo bundleInfo;
80     if (!GetBundleInfo(bundleInfo)) {
81         TAG_LOGE(AAFwkTag::PROCESSMGR, "GetBundleInfo failed");
82         return ChildProcessManagerErrorCode::ERR_GET_BUNDLE_INFO_FAILED;
83     }
84 
85     RegisterSignal();
86     pid = fork();
87     if (pid < 0) {
88         TAG_LOGE(AAFwkTag::PROCESSMGR, "Fork process failed");
89         return ChildProcessManagerErrorCode::ERR_FORK_FAILED;
90     }
91     MakeProcessName(srcEntry); // set process name
92     if (pid == 0) {
93         const char *processName = g_debugOption.processName.c_str();
94         if (prctl(PR_SET_NAME, processName) < 0) {
95             TAG_LOGW(AAFwkTag::PROCESSMGR, "Set process name failed with %{public}d", errno);
96         }
97         HandleChildProcessBySelfFork(srcEntry, bundleInfo);
98     }
99     return ChildProcessManagerErrorCode::ERR_OK;
100 }
101 
StartChildProcessByAppSpawnFork(const std::string & srcEntry,pid_t & pid)102 ChildProcessManagerErrorCode ChildProcessManager::StartChildProcessByAppSpawnFork(
103     const std::string &srcEntry, pid_t &pid)
104 {
105     AppExecFwk::ChildProcessArgs args;
106     AppExecFwk::ChildProcessOptions options;
107     return StartChildProcessWithArgs(srcEntry, pid, AppExecFwk::CHILD_PROCESS_TYPE_JS, args, options);
108 }
109 
StartChildProcessWithArgs(const std::string & srcEntry,pid_t & pid,int32_t childProcessType,const AppExecFwk::ChildProcessArgs & args,const AppExecFwk::ChildProcessOptions & options)110 ChildProcessManagerErrorCode ChildProcessManager::StartChildProcessWithArgs(
111     const std::string &srcEntry, pid_t &pid, int32_t childProcessType, const AppExecFwk::ChildProcessArgs &args,
112     const AppExecFwk::ChildProcessOptions &options)
113 {
114     TAG_LOGI(AAFwkTag::PROCESSMGR, "StartChildProcessWithArgs, childProcessType:%{public}d, startWitDebug: %{public}d,"
115         " processName:%{public}s, native:%{public}d, entryParams size:%{public}zu, fdsSize:%{public}zu,"
116         " options.isolationMode:%{public}d", childProcessType, g_debugOption.isStartWithDebug,
117         g_debugOption.processName.c_str(), g_debugOption.isStartWithNative, args.entryParams.length(), args.fds.size(),
118         options.isolationMode);
119     ChildProcessManagerErrorCode errorCode = PreCheck(childProcessType);
120     if (errorCode != ChildProcessManagerErrorCode::ERR_OK) {
121         return errorCode;
122     }
123     sptr<AppExecFwk::IAppMgr> appMgr = GetAppMgr();
124     if (appMgr == nullptr) {
125         TAG_LOGE(AAFwkTag::PROCESSMGR, "GetAppMgr failed");
126         return ChildProcessManagerErrorCode::ERR_GET_APP_MGR_FAILED;
127     }
128     AppExecFwk::ChildProcessRequest request;
129     request.srcEntry = srcEntry;
130     request.childProcessType = childProcessType;
131     request.isStartWithDebug = g_debugOption.isStartWithDebug;
132     request.args = args;
133     request.options = options;
134     std::lock_guard<std::mutex> lock(childProcessCountLock_);
135     request.childProcessCount = childProcessCount_;
136     auto ret = appMgr->StartChildProcess(pid, request);
137     childProcessCount_++;
138     TAG_LOGD(AAFwkTag::PROCESSMGR, "AppMgr StartChildProcess ret:%{public}d", ret);
139     if (ret != ERR_OK) {
140         TAG_LOGE(AAFwkTag::PROCESSMGR, "StartChildProcess error:%{public}d", ret);
141         return ChildProcessManagerErrorUtil::GetChildProcessManagerErrorCode(ret);
142     }
143     return ChildProcessManagerErrorCode::ERR_OK;
144 }
145 
StartNativeChildProcessByAppSpawnFork(const std::string & libName,const sptr<IRemoteObject> & callbackStub)146 ChildProcessManagerErrorCode ChildProcessManager::StartNativeChildProcessByAppSpawnFork(
147     const std::string &libName, const sptr<IRemoteObject> &callbackStub)
148 {
149     TAG_LOGI(AAFwkTag::PROCESSMGR, "libName:%{private}s", libName.c_str());
150     ChildProcessManagerErrorCode errorCode = PreCheck(AppExecFwk::CHILD_PROCESS_TYPE_NATIVE);
151     if (errorCode != ChildProcessManagerErrorCode::ERR_OK) {
152         return errorCode;
153     }
154 
155     sptr<AppExecFwk::IAppMgr> appMgr = GetAppMgr();
156     if (appMgr == nullptr) {
157         TAG_LOGE(AAFwkTag::PROCESSMGR, "GetAppMgr failed");
158         return ChildProcessManagerErrorCode::ERR_GET_APP_MGR_FAILED;
159     }
160 
161     std::lock_guard<std::mutex> lock(childProcessCountLock_);
162     auto ret = appMgr->StartNativeChildProcess(libName, childProcessCount_, callbackStub);
163     TAG_LOGD(AAFwkTag::PROCESSMGR, "StartNativeChildProcess ret:%{public}d", ret);
164 
165     if (ret != ERR_OK) {
166         TAG_LOGE(AAFwkTag::PROCESSMGR, "StartNativeChildProcess error:%{public}d", ret);
167         return ChildProcessManagerErrorUtil::GetChildProcessManagerErrorCode(ret);
168     }
169 
170     ++childProcessCount_;
171     return ChildProcessManagerErrorCode::ERR_OK;
172 }
173 
RegisterSignal()174 void ChildProcessManager::RegisterSignal()
175 {
176     if (!signalRegistered_) {
177         signalRegistered_ = true;
178         TAG_LOGD(AAFwkTag::PROCESSMGR, "Register signal");
179         signal(SIGCHLD, ChildProcessManager::HandleSigChild);
180     }
181 }
182 
HandleSigChild(int32_t signo)183 void ChildProcessManager::HandleSigChild(int32_t signo)
184 {
185     while (waitpid(-1, NULL, WNOHANG) > 0) {
186         continue;
187     }
188 }
189 
PreCheck()190 ChildProcessManagerErrorCode ChildProcessManager::PreCheck()
191 {
192     if (!AAFwk::AppUtils::GetInstance().IsMultiProcessModel()) {
193         TAG_LOGE(AAFwkTag::PROCESSMGR, "Multi process model disabled");
194         return ChildProcessManagerErrorCode::ERR_MULTI_PROCESS_MODEL_DISABLED;
195     }
196     if (IsChildProcess()) {
197         TAG_LOGE(AAFwkTag::PROCESSMGR, "Already in child process");
198         return ChildProcessManagerErrorCode::ERR_ALREADY_IN_CHILD_PROCESS;
199     }
200     return ChildProcessManagerErrorCode::ERR_OK;
201 }
202 
PreCheck(int32_t childProcessType)203 ChildProcessManagerErrorCode ChildProcessManager::PreCheck(int32_t childProcessType)
204 {
205     if (!AAFwk::AppUtils::GetInstance().IsMultiProcessModel() &&
206         childProcessType != AppExecFwk::CHILD_PROCESS_TYPE_NATIVE_ARGS &&
207         childProcessType != AppExecFwk::CHILD_PROCESS_TYPE_NATIVE) {
208         TAG_LOGE(AAFwkTag::PROCESSMGR, "Not support child process.");
209         auto useNewErrorCode = childProcessType != AppExecFwk::CHILD_PROCESS_TYPE_JS;
210         return useNewErrorCode ? ChildProcessManagerErrorCode::ERR_MULTI_PROCESS_MODEL_DISABLED_NEW :
211             ChildProcessManagerErrorCode::ERR_MULTI_PROCESS_MODEL_DISABLED;
212     }
213     if (isChildProcessBySelfFork_) {
214         TAG_LOGE(AAFwkTag::PROCESSMGR, "Already in child process");
215         return ChildProcessManagerErrorCode::ERR_ALREADY_IN_CHILD_PROCESS;
216     }
217     return ChildProcessManagerErrorCode::ERR_OK;
218 }
219 
IsChildProcess()220 bool ChildProcessManager::IsChildProcess()
221 {
222     return isChildProcessBySelfFork_ || HasChildProcessRecord();
223 }
224 
IsChildProcessBySelfFork()225 bool ChildProcessManager::IsChildProcessBySelfFork()
226 {
227     return isChildProcessBySelfFork_;
228 }
229 
HandleChildProcessBySelfFork(const std::string & srcEntry,const AppExecFwk::BundleInfo & bundleInfo)230 void ChildProcessManager::HandleChildProcessBySelfFork(const std::string &srcEntry,
231     const AppExecFwk::BundleInfo &bundleInfo)
232 {
233     TAG_LOGD(AAFwkTag::PROCESSMGR, "start");
234     isChildProcessBySelfFork_ = true;
235     std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::GetMainEventRunner();
236     if (eventRunner == nullptr) {
237         TAG_LOGE(AAFwkTag::PROCESSMGR, "null eventRunner");
238         return;
239     }
240     eventRunner->Stop();
241 
242     AppExecFwk::HapModuleInfo hapModuleInfo;
243     if (!GetEntryHapModuleInfo(bundleInfo, hapModuleInfo)) {
244         TAG_LOGE(AAFwkTag::PROCESSMGR, "GetHapModuleInfo failed");
245         return;
246     }
247 
248     auto runtime = CreateRuntime(bundleInfo, hapModuleInfo, false, g_jitEnabled);
249     if (!runtime) {
250         TAG_LOGE(AAFwkTag::PROCESSMGR, "Create runtime failed");
251         return;
252     }
253     TAG_LOGD(AAFwkTag::PROCESSMGR, "StartDebugMode, isStartWithDebug: %{public}d, processName: %{public}s, "
254         "isDebugApp: %{public}d, isStartWithNative: %{public}d", g_debugOption.isStartWithDebug,
255         g_debugOption.processName.c_str(), g_debugOption.isDebugApp, g_debugOption.isStartWithNative);
256     runtime->StartDebugMode(g_debugOption);
257     std::string srcPath;
258     srcPath.append(hapModuleInfo.moduleName).append("/").append(srcEntry);
259     LoadJsFile(srcPath, hapModuleInfo, runtime);
260     TAG_LOGD(AAFwkTag::PROCESSMGR, "end");
261     exit(0);
262 }
263 
LoadJsFile(const std::string & srcEntry,const AppExecFwk::HapModuleInfo & hapModuleInfo,std::unique_ptr<AbilityRuntime::Runtime> & runtime,std::shared_ptr<AppExecFwk::ChildProcessArgs> args)264 bool ChildProcessManager::LoadJsFile(const std::string &srcEntry, const AppExecFwk::HapModuleInfo &hapModuleInfo,
265     std::unique_ptr<AbilityRuntime::Runtime> &runtime, std::shared_ptr<AppExecFwk::ChildProcessArgs> args)
266 {
267     std::shared_ptr<ChildProcessStartInfo> processStartInfo = std::make_shared<ChildProcessStartInfo>();
268     std::string filename = std::filesystem::path(srcEntry).stem();
269     processStartInfo->name = filename;
270     processStartInfo->moduleName = hapModuleInfo.moduleName;
271     processStartInfo->hapPath = hapModuleInfo.hapPath;
272     processStartInfo->srcEntry = srcEntry;
273     processStartInfo->isEsModule = (hapModuleInfo.compileMode == AppExecFwk::CompileMode::ES_MODULE);
274 
275     auto process = ChildProcess::Create(runtime);
276     if (process == nullptr) {
277         TAG_LOGE(AAFwkTag::PROCESSMGR, "Create ChildProcess failed");
278         return false;
279     }
280     bool ret = process->Init(processStartInfo);
281     if (!ret) {
282         TAG_LOGE(AAFwkTag::PROCESSMGR, "init failed");
283         return false;
284     }
285     if (args) {
286         process->OnStart(args);
287     } else {
288         process->OnStart();
289     }
290     TAG_LOGD(AAFwkTag::PROCESSMGR, "end");
291     return true;
292 }
293 
LoadNativeLib(const std::string & moduleName,const std::string & libPath,const sptr<IRemoteObject> & mainProcessCb)294 bool ChildProcessManager::LoadNativeLib(const std::string &moduleName,
295     const std::string &libPath, const sptr<IRemoteObject> &mainProcessCb)
296 {
297     auto childProcess = NativeChildIpcProcess::Create();
298     if (childProcess == nullptr) {
299         TAG_LOGE(AAFwkTag::PROCESSMGR, "Failed create NativeChildIpcProcess");
300         return false;
301     }
302 
303     std::shared_ptr<ChildProcessStartInfo> processStartInfo = std::make_shared<ChildProcessStartInfo>();
304     processStartInfo->moduleName = moduleName;
305     processStartInfo->name = std::filesystem::path(libPath).stem();
306     processStartInfo->srcEntry = libPath;
307     processStartInfo->ipcObj = mainProcessCb;
308     if (!childProcess->Init(processStartInfo)) {
309         TAG_LOGE(AAFwkTag::PROCESSMGR, "NativeChildIpcProcess init failed");
310         return false;
311     }
312 
313     childProcess->OnStart();
314     TAG_LOGD(AAFwkTag::PROCESSMGR, "end");
315     return true;
316 }
317 
LoadNativeLibWithArgs(const std::string & moduleName,const std::string & srcEntry,const std::string & entryFunc,std::shared_ptr<AppExecFwk::ChildProcessArgs> args)318 bool ChildProcessManager::LoadNativeLibWithArgs(const std::string &moduleName, const std::string &srcEntry,
319     const std::string &entryFunc, std::shared_ptr<AppExecFwk::ChildProcessArgs> args)
320 {
321     TAG_LOGI(AAFwkTag::PROCESSMGR, "moduleName:%{public}s, srcEntry:%{public}s, entryFunc:%{public}s.",
322         moduleName.c_str(), srcEntry.c_str(), entryFunc.c_str());
323     auto childProcess = NativeArgsChildProcess::Create();
324     if (childProcess == nullptr) {
325         TAG_LOGE(AAFwkTag::PROCESSMGR, "Failed create NativeArgsChildProcess.");
326         return false;
327     }
328 
329     std::shared_ptr<ChildProcessStartInfo> processStartInfo = std::make_shared<ChildProcessStartInfo>();
330     processStartInfo->moduleName = moduleName;
331     processStartInfo->srcEntry = srcEntry;
332     processStartInfo->entryFunc = entryFunc;
333     if (!childProcess->Init(processStartInfo)) {
334         TAG_LOGE(AAFwkTag::PROCESSMGR, "NativeArgsChildProcess init failed.");
335         return false;
336     }
337 
338     childProcess->OnStart(args);
339     TAG_LOGD(AAFwkTag::PROCESSMGR, "LoadNativeLibWithArgs end.");
340     return true;
341 }
342 
CreateRuntime(const AppExecFwk::BundleInfo & bundleInfo,const AppExecFwk::HapModuleInfo & hapModuleInfo,const bool fromAppSpawn,const bool jitEnabled)343 std::unique_ptr<AbilityRuntime::Runtime> ChildProcessManager::CreateRuntime(const AppExecFwk::BundleInfo &bundleInfo,
344     const AppExecFwk::HapModuleInfo &hapModuleInfo, const bool fromAppSpawn, const bool jitEnabled)
345 {
346     AppExecFwk::ApplicationInfo applicationInfo = bundleInfo.applicationInfo;
347     AbilityRuntime::Runtime::Options options;
348     options.codePath = AbilityBase::Constants::LOCAL_CODE_PATH;
349     options.bundleName = hapModuleInfo.bundleName;
350     options.hapPath = hapModuleInfo.hapPath;
351     options.moduleName = hapModuleInfo.moduleName;
352     options.isBundle = (hapModuleInfo.compileMode != AppExecFwk::CompileMode::ES_MODULE);
353     options.uid = applicationInfo.uid;
354     options.isDebugVersion = applicationInfo.debug;
355     options.arkNativeFilePath = applicationInfo.arkNativeFilePath;
356     options.apiTargetVersion = applicationInfo.apiTargetVersion;
357     options.loadAce = true;
358     options.jitEnabled = jitEnabled;
359 
360     for (auto moduleItem : bundleInfo.hapModuleInfos) {
361         options.pkgContextInfoJsonStringMap[moduleItem.moduleName] = moduleItem.hapPath;
362         options.packageNameList[moduleItem.moduleName] = moduleItem.packageName;
363     }
364 
365     std::shared_ptr<AppExecFwk::EventRunner> eventRunner =
366         fromAppSpawn ? AppExecFwk::EventRunner::GetMainEventRunner() : AppExecFwk::EventRunner::Create();
367     options.eventRunner = eventRunner;
368 
369     return AbilityRuntime::Runtime::Create(options);
370 }
371 
GetBundleInfo(AppExecFwk::BundleInfo & bundleInfo)372 bool ChildProcessManager::GetBundleInfo(AppExecFwk::BundleInfo &bundleInfo)
373 {
374     auto sysMrgClient = DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance();
375     if (sysMrgClient == nullptr) {
376         TAG_LOGE(AAFwkTag::PROCESSMGR, "Get SysMrgClient failed");
377         return false;
378     }
379     auto bundleObj = sysMrgClient->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
380     if (bundleObj == nullptr) {
381         TAG_LOGE(AAFwkTag::PROCESSMGR, "null bundleObj");
382         return false;
383     }
384     auto bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
385     if (bundleMgr == nullptr) {
386         TAG_LOGE(AAFwkTag::PROCESSMGR, "null bundleMgr");
387         return false;
388     }
389     return (bundleMgr->GetBundleInfoForSelf(
390         (static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
391         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) +
392         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA)), bundleInfo) == ERR_OK);
393 }
394 
GetEntryHapModuleInfo(const AppExecFwk::BundleInfo & bundleInfo,AppExecFwk::HapModuleInfo & hapModuleInfo)395 bool ChildProcessManager::GetEntryHapModuleInfo(const AppExecFwk::BundleInfo &bundleInfo,
396     AppExecFwk::HapModuleInfo &hapModuleInfo)
397 {
398     if (bundleInfo.hapModuleInfos.empty()) {
399         TAG_LOGE(AAFwkTag::PROCESSMGR, "hapModuleInfos empty");
400         return false;
401     }
402     TAG_LOGD(AAFwkTag::PROCESSMGR, "hapModueInfos size: %{public}zu", bundleInfo.hapModuleInfos.size());
403     bool result = false;
404     for (const auto &info : bundleInfo.hapModuleInfos) {
405         if (info.moduleType == AppExecFwk::ModuleType::ENTRY) {
406             result = true;
407             hapModuleInfo = info;
408             break;
409         }
410     }
411     return result;
412 }
413 
GetHapModuleInfo(const AppExecFwk::BundleInfo & bundleInfo,const std::string & moduleName,AppExecFwk::HapModuleInfo & hapModuleInfo)414 bool ChildProcessManager::GetHapModuleInfo(const AppExecFwk::BundleInfo &bundleInfo, const std::string &moduleName,
415     AppExecFwk::HapModuleInfo &hapModuleInfo)
416 {
417     if (bundleInfo.hapModuleInfos.empty()) {
418         TAG_LOGE(AAFwkTag::PROCESSMGR, "hapModuleInfos empty!");
419         return false;
420     }
421     TAG_LOGD(AAFwkTag::PROCESSMGR, "hapModueInfos size: %{public}zu", bundleInfo.hapModuleInfos.size());
422     bool result = false;
423     for (const auto &info : bundleInfo.hapModuleInfos) {
424         if (info.name == moduleName) {
425             result = true;
426             hapModuleInfo = info;
427             break;
428         }
429     }
430     return result;
431 }
432 
HasChildProcessRecord()433 bool ChildProcessManager::HasChildProcessRecord()
434 {
435     sptr<AppExecFwk::IAppMgr> appMgr = GetAppMgr();
436     if (appMgr == nullptr) {
437         TAG_LOGE(AAFwkTag::PROCESSMGR, "GetAppMgr failed");
438         return false;
439     }
440     AppExecFwk::ChildProcessInfo info;
441     return appMgr->GetChildProcessInfoForSelf(info) == ERR_OK;
442 }
443 
GetAppMgr()444 sptr<AppExecFwk::IAppMgr> ChildProcessManager::GetAppMgr()
445 {
446     auto sysMrgClient = DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance();
447     if (sysMrgClient == nullptr) {
448         TAG_LOGE(AAFwkTag::PROCESSMGR, "Get SysMrgClient failed");
449         return nullptr;
450     }
451     auto object = sysMrgClient->GetSystemAbility(APP_MGR_SERVICE_ID);
452     if (object == nullptr) {
453         TAG_LOGE(AAFwkTag::PROCESSMGR, "GetAppMgr failed");
454         return nullptr;
455     }
456     return iface_cast<AppExecFwk::IAppMgr>(object);
457 }
458 
SetForkProcessJITEnabled(bool jitEnabled)459 void ChildProcessManager::SetForkProcessJITEnabled(bool jitEnabled)
460 {
461     g_jitEnabled = jitEnabled;
462 }
463 
SetForkProcessDebugOption(const std::string bundleName,const bool isStartWithDebug,const bool isDebugApp,const bool isStartWithNative)464 void ChildProcessManager::SetForkProcessDebugOption(const std::string bundleName, const bool isStartWithDebug,
465     const bool isDebugApp, const bool isStartWithNative)
466 {
467     g_debugOption.bundleName = bundleName;
468     g_debugOption.isStartWithDebug = isStartWithDebug;
469     g_debugOption.isDebugApp = isDebugApp;
470     g_debugOption.isStartWithNative = isStartWithNative;
471 }
472 
SetAppSpawnForkDebugOption(Runtime::DebugOption & debugOption,std::shared_ptr<AppExecFwk::ChildProcessInfo> processInfo)473 void ChildProcessManager::SetAppSpawnForkDebugOption(Runtime::DebugOption &debugOption,
474     std::shared_ptr<AppExecFwk::ChildProcessInfo> processInfo)
475 {
476     if (!processInfo) {
477         TAG_LOGE(AAFwkTag::PROCESSMGR, "SetAppSpawnDebugOption failed, processInfo is nullptr.");
478         return;
479     }
480     debugOption.processName = processInfo->processName;
481     debugOption.isStartWithDebug = processInfo->isStartWithDebug;
482     debugOption.isDebugApp = processInfo->isDebugApp;
483     debugOption.isStartWithNative = processInfo->isStartWithNative;
484 }
485 
MakeProcessName(const std::string & srcEntry)486 void ChildProcessManager::MakeProcessName(const std::string &srcEntry)
487 {
488     std::string processName = g_debugOption.bundleName;
489     if (srcEntry.empty()) {
490         TAG_LOGE(AAFwkTag::PROCESSMGR, "srcEntry empty");
491     } else {
492         TAG_LOGW(AAFwkTag::PROCESSMGR, "srcEntry not empty");
493         std::string filename = std::filesystem::path(srcEntry).stem();
494         if (!filename.empty()) {
495             processName.append(":");
496             processName.append(filename);
497         }
498     }
499     std::lock_guard<std::mutex> lock(childProcessCountLock_);
500     processName.append(std::to_string(childProcessCount_));
501     childProcessCount_++;
502     TAG_LOGD(AAFwkTag::PROCESSMGR, "processName: %{public}s", processName.c_str());
503     g_debugOption.processName = processName;
504 }
505 
GetModuleNameFromSrcEntry(const std::string & srcEntry)506 std::string ChildProcessManager::GetModuleNameFromSrcEntry(const std::string &srcEntry)
507 {
508     std::string::size_type pos = srcEntry.find_first_of('/');
509     if (pos == std::string::npos) {
510         return "";
511     }
512     std::string moduleName = srcEntry.substr(0, pos);
513     if (moduleName == ".") {
514         return "";
515     }
516     return moduleName;
517 }
518 }  // namespace AbilityRuntime
519 }  // namespace OHOS