1 /*
2  * Copyright (c) 2024 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 "ipc_cskeleton.h"
17 #include "ipc_error_code.h"
18 #include "log_tags.h"
19 #include "ipc_debug.h"
20 #include "ipc_skeleton.h"
21 #include "ipc_thread_skeleton.h"
22 
23 #include <securec.h>
24 
25 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, OHOS::LOG_ID_IPC_CAPI, "IPCSkeleton" };
26 
27 static constexpr int MIN_THREAD_NUM = 1;
28 static constexpr int MAX_THREAD_NUM = 32;
29 
OH_IPCSkeleton_JoinWorkThread(void)30 void OH_IPCSkeleton_JoinWorkThread(void)
31 {
32     OHOS::IPCSkeleton::JoinWorkThread();
33 }
34 
OH_IPCSkeleton_StopWorkThread(void)35 void OH_IPCSkeleton_StopWorkThread(void)
36 {
37     OHOS::IPCSkeleton::StopWorkThread();
38 }
39 
OH_IPCSkeleton_GetCallingTokenId(void)40 uint64_t OH_IPCSkeleton_GetCallingTokenId(void)
41 {
42     return OHOS::IPCSkeleton::GetCallingFullTokenID();
43 }
44 
OH_IPCSkeleton_GetFirstTokenId(void)45 uint64_t OH_IPCSkeleton_GetFirstTokenId(void)
46 {
47     return OHOS::IPCSkeleton::GetFirstFullTokenID();
48 }
49 
OH_IPCSkeleton_GetSelfTokenId(void)50 uint64_t OH_IPCSkeleton_GetSelfTokenId(void)
51 {
52     return OHOS::IPCSkeleton::GetSelfTokenID();
53 }
54 
OH_IPCSkeleton_GetCallingPid(void)55 uint64_t OH_IPCSkeleton_GetCallingPid(void)
56 {
57     return static_cast<uint64_t>(OHOS::IPCSkeleton::GetCallingPid());
58 }
59 
OH_IPCSkeleton_GetCallingUid(void)60 uint64_t OH_IPCSkeleton_GetCallingUid(void)
61 {
62     return static_cast<uint64_t>(OHOS::IPCSkeleton::GetCallingUid());
63 }
64 
OH_IPCSkeleton_IsLocalCalling(void)65 int OH_IPCSkeleton_IsLocalCalling(void)
66 {
67     return OHOS::IPCSkeleton::IsLocalCalling() ? 1 : 0;
68 }
69 
OH_IPCSkeleton_SetMaxWorkThreadNum(const int maxThreadNum)70 int OH_IPCSkeleton_SetMaxWorkThreadNum(const int maxThreadNum)
71 {
72     if (maxThreadNum < MIN_THREAD_NUM || maxThreadNum > MAX_THREAD_NUM) {
73         ZLOGE(LOG_LABEL, "Check param error!");
74         return OH_IPC_CHECK_PARAM_ERROR;
75     }
76     return OHOS::IPCSkeleton::SetMaxWorkThreadNum(maxThreadNum) ? OH_IPC_SUCCESS : OH_IPC_INNER_ERROR;
77 }
78 
OH_IPCSkeleton_SetCallingIdentity(const char * identity)79 int OH_IPCSkeleton_SetCallingIdentity(const char *identity)
80 {
81     if (identity == nullptr) {
82         ZLOGE(LOG_LABEL, "Check param error!");
83         return OH_IPC_CHECK_PARAM_ERROR;
84     }
85     std::string str = identity;
86     return OHOS::IPCSkeleton::SetCallingIdentity(str) ? OH_IPC_SUCCESS : OH_IPC_INNER_ERROR;
87 }
88 
OH_IPCSkeleton_ResetCallingIdentity(char ** identity,int32_t * len,OH_IPC_MemAllocator allocator)89 int OH_IPCSkeleton_ResetCallingIdentity(char **identity, int32_t *len, OH_IPC_MemAllocator allocator)
90 {
91     if (identity == nullptr || len == nullptr || allocator == nullptr) {
92         ZLOGE(LOG_LABEL, "Check param error!");
93         return OH_IPC_CHECK_PARAM_ERROR;
94     }
95     std::string str(OHOS::IPCSkeleton::ResetCallingIdentity());
96     int length = static_cast<int>(str.length()) + 1;
97     *identity = static_cast<char*>(allocator(length));
98     if (*identity == nullptr) {
99         ZLOGE(LOG_LABEL, "Memory allocator failed!");
100         return OH_IPC_MEM_ALLOCATOR_ERROR;
101     }
102     if (memcpy_s(*identity, length, str.c_str(), length) != EOK) {
103         ZLOGE(LOG_LABEL, "Memcpy string failed!");
104         return OH_IPC_INNER_ERROR;
105     }
106     *len = length;
107     return OH_IPC_SUCCESS;
108 }
109 
OH_IPCSkeleton_IsHandlingTransaction(void)110 int OH_IPCSkeleton_IsHandlingTransaction(void)
111 {
112     return (OHOS::IPCThreadSkeleton::GetActiveInvoker() != nullptr) ? 1 : 0;
113 }
114