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 "ipc_skeleton.h"
17
18 #include "ipc_process_skeleton.h"
19 #include "rpc_errno.h"
20 #include "rpc_log.h"
21 #include "rpc_types.h"
22 #include "securec.h"
23 #include "utils_list.h"
24
25 // default is 4 max is 16
SetMaxWorkThreadNum(int32_t maxThreadNum)26 int32_t SetMaxWorkThreadNum(int32_t maxThreadNum)
27 {
28 if (GetCurrentSkeleton() == NULL) {
29 RPC_LOG_ERROR("init ipc process skeleton failed.");
30 return ERR_IPC_SKELETON_NOT_INIT;
31 }
32 if ((maxThreadNum < SET_MAX_THREADS_DEFAULT) || (maxThreadNum > SET_MAX_THREADS_MAX)) {
33 RPC_LOG_ERROR("max thread num is invalid.");
34 return ERR_INVALID_PARAM;
35 }
36 return SetMaxWorkThread(maxThreadNum);
37 }
38
39 // join current thread into work loop.
JoinWorkThread(void)40 void JoinWorkThread(void)
41 {
42 if (GetCurrentSkeleton() == NULL) {
43 RPC_LOG_ERROR("init ipc process skeleton failed.");
44 return;
45 }
46 return JoinMainWorkThread();
47 }
48
GetCallingPid(void)49 pid_t GetCallingPid(void)
50 {
51 return ProcessGetCallingPid();
52 }
53
GetCallingUid(void)54 pid_t GetCallingUid(void)
55 {
56 return ProcessGetCallingUid();
57 }
58
GetContextObject(void)59 const SvcIdentity *GetContextObject(void)
60 {
61 if (GetCurrentSkeleton() == NULL) {
62 RPC_LOG_ERROR("init ipc process skeleton failed.");
63 return NULL;
64 }
65 return GetRegistryObject();
66 }
67
SetContextObject(SvcIdentity target)68 int32_t SetContextObject(SvcIdentity target)
69 {
70 if (GetCurrentSkeleton() == NULL) {
71 RPC_LOG_ERROR("init ipc process skeleton failed.");
72 return ERR_IPC_SKELETON_NOT_INIT;
73 }
74 if (target.cookie == 0) {
75 RPC_LOG_ERROR("samgr stub func is NULL.");
76 return ERR_INVALID_PARAM;
77 }
78 return SetRegistryObject(target);
79 }
80
SendRequest(SvcIdentity target,uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option,uintptr_t * buffer)81 int32_t SendRequest(SvcIdentity target, uint32_t code, IpcIo *data, IpcIo *reply,
82 MessageOption option, uintptr_t *buffer)
83 {
84 if (GetCurrentSkeleton() == NULL) {
85 RPC_LOG_ERROR("init ipc process skeleton failed.");
86 return ERR_IPC_SKELETON_NOT_INIT;
87 }
88 return ProcessSendRequest(target, code, data, reply, option, buffer);
89 }
90
AddDeathRecipient(SvcIdentity target,OnRemoteDead deathFunc,void * args,uint32_t * cbId)91 int32_t AddDeathRecipient(SvcIdentity target, OnRemoteDead deathFunc, void *args, uint32_t *cbId)
92 {
93 if (GetCurrentSkeleton() == NULL) {
94 RPC_LOG_ERROR("init ipc process skeleton failed.");
95 return ERR_IPC_SKELETON_NOT_INIT;
96 }
97 if (target.handle < 0) {
98 RPC_LOG_ERROR("add death recipient is invalid handle.");
99 return ERR_INVALID_PARAM;
100 }
101 return ProcessAddDeathRecipient(target.handle, deathFunc, args, cbId);
102 }
103
RemoveDeathRecipient(SvcIdentity target,uint32_t cbId)104 int32_t RemoveDeathRecipient(SvcIdentity target, uint32_t cbId)
105 {
106 if (GetCurrentSkeleton() == NULL) {
107 RPC_LOG_ERROR("init ipc process skeleton failed.");
108 return ERR_IPC_SKELETON_NOT_INIT;
109 }
110 if (target.handle < 0) {
111 RPC_LOG_ERROR("add death recipient is invalid handle.");
112 return ERR_INVALID_PARAM;
113 }
114 return ProcessRemoveDeathRecipient(target.handle, cbId);
115 }
116
FreeBuffer(void * ptr)117 int32_t FreeBuffer(void *ptr)
118 {
119 if (ptr == NULL) {
120 RPC_LOG_ERROR("ptr is null, no data to free");
121 return ERR_INVALID_PARAM;
122 }
123 if (GetCurrentSkeleton() == NULL) {
124 RPC_LOG_ERROR("init ipc process skeleton failed.");
125 return ERR_IPC_SKELETON_NOT_INIT;
126 }
127 return ProcessFreeBuffer(ptr);
128 }
129
MessageOptionInit(MessageOption * option)130 int32_t MessageOptionInit(MessageOption *option)
131 {
132 if (option == NULL) {
133 RPC_LOG_ERROR("option is null");
134 return ERR_INVALID_PARAM;
135 }
136 option->flags = TF_OP_SYNC;
137 option->waitTime = RPC_DEFAULT_SEND_WAIT_TIME;
138 option->args = NULL;
139 return ERR_NONE;
140 }
141
ReleaseSvc(SvcIdentity target)142 int32_t ReleaseSvc(SvcIdentity target)
143 {
144 if (GetCurrentSkeleton() == NULL) {
145 RPC_LOG_ERROR("init ipc process skeleton failed.");
146 return ERR_IPC_SKELETON_NOT_INIT;
147 }
148 if (target.handle <= 0) {
149 RPC_LOG_ERROR("release svc is invalid handle.");
150 return ERR_INVALID_PARAM;
151 }
152 return DeleteHandle(target.handle);
153 }