summaryrefslogtreecommitdiffstats
path: root/task_manager/server/src/tskm_comm.cpp
blob: b177b9b8c7f404bdb7e2ca32c9f67efca0c34371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _GNU_SOURCE
#warning "_GNU_SOURCE not defined, so define here"
#define _GNU_SOURCE
#endif
#include "tskm_comm.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include <native_service/cl_monitor.h>

#include "tskm_debug.h"
#include "tskm_util.h"


/**********************************************************
 * Internal functions
 **********************************************************/
TSKM_STATIC TSKM_SRV_CONNENT_t*
addConnFd(TSKM_SRV_CONNENT_LIST_t* list, TSKM_SRV_CONNENT_t* conn) {
  TSKM_SRV_CONNENT_t* retConn;
  if (list->num >= TSKM_COMM_CONNECT_MAX) {
    TSKM_ASSERT(0)
    return NULL;
  }

  retConn = &list->conn[list->num];
  list->conn[list->num] = *conn;
  list->num++;
  return retConn;
}

TSKM_STATIC void delConnFd(TSKM_SRV_CONNENT_LIST_t* list,
                           TSKM_SRV_CONNENT_t* conn) {
  uint32_t ii;
  TSKM_BOOL_t isFind = TSKM_FALSE;
  for (ii = 0; ii < list->num; ii++) {
    if (!isFind && list->conn[ii].connFd == conn->connFd) {
      isFind = TSKM_TRUE;
      list->num--;
    }

    if (isFind && (ii < list->num)) {
      list->conn[ii] = list->conn[ii + 1];
    }
  }
  TSKM_ASSERT(isFind);
}

/**********************************************************
 * Public functions
 **********************************************************/
TSKM_ERR_t tskm_srvSockCreate(const char *sockName,
                              TSKM_SRV_SOCK_CTX_t* p_sock) {
  int fd = -1;
  int ret;
  int sockRet;
  int enable = 1;
  int listenNum = TSKM_COMM_CONNECT_MAX;

  struct sockaddr_un unix_addr = { };

  fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  if (fd < 0) {  // LCOV_EXCL_BR_LINE 5: For processing initializing process
    // LCOV_EXCL_START 5: For processing initializing process
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }

  if (0 == access(sockName, F_OK)) {  // LCOV_EXCL_BR_LINE 5: For processing initializing process
    TSKM_ASSERT(0);
    unlink(sockName);
  }

  unix_addr.sun_family = AF_UNIX;
  strncpy(unix_addr.sun_path, sockName, sizeof(unix_addr.sun_path) - 1);

  sockRet = bind(fd, (struct sockaddr *) &unix_addr, sizeof(unix_addr));
  if (sockRet != 0) {  // LCOV_EXCL_BR_LINE 5: For processing initializing process
    // LCOV_EXCL_START 5: For processing initializing process
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }

  ret = chmod(sockName, S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH);
  if (ret != 0) {  // LCOV_EXCL_BR_LINE 5: For processing initializing process
    // LCOV_EXCL_START 5: For processing initializing process
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }

  sockRet = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &enable, sizeof(enable));
  if (sockRet != 0) {  // LCOV_EXCL_BR_LINE 5:For processing initializing process
    // LCOV_EXCL_START 5: For processing initializing process
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }

  sockRet = listen(fd, listenNum);
  if (sockRet != 0) {  // LCOV_EXCL_BR_LINE 5: For process initialization processing
    // LCOV_EXCL_START 5: For process initialization processing
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }

  memset(p_sock, 0, sizeof(*p_sock));
  p_sock->sockFd = fd;
  p_sock->sockName = sockName;

  return TSKM_E_OK;
  // LCOV_EXCL_START 5: For process initialization processing
  ERROR:
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  if (fd != -1) {
    close(fd);
  }
  return TSKM_E_NG;
  // LCOV_EXCL_STOP
}

TSKM_SRV_CONNENT_t*
tskm_srvSockConnect(TSKM_SRV_SOCK_CTX_t* p_sock) {
  int fd = -1;
  socklen_t len;
  int sockRet;
  TSKM_SRV_CONNENT_t conn;
  TSKM_SRV_CONNENT_t *retConn;
  struct ucred credent;
  struct sockaddr_un unix_addr;

  memset(&conn, 0, sizeof(conn));
  conn.connFd = -1;

  len = sizeof(unix_addr);
  fd = accept(p_sock->sockFd, (struct sockaddr*) &unix_addr,
              (socklen_t *) &len);  // NOLINT (readability/casting)
  if (fd < 0) {  // LCOV_EXCL_BR_LINE 5: Accept's Error-Handling Process
    // LCOV_EXCL_START 5: Accept's Error-Handling Process
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }
  conn.connFd = fd;

  len = sizeof(credent);
  sockRet = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &credent, &len);
  if (sockRet != 0) {  // LCOV_EXCL_BR_LINE 5: Getsockopt's Error-Handling Process
    // LCOV_EXCL_START 5: Getsockopt's Error-Handling Process
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }
  conn.pid = credent.pid;

  retConn = addConnFd(&p_sock->connList, &conn);
  if (retConn == NULL) {  // LCOV_EXCL_BR_LINE 5: Connect's Error-Handling Process
    // LCOV_EXCL_START 5: Connect's Error-Handling Process
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }
  return retConn;
  // LCOV_EXCL_START 5: Error-Handling Process
  ERROR:
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  if (conn.connFd != -1) {
    tskm_sockDestory(conn.connFd);
  }
  return NULL;
  // LCOV_EXCL_STOP
}

void tskm_srvSockDisconnect(TSKM_SRV_SOCK_CTX_t* p_sock,
                            TSKM_SRV_CONNENT_t *p_conn) {
  int fd = p_conn->connFd;
  delConnFd(&p_sock->connList, p_conn);
  tskm_sockDestory(fd);
}

void tskm_srvSockDestory(TSKM_SRV_SOCK_CTX_t* p_sock) {  // LCOV_EXCL_START 6: Because the condition cannot be set
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  if (p_sock->sockFd) {
    tskm_sockDestory(p_sock->sockFd);
    p_sock->sockFd = 0;

    TSKM_ASSERT_ERRNO(0 == unlink(p_sock->sockName));
  }
}
// LCOV_EXCL_STOP
int tskm_cliSockConnect(const char* sockName) {
  int fd = -1;
  int sockRet;
  struct sockaddr_un unix_addr = { };

  /* Create socket */
  fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  if (fd < 0) {  // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
    // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }

  unix_addr.sun_family = AF_UNIX;
  strncpy(unix_addr.sun_path, sockName, sizeof(unix_addr.sun_path) - 1);
  sockRet = connect(fd, (struct sockaddr*) &unix_addr, sizeof(unix_addr));
  if (sockRet < 0) {  // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
    // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }
  return fd;
  // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
  ERROR:
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  if (fd != -1) {
    TSKM_ASSERT_ERRNO(0 == close(fd));
  }
  return -1;
  // LCOV_EXCL_STOP
}

int tskm_sockRcv(int fd, TSKM_EVENT_INFO_t* p_ev) {
  int ret;
  TSKM_EVENT_INFO_t ev;
  ret = static_cast<int>(recv(fd, &ev, sizeof(ev), 0));

  // LCOV_EXCL_BR_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
  if (ret > (int) sizeof(ev)) {  // NOLINT (readability/casting)
    // LCOV_EXCL_BR_STOP
    // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }

  if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
    // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP
  }
  if (ret > 0) {  // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
    TSKM_PRINTF(TSKM_LOG_MSG, "recv:%s from:%d ret:%d",
                tskm_convEvent2Str(ev.event), ev.fromPid, ret);
  }

  *p_ev = ev;

  if (p_ev->hasExtend && (0 != p_ev->extendSize)) {
    TSKM_PRINTF(TSKM_LOG_MSG, "rcv:ex(%d) ", p_ev->extendSize);

    p_ev->extendPrm = malloc(p_ev->extendSize);
    if (!p_ev->extendPrm) {  // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
      // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      TSKM_ASSERT(0);
      goto ERROR;
      // LCOV_EXCL_STOP
    }

    ret = static_cast<int>(recv(fd, p_ev->extendPrm, p_ev->extendSize, 0));

    // LCOV_EXCL_BR_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
    if (ret > (int) p_ev->extendSize) {  // NOLINT (readability/casting)
      // LCOV_EXCL_BR_STOP
      // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      TSKM_ASSERT_ERRNO(0);
      goto ERROR;
      // LCOV_EXCL_STOP
    }

    if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: Checked in Death testing, but it is not reflected in the coverage and excluded
      // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      TSKM_ASSERT_ERRNO(0);
      goto ERROR;
      // LCOV_EXCL_STOP
    }
  }

  return ret;

  // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
  ERROR:
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  return -1;
  // LCOV_EXCL_STOP
}

int tskm_sockSend(int fd, TSKM_EVENT_INFO_t* p_ev) {
  int ret;
  p_ev->fromPid = getpid();
  TSKM_PRINTF(TSKM_LOG_MSG, "send:%s ", tskm_convEvent2Str(p_ev->event));
  ret = static_cast<int>(send(fd, p_ev, sizeof(*p_ev), MSG_NOSIGNAL));
  if (ret != sizeof(*p_ev)) {
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
  }
  // Because it is entered only when called from a debugging function (pri_sendDebugDumpRes)
  if (p_ev->hasExtend && p_ev->extendPrm && (0 != p_ev->extendSize)) {
    TSKM_PRINTF(TSKM_LOG_MSG, "send:ex(%d) ", p_ev->extendSize);

    ret = static_cast<int>(send(fd, p_ev->extendPrm, p_ev->extendSize, MSG_NOSIGNAL));
    if (ret != (int)p_ev->extendSize) {  // NOLINT (readability/casting)
      TSKM_ASSERT_ERRNO(0);
      goto ERROR;
    }
  }

  return ret;

  ERROR: return -1;
}

void tskm_sockDestory(int fd) {
  TSKM_ASSERT_ERRNO(0 == shutdown(fd, SHUT_RDWR));
  TSKM_ASSERT_ERRNO(0 == close(fd));
}

/******************************************************************
 *        Initializing (Process)
 ******************************************************************/
int tskm_comm_procInit(void) {
  int ret;

  ret = CL_MonitorInit(CL_MONITOR_INIT_USER);

  if (ret != 0) {
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
  }

  return 0;

  // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
  ERROR:
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  return -1;
  // LCOV_EXCL_STOP
}

/*********************************************************
 *        Service Error Monitoring Status Setting
 *********************************************************/
int tskm_comm_setSvcWatchState(uint32_t id, BOOL bIsRun, uint32_t timeout) {
  int ret = 0;

  CL_MonitorState_t state = CL_MONITOR_STATE_SLEEP;

  if (bIsRun) {
    state = CL_MONITOR_STATE_RUN;
  }

  ret = CL_MonitorSetEntry(CL_MONITOR_TYPE_GENERIC, id, state, timeout, 0);
  if (0 != ret) {
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
  }

  return 0;
  ERROR: return -1;
}  // LCOV_EXCL_BR_LINE 10: Final line