• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "native_child_process.h"
17 #include <map>
18 #include <mutex>
19 #include "hilog_tag_wrapper.h"
20 #include "native_child_callback.h"
21 #include "child_process_args_manager.h"
22 #include "child_process_manager.h"
23 #include "child_callback_manager.h"
24 #include "child_process_manager_error_utils.h"
25 
26 using namespace OHOS;
27 using namespace OHOS::AbilityRuntime;
28 
29 namespace {
30 
31 std::mutex g_mutexCallBackObj;
32 constexpr size_t MAX_KEY_SIZE = 20;
33 constexpr size_t MAX_FD_SIZE = 16;
34 
35 } // Anonymous namespace
36 
OH_Ability_CreateNativeChildProcess(const char * libName,OH_Ability_OnNativeChildProcessStarted onProcessStarted)37 int OH_Ability_CreateNativeChildProcess(const char* libName, OH_Ability_OnNativeChildProcessStarted onProcessStarted)
38 {
39     if (libName == nullptr || *libName == '\0' || onProcessStarted == nullptr) {
40         TAG_LOGE(AAFwkTag::PROCESSMGR, "null libname or callback");
41         return NCP_ERR_INVALID_PARAM;
42     }
43 
44     std::string strLibName(libName);
45     if (strLibName.find("../") != std::string::npos) {
46         TAG_LOGE(AAFwkTag::PROCESSMGR, "relative path not allow");
47         return NCP_ERR_INVALID_PARAM;
48     }
49 
50     sptr<IRemoteObject> callbackStub(new (std::nothrow) NativeChildCallback(onProcessStarted));
51     if (!callbackStub) {
52         TAG_LOGE(AAFwkTag::PROCESSMGR, "null callbackStub");
53         return NCP_ERR_INTERNAL;
54     }
55 
56     ChildProcessManager &mgr = ChildProcessManager::GetInstance();
57     auto cpmErr = mgr.StartNativeChildProcessByAppSpawnFork(strLibName, callbackStub);
58     if (cpmErr != ChildProcessManagerErrorCode::ERR_OK) {
59         return ChildProcessManagerErrorUtil::CvtChildProcessManagerErrCode(cpmErr);
60     }
61 
62     ChildCallbackManager::GetInstance().AddRemoteObject(callbackStub);
63     return NCP_NO_ERROR;
64 }
65 
OH_Ability_StartNativeChildProcess(const char * entry,NativeChildProcess_Args args,NativeChildProcess_Options options,int32_t * pid)66 Ability_NativeChildProcess_ErrCode OH_Ability_StartNativeChildProcess(const char* entry,
67     NativeChildProcess_Args args, NativeChildProcess_Options options, int32_t *pid)
68 {
69     if (entry == nullptr || *entry == '\0') {
70         TAG_LOGE(AAFwkTag::PROCESSMGR, "Invalid entry");
71         return NCP_ERR_INVALID_PARAM;
72     }
73     std::string entryName(entry);
74     if (entryName.find(":") == std::string::npos) {
75         TAG_LOGE(AAFwkTag::PROCESSMGR, "entry point misses a colon");
76         return NCP_ERR_INVALID_PARAM;
77     }
78     if (pid == nullptr) {
79         TAG_LOGE(AAFwkTag::PROCESSMGR, "pid null");
80         return NCP_ERR_INVALID_PARAM;
81     }
82 
83     std::map<std::string, int32_t> fds;
84     NativeChildProcess_Fd* cur = args.fdList.head;
85     while (cur != nullptr) {
86         if (!cur->fdName) {
87             TAG_LOGE(AAFwkTag::PROCESSMGR, "fdName null");
88             return NCP_ERR_INVALID_PARAM;
89         }
90         std::string key(cur->fdName);
91         if (key.size() > MAX_KEY_SIZE) {
92             TAG_LOGE(AAFwkTag::PROCESSMGR, "fd name too long");
93             return NCP_ERR_INVALID_PARAM;
94         }
95         fds.emplace(key, cur->fd);
96         cur = cur->next;
97     }
98     if (fds.size() > MAX_FD_SIZE) {
99         TAG_LOGE(AAFwkTag::PROCESSMGR, "too many fds");
100         return NCP_ERR_INVALID_PARAM;
101     }
102     AppExecFwk::ChildProcessArgs childArgs;
103     childArgs.fds = fds;
104     if (args.entryParams != nullptr && *(args.entryParams) != '\0') {
105         std::string entryParams(args.entryParams);
106         childArgs.entryParams = entryParams;
107     }
108     AppExecFwk::ChildProcessOptions childProcessOptions;
109     childProcessOptions.isolationMode = options.isolationMode == NCP_ISOLATION_MODE_ISOLATED;
110     int32_t childProcessType = AppExecFwk::CHILD_PROCESS_TYPE_NATIVE_ARGS;
111 
112     ChildProcessManager &mgr = ChildProcessManager::GetInstance();
113     auto cpmErr = mgr.StartChildProcessWithArgs(entryName, *pid, childProcessType, childArgs, childProcessOptions);
114     if (cpmErr != ChildProcessManagerErrorCode::ERR_OK) {
115         return ChildProcessManagerErrorUtil::CvtChildProcessManagerErrCode(cpmErr);
116     }
117     return NCP_NO_ERROR;
118 }
119 
OH_Ability_GetCurrentChildProcessArgs()120 NativeChildProcess_Args* OH_Ability_GetCurrentChildProcessArgs()
121 {
122     NativeChildProcess_Args* result = ChildProcessArgsManager::GetInstance().GetChildProcessArgs();
123     if (result == nullptr) {
124         TAG_LOGE(AAFwkTag::PROCESSMGR, "GetChildProcessArgs null");
125     }
126     return result;
127 }