1 /*
2 * Copyright (C) 2023 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 "devattest_task.h"
17
18 #include <pthread.h>
19 #include "iservice_registry.h"
20 #include "singleton.h"
21 #include "devattest_log.h"
22 #include "devattest_errno.h"
23 #include "attest_entry.h"
24
25 namespace OHOS {
26 namespace DevAttest {
27 using namespace OHOS;
28
29 constexpr std::int32_t SA_ID_DEVICE_ATTEST_SERVICE = 5501;
30 const char* ATTEST_RUN_TASK_ID = "attest_run";
DevAttestTask()31 DevAttestTask::DevAttestTask()
32 {
33 }
34
~DevAttestTask()35 DevAttestTask::~DevAttestTask()
36 {
37 }
38
CreateThread()39 bool DevAttestTask::CreateThread()
40 {
41 pthread_t tid;
42 pthread_attr_t attr;
43 pthread_attr_init(&attr);
44 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
45 int priority = 0;
46 struct sched_param sched = {static_cast<int>(priority)};
47 pthread_attr_setschedparam(&attr, &sched);
48 int ret = pthread_create(&tid, &attr, DevAttestTask::Run, NULL);
49 if (ret != DEVATTEST_SUCCESS) {
50 HILOGE("thread create failed, ret: %{public}d", ret);
51 return false;
52 }
53 return true;
54 }
55
Run(void * arg)56 void* DevAttestTask::Run(void* arg)
57 {
58 (void)pthread_setname_np(pthread_self(), ATTEST_RUN_TASK_ID); // set pthread name, at most 15 bytes.
59 (void)AttestTask();
60 UnloadTask();
61 HILOGI("Thread exited...");
62 return nullptr;
63 }
64
UnloadTask(void)65 void DevAttestTask::UnloadTask(void)
66 {
67 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
68 if (samgrProxy == nullptr) {
69 HILOGE("get samgr failed");
70 return;
71 }
72 int32_t ret = samgrProxy->UnloadSystemAbility(SA_ID_DEVICE_ATTEST_SERVICE);
73 if (ret != DEVATTEST_SUCCESS) {
74 HILOGE("remove system ability failed");
75 return;
76 }
77 }
78 } // end of DevAttest
79 } // end of OHOS