• 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 "co_auth_service.h"
17 
18 #include <cinttypes>
19 #include <mutex>
20 #include <thread>
21 
22 #include "string_ex.h"
23 
24 #include "executor_messenger_service.h"
25 #include "hdi_wrapper.h"
26 #include "hisysevent_adapter.h"
27 #include "iam_logger.h"
28 #include "iam_ptr.h"
29 #include "iam_para2str.h"
30 #include "ipc_common.h"
31 #include "iam_time.h"
32 #include "iam_common_defines.h"
33 #include "ipc_skeleton.h"
34 #include "parameter.h"
35 #include "relative_timer.h"
36 #include "resource_node_pool.h"
37 #include "template_cache_manager.h"
38 
39 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
40 
41 namespace OHOS {
42 namespace UserIam {
43 namespace UserAuth {
44 REGISTER_SYSTEM_ABILITY_BY_ID(CoAuthService, SUBSYS_USERIAM_SYS_ABILITY_AUTHEXECUTORMGR, true);
45 constexpr int32_t USERIAM_IPC_THREAD_NUM = 4;
46 
CoAuthService(int32_t systemAbilityId,bool runOnCreate)47 CoAuthService::CoAuthService(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(systemAbilityId, runOnCreate)
48 {
49     IAM_LOGI("CoAuthService init");
50 }
51 
OnStart()52 void CoAuthService::OnStart()
53 {
54     static std::mutex mutex;
55     static uint32_t timerId = 0;
56     std::lock_guard<std::mutex> guard(mutex);
57     IAM_LOGI("Start service");
58     IPCSkeleton::SetMaxWorkThreadNum(USERIAM_IPC_THREAD_NUM);
59     if (!Publish(this)) {
60         IAM_LOGE("Failed to publish service");
61         return;
62     }
63 
64     if (timerId != 0) {
65         RelativeTimer::GetInstance().Unregister(timerId);
66     }
67     timerId = RelativeTimer::GetInstance().Register(Init, 0);
68 }
69 
OnStop()70 void CoAuthService::OnStop()
71 {
72     IAM_LOGI("Stop service");
73 }
74 
ExecutorRegister(const ExecutorRegisterInfo & info,sptr<ExecutorCallbackInterface> & callback)75 uint64_t CoAuthService::ExecutorRegister(const ExecutorRegisterInfo &info, sptr<ExecutorCallbackInterface> &callback)
76 {
77     if (callback == nullptr) {
78         IAM_LOGE("executor callback is nullptr");
79         return INVALID_EXECUTOR_INDEX;
80     }
81     if (!IpcCommon::CheckPermission(*this, ACCESS_AUTH_RESPOOL)) {
82         IAM_LOGE("failed to check permission");
83         return INVALID_EXECUTOR_INDEX;
84     }
85     std::vector<uint64_t> templateIdList;
86     std::vector<uint8_t> fwkPublicKey;
87     auto executorCallback = Common::SptrToStdSharedPtr<ExecutorCallbackInterface>(callback);
88     auto resourceNode = ResourceNode::MakeNewResource(info, executorCallback, templateIdList, fwkPublicKey);
89     if (resourceNode == nullptr) {
90         IAM_LOGE("create resource node failed");
91         return INVALID_EXECUTOR_INDEX;
92     }
93     if (!ResourceNodePool::Instance().Insert(resourceNode)) {
94         IAM_LOGE("insert resource node failed");
95         return INVALID_EXECUTOR_INDEX;
96     }
97 
98     sptr<ExecutorMessengerInterface> messenger = ExecutorMessengerService::GetInstance();
99     executorCallback->OnMessengerReady(messenger, fwkPublicKey, templateIdList);
100     uint64_t executorIndex = resourceNode->GetExecutorIndex();
101     int32_t executorType = resourceNode->GetAuthType();
102     IAM_LOGI("register successful, executorType is %{public}d, executorIndex is ****%{public}hx",
103         executorType, static_cast<uint16_t>(executorIndex));
104     if (auto obj = executorCallback->AsObject(); obj) {
105         obj->AddDeathRecipient(new (std::nothrow) IpcCommon::PeerDeathRecipient([executorIndex, executorType]() {
106             auto result = ResourceNodePool::Instance().Delete(executorIndex);
107             IAM_LOGI("delete executor %{public}s, executorIndex is ****%{public}hx", (result ? "succ" : "failed"),
108                 static_cast<uint16_t>(executorIndex));
109             std::string executorDesc = "executor, type " + std::to_string(executorType);
110             UserIam::UserAuth::ReportSystemFault(Common::GetNowTimeString(), executorDesc);
111         }));
112     }
113     IAM_LOGI("update template cache after register success");
114     TemplateCacheManager::GetInstance().UpdateTemplateCache(resourceNode->GetAuthType());
115     return executorIndex;
116 }
117 
Init()118 void CoAuthService::Init()
119 {
120     auto hdi = HdiWrapper::GetHdiRemoteObjInstance();
121     if (hdi) {
122         hdi->AddDeathRecipient(new (std::nothrow) IpcCommon::PeerDeathRecipient([]() {
123             ResourceNodePool::Instance().DeleteAll();
124             RelativeTimer::GetInstance().Register(Init, DEFER_TIME);
125             IAM_LOGI("delete all executors for hdi dead");
126             UserIam::UserAuth::ReportSystemFault(Common::GetNowTimeString(), "user_auth_hdi host");
127         }));
128         IAM_LOGI("set fwk ready parameter");
129         SetParameter("bootevent.useriam.fwkready", "false");
130         SetParameter("bootevent.useriam.fwkready", "true");
131     } else {
132         RelativeTimer::GetInstance().Register(Init, DEFER_TIME);
133     }
134 }
135 
Dump(int fd,const std::vector<std::u16string> & args)136 int CoAuthService::Dump(int fd, const std::vector<std::u16string> &args)
137 {
138     IAM_LOGI("start");
139     if (fd < 0) {
140         IAM_LOGE("invalid parameters");
141         dprintf(fd, "Invalid parameters.\n");
142         return INVALID_PARAMETERS;
143     }
144     std::string arg0 = (args.empty() ? "" : Str16ToStr8(args[0]));
145     if (arg0.empty() || arg0.compare("-h") == 0) {
146         dprintf(fd, "Usage:\n");
147         dprintf(fd, "      -h: command help.\n");
148         dprintf(fd, "      -l: resource pool dump.\n");
149         return SUCCESS;
150     }
151     if (arg0.compare("-l") == 0) {
152         ResourceNodePool::Instance().Enumerate([fd](const std::weak_ptr<ResourceNode> &node) {
153             auto nodeTmp = node.lock();
154             if (nodeTmp != nullptr) {
155                 dprintf(fd, "ExecutorIndex is: %" PRIx64 ".\n", nodeTmp->GetExecutorIndex());
156                 dprintf(fd, "ExecutorType is: %s.\n", Common::AuthTypeToStr(nodeTmp->GetAuthType()));
157             }
158         });
159         return SUCCESS;
160     }
161     IAM_LOGE("invalid option");
162     dprintf(fd, "Invalid option\n");
163     return GENERAL_ERROR;
164 }
165 } // namespace UserAuth
166 } // namespace UserIam
167 } // namespace OHOS