• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "hc_init_protection.h"
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include "hc_log.h"
20 #include "hc_thread.h"
21 #include "ohos_init.h"
22 
23 static HcMutex g_countMutex;
24 static int g_singleCount = 0;
25 
CheckInit(void)26 int CheckInit(void)
27 {
28     (void)LockHcMutex(&g_countMutex);
29     if (g_singleCount < 0) {
30         g_singleCount = 0;
31     } else if (g_singleCount > 0) {
32         g_singleCount++;
33         UnlockHcMutex(&g_countMutex);
34         return FINISH_INIT;
35     }
36     UnlockHcMutex(&g_countMutex);
37     return CONTINUE_INIT;
38 }
39 
CheckDestroy(void)40 int CheckDestroy(void)
41 {
42     (void)LockHcMutex(&g_countMutex);
43     if (g_singleCount == 0) {
44         UnlockHcMutex(&g_countMutex);
45         return FINISH_DESTROY;
46     }
47     if (g_singleCount < 0) {
48         g_singleCount = 0;
49         UnlockHcMutex(&g_countMutex);
50         return FINISH_DESTROY;
51     }
52     if (g_singleCount > 1) {
53         g_singleCount--;
54         UnlockHcMutex(&g_countMutex);
55         return FINISH_DESTROY;
56     }
57     UnlockHcMutex(&g_countMutex);
58     return CONTINUE_DESTROY;
59 }
60 
SetInitStatus(void)61 void SetInitStatus(void)
62 {
63     (void)LockHcMutex(&g_countMutex);
64     g_singleCount = 1;
65     UnlockHcMutex(&g_countMutex);
66     return;
67 }
68 
SetDeInitStatus(void)69 void SetDeInitStatus(void)
70 {
71     (void)LockHcMutex(&g_countMutex);
72     g_singleCount = 0;
73     UnlockHcMutex(&g_countMutex);
74     return;
75 }
76 
InitSecurityDevAuth(void)77 void InitSecurityDevAuth(void)
78 {
79     int32_t res = InitHcMutex(&g_countMutex, false);
80     if (res != 0) {
81         LOGE("When init security auth, init mutex failed");
82     }
83 }
84 SYS_SERVICE_INIT(InitSecurityDevAuth);
85 
DestroySecurityDevAuth(void)86 void DestroySecurityDevAuth(void)
87 {
88     DestroyHcMutex(&g_countMutex);
89 }