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