• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "dslm_service.h"
17 
18 #include <dlfcn.h>
19 #include <thread>
20 
21 #include "iremote_object.h"
22 #include "singleton.h"
23 #include "utils_log.h"
24 
25 #include "device_security_defines.h"
26 #include "dslm_hidumper.h"
27 #include "dslm_ipc_process.h"
28 #include "dslm_rpc_process.h"
29 
30 namespace OHOS {
31 namespace Security {
32 namespace DeviceSecurityLevel {
33 REGISTER_SYSTEM_ABILITY_BY_ID(DslmService, DEVICE_SECURITY_LEVEL_MANAGER_SA_ID, true);
34 
DslmService(int32_t saId,bool runOnCreate)35 DslmService::DslmService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate), IRemoteStub(true)
36 {
37     SECURITY_LOG_INFO("object initialization");
38     ProcessLoadPlugin();
39 }
40 
OnStart()41 void DslmService::OnStart()
42 {
43     SECURITY_LOG_INFO("start");
44     std::thread thread([this]() {
45         if (InitService() == SUCCESS) {
46             SECURITY_LOG_INFO("init service success");
47         }
48         if (!Publish(this)) {
49             SECURITY_LOG_ERROR("publish service failed");
50         }
51     });
52     thread.detach();
53 }
54 
OnStop()55 void DslmService::OnStop()
56 {
57     UnInitService();
58     ProcessUnloadPlugin();
59     SECURITY_LOG_INFO("stop service");
60 }
61 
Dump(int fd,const std::vector<std::u16string> & args)62 int32_t DslmService::Dump(int fd, const std::vector<std::u16string> &args)
63 {
64     DslmDumper(fd);
65     return 0;
66 }
67 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)68 int32_t DslmService::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
69 {
70     do {
71         if (IDeviceSecurityLevel::GetDescriptor() != data.ReadInterfaceToken()) {
72             SECURITY_LOG_ERROR("local descriptor is not equal remote");
73             break;
74         }
75         switch (code) {
76             case CMD_GET_DEVICE_SECURITY_LEVEL:
77                 return ProcessGetDeviceSecurityLevel(data, reply);
78             default:
79                 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
80         }
81     } while (false);
82 
83     return ERR_REQUEST_CODE_ERR;
84 }
85 
ProcessGetDeviceSecurityLevel(MessageParcel & data,MessageParcel & reply)86 int32_t DslmService::ProcessGetDeviceSecurityLevel(MessageParcel &data, MessageParcel &reply)
87 {
88     return Singleton<DslmIpcProcess>::GetInstance().DslmProcessGetDeviceSecurityLevel(data, reply);
89 }
90 
ProcessLoadPlugin(void)91 void DslmService::ProcessLoadPlugin(void)
92 {
93 #ifdef PLUGIN_SO_PATH
94     handle_ = dlopen(PLUGIN_SO_PATH, RTLD_NOW);
95     if (!handle_) {
96         SECURITY_LOG_ERROR("load %{public}s failed for %{public}s", PLUGIN_SO_PATH, dlerror());
97     }
98 #endif
99 }
100 
ProcessUnloadPlugin(void)101 void DslmService::ProcessUnloadPlugin(void)
102 {
103 #ifdef PLUGIN_SO_PATH
104     if (handle_) {
105         dlclose(handle_);
106         SECURITY_LOG_INFO("unload %{public}s", PLUGIN_SO_PATH);
107     }
108 #endif
109 }
110 } // namespace DeviceSecurityLevel
111 } // namespace Security
112 } // namespace OHOS
113