2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef OTHERSERVICE_RPCTHREAD_H_ // NOLINT(build/header_guard)
18 #define OTHERSERVICE_RPCTHREAD_H_ // NOLINT(build/header_guard)
22 /** @addtogroup RPClib_in
26 #define RPC_NO_SOCKET -1
27 #define RPC_NO_THREAD 0
29 #define RPC_MAIN_SUB_COMMAND "%-2d %-16lx"
30 #define RPC_MAIN_SUB_COMMAND_SIZE 20
31 #define RPC_MAIN_SUB_COMMANDs "%d %lx"
32 /** Type of command sent from the main thread to the subthread */
34 RPC_COMMAND_ADD_SERVER, /* Addition of RPC_ID */
35 RPC_COMMAND_REMOVE_SERVER, /* Remove RPC_ID */
36 RPC_COMMAND_EXIT, /* Sub-thread termination (when all RPC_ID are exhausted) */
39 #define APICALL_TIMEOUT_DEFAULT -1
41 /** Periodic checking timeout (mSec) for detecting deadlocks */
42 #define TIMEOUT_FOR_DEADLOCK_CHECK 1000
44 /** Length of the socket name
45 API request datagram socket = '\0' + "5-digit number associated with RPC_ID"
46 Authentication stream socket = '\0' + "secure_" + "5-digit number associated with RPC_ID" */
47 #define SOCK_NAME_LEN (1+5) /* API request datagram socket */
48 #define SECURE_SOCK_NAME_LEN (1+12) /* Authentication stream socket */
50 #define rpc_mutex_lock pthread_mutex_lock
51 #define rpc_mutex_unlock pthread_mutex_unlock
53 /** Upper limit of UID and GID list registration */
54 #define CREDENTIAL_LIST_NUM_MAX 32
56 /** Authentication result to be sent to the client */
58 UINT16 certify_res; /**< Client Authentication Result */
59 pid_t srvr_pid; /**< Server PID */
65 /** Received API call request */
67 RPC_ID client; /**< API ID of the requested program */
68 UINT16 api_num; /**< Calling API number */
69 char *args_string; /**< API Call Arguments */
70 UINT32 args_size; /**< Number of bytes in the argument */
73 /** Source Client Socket Name Management Table */
74 typedef struct RpcClientSockNameInfoST RpcClientSockNameInfo;
75 struct RpcClientSockNameInfoST {
76 char client_sock_name[SOCK_NAME_LEN]; /**< Socket Name List for Source Client */
77 pid_t pid; /**< Source pid */
78 uid_t uid; /**< Source uid */
79 gid_t gid; /**< Source gid */
80 int wd; /**< Non-negative inotify monitored descriptors */
81 RpcClientSockNameInfo *next; /**< Pointer to the next node */
84 /** Communication server PID management table */
85 typedef struct RpcSrvrPidInfoST RpcSrvrPidInfo;
86 struct RpcSrvrPidInfoST {
87 RPC_ID srvr_rpc_id; /**< Communication destination server RPC_ID */
88 pid_t srvr_pid; /**< Communication destination server pid */
89 int wd; /**< Non-negative inotify monitored descriptors */
90 RpcSrvrPidInfo *next; /**< Pointer to the next node */
93 /** Buffer structure for API call acceptance processing.
94 * This function is allocated only when the dispatch function is specified by RPC_start().
97 RPC_dispatch_func_t dispatch_func;/**< Dispatch Functions to APIs (in stub) */
98 #define RPC_apicall_dispatch_func(id) \
99 ((id)->apicall ? (id)->apicall->dispatch_func : 0)
101 INT32 pipe_sub_main[2];/**< Notification pipe from the subthread to the main thread */
102 #define RPC_pipe_sub_main(th) ((th)->srvr_id->apicall->pipe_sub_main)
104 UINT16 num_queue;/**< Number of queued API call requests */
105 #define RPC_apicall_num_queue(id) \
106 ((id)->apicall ? (id)->apicall->num_queue : 0)
107 #define RPC_apicall_num_queue_inc(id) ((id)->apicall->num_queue++)
108 #define RPC_apicall_num_queue_dec(id) ((id)->apicall->num_queue--)
110 rpc_apicall_queue queue[RPC_MAX_APICALL_QUEUE];/**< Queuing API Call Requests (FIFO) */
111 #define RPC_apicall_queue(id, i) ((id)->apicall->queue[(i)])
112 #define RPC_apicall_queue_client(id, i) ((id)->apicall->queue[(i)].client)
113 #define RPC_apicall_queue_api_num(id, i) ((id)->apicall->queue[(i)].api_num)
114 #define RPC_apicall_queue_args(id, i) ((id)->apicall->queue[(i)].args_string)
115 #define RPC_apicall_queue_args_size(id, i) ((id)->apicall->queue[(i)].args_size)
117 RPC_ID in_process_client;/**< Clients running on ID */
118 #define RPC_apicall_in_process_client(id) ((id)->apicall->in_process_client)
120 INT32 timeout_sec;/**< API processing timeout */
121 #define RPC_apicall_api_timeout_sec(id) ((id)->apicall->timeout_sec)
123 INT32 secure_check;/**< Client Authentication Check Enabled/Disabled */
124 #define RPC_secure_check(id) ((id)->apicall->secure_check)
126 INT32 regist_credential_info;/**< Registration of authentication information */
127 #define RPC_regist_credential_info(id) ((id)->apicall->regist_credential_info)
129 uid_t *uid_list;/**< List of UIDs that can communicate */
130 #define RPC_uid_list(id, i) ((id)->apicall->uid_list[(i)])
132 INT32 uid_num;/**< Number of UID list elements that can communicate */
133 #define RPC_uid_num(id) ((id)->apicall->uid_num)
135 gid_t *gid_list;/**< GID list that can communicate */
136 #define RPC_gid_list(id, i) ((id)->apicall->gid_list[(i)])
138 INT32 gid_num;/**< Number of GID list elements that can communicate */
139 #define RPC_gid_num(id) ((id)->apicall->gid_num)
141 RpcClientSockNameInfo *sock_info_head;/**< Leading node of the source client's socket information management table */
142 #define RPC_sock_info_head(id) ((id)->apicall->sock_info_head)
143 #define RPC_client_sock_name(id) ((id)->apicall->sock_info_head->client_sock_name)
144 #define RPC_client_sock_pid(id) ((id)->apicall->sock_info_head->pid)
145 #define RPC_client_sock_next_node(id) ((id)->apicall->sock_info_head->next)
147 INT32 client_sock_name_num;/**< Number of elements in the source client's socket name list */
148 #define RPC_client_sock_name_num(id) ((id)->apicall->client_sock_name_num)
149 #define RPC_client_sock_name_num_inc(id) ((id)->apicall->client_sock_name_num++)
150 #define RPC_client_sock_name_num_dec(id) ((id)->apicall->client_sock_name_num--)
153 struct RpcThreadInfo;
155 /** Structure that holds information about each RPC_ID */
156 typedef struct RpcIdInfo {
157 struct RpcThreadInfo *thread_info;
159 RPC_ID port;/**< Port number (=ID) used by the RPC library */
160 #define RPC_port(id) ((id)->port)
161 #define RPC_my_id(id) RPC_port(id)
162 #define rpc_get_port(id) (id)
164 INT32 sock;/**< Sockets used by the RPC library */
165 #define RPC_my_sock(id) ((id)->sock)
167 INT32 secure_sock;/**< Authentication socket used by the RPC library */
168 #define RPC_my_secure_sock(id) ((id)->secure_sock)
170 int inotify_fd;/**< Server process monitoring inotify */
171 #define RPC_clnt_inotify_fd(id) ((id)->inotify_fd)
173 INT32 count;/**< Number of clients using the same RPC_ID */
174 #define RPC_clnt_count(id) ((id)->count)
175 #define RPC_inc_clnt_count(id) ((id)->count++)
176 #define RPC_dec_clnt_count(id) ((id)->count--)
178 RpcApicallInfo *apicall;/**< Information for API call acceptance processing */
179 #define RPC_apicall_info(id) ((id)->apicall)
181 RpcSrvrPidInfo *srvr_pid_head;/**< Communication destination server PID management table top node */
182 #define RPC_srvr_pid_head(id) ((id)->srvr_pid_head)
183 #define RPC_srvr_rpc_id(id) ((id)->srvr_pid_head->srvr_rpc_id)
184 #define RPC_srvr_pid(id) ((id)->srvr_pid_head->srvr_pid)
185 #define RPC_srvr_pid_next_node(id) ((id)->srvr_pid_head->next)
187 UINT32 return_str_len;/**<Number of bytes in the returned string as a result of an API call*/
188 /* Including the terminating '\0' */
189 #define RPC_apicall_return_str_len(id) ((id)->return_str_len)
191 UINT8 *return_str;/**<String returned as a result of an API call*/
192 #define RPC_apicall_return_str(id) ((id)->return_str)
195 /** Received response packet */
197 RPC_ID id;/**< ID of the thread that sent the response */
198 UINT16 type;/**< Response type */
199 UINT32 seq_num;/**< The packet number to which this was sent (the response to this) */
202 /** Structure that stores the state of each thread that called the RPC library */
203 typedef struct RpcThreadInfo {
204 pthread_mutex_t th_mtx;/**< Mutex for modifying this struct */
205 #define RPC_THREAD_MUTEX_LOCK(th) (rpc_mutex_lock(&((th)->th_mtx)))
206 #define RPC_THREAD_MUTEX_UNLOCK(th) (rpc_mutex_unlock(&((th)->th_mtx)))
208 pthread_t thread;/**< Thread calling the RPC library */
209 #define RPC_main_thread(th) ((th)->thread)
211 UINT32 sequence_number;/**< Sequence number given to the transmitted packet */
212 /* Send 30 times per second from the beginning of the thread, then wrap around two years.
213 * It is not necessary to wrap around 0 because there is no comparison.
215 UINT32 magic;/**< Magic number to detect corrupted memories */
217 RpcIdInfo *srvr_id;/**< Information by RPC_ID (server) */
218 #define RPC_srvr_idinfo(th) ((th)->srvr_id)
220 RpcIdInfo *clnt_id;/**< Info by RPC_ID (client) */
221 #define RPC_clnt_idinfo(th) ((th)->clnt_id)
224 extern pthread_t g_rpc_thread;
225 extern UINT32 g_rpc_thread_alive;
229 extern int g_rpc_pipe_main_sub[2];
231 RpcThreadInfo *RpcMyThreadInfo(void);
232 RpcThreadInfo *RpcCreateThreadInfo(void);
233 int RpcCreateIdInfo(RpcThreadInfo *th, RPC_ID id,
234 RPC_dispatch_func_t dispatch, INT32 secure_check);
235 void RpcDestroyIdInfo(RpcThreadInfo *th, RpcIdInfo *idinfo);
236 void RpcDestroyThreadInfo(void);
237 void RpcUnlinkSocketFiles(void);
240 #endif // OTHERSERVICE_RPCTHREAD_H_