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