diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..7a0ff793f07b908285e892d4fa97e311c7fbd639 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,67 @@ +{ + "files.associations": { + "functional": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "ranges": "cpp", + "regex": "cpp", + "semaphore": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "typeinfo": "cpp", + "variant": "cpp" + } +} \ No newline at end of file diff --git a/frameworks/native/ability/native/child_process_manager/child_process_manager.cpp b/frameworks/native/ability/native/child_process_manager/child_process_manager.cpp index 7db258af22682d6580fec10cdac50eace44ec4cd..ec7c1fa07f6751729ba0d9f0fbaf79786c17de41 100644 --- a/frameworks/native/ability/native/child_process_manager/child_process_manager.cpp +++ b/frameworks/native/ability/native/child_process_manager/child_process_manager.cpp @@ -14,7 +14,7 @@ */ #include "child_process_manager.h" - +#include "native_child_process.h" #include #include #include @@ -44,6 +44,7 @@ #include "sys_mgr_client.h" #include "system_ability_definition.h" + namespace OHOS { namespace AbilityRuntime { namespace { @@ -162,7 +163,7 @@ ChildProcessManagerErrorCode ChildProcessManager::StartChildProcessWithArgs( } ChildProcessManagerErrorCode ChildProcessManager::StartNativeChildProcessByAppSpawnFork( - const std::string &libName, const sptr &callbackStub) + const std::string &libName, const sptr &callbackStub, Ability_ChildProcessConfigs* configs) { TAG_LOGI(AAFwkTag::PROCESSMGR, "libName:%{private}s", libName.c_str()); ChildProcessManagerErrorCode errorCode = PreCheck(AppExecFwk::CHILD_PROCESS_TYPE_NATIVE); @@ -176,8 +177,15 @@ ChildProcessManagerErrorCode ChildProcessManager::StartNativeChildProcessByAppSp return ChildProcessManagerErrorCode::ERR_GET_APP_MGR_FAILED; } + std::string processName; + if(configs != nullptr) { + processName = configs->processName; + } else { + processName = ""; + } + std::lock_guard lock(childProcessCountLock_); - auto ret = appMgr->StartNativeChildProcess(libName, childProcessCount_, callbackStub); + auto ret = appMgr->StartNativeChildProcess(libName, childProcessCount_, callbackStub, processName); TAG_LOGD(AAFwkTag::PROCESSMGR, "StartNativeChildProcess ret:%{public}d", ret); if (ret != ERR_OK) { diff --git a/frameworks/native/child_process/src/native_child_process.cpp b/frameworks/native/child_process/src/native_child_process.cpp index 3f731c729a4c73ccca9c4c919a105e9094d91fea..4442d4ba5eb343d8779e3ce5e08834e2f83a0b5f 100644 --- a/frameworks/native/child_process/src/native_child_process.cpp +++ b/frameworks/native/child_process/src/native_child_process.cpp @@ -127,6 +127,101 @@ NativeChildProcess_Args* OH_Ability_GetCurrentChildProcessArgs() return result; } + +Ability_ChildProcessConfigs* OH_Ability_CreateChildProcessConfigs() +{ + std::unique_ptr configs = std::make_unique(); + return configs.release(); +} + +Ability_NativeChildProcess_ErrCode OH_Ability_DestroyChildProcessConfigs(Ability_ChildProcessConfigs* configs) +{ + if(configs == nullptr){ + TAG_LOGE(AAFwkTag::APPKIT, "null Ability_ChildProcessConfigs"); + return NCP_ERR_INVALID_PARAM; + } + delete configs; + configs = nullptr; + return NCP_NO_ERROR; +} + +Ability_NativeChildProcess_ErrCode OH_Ability_ChildProcessConfigs_SetIsolationMode( + Ability_ChildProcessConfigs* configs, NativeChildProcess_IsolationMode isolationMode) +{ + if(configs == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "null Ability_ChildProcessConfigs"); + return NCP_ERR_INVALID_PARAM; + } else { + configs->isolationMode = isolationMode; + return NCP_NO_ERROR; + } +} + +Ability_NativeChildProcess_ErrCode OH_Ability_ChildProcessConfigs_SetProcessName(Ability_ChildProcessConfigs* configs, + const char* processName) +{ + if (configs == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "null Ability_ChildProcessConfigs"); + return NCP_ERR_INVALID_PARAM; + } + if (processName == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "null processName"); + return NCP_ERR_INVALID_PARAM; + } + int processNameLen = strlen(processName); + if(processNameLen == 0) { + TAG_LOGE(AAFwkTag::APPKIT, "processName is 0"); + return NCP_ERR_INVALID_PARAM; + } + if (processNameLen > 64) { + TAG_LOGE(AAFwkTag::APPKIT, "processName is larger than 64"); + return NCP_ERR_INVALID_PARAM; + } + int i = 0; + while (i < processNameLen) { + if((processName[i] >= 'a' && processName[i] <= 'z') || + (processName[i] >= 'A' && processName[i] <= 'Z') || + (processName[i] >= '0' && processName[i] <= '9') || + processName[i] == '_'){ + i++; + }else{ + TAG_LOGE(AAFwkTag::APPKIT, "Wrong processName"); + return NCP_ERR_INVALID_PARAM; + } + } + configs->processName = std::string(processName); + return NCP_NO_ERROR; +} + +Ability_NativeChildProcess_ErrCode OH_Ability_CreateNativeChildProcessWithConfigs(const char* libName, + Ability_ChildProcessConfigs* configs, OH_Ability_OnNativeChildProcessStarted onProcessStarted) +{ + if (libName == nullptr || *libName == '\0' || onProcessStarted == nullptr || configs == nullptr) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "null libname or callback or configs"); + return NCP_ERR_INVALID_PARAM; + } + + std::string strLibName(libName); + if (strLibName.find("../") != std::string::npos) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "relative path not allow"); + return NCP_ERR_INVALID_PARAM; + } + + sptr callbackStub(new (std::nothrow) NativeChildCallback(onProcessStarted)); + if (!callbackStub) { + TAG_LOGE(AAFwkTag::PROCESSMGR, "null callbackStub"); + return NCP_ERR_INTERNAL; + } + + ChildProcessManager &mgr = ChildProcessManager::GetInstance(); + auto cpmErr = mgr.StartNativeChildProcessByAppSpawnFork(strLibName, callbackStub, configs); + if (cpmErr != ChildProcessManagerErrorCode::ERR_OK) { + return ChildProcessManagerErrorUtil::CvtChildProcessManagerErrCode(cpmErr); + } + + ChildCallbackManager::GetInstance().AddRemoteObject(callbackStub); + return NCP_NO_ERROR; +} sptr GetGlobalNativeChildCallbackStub() { std::lock_guard lock(g_callbackStubMutex); diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h index fd474e550268c59fc4b29ee9a752605ddabdbf9a..be632e4b6f77659e24399a1e83f7f88f79ad6eb0 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h @@ -888,7 +888,7 @@ public: * @return Returns ERR_OK on success, others on failure. */ virtual int32_t StartNativeChildProcess(const std::string &libName, int32_t childProcessCount, - const sptr &callback) = 0; + const sptr &callback, const std::string &processName) = 0; #endif // SUPPORT_CHILD_PROCESS /** diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h index bc6de5544db8a292a3f8431a80ae5291b023c263..59d6686308efa705dad5f6eb0bb89384ffd3b307 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h @@ -777,7 +777,7 @@ public: * @return Returns ERR_OK on success, others on failure. */ int32_t StartNativeChildProcess(const std::string &libName, int32_t childProcessCount, - const sptr &callback) override; + const sptr &callback, const std::string &processName) override; #endif // SUPPORT_CHILD_PROCESS /** diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp index 474fcfb8612554078f00c7c7bad01e5bc8c8947e..2c6897841293e1ebce8d7a06b34776e49e395ccc 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp @@ -2075,7 +2075,7 @@ void AppMgrProxy::SetAppAssertionPauseState(bool flag) #ifdef SUPPORT_CHILD_PROCESS int32_t AppMgrProxy::StartNativeChildProcess(const std::string &libName, int32_t childProcessCount, - const sptr &callback) + const sptr &callback, const std::string &processName) { TAG_LOGD(AAFwkTag::APPMGR, "called"); if (libName.empty() || !callback) { @@ -2093,6 +2093,7 @@ int32_t AppMgrProxy::StartNativeChildProcess(const std::string &libName, int32_t PARCEL_UTIL_WRITE_RET_INT(data, String, libName); PARCEL_UTIL_WRITE_RET_INT(data, Int32, childProcessCount); PARCEL_UTIL_WRITE_RET_INT(data, RemoteObject, callback); + PARCEL_UTIL_WRITE_RET_INT(data, String, processName); PARCEL_UTIL_SENDREQ_RET_INT(AppMgrInterfaceCode::START_NATIVE_CHILD_PROCESS, data, reply, option); return reply.ReadInt32(); diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp index 6032db58275c1d9333738087614083a7f9aee715..28d956e8377ae808ca06dd85dc166056ce77f6a2 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp @@ -1808,7 +1808,8 @@ int32_t AppMgrStub::HandleStartNativeChildProcess(MessageParcel &data, MessagePa std::string libName = data.ReadString(); int32_t childCount = data.ReadInt32(); sptr callback = data.ReadRemoteObject(); - int32_t result = StartNativeChildProcess(libName, childCount, callback); + std::string processName = data.ReadString(); + int32_t result = StartNativeChildProcess(libName, childCount, callback, processName); if (!reply.WriteInt32(result)) { TAG_LOGE(AAFwkTag::APPMGR, "Write ret error."); return IPC_STUB_ERR; diff --git a/interfaces/inner_api/child_process_manager/include/child_process_manager.h b/interfaces/inner_api/child_process_manager/include/child_process_manager.h index db8c8b824a43c23713bb5687cf2432564acf411d..e9b1c34b4cd86d7b3b8c3418649d788ca6eab849 100644 --- a/interfaces/inner_api/child_process_manager/include/child_process_manager.h +++ b/interfaces/inner_api/child_process_manager/include/child_process_manager.h @@ -29,6 +29,7 @@ #include "hap_module_info.h" #include "runtime.h" #include "iremote_object.h" +#include "native_child_process.h" namespace OHOS { namespace AbilityRuntime { @@ -46,7 +47,7 @@ public: int32_t childProcessType, const AppExecFwk::ChildProcessArgs &args, const AppExecFwk::ChildProcessOptions &options); ChildProcessManagerErrorCode StartNativeChildProcessByAppSpawnFork( - const std::string &libName, const sptr &callbackStub); + const std::string &libName, const sptr &callbackStub, Ability_ChildProcessConfigs* configs = nullptr); bool GetBundleInfo(AppExecFwk::BundleInfo &bundleInfo); bool GetEntryHapModuleInfo(const AppExecFwk::BundleInfo &bundleInfo, AppExecFwk::HapModuleInfo &hapModuleInfo); bool GetHapModuleInfo(const AppExecFwk::BundleInfo &bundleInfo, const std::string &moduleName, diff --git a/interfaces/kits/c/ability/ability_runtime/child_process/native_child_process.h b/interfaces/kits/c/ability/ability_runtime/child_process/native_child_process.h index 3a25cc7a45590a35c24fc9d2d4ca62568d1006c4..e36c135b98bf06a90cd121fd7fd690c5dd06e2cc 100644 --- a/interfaces/kits/c/ability/ability_runtime/child_process/native_child_process.h +++ b/interfaces/kits/c/ability/ability_runtime/child_process/native_child_process.h @@ -16,8 +16,10 @@ #ifndef OHOS_ABILITY_RUNTIME_C_NATIVE_CHILD_PROCESS_H #define OHOS_ABILITY_RUNTIME_C_NATIVE_CHILD_PROCESS_H +#include #include "ipc_cparcel.h" + /** * @addtogroup ChildProcess * @{ @@ -119,6 +121,7 @@ typedef enum Ability_NativeChildProcess_ErrCode { } Ability_NativeChildProcess_ErrCode; + /** * @brief Defines a callback function for notifying the child process startup result. * @@ -248,6 +251,82 @@ typedef struct NativeChildProcess_Args { struct NativeChildProcess_FdList fdList; } NativeChildProcess_Args; +/** +* @brief Defines a struct for the child process configs. +* @since 20 +*/ +typedef struct Ability_ChildProcessConfigs{ + std::string processName = ""; + NativeChildProcess_IsolationMode isolationMode; +} Ability_ChildProcessConfigs; + +/** +* @brief Creates a new child process configs object. +* The caller is responsible for destroying the returned object by calling +* {@link OH_Ability_DestroyChildProcessConfigs} to avoid memory leaks. +* @return Returns a pointer to the newly created {@link Ability_ChildProcessConfigs} object if successful. +* Returns nullptr if an internal error occurs or memory allocation fails. +* @since 20 +*/ +Ability_ChildProcessConfigs* OH_Ability_CreateChildProcessConfigs(); + +/** +* @brief Sets the process name for the specified child process configs. +* +* @param configs Pointer to the child process configs object. Must not be nullptr. +* @param processName The process name to set. +* Must be a non-empty string containing only letters, digits, or underscores. +* Maximum length is 64 characters. +* The name ultimately assigned to the process is {bundleName}:{processName}. +* @return Returns {@link NCP_NO_ERROR} if the process name is set successful. +* Returns {@link NCP_NO_ERR_INVALID_PARAM} if the input parameters are invalid. +* @since 20 +*/ +Ability_NativeChildProcess_ErrCode OH_Ability_ChildProcessConfigs_SetProcessName(Ability_ChildProcessConfigs* configs, + const char* processName); + +/** + * @brief Creates a child process, loads the specified dynamic library file, and returns the startup result + * asynchronously through a callback parameter. + * The callback notification is an independent thread. When implementing the callback function, + * pay attention to thread synchronization and do not perform time-consuming operations to avoid long-time blocking. + * + * The dynamic library specified must implement and export the following functions: + * 1. OHIPCRemoteStub* NativeChildProcess_OnConnect() + * 2. void NativeChildProcess_MainProc() + * + * The processing logic sequence is shown in the following pseudocode: + * Main process: + * 1. OH_Ability_CreateNativeChildProcess(libName, onProcessStartedCallback) + * Child process: + * 2. dlopen(libName) + * 3. dlsym("NativeChildProcess_OnConnect") + * 4. dlsym("NativeChildProcess_MainProc") + * 5. ipcRemote = NativeChildProcess_OnConnect() + * 6. NativeChildProcess_MainProc() + * Main process: + * 7. onProcessStartedCallback(ipcRemote, errCode) + * Child process: + * 8. The child process exits after the NativeChildProcess_MainProc() function is returned. + * + * @param libName Name of the dynamic library file loaded in the child process. The value cannot be nullptr. + * @param configs Pointer to the child process configs object. The value cannot be nullptr. + * @param onProcessStarted Pointer to the callback function for notifying the child process startup result. + * The value cannot be nullptr. For details, see {@link OH_Ability_OnNativeChildProcessStarted}. + * @return Returns {@link NCP_NO_ERROR} if the call is successful, but the actual startup result is notified by the + * callback function. + * Returns {@link NCP_ERR_INVALID_PARAM} if the dynamic library name or callback function pointer is invalid. + * Returns {@link NCP_ERR_NOT_SUPPORTED} if the device does not support the creation of native child processes. + * Returns {@link NCP_ERR_MULTI_PROCESS_DISABLED} if the multi-process mode is disabled on the device. + * Returns {@link NCP_ERR_ALREADY_IN_CHILD} if it is not allowed to create another child process in the child process. + * Returns {@link NCP_ERR_MAX_CHILD_PROCESSES_REACHED} if the maximum number of native child processes is reached. + * For details, see {@link Ability_NativeChildProcess_ErrCode}. + * @see OH_Ability_OnNativeChildProcessStarted + * @since 12 + */ + Ability_NativeChildProcess_ErrCode OH_Ability_CreateNativeChildProcessWithConfigs(const char* libName, + Ability_ChildProcessConfigs* configs, OH_Ability_OnNativeChildProcessStarted onProcessStarted); + /** * @brief Starts a child process, loads the specified dynamic library file. * @@ -284,6 +363,66 @@ Ability_NativeChildProcess_ErrCode OH_Ability_StartNativeChildProcess( const char* entry, NativeChildProcess_Args args, NativeChildProcess_Options options, int32_t *pid); +/** + * @brief Starts a child process, loads the specified dynamic library file. + * + * The dynamic library specified must implement a function with NativeChildProcess_Args as a + * pamameter(function name can be customized), and export the function, such as: + * 1. void Main(NativeChildProcess_Args args); + * + * The processing logic sequence is shown in the following pseudocode: + * Main process: + * 1. OH_Ability_StartNativeChildProcess(entryPoint, args, options) + * Child process: + * 2. dlopen(libName) + * 3. dlsym("Main") + * 4. Main(args) + * 5. The child process exits after the Main(args) function is returned + * + * @param entry Dynamic library and entry function loaded in child process, such as "libEntry.so:Main". + * The value cannot be nullptr. + * @param args The arguments passed to the child process. + * For details, see {@link NativeChildProcess_Args}. + * @param configs Pointer to the child process configs object. The value cannot be null. + * For details, see {@link Ability_ChildProcessConfigs}. + * @param pid The started child process id. + * @return Returns {@link NCP_NO_ERROR} if the call is successful. + * Returns {@link NCP_ERR_INVALID_PARAM} if the dynamic library name or callback function pointer is invalid.\n + * Returns {@link NCP_ERR_NOT_SUPPORTED} if the device does not support the creation of native child processes.\n + * Returns {@link NCP_ERR_ALREADY_IN_CHILD} if it is not allowed to create another child process in the child process.\n + * Returns {@link NCP_ERR_MAX_CHILD_PROCESSES_REACHED} if the maximum number of native child processes is reached.\n + * For details, see {@link Ability_NativeChildProcess_ErrCode}. + * @since 13 + */ + Ability_NativeChildProcess_ErrCode OH_Ability_StartNativeChildProcessWithConfigs( + const char* entry, NativeChildProcess_Args args, Ability_ChildProcessConfigs* configs, int32_t *pid); + +/** +* @brief Destroys a child process configs object and releases associated rescources. +* +* @param configs Pointer to the child process configs object to be destroyed. +* After this call, the pointer becomes invalid and must not be used. +* Passing nullptr is allowed and will be ignored. +* {@link OH_Ability_DestroyChildProcessConfigs} to avoid memory leaks. +* @return Returns {@link NCP_NO_ERROR} if the operation is successful or if the input is nullptr. +* Returns {@link NCP_NO_ERR_INVALID_PARAM} if the input parameters are invalid. +* @since 20 +*/ +Ability_NativeChildProcess_ErrCode OH_Ability_DestroyChildProcessConfigs(Ability_ChildProcessConfigs* configs); + +/** +* @brief Sets the isolation mode for the specified child process configs. +* The isolationMode only takes effect in {@link OH_Ability_StartNativeChildProcessWithConfigs}. +* +* @param configs Pointer to the child process configs object. Must not be nullptr. +* @param isolationMode The isolation mode to set. See {@link NativeChildProcess_IsolationMode} for details. +* @return Returns {@link NCP_NO_ERROR} if the isolation mode is set successful. +* Returns {@link NCP_NO_ERR_INVALID_PARAM} if the input parameters are invalid. +* @since 20 +*/ +Ability_NativeChildProcess_ErrCode OH_Ability_ChildProcessConfigs_SetIsolationMode( + Ability_ChildProcessConfigs* configs, NativeChildProcess_IsolationMode isolationMode); + /** * @brief Child process get self NativeChildProcess_Args. * diff --git a/services/appmgr/include/app_mgr_service.h b/services/appmgr/include/app_mgr_service.h index b8722e8e83d57b652c00a8c557251c094884038f..df96719bb0512f0b58ba5179971f35ea18978e29 100644 --- a/services/appmgr/include/app_mgr_service.h +++ b/services/appmgr/include/app_mgr_service.h @@ -803,7 +803,7 @@ public: * @return Returns ERR_OK on success, others on failure. */ int32_t StartNativeChildProcess(const std::string &libName, int32_t childProcessCount, - const sptr &callback) override; + const sptr &callback, const std::string &processName) override; #endif // SUPPORT_CHILD_PROCESS /** diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index a9ff630606a81324501b5e9523350ead8b12e9eb..769c436a3fd4c174ad350cf5697a0b4615973df5 100644 --- a/services/appmgr/include/app_mgr_service_inner.h +++ b/services/appmgr/include/app_mgr_service_inner.h @@ -1268,7 +1268,7 @@ public: * @return Returns ERR_OK on success, others on failure. */ virtual int32_t StartNativeChildProcess(const pid_t hostPid, - const std::string &libName, int32_t childProcessCount, const sptr &callback); + const std::string &libName, int32_t childProcessCount, const sptr &callback, const std::string &processName); #endif // SUPPORT_CHILD_PROCESS virtual int32_t RegisterNativeChildExitNotify(const sptr &callback); diff --git a/services/appmgr/include/child_process_record.h b/services/appmgr/include/child_process_record.h index a36cb57f23f2f778e4179ff1011293b21567481e..e14d826952d9f48589cabd3bb1d010cffb269058 100644 --- a/services/appmgr/include/child_process_record.h +++ b/services/appmgr/include/child_process_record.h @@ -36,6 +36,8 @@ public: const std::shared_ptr hostRecord); ChildProcessRecord(pid_t hostPid, const std::string &libName, const std::shared_ptr hostRecord, const sptr &mainProcessCb, int32_t childProcessCount, bool isStartWithDebug); + ChildProcessRecord(pid_t hostPid, const std::string &libName, const std::shared_ptr hostRecord, + const sptr &mainProcessCb, int32_t childProcessCount, bool isStartWithDebug, const std::string &processName); virtual ~ChildProcessRecord(); static std::shared_ptr CreateChildProcessRecord(pid_t hostPid, @@ -43,6 +45,9 @@ public: static std::shared_ptr CreateNativeChildProcessRecord(pid_t hostPid, const std::string &libName, const std::shared_ptr hostRecord, const sptr &mainProcessCb, int32_t childProcessCount, bool isStartWithDebug); + static std::shared_ptr CreateNativeChildProcessRecord(pid_t hostPid, const std::string &libName, + const std::shared_ptr hostRecord, const sptr &mainProcessCb, + int32_t childProcessCount, bool isStartWithDebug, const std::string &processName); void SetPid(pid_t pid); pid_t GetPid() const; @@ -69,7 +74,7 @@ public: bool IsNativeSpawnStarted() const; private: - void MakeProcessName(const std::shared_ptr hostRecord); + void MakeProcessName(const std::shared_ptr hostRecord, std::string processName = ""); bool isStartWithDebug_; pid_t pid_ = 0; diff --git a/services/appmgr/src/app_mgr_service.cpp b/services/appmgr/src/app_mgr_service.cpp index b5ba011375e82e2c76445923b7d224a819618a51..b76c318e98a92405321fa683715663d769961456 100644 --- a/services/appmgr/src/app_mgr_service.cpp +++ b/services/appmgr/src/app_mgr_service.cpp @@ -1709,7 +1709,7 @@ void AppMgrService::SetAppAssertionPauseState(bool flag) #ifdef SUPPORT_CHILD_PROCESS int32_t AppMgrService::StartNativeChildProcess(const std::string &libName, int32_t childProcessCount, - const sptr &callback) + const sptr &callback, const std::string &processName) { TAG_LOGI(AAFwkTag::APPMGR, "call"); if (!IsReady()) { @@ -1718,7 +1718,7 @@ int32_t AppMgrService::StartNativeChildProcess(const std::string &libName, int32 } return appMgrServiceInner_->StartNativeChildProcess( - IPCSkeleton::GetCallingPid(), libName, childProcessCount, callback); + IPCSkeleton::GetCallingPid(), libName, childProcessCount, callback, processName); } #endif // SUPPORT_CHILD_PROCESS diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 25810a5ae75c714cc67532e67c21b837393aa064..af790a298a1b47d6214d83c6b0d57670b8aab8f2 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -9058,7 +9058,7 @@ void AppMgrServiceInner::OnAppCacheStateChanged(const std::shared_ptr &callback) + int32_t childProcessCount, const sptr &callback, const std::string &processName) { TAG_LOGI(AAFwkTag::APPMGR, "hostPid:%{public}d", hostPid); if (hostPid <= 0 || libName.empty() || !callback) { @@ -9104,7 +9104,7 @@ int32_t AppMgrServiceInner::StartNativeChildProcess(const pid_t hostPid, const s pid_t dummyChildPid = 0; auto nativeChildRecord = ChildProcessRecord::CreateNativeChildProcessRecord( - hostPid, libName, appRecord, callback, childProcessCount, false); + hostPid, libName, appRecord, callback, childProcessCount, false, processName); ChildProcessArgs args; ChildProcessOptions options; return StartChildProcessImpl(nativeChildRecord, appRecord, dummyChildPid, args, options); diff --git a/services/appmgr/src/child_process_record.cpp b/services/appmgr/src/child_process_record.cpp index 82c282875d389fd6b1fcdc885e7398ee5332f4b3..4e26832b96ade40919e61a7498e24b95e9b4fe2e 100644 --- a/services/appmgr/src/child_process_record.cpp +++ b/services/appmgr/src/child_process_record.cpp @@ -47,6 +47,17 @@ ChildProcessRecord::ChildProcessRecord(pid_t hostPid, const std::string &libName MakeProcessName(hostRecord); } +ChildProcessRecord::ChildProcessRecord(pid_t hostPid, const std::string &libName, + const std::shared_ptr hostRecord, const sptr &mainProcessCb, + int32_t childProcessCount, bool isStartWithDebug, const std::string &processName) + : isStartWithDebug_(isStartWithDebug), hostPid_(hostPid), childProcessCount_(childProcessCount), + childProcessType_(CHILD_PROCESS_TYPE_NATIVE), hostRecord_(hostRecord), mainProcessCb_(mainProcessCb), + srcEntry_(libName) +{ + MakeProcessName(hostRecord, processName); +} + + ChildProcessRecord::~ChildProcessRecord() { TAG_LOGD(AAFwkTag::APPMGR, "called"); @@ -81,6 +92,19 @@ std::shared_ptr ChildProcessRecord::CreateNativeChildProcess childProcessCount, isStartWithDebug); } +std::shared_ptr ChildProcessRecord::CreateNativeChildProcessRecord( + pid_t hostPid, const std::string &libName, const std::shared_ptr hostRecord, + const sptr &mainProcessCb, int32_t childProcessCount, bool isStartWithDebug, const std::string &processName) +{ + TAG_LOGD(AAFwkTag::APPMGR, "hostPid: %{public}d, libName: %{public}s", hostPid, libName.c_str()); + if (hostPid <= 0 || libName.empty() || !hostRecord || !mainProcessCb) { + TAG_LOGE(AAFwkTag::APPMGR, "invalid parameter"); + return nullptr; + } + return std::make_shared(hostPid, libName, hostRecord, mainProcessCb, + childProcessCount, isStartWithDebug, processName); +} + void ChildProcessRecord::SetPid(pid_t pid) { pid_ = pid; @@ -179,7 +203,7 @@ void ChildProcessRecord::ScheduleExitProcessSafely() scheduler_->ScheduleExitProcessSafely(); } -void ChildProcessRecord::MakeProcessName(const std::shared_ptr hostRecord) +void ChildProcessRecord::MakeProcessName(const std::shared_ptr hostRecord, std::string processName) { if (!hostRecord) { TAG_LOGW(AAFwkTag::APPMGR, "hostRecord empty"); @@ -190,16 +214,21 @@ void ChildProcessRecord::MakeProcessName(const std::shared_ptr TAG_LOGW(AAFwkTag::APPMGR, "srcEntry empty"); return; } - std::string filename = std::filesystem::path(srcEntry_).stem(); - if (!filename.empty()) { + if(processName != ""){ processName_.append(":"); - if (childProcessType_ == CHILD_PROCESS_TYPE_NATIVE || childProcessType_ == CHILD_PROCESS_TYPE_NATIVE_ARGS) { - processName_.append("Native_"); + processName_.append(processName); + }else{ + std::string filename = std::filesystem::path(srcEntry_).stem(); + if (!filename.empty()) { + processName_.append(":"); + if (childProcessType_ == CHILD_PROCESS_TYPE_NATIVE || childProcessType_ == CHILD_PROCESS_TYPE_NATIVE_ARGS) { + processName_.append("Native_"); + } + + processName_.append(filename); } - - processName_.append(filename); + processName_.append(std::to_string(childProcessCount_)); } - processName_.append(std::to_string(childProcessCount_)); TAG_LOGD(AAFwkTag::APPMGR, "MakeSpawnForkProcessName processName is %{public}s", processName_.c_str()); }