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.
18 #warning "_GNU_SOURCE not defined, so define here"
21 #include "tskm_comm.h"
22 #include <sys/types.h>
23 #include <sys/socket.h>
29 #include <native_service/cl_monitor.h>
31 #include "tskm_debug.h"
32 #include "tskm_util.h"
35 /**********************************************************
37 **********************************************************/
38 TSKM_STATIC TSKM_SRV_CONNENT_t*
39 addConnFd(TSKM_SRV_CONNENT_LIST_t* list, TSKM_SRV_CONNENT_t* conn) {
40 TSKM_SRV_CONNENT_t* retConn;
41 if (list->num >= TSKM_COMM_CONNECT_MAX) {
46 retConn = &list->conn[list->num];
47 list->conn[list->num] = *conn;
52 TSKM_STATIC void delConnFd(TSKM_SRV_CONNENT_LIST_t* list,
53 TSKM_SRV_CONNENT_t* conn) {
55 TSKM_BOOL_t isFind = TSKM_FALSE;
56 for (ii = 0; ii < list->num; ii++) {
57 if (!isFind && list->conn[ii].connFd == conn->connFd) {
62 if (isFind && (ii < list->num)) {
63 list->conn[ii] = list->conn[ii + 1];
69 /**********************************************************
71 **********************************************************/
72 TSKM_ERR_t tskm_srvSockCreate(const char *sockName,
73 TSKM_SRV_SOCK_CTX_t* p_sock) {
78 int listenNum = TSKM_COMM_CONNECT_MAX;
80 struct sockaddr_un unix_addr = { };
82 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
83 if (fd < 0) { // LCOV_EXCL_BR_LINE 5: For processing initializing process
84 // LCOV_EXCL_START 5: For processing initializing process
85 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
91 if (0 == access(sockName, F_OK)) { // LCOV_EXCL_BR_LINE 5: For processing initializing process
96 unix_addr.sun_family = AF_UNIX;
97 strncpy(unix_addr.sun_path, sockName, sizeof(unix_addr.sun_path) - 1);
99 sockRet = bind(fd, (struct sockaddr *) &unix_addr, sizeof(unix_addr));
100 if (sockRet != 0) { // LCOV_EXCL_BR_LINE 5: For processing initializing process
101 // LCOV_EXCL_START 5: For processing initializing process
102 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
103 TSKM_ASSERT_ERRNO(0);
108 ret = chmod(sockName, S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
109 if (ret != 0) { // LCOV_EXCL_BR_LINE 5: For processing initializing process
110 // LCOV_EXCL_START 5: For processing initializing process
111 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
112 TSKM_ASSERT_ERRNO(0);
117 sockRet = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable));
118 if (sockRet != 0) { // LCOV_EXCL_BR_LINE 5:For processing initializing process
119 // LCOV_EXCL_START 5: For processing initializing process
120 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
121 TSKM_ASSERT_ERRNO(0);
126 sockRet = listen(fd, listenNum);
127 if (sockRet != 0) { // LCOV_EXCL_BR_LINE 5: For process initialization processing
128 // LCOV_EXCL_START 5: For process initialization processing
129 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
130 TSKM_ASSERT_ERRNO(0);
135 memset(p_sock, 0, sizeof(*p_sock));
137 p_sock->sockName = sockName;
140 // LCOV_EXCL_START 5: For process initialization processing
142 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
151 tskm_srvSockConnect(TSKM_SRV_SOCK_CTX_t* p_sock) {
155 TSKM_SRV_CONNENT_t conn;
156 TSKM_SRV_CONNENT_t *retConn;
157 struct ucred credent;
158 struct sockaddr_un unix_addr;
160 memset(&conn, 0, sizeof(conn));
163 len = sizeof(unix_addr);
164 fd = accept(p_sock->sockFd, (struct sockaddr*) &unix_addr,
165 (socklen_t *) &len); // NOLINT (readability/casting)
166 if (fd < 0) { // LCOV_EXCL_BR_LINE 5: Accept's Error-Handling Process
167 // LCOV_EXCL_START 5: Accept's Error-Handling Process
168 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
169 TSKM_ASSERT_ERRNO(0);
175 len = sizeof(credent);
176 sockRet = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &credent, &len);
177 if (sockRet != 0) { // LCOV_EXCL_BR_LINE 5: Getsockopt's Error-Handling Process
178 // LCOV_EXCL_START 5: Getsockopt's Error-Handling Process
179 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
180 TSKM_ASSERT_ERRNO(0);
184 conn.pid = credent.pid;
186 retConn = addConnFd(&p_sock->connList, &conn);
187 if (retConn == NULL) { // LCOV_EXCL_BR_LINE 5: Connect's Error-Handling Process
188 // LCOV_EXCL_START 5: Connect's Error-Handling Process
189 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
195 // LCOV_EXCL_START 5: Error-Handling Process
197 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
198 if (conn.connFd != -1) {
199 tskm_sockDestory(conn.connFd);
205 void tskm_srvSockDisconnect(TSKM_SRV_SOCK_CTX_t* p_sock,
206 TSKM_SRV_CONNENT_t *p_conn) {
207 int fd = p_conn->connFd;
208 delConnFd(&p_sock->connList, p_conn);
209 tskm_sockDestory(fd);
212 void tskm_srvSockDestory(TSKM_SRV_SOCK_CTX_t* p_sock) { // LCOV_EXCL_START 6: Because the condition cannot be set
213 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
214 if (p_sock->sockFd) {
215 tskm_sockDestory(p_sock->sockFd);
218 TSKM_ASSERT_ERRNO(0 == unlink(p_sock->sockName));
222 int tskm_cliSockConnect(const char* sockName) {
225 struct sockaddr_un unix_addr = { };
228 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
229 if (fd < 0) { // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
230 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
231 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
232 TSKM_ASSERT_ERRNO(0);
237 unix_addr.sun_family = AF_UNIX;
238 strncpy(unix_addr.sun_path, sockName, sizeof(unix_addr.sun_path) - 1);
239 sockRet = connect(fd, (struct sockaddr*) &unix_addr, sizeof(unix_addr));
240 if (sockRet < 0) { // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
241 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
242 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
243 TSKM_ASSERT_ERRNO(0);
248 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
250 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
252 TSKM_ASSERT_ERRNO(0 == close(fd));
258 int tskm_sockRcv(int fd, TSKM_EVENT_INFO_t* p_ev) {
260 TSKM_EVENT_INFO_t ev;
261 ret = static_cast<int>(recv(fd, &ev, sizeof(ev), 0));
263 // LCOV_EXCL_BR_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
264 if (ret > (int) sizeof(ev)) { // NOLINT (readability/casting)
266 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
267 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
268 TSKM_ASSERT_ERRNO(0);
273 if (ret < 0) { // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
274 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
275 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
276 TSKM_ASSERT_ERRNO(0);
280 if (ret > 0) { // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
281 TSKM_PRINTF(TSKM_LOG_MSG, "recv:%s from:%d ret:%d",
282 tskm_convEvent2Str(ev.event), ev.fromPid, ret);
287 if (p_ev->hasExtend && (0 != p_ev->extendSize)) {
288 TSKM_PRINTF(TSKM_LOG_MSG, "rcv:ex(%d) ", p_ev->extendSize);
290 p_ev->extendPrm = malloc(p_ev->extendSize);
291 if (!p_ev->extendPrm) { // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
292 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
293 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
299 ret = static_cast<int>(recv(fd, p_ev->extendPrm, p_ev->extendSize, 0));
301 // LCOV_EXCL_BR_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
302 if (ret > (int) p_ev->extendSize) { // NOLINT (readability/casting)
304 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
305 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
306 TSKM_ASSERT_ERRNO(0);
311 if (ret < 0) { // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
312 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
313 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
314 TSKM_ASSERT_ERRNO(0);
322 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
324 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
329 int tskm_sockSend(int fd, TSKM_EVENT_INFO_t* p_ev) {
331 p_ev->fromPid = getpid();
332 TSKM_PRINTF(TSKM_LOG_MSG, "send:%s ", tskm_convEvent2Str(p_ev->event));
333 ret = static_cast<int>(send(fd, p_ev, sizeof(*p_ev), MSG_NOSIGNAL));
334 if (ret != sizeof(*p_ev)) {
335 TSKM_ASSERT_ERRNO(0);
338 // Because it is entered only when called from a debugging function (pri_sendDebugDumpRes)
339 if (p_ev->hasExtend && p_ev->extendPrm && (0 != p_ev->extendSize)) {
340 TSKM_PRINTF(TSKM_LOG_MSG, "send:ex(%d) ", p_ev->extendSize);
342 ret = static_cast<int>(send(fd, p_ev->extendPrm, p_ev->extendSize, MSG_NOSIGNAL));
343 if (ret != (int)p_ev->extendSize) { // NOLINT (readability/casting)
344 TSKM_ASSERT_ERRNO(0);
354 void tskm_sockDestory(int fd) {
355 TSKM_ASSERT_ERRNO(0 == shutdown(fd, SHUT_RDWR));
356 TSKM_ASSERT_ERRNO(0 == close(fd));
359 /******************************************************************
360 * Initializing (Process)
361 ******************************************************************/
362 int tskm_comm_procInit(void) {
365 ret = CL_MonitorInit(CL_MONITOR_INIT_USER);
368 TSKM_ASSERT_ERRNO(0);
374 // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
376 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
381 /*********************************************************
382 * Service Error Monitoring Status Setting
383 *********************************************************/
384 int tskm_comm_setSvcWatchState(uint32_t id, BOOL bIsRun, uint32_t timeout) {
387 CL_MonitorState_t state = CL_MONITOR_STATE_SLEEP;
390 state = CL_MONITOR_STATE_RUN;
393 ret = CL_MonitorSetEntry(CL_MONITOR_TYPE_GENERIC, id, state, timeout, 0);
395 TSKM_ASSERT_ERRNO(0);
401 } // LCOV_EXCL_BR_LINE 10: Final line