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