1 /*
2 * Copyright (c) 2021-2022 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 "appspawn_adapter.h"
17
18 #include <string>
19 #include "appspawn_service.h"
20 #include "json_utils.h"
21 #include "sandbox_utils.h"
22
23 using namespace std;
24 using namespace OHOS;
25 using namespace OHOS::AppSpawn;
26
27 namespace {
28 const std::string MODULE_TEST_BUNDLE_NAME("moduleTestProcessName");
29 const std::string NAMESPACE_JSON_CONFIG("/system/etc/sandbox/sandbox-config.json");
30 #if defined (__aarch64__) || defined (__x86_64__)
31 const std::string APP_JSON_CONFIG("/system/etc/sandbox/appdata-sandbox64.json");
32 #else
33 const std::string APP_JSON_CONFIG("/system/etc/sandbox/appdata-sandbox.json");
34 #endif
35 const std::string PRODUCT_JSON_CONFIG("/system/etc/sandbox/product-sandbox.json");
36 }
37
LoadAppSandboxConfig(void)38 void LoadAppSandboxConfig(void)
39 {
40 // load sandbox config
41 nlohmann::json appSandboxConfig;
42 bool rc = JsonUtils::GetJsonObjFromJson(appSandboxConfig, APP_JSON_CONFIG);
43 APPSPAWN_CHECK_ONLY_LOG(rc, "AppSpawnServer::Failed to load app private sandbox config");
44 SandboxUtils::StoreJsonConfig(appSandboxConfig);
45
46 rc = JsonUtils::GetJsonObjFromJson(appSandboxConfig, PRODUCT_JSON_CONFIG);
47 APPSPAWN_CHECK_ONLY_LOG(rc, "AppSpawnServer::Failed to load app product sandbox config");
48 SandboxUtils::StoreProductJsonConfig(appSandboxConfig);
49
50 nlohmann::json appNamespaceConfig;
51 rc = JsonUtils::GetJsonObjFromJson(appNamespaceConfig, NAMESPACE_JSON_CONFIG);
52 APPSPAWN_CHECK_ONLY_LOG(rc, "AppSpawnServer::Failed to load app sandbox namespace config");
53 SandboxUtils::StoreNamespaceJsonConfig(appNamespaceConfig);
54 }
55
SetAppSandboxProperty(struct AppSpawnContent_ * content,AppSpawnClient * client)56 int32_t SetAppSandboxProperty(struct AppSpawnContent_ *content, AppSpawnClient *client)
57 {
58 APPSPAWN_CHECK(client != NULL, return -1, "Invalid appspwn client");
59 AppSpawnClientExt *appProperty = reinterpret_cast<AppSpawnClientExt *>(client);
60 appProperty->property.cloneFlags = client->cloneFlags;
61 int ret = SandboxUtils::SetAppSandboxProperty(&appProperty->property);
62 // for module test do not create sandbox
63 if (strncmp(appProperty->property.bundleName,
64 MODULE_TEST_BUNDLE_NAME.c_str(), MODULE_TEST_BUNDLE_NAME.size()) == 0) {
65 return 0;
66 }
67 return ret;
68 }
69
GetAppNamespaceFlags(const char * bundleName)70 uint32_t GetAppNamespaceFlags(const char *bundleName)
71 {
72 return SandboxUtils::GetNamespaceFlagsFromConfig(bundleName);
73 }
74
75