1 /* 2 * Copyright (c) 2023 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_CHILD_PROCESS_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_CHILD_PROCESS_MANAGER_H 18 19 #include <mutex> 20 #include <string> 21 #include <sys/types.h> 22 23 #include "app_mgr_interface.h" 24 #include "bundle_info.h" 25 #include "child_process_args.h" 26 #include "child_process_info.h" 27 #include "child_process_manager_error_utils.h" 28 #include "child_process_options.h" 29 #include "hap_module_info.h" 30 #include "runtime.h" 31 #include "iremote_object.h" 32 33 namespace OHOS { 34 namespace AbilityRuntime { 35 class ChildProcessManager { 36 public: 37 static ChildProcessManager &GetInstance(); 38 ~ChildProcessManager(); 39 40 static void HandleSigChild(int32_t signo); 41 bool IsChildProcess(); 42 bool IsChildProcessBySelfFork(); 43 ChildProcessManagerErrorCode StartChildProcessBySelfFork(const std::string &srcEntry, pid_t &pid); 44 ChildProcessManagerErrorCode StartChildProcessByAppSpawnFork(const std::string &srcEntry, pid_t &pid); 45 ChildProcessManagerErrorCode StartChildProcessWithArgs(const std::string &srcEntry, pid_t &pid, 46 int32_t childProcessType, const AppExecFwk::ChildProcessArgs &args, 47 const AppExecFwk::ChildProcessOptions &options); 48 ChildProcessManagerErrorCode StartNativeChildProcessByAppSpawnFork( 49 const std::string &libName, const sptr<IRemoteObject> &callbackStub); 50 bool GetBundleInfo(AppExecFwk::BundleInfo &bundleInfo); 51 bool GetEntryHapModuleInfo(const AppExecFwk::BundleInfo &bundleInfo, AppExecFwk::HapModuleInfo &hapModuleInfo); 52 bool GetHapModuleInfo(const AppExecFwk::BundleInfo &bundleInfo, const std::string &moduleName, 53 AppExecFwk::HapModuleInfo &hapModuleInfo); 54 std::unique_ptr<AbilityRuntime::Runtime> CreateRuntime(const AppExecFwk::BundleInfo &bundleInfo, 55 const AppExecFwk::HapModuleInfo &hapModuleInfo, const bool fromAppSpawn, const bool jitEnabled); 56 bool LoadJsFile(const std::string &srcEntry, const AppExecFwk::HapModuleInfo &hapModuleInfo, 57 std::unique_ptr<AbilityRuntime::Runtime> &runtime, 58 std::shared_ptr<AppExecFwk::ChildProcessArgs> args = nullptr); 59 bool LoadNativeLib(const std::string &moduleName, const std::string &libPath, 60 const sptr<IRemoteObject> &mainProcessCb); 61 bool LoadNativeLibWithArgs(const std::string &moduleName, const std::string &srcEntry, 62 const std::string &entryFunc, std::shared_ptr<AppExecFwk::ChildProcessArgs> args); 63 void SetForkProcessJITEnabled(bool jitEnabled); 64 void SetForkProcessDebugOption(const std::string bundleName, const bool isStartWithDebug, const bool isDebugApp, 65 const bool isStartWithNative); 66 void SetAppSpawnForkDebugOption(Runtime::DebugOption &debugOption, 67 std::shared_ptr<AppExecFwk::ChildProcessInfo> processInfo); 68 std::string GetModuleNameFromSrcEntry(const std::string &srcEntry); 69 70 private: 71 ChildProcessManager(); 72 73 ChildProcessManagerErrorCode PreCheck(); 74 ChildProcessManagerErrorCode PreCheck(int32_t childProcessType); 75 void RegisterSignal(); 76 void HandleChildProcessBySelfFork(const std::string &srcEntry, const AppExecFwk::BundleInfo &bundleInfo); 77 bool HasChildProcessRecord(); 78 sptr<AppExecFwk::IAppMgr> GetAppMgr(); 79 void MakeProcessName(const std::string &srcEntry); 80 81 static bool signalRegistered_; 82 bool isChildProcessBySelfFork_ = false; 83 int32_t childProcessCount_ = 0; 84 std::mutex childProcessCountLock_; 85 86 DISALLOW_COPY_AND_MOVE(ChildProcessManager); 87 }; 88 } // namespace AAFwk 89 } // namespace OHOS 90 #endif // OHOS_ABILITY_RUNTIME_CHILD_PROCESS_MANAGER_H 91