1 /*
2 * Copyright (c) 2025 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 "driver_install_ext.h"
17
18 #include <dlfcn.h>
19
20 #include "app_log_wrapper.h"
21 #include "parameters.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25
26 namespace {
27 constexpr const char *REDIRECT_PATH_FUNCTION_NAME = "RedirectDriverInstallExtPath";
28 constexpr const char *GET_DRIVER_EXCUTE_FUNCTION_NAME = "GetDriverExecuteExtPaths";
29 constexpr const char *LIB64_DRIVER_INSTALL_EXT_SO_PATH = "system/lib64/libdriver_install_ext.z.so";
30 constexpr const char *PARAM_EXT_SPACE = "persist.space_mgr_service.enterprise_space_init";
31 const std::string EXT_SPACE_ENABLE = "2";
32 } // namespace
33
DriverInstallExtHandler()34 DriverInstallExtHandler::DriverInstallExtHandler()
35 {
36 OpenDriverInstallHandler();
37 }
38
~DriverInstallExtHandler()39 DriverInstallExtHandler::~DriverInstallExtHandler()
40 {
41 ClearSymbols();
42 }
43
RedirectDriverInstallExtPath(std::string & path)44 void DriverInstallExtHandler::RedirectDriverInstallExtPath(std::string &path)
45 {
46 if (redirectDriverInstallExtPath_ != nullptr) {
47 redirectDriverInstallExtPath_(path);
48 return;
49 }
50 APP_LOGW("redirectDriverInstallExtPath_ func is nullptr");
51 }
52
GetDriverExecuteExtPaths(std::vector<std::string> & paths)53 void DriverInstallExtHandler::GetDriverExecuteExtPaths(std::vector<std::string> &paths)
54 {
55 if (getDriverExecuteExtPathsFunc_ != nullptr) {
56 getDriverExecuteExtPathsFunc_(paths);
57 return;
58 }
59 APP_LOGW("getDriverExecuteExtPathsFunc_ func is nullptr");
60 }
61
IsExtSpaceEnable()62 bool DriverInstallExtHandler::IsExtSpaceEnable()
63 {
64 std::string extSpaceEnable = OHOS::system::GetParameter(PARAM_EXT_SPACE, "");
65 if (extSpaceEnable == EXT_SPACE_ENABLE) {
66 APP_LOGI("IsExtSpaceEnable return true.");
67 return true;
68 }
69 APP_LOGW("IsExtSpaceEnable return false.");
70 return false;
71 }
72
OpenDriverInstallHandler()73 bool DriverInstallExtHandler::OpenDriverInstallHandler()
74 {
75 if (!IsExtSpaceEnable()) {
76 return false;
77 }
78 APP_LOGD("OpenDriverInstallHandler start");
79 driverInstallerHandle_ = dlopen(LIB64_DRIVER_INSTALL_EXT_SO_PATH, RTLD_NOW);
80 if (driverInstallerHandle_ == nullptr) {
81 APP_LOGE("open driverInstall lib failed %{public}s", dlerror());
82 return false;
83 }
84 redirectDriverInstallExtPath_ =
85 reinterpret_cast<RedirectPathFunc>(dlsym(driverInstallerHandle_, REDIRECT_PATH_FUNCTION_NAME));
86 if (redirectDriverInstallExtPath_ == nullptr) {
87 APP_LOGE("dlsym redirectDriverInstallExtPath_ failed %{public}s", dlerror());
88 ClearSymbols();
89 return false;
90 }
91 getDriverExecuteExtPathsFunc_ =
92 reinterpret_cast<GetDriverExtPathsFunc>(dlsym(driverInstallerHandle_, GET_DRIVER_EXCUTE_FUNCTION_NAME));
93 if (getDriverExecuteExtPathsFunc_ == nullptr) {
94 APP_LOGE("dlsym getDriverExecuteExtPathsFunc_ failed %{public}s", dlerror());
95 ClearSymbols();
96 return false;
97 }
98 return true;
99 }
100
ClearSymbols()101 void DriverInstallExtHandler::ClearSymbols()
102 {
103 redirectDriverInstallExtPath_ = nullptr;
104 getDriverExecuteExtPathsFunc_ = nullptr;
105 if (driverInstallerHandle_) {
106 dlclose(driverInstallerHandle_);
107 }
108 driverInstallerHandle_ = nullptr;
109 }
110
111 } // AppExecFwk
112 } // OHOS