Line data Source code
1 : /*
2 : * SPDX license identifier: MPL-2.0
3 : *
4 : * Copyright (C) 2011-2015, BMW AG
5 : *
6 : * This file is part of COVESA Project DLT - Diagnostic Log and Trace.
7 : *
8 : * This Source Code Form is subject to the terms of the
9 : * Mozilla Public License (MPL), v. 2.0.
10 : * If a copy of the MPL was not distributed with this file,
11 : * You can obtain one at http://mozilla.org/MPL/2.0/.
12 : *
13 : * For further information see http://www.covesa.org/.
14 : */
15 :
16 : /*!
17 : * \author
18 : * Alexander Wenzel <alexander.aw.wenzel@bmw.de>
19 : * Markus Klein <Markus.Klein@esk.fraunhofer.de>
20 : * Mikko Rapeli <mikko.rapeli@bmw.de>
21 : *
22 : * \copyright Copyright © 2011-2015 BMW AG. \n
23 : * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
24 : *
25 : * \file dlt-daemon.c
26 : */
27 :
28 : #include <netdb.h>
29 : #include <ctype.h>
30 : #include <stdio.h> /* for printf() and fprintf() */
31 : #include <sys/socket.h> /* for socket(), connect(), (), and recv() */
32 : #include <sys/un.h>
33 : #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
34 : #include <stdlib.h> /* for atoi() and exit() */
35 : #include <string.h> /* for memset() */
36 : #include <unistd.h> /* for close() and access */
37 : #include <fcntl.h>
38 : #include <signal.h>
39 : #include <syslog.h>
40 : #include <errno.h>
41 : #include <pthread.h>
42 : #include <grp.h>
43 :
44 : #ifdef linux
45 : # include <sys/timerfd.h>
46 : #endif
47 : #include <sys/stat.h>
48 : #include <sys/time.h>
49 : #include <libgen.h>
50 :
51 : #if defined(linux) && defined(__NR_statx)
52 : # include <linux/stat.h>
53 : #endif
54 :
55 : #ifdef DLT_DAEMON_VSOCK_IPC_ENABLE
56 : # ifdef linux
57 : # include <linux/vm_sockets.h>
58 : # endif
59 : # ifdef __QNX__
60 : # include <vm_sockets.h>
61 : # endif
62 : #endif
63 :
64 : #include <getopt.h>
65 : #ifndef CONFIGURATION_FILES_DIR
66 : #define CONFIGURATION_FILES_DIR "/etc"
67 : #endif
68 : #ifndef DLT_USER_IPC_PATH
69 : #define DLT_USER_IPC_PATH "/tmp"
70 : #endif
71 :
72 : #include <getopt.h>
73 : #ifndef CONFIGURATION_FILES_DIR
74 : #define CONFIGURATION_FILES_DIR "/etc"
75 : #endif
76 : #ifndef DLT_USER_IPC_PATH
77 : #define DLT_USER_IPC_PATH "/tmp"
78 : #endif
79 :
80 : #include "dlt_types.h"
81 : #include "dlt-daemon.h"
82 : #include "dlt-daemon_cfg.h"
83 : #include "dlt_daemon_common_cfg.h"
84 :
85 : #include "dlt_daemon_socket.h"
86 : #include "dlt_daemon_unix_socket.h"
87 : #include "dlt_daemon_serial.h"
88 :
89 : #include "dlt_daemon_client.h"
90 : #include "dlt_daemon_connection.h"
91 : #include "dlt_daemon_event_handler.h"
92 : #include "dlt_daemon_offline_logstorage.h"
93 : #include "dlt_gateway.h"
94 :
95 : #ifdef UDP_CONNECTION_SUPPORT
96 : # include "dlt_daemon_udp_socket.h"
97 : #endif
98 : #if defined(DLT_SYSTEMD_WATCHDOG_ENABLE) || defined(DLT_SYSTEMD_ENABLE)
99 : # include "sd-daemon.h"
100 : #endif
101 :
102 : /**
103 : * \defgroup daemon DLT Daemon
104 : * \addtogroup daemon
105 : \{
106 : */
107 :
108 : #define DLT_DAEMON_APP_ID "DLTD"
109 : #define DLT_DAEMON_CTX_ID "INTM"
110 :
111 : static int dlt_daemon_log_internal(DltDaemon *daemon,
112 : DltDaemonLocal *daemon_local, char *str,
113 : DltLogLevelType level, const char *app_id,
114 : const char *ctx_id, int verbose);
115 :
116 : static int dlt_daemon_check_numeric_setting(char *token,
117 : char *value,
118 : unsigned long *data);
119 :
120 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
121 :
122 : struct DltTraceLoadLogParams {
123 : DltDaemon *daemon;
124 : DltDaemonLocal *daemon_local;
125 : int verbose;
126 : char *app_id;
127 : };
128 :
129 : static DltReturnValue dlt_daemon_output_internal_msg(DltLogLevelType loglevel, const char *text, void *params);
130 : static void dlt_trace_load_free(DltDaemon* daemon);
131 :
132 : pthread_rwlock_t trace_load_rw_lock;
133 : #endif
134 :
135 : /* used in main event loop and signal handler */
136 : int g_exit = 0;
137 :
138 : int g_signo = 0;
139 :
140 : /* used for value from conf file */
141 : static int value_length = 1024;
142 :
143 : static char dlt_timer_conn_types[DLT_TIMER_UNKNOWN + 1] = {
144 : [DLT_TIMER_PACKET] = DLT_CONNECTION_ONE_S_TIMER,
145 : [DLT_TIMER_ECU] = DLT_CONNECTION_SIXTY_S_TIMER,
146 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
147 : [DLT_TIMER_SYSTEMD] = DLT_CONNECTION_SYSTEMD_TIMER,
148 : #endif
149 : [DLT_TIMER_GATEWAY] = DLT_CONNECTION_GATEWAY_TIMER,
150 : [DLT_TIMER_UNKNOWN] = DLT_CONNECTION_TYPE_MAX
151 : };
152 :
153 : static char dlt_timer_names[DLT_TIMER_UNKNOWN + 1][32] = {
154 : [DLT_TIMER_PACKET] = "Timing packet",
155 : [DLT_TIMER_ECU] = "ECU version",
156 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
157 : [DLT_TIMER_SYSTEMD] = "Systemd watchdog",
158 : #endif
159 : [DLT_TIMER_GATEWAY] = "Gateway",
160 : [DLT_TIMER_UNKNOWN] = "Unknown timer"
161 : };
162 :
163 : #ifdef __QNX__
164 : static int dlt_timer_pipes[DLT_TIMER_UNKNOWN][2] = {
165 : /* [timer_id] = {read_pipe, write_pipe} */
166 : [DLT_TIMER_PACKET] = {DLT_FD_INIT, DLT_FD_INIT},
167 : [DLT_TIMER_ECU] = {DLT_FD_INIT, DLT_FD_INIT},
168 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
169 : [DLT_TIMER_SYSTEMD] = {DLT_FD_INIT, DLT_FD_INIT},
170 : #endif
171 : [DLT_TIMER_GATEWAY] = {DLT_FD_INIT, DLT_FD_INIT}
172 : };
173 :
174 : static pthread_t timer_threads[DLT_TIMER_UNKNOWN] = {
175 : [DLT_TIMER_PACKET] = 0,
176 : [DLT_TIMER_ECU] = 0,
177 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
178 : [DLT_TIMER_SYSTEMD] = 0,
179 : #endif
180 : [DLT_TIMER_GATEWAY] = 0
181 : };
182 :
183 : static DltDaemonPeriodicData *timer_data[DLT_TIMER_UNKNOWN] = {
184 : [DLT_TIMER_PACKET] = NULL,
185 : [DLT_TIMER_ECU] = NULL,
186 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
187 : [DLT_TIMER_SYSTEMD] = NULL,
188 : #endif
189 : [DLT_TIMER_GATEWAY] = NULL
190 : };
191 :
192 : void close_pipes(int fds[2])
193 : {
194 : if (fds[0] > 0) {
195 : close(fds[0]);
196 : fds[0] = DLT_FD_INIT;
197 : }
198 :
199 : if (fds[1] > 0) {
200 : close(fds[1]);
201 : fds[1] = DLT_FD_INIT;
202 : }
203 : }
204 :
205 : #endif // __QNX__
206 :
207 : /**
208 : * Print usage information of tool.
209 : */
210 0 : void usage()
211 : {
212 : char version[DLT_DAEMON_TEXTBUFSIZE];
213 0 : dlt_get_version(version, DLT_DAEMON_TEXTBUFSIZE);
214 :
215 : /*printf("DLT logging daemon %s %s\n", _DLT_PACKAGE_VERSION, _DLT_PACKAGE_VERSION_STATE); */
216 : /*printf("Compile options: %s %s %s %s",_DLT_SYSTEMD_ENABLE, _DLT_SYSTEMD_WATCHDOG_ENABLE, _DLT_TEST_ENABLE, _DLT_SHM_ENABLE); */
217 : printf("%s", version);
218 : printf("Usage: dlt-daemon [options]\n");
219 : printf("Options:\n");
220 : printf(" -d Daemonize\n");
221 : printf(" -h Usage\n");
222 : printf(" -c filename DLT daemon configuration file (Default: %s/dlt.conf)\n", CONFIGURATION_FILES_DIR);
223 : printf(" -x version Protocol version (1=DLT v1, 2=DLT v2)\n");
224 :
225 : #ifdef DLT_DAEMON_USE_FIFO_IPC
226 : printf(" -t directory Directory for local fifo and user-pipes (Default: /tmp)\n");
227 : printf(" (Applications wanting to connect to a daemon using a\n");
228 : printf(" custom directory need to be started with the environment \n");
229 : printf(" variable DLT_PIPE_DIR set appropriately)\n");
230 : #endif
231 :
232 : #ifdef DLT_SHM_ENABLE
233 : printf(" -s filename The file name to create the share memory (Default: /dlt-shm)\n");
234 : printf(" (Applications wanting to connect to a daemon using a\n");
235 : printf(" custom shm name need to be started with the environment \n");
236 : printf(" variable DLT_SHM_NAME set appropriately)\n");
237 : #endif
238 : printf(" -p port port to monitor for incoming requests (Default: 3490)\n");
239 : printf(" (Applications wanting to connect to a daemon using a custom\n");
240 : printf(" port need to be started with the environment variable\n");
241 : printf(" DLT_DAEMON_TCP_PORT set appropriately)\n");
242 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
243 : printf(" -a filename The filename for load default app id log levels (Default: " CONFIGURATION_FILES_DIR "/dlt-log-levels.conf)\n");
244 : #endif
245 :
246 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
247 : printf(" -l filename The filename for load limits (Default: " CONFIGURATION_FILES_DIR "/dlt-trace-load.conf)\n");
248 : #endif
249 :
250 : #
251 0 : } /* usage() */
252 :
253 : /**
254 : * Option handling
255 : */
256 1 : int option_handling(DltDaemonLocal *daemon_local, int argc, char *argv[])
257 : {
258 : int c;
259 : char options[255];
260 : memset(options, 0, sizeof options);
261 : const char *const default_options = "hdc:t:p:x:";
262 : strcpy(options, default_options);
263 :
264 1 : if (daemon_local == 0) {
265 0 : fprintf (stderr, "Invalid parameter passed to option_handling()\n");
266 0 : return -1;
267 : }
268 :
269 : /* Initialize flags */
270 : memset(daemon_local, 0, sizeof(DltDaemonLocal));
271 :
272 : /* default values */
273 1 : daemon_local->flags.port = DLT_DAEMON_TCP_PORT;
274 :
275 : #ifdef DLT_DAEMON_USE_FIFO_IPC
276 1 : dlt_log_set_fifo_basedir(DLT_USER_IPC_PATH);
277 : #endif
278 :
279 : #ifdef DLT_SHM_ENABLE
280 : strncpy(dltShmName, "/dlt-shm", NAME_MAX);
281 : #endif
282 :
283 1 : opterr = 0;
284 :
285 : #ifdef DLT_SHM_ENABLE
286 : strcpy(options + strlen(options), "s:");
287 : #endif
288 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
289 : strcpy(options + strlen(options), "a:");
290 : #endif
291 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
292 : strcpy(options + strlen(options), "l:");
293 : #endif
294 2 : while ((c = getopt(argc, argv, options)) != -1)
295 1 : switch (c) {
296 0 : case 'x':
297 : {
298 0 : int proto = atoi(optarg);
299 0 : if (proto == 1 || proto == 2) {
300 0 : daemon_local->flags.protocolVersion = proto;
301 : } else {
302 0 : fprintf(stderr, "Invalid protocol version '%s'. Use 1 or 2.\n", optarg);
303 0 : return -1;
304 : }
305 0 : break;
306 : }
307 0 : case 'd':
308 : {
309 0 : daemon_local->flags.dflag = 1;
310 0 : break;
311 : }
312 1 : case 'c':
313 : {
314 1 : strncpy(daemon_local->flags.cvalue, optarg, NAME_MAX);
315 : break;
316 : }
317 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
318 : case 'a':
319 : {
320 : strncpy(daemon_local->flags.avalue, optarg, NAME_MAX);
321 : break;
322 : }
323 : #endif
324 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
325 : case 'l':
326 : {
327 : strncpy(daemon_local->flags.lvalue, optarg, NAME_MAX);
328 : break;
329 : }
330 : #endif
331 : #ifdef DLT_DAEMON_USE_FIFO_IPC
332 0 : case 't':
333 : {
334 0 : dlt_log_set_fifo_basedir(optarg);
335 0 : break;
336 : }
337 : #endif
338 :
339 : #ifdef DLT_SHM_ENABLE
340 : case 's':
341 : {
342 : strncpy(dltShmName, optarg, NAME_MAX);
343 : break;
344 : }
345 : #endif
346 0 : case 'p':
347 : {
348 0 : daemon_local->flags.port = (unsigned int) atoi(optarg);
349 :
350 0 : if (daemon_local->flags.port == 0) {
351 0 : fprintf (stderr, "Invalid port `%s' specified.\n", optarg);
352 0 : return -1;
353 : }
354 :
355 : break;
356 : }
357 0 : case 'h':
358 : {
359 0 : usage();
360 0 : return -2; /* return no error */
361 : }
362 :
363 0 : case '?':
364 : {
365 0 : if ((optopt == 'c') || (optopt == 't') || (optopt == 'p')
366 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
367 : || (optopt == 'a')
368 : #endif
369 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
370 : || (optopt == 'l')
371 : #endif
372 : )
373 0 : fprintf (stderr, "Option -%c requires an argument.\n", optopt);
374 0 : else if (isprint (optopt))
375 0 : fprintf (stderr, "Unknown option `-%c'.\n", optopt);
376 : else
377 0 : fprintf (stderr, "Unknown option character `\\x%x'.\n", (uint32_t)optopt);
378 :
379 : /* unknown or wrong option used, show usage information and terminate */
380 0 : usage();
381 0 : return -1;
382 : }
383 0 : default:
384 : {
385 0 : fprintf (stderr, "Invalid option, this should never occur!\n");
386 0 : return -1;
387 : }
388 : }
389 :
390 : /* switch() */
391 :
392 : #ifdef DLT_DAEMON_USE_FIFO_IPC
393 : int ret;
394 1 : size_t base_dir_len = strlen(dltFifoBaseDir);
395 :
396 : /* Check if base directory path is too long to append suffixes */
397 1 : if (base_dir_len > DLT_PATH_MAX - 10) { /* 10 for "/dltpipes\0" */
398 0 : dlt_log(LOG_ERR, "FIFO base directory path too long!\n");
399 0 : return -1;
400 : }
401 :
402 1 : ret = snprintf(daemon_local->flags.userPipesDir, DLT_PATH_MAX,
403 : "%s/dltpipes", dltFifoBaseDir);
404 1 : if (ret < 0 || ret >= DLT_PATH_MAX) {
405 0 : dlt_log(LOG_ERR, "Failed to construct userPipesDir path!\n");
406 0 : return -1;
407 : }
408 :
409 1 : ret = snprintf(daemon_local->flags.daemonFifoName, DLT_PATH_MAX,
410 : "%s/dlt", dltFifoBaseDir);
411 1 : if (ret < 0 || ret >= DLT_PATH_MAX) {
412 0 : dlt_log(LOG_ERR, "Failed to construct daemonFifoName path!\n");
413 0 : return -1;
414 : }
415 : #endif
416 :
417 : #ifdef DLT_SHM_ENABLE
418 : strncpy(daemon_local->flags.dltShmName, dltShmName, NAME_MAX);
419 : #endif
420 :
421 : return 0;
422 :
423 : } /* option_handling() */
424 :
425 : /**
426 : * Option file parser
427 : */
428 1 : int option_file_parser(DltDaemonLocal *daemon_local)
429 1 : {
430 : FILE *pFile;
431 1 : char line[value_length - 1];
432 1 : char token[value_length];
433 1 : char value[value_length];
434 : char *pch;
435 : const char *filename;
436 : ssize_t n;
437 :
438 : /* set default values for configuration */
439 1 : daemon_local->flags.sharedMemorySize = DLT_SHM_SIZE;
440 1 : daemon_local->flags.sendMessageTime = 0;
441 1 : daemon_local->flags.offlineTraceDirectory[0] = 0;
442 1 : daemon_local->flags.offlineTraceFileSize = 1000000;
443 1 : daemon_local->flags.offlineTraceMaxSize = 4000000;
444 1 : daemon_local->flags.offlineTraceFilenameTimestampBased = true;
445 1 : daemon_local->flags.loggingMode = DLT_LOG_TO_CONSOLE;
446 1 : daemon_local->flags.loggingLevel = LOG_INFO;
447 :
448 : #ifdef DLT_DAEMON_USE_UNIX_SOCKET_IPC
449 : n = snprintf(daemon_local->flags.loggingFilename,
450 : sizeof(daemon_local->flags.loggingFilename),
451 : "%s/dlt.log", DLT_USER_IPC_PATH);
452 : #else /* DLT_DAEMON_USE_FIFO_IPC */
453 1 : n = snprintf(daemon_local->flags.loggingFilename,
454 : sizeof(daemon_local->flags.loggingFilename),
455 : "%s/dlt.log", dltFifoBaseDir);
456 : #endif
457 :
458 1 : if (n < 0 || (size_t)n > sizeof(daemon_local->flags.loggingFilename)) {
459 0 : dlt_vlog(LOG_WARNING, "%s: snprintf truncation/error(%ld) %s\n",
460 : __func__, (long int)n, daemon_local->flags.loggingFilename);
461 : }
462 1 : daemon_local->flags.enableLoggingFileLimit = false;
463 1 : daemon_local->flags.loggingFileSize = 250000;
464 1 : daemon_local->flags.loggingFileMaxSize = 1000000;
465 :
466 1 : daemon_local->timeoutOnSend = 4;
467 1 : daemon_local->RingbufferMinSize = DLT_DAEMON_RINGBUFFER_MIN_SIZE;
468 1 : daemon_local->RingbufferMaxSize = DLT_DAEMON_RINGBUFFER_MAX_SIZE;
469 1 : daemon_local->RingbufferStepSize = DLT_DAEMON_RINGBUFFER_STEP_SIZE;
470 1 : daemon_local->daemonFifoSize = 0;
471 1 : daemon_local->flags.sendECUSoftwareVersion = 0;
472 1 : memset(daemon_local->flags.pathToECUSoftwareVersion, 0, sizeof(daemon_local->flags.pathToECUSoftwareVersion));
473 1 : memset(daemon_local->flags.ecuSoftwareVersionFileField, 0, sizeof(daemon_local->flags.ecuSoftwareVersionFileField));
474 1 : daemon_local->flags.sendTimezone = 0;
475 1 : daemon_local->flags.offlineLogstorageMaxDevices = 0;
476 1 : daemon_local->flags.offlineLogstorageDirPath[0] = 0;
477 1 : daemon_local->flags.offlineLogstorageTimestamp = 1;
478 1 : daemon_local->flags.offlineLogstorageDelimiter = '_';
479 1 : daemon_local->flags.offlineLogstorageMaxCounter = UINT_MAX;
480 1 : daemon_local->flags.offlineLogstorageMaxCounterIdx = 0;
481 1 : daemon_local->flags.offlineLogstorageOptionalCounter = false;
482 1 : daemon_local->flags.offlineLogstorageCacheSize = 30000; /* 30MB */
483 1 : dlt_daemon_logstorage_set_logstorage_cache_size(
484 : daemon_local->flags.offlineLogstorageCacheSize);
485 1 : strncpy(daemon_local->flags.ctrlSockPath,
486 : DLT_DAEMON_DEFAULT_CTRL_SOCK_PATH,
487 : sizeof(daemon_local->flags.ctrlSockPath));
488 : #ifdef DLT_DAEMON_USE_UNIX_SOCKET_IPC
489 : snprintf(daemon_local->flags.appSockPath, DLT_IPC_PATH_MAX, "%s/dlt", DLT_USER_IPC_PATH);
490 :
491 : if (strlen(DLT_USER_IPC_PATH) > DLT_IPC_PATH_MAX)
492 : fprintf(stderr, "Provided path too long...trimming it to path[%s]\n",
493 : daemon_local->flags.appSockPath);
494 :
495 : #else /* DLT_DAEMON_USE_FIFO_IPC */
496 1 : memset(daemon_local->flags.daemonFifoGroup, 0, sizeof(daemon_local->flags.daemonFifoGroup));
497 : #endif
498 1 : daemon_local->flags.gatewayMode = 0;
499 1 : strncpy(daemon_local->flags.gatewayConfigFile,
500 : DLT_GATEWAY_CONFIG_PATH,
501 : DLT_DAEMON_FLAG_MAX);
502 1 : daemon_local->flags.autoResponseGetLogInfoOption = 7;
503 1 : daemon_local->flags.contextLogLevel = DLT_LOG_INFO;
504 1 : daemon_local->flags.contextTraceStatus = DLT_TRACE_STATUS_OFF;
505 1 : daemon_local->flags.enforceContextLLAndTS = 0; /* default is off */
506 : #ifdef UDP_CONNECTION_SUPPORT
507 : daemon_local->UDPConnectionSetup = MULTICAST_CONNECTION_ENABLED;
508 : strncpy(daemon_local->UDPMulticastIPAddress, MULTICASTIPADDRESS, MULTICASTIP_MAX_SIZE - 1);
509 : daemon_local->UDPMulticastIPPort = MULTICASTIPPORT;
510 : #endif
511 1 : daemon_local->flags.ipNodes = NULL;
512 1 : daemon_local->flags.injectionMode = 1;
513 :
514 : /* open configuration file */
515 1 : if (daemon_local->flags.cvalue[0])
516 1 : filename = daemon_local->flags.cvalue;
517 : else
518 : filename = CONFIGURATION_FILES_DIR "/dlt.conf";
519 :
520 : /*printf("Load configuration from file: %s\n",filename); */
521 1 : pFile = fopen (filename, "r");
522 :
523 1 : if (pFile != NULL) {
524 : while (1) {
525 : /* fetch line from configuration file */
526 14 : if (fgets (line, value_length - 1, pFile) != NULL) {
527 6 : pch = strtok (line, " =\r\n");
528 6 : token[0] = 0;
529 6 : value[0] = 0;
530 :
531 11 : while (pch != NULL) {
532 10 : if (strcmp(pch, "#") == 0)
533 : break;
534 :
535 10 : if (token[0] == 0) {
536 5 : strncpy(token, pch, sizeof(token) - 1);
537 5 : token[sizeof(token) - 1] = 0;
538 : }
539 : else {
540 5 : strncpy(value, pch, sizeof(value) - 1);
541 5 : value[sizeof(value) - 1] = 0;
542 5 : break;
543 : }
544 :
545 5 : pch = strtok (NULL, " =\r\n");
546 : }
547 :
548 6 : if (token[0] && value[0]) {
549 : /* parse arguments here */
550 5 : if (strcmp(token, "Verbose") == 0) {
551 0 : daemon_local->flags.vflag = atoi(value);
552 : /*printf("Option: %s=%s\n",token,value); */
553 : }
554 5 : else if (strcmp(token, "PrintASCII") == 0)
555 : {
556 0 : daemon_local->flags.aflag = atoi(value);
557 : /*printf("Option: %s=%s\n",token,value); */
558 : }
559 5 : else if (strcmp(token, "PrintHex") == 0)
560 : {
561 0 : daemon_local->flags.xflag = atoi(value);
562 : /*printf("Option: %s=%s\n",token,value); */
563 : }
564 5 : else if (strcmp(token, "PrintHeadersOnly") == 0)
565 : {
566 0 : daemon_local->flags.sflag = atoi(value);
567 : /*printf("Option: %s=%s\n",token,value); */
568 : }
569 5 : else if (strcmp(token, "SendSerialHeader") == 0)
570 : {
571 0 : daemon_local->flags.lflag = atoi(value);
572 : /*printf("Option: %s=%s\n",token,value); */
573 : }
574 5 : else if (strcmp(token, "SendContextRegistration") == 0)
575 : {
576 0 : daemon_local->flags.rflag = atoi(value);
577 : /*printf("Option: %s=%s\n",token,value); */
578 : }
579 5 : else if (strcmp(token, "SendContextRegistrationOption") == 0)
580 : {
581 0 : daemon_local->flags.autoResponseGetLogInfoOption = atoi(value);
582 : /*printf("Option: %s=%s\n",token,value); */
583 : }
584 5 : else if (strcmp(token, "SendMessageTime") == 0)
585 : {
586 0 : daemon_local->flags.sendMessageTime = atoi(value);
587 : /*printf("Option: %s=%s\n",token,value); */
588 : }
589 5 : else if (strcmp(token, "RS232SyncSerialHeader") == 0)
590 : {
591 0 : daemon_local->flags.mflag = atoi(value);
592 : /*printf("Option: %s=%s\n",token,value); */
593 : }
594 5 : else if (strcmp(token, "TCPSyncSerialHeader") == 0)
595 : {
596 0 : daemon_local->flags.nflag = atoi(value);
597 : /*printf("Option: %s=%s\n",token,value); */
598 : }
599 5 : else if (strcmp(token, "RS232DeviceName") == 0)
600 : {
601 0 : strncpy(daemon_local->flags.yvalue, value, NAME_MAX);
602 0 : daemon_local->flags.yvalue[NAME_MAX] = 0;
603 : /*printf("Option: %s=%s\n",token,value); */
604 : }
605 5 : else if (strcmp(token, "RS232Baudrate") == 0)
606 : {
607 0 : strncpy(daemon_local->flags.bvalue, value, NAME_MAX);
608 0 : daemon_local->flags.bvalue[NAME_MAX] = 0;
609 : /*printf("Option: %s=%s\n",token,value); */
610 : }
611 5 : else if (strcmp(token, "ECUId") == 0)
612 : {
613 0 : strncpy(daemon_local->flags.evalue, value, NAME_MAX);
614 0 : daemon_local->flags.evalue[NAME_MAX] = 0;
615 : /*printf("Option: %s=%s\n",token,value); */
616 : }
617 5 : else if (strcmp(token, "PersistanceStoragePath") == 0)
618 : {
619 0 : strncpy(daemon_local->flags.ivalue, value, NAME_MAX);
620 0 : daemon_local->flags.ivalue[NAME_MAX] = 0;
621 : /*printf("Option: %s=%s\n",token,value); */
622 : }
623 5 : else if (strcmp(token, "LoggingMode") == 0)
624 : {
625 0 : daemon_local->flags.loggingMode = (DltLoggingMode)atoi(value);
626 : /*printf("Option: %s=%s\n",token,value); */
627 : }
628 5 : else if (strcmp(token, "LoggingLevel") == 0)
629 : {
630 0 : daemon_local->flags.loggingLevel = atoi(value);
631 : /*printf("Option: %s=%s\n",token,value); */
632 : }
633 5 : else if (strcmp(token, "LoggingFilename") == 0)
634 : {
635 : strncpy(daemon_local->flags.loggingFilename,
636 : value,
637 : sizeof(daemon_local->flags.loggingFilename) - 1);
638 0 : daemon_local->flags.loggingFilename[sizeof(daemon_local->flags.loggingFilename) - 1] = 0;
639 : /*printf("Option: %s=%s\n",token,value); */
640 : }
641 5 : else if (strcmp(token, "EnableLoggingFileLimit") == 0)
642 : {
643 0 : daemon_local->flags.enableLoggingFileLimit = (bool)atoi(value);
644 : }
645 5 : else if (strcmp(token, "LoggingFileSize") == 0)
646 : {
647 0 : daemon_local->flags.loggingFileSize = atoi(value);
648 : }
649 5 : else if (strcmp(token, "LoggingFileMaxSize") == 0)
650 : {
651 0 : daemon_local->flags.loggingFileMaxSize = atoi(value);
652 : }
653 5 : else if (strcmp(token, "TimeOutOnSend") == 0)
654 : {
655 0 : daemon_local->timeoutOnSend = atoi(value);
656 : /*printf("Option: %s=%s\n",token,value); */
657 : }
658 5 : else if (strcmp(token, "RingbufferMinSize") == 0)
659 : {
660 0 : if (dlt_daemon_check_numeric_setting(token,
661 : value, &(daemon_local->RingbufferMinSize)) < 0) {
662 0 : fclose (pFile);
663 0 : return -1;
664 : }
665 : }
666 5 : else if (strcmp(token, "RingbufferMaxSize") == 0)
667 : {
668 0 : if (dlt_daemon_check_numeric_setting(token,
669 : value, &(daemon_local->RingbufferMaxSize)) < 0) {
670 0 : fclose (pFile);
671 0 : return -1;
672 : }
673 : }
674 5 : else if (strcmp(token, "RingbufferStepSize") == 0)
675 : {
676 0 : if (dlt_daemon_check_numeric_setting(token,
677 : value, &(daemon_local->RingbufferStepSize)) < 0) {
678 0 : fclose (pFile);
679 0 : return -1;
680 : }
681 : }
682 5 : else if (strcmp(token, "SharedMemorySize") == 0)
683 : {
684 0 : daemon_local->flags.sharedMemorySize = atoi(value);
685 : /*printf("Option: %s=%s\n",token,value); */
686 : }
687 5 : else if (strcmp(token, "OfflineTraceDirectory") == 0)
688 : {
689 0 : strncpy(daemon_local->flags.offlineTraceDirectory, value,
690 : sizeof(daemon_local->flags.offlineTraceDirectory) - 1);
691 : daemon_local->flags.offlineTraceDirectory[sizeof(daemon_local->flags.offlineTraceDirectory) -
692 0 : 1] = 0;
693 : /*printf("Option: %s=%s\n",token,value); */
694 : }
695 5 : else if (strcmp(token, "OfflineTraceFileSize") == 0)
696 : {
697 0 : daemon_local->flags.offlineTraceFileSize = atoi(value);
698 : /*printf("Option: %s=%s\n",token,value); */
699 : }
700 5 : else if (strcmp(token, "OfflineTraceMaxSize") == 0)
701 : {
702 0 : daemon_local->flags.offlineTraceMaxSize = atoi(value);
703 : /*printf("Option: %s=%s\n",token,value); */
704 : }
705 5 : else if (strcmp(token, "OfflineTraceFileNameTimestampBased") == 0)
706 : {
707 0 : daemon_local->flags.offlineTraceFilenameTimestampBased = (bool)atoi(value);
708 : /*printf("Option: %s=%s\n",token,value); */
709 : }
710 5 : else if (strcmp(token, "SendECUSoftwareVersion") == 0)
711 : {
712 0 : daemon_local->flags.sendECUSoftwareVersion = atoi(value);
713 : /*printf("Option: %s=%s\n",token,value); */
714 : }
715 5 : else if (strcmp(token, "PathToECUSoftwareVersion") == 0)
716 : {
717 : strncpy(daemon_local->flags.pathToECUSoftwareVersion, value,
718 : sizeof(daemon_local->flags.pathToECUSoftwareVersion) - 1);
719 : daemon_local->flags.pathToECUSoftwareVersion[sizeof(daemon_local->flags.pathToECUSoftwareVersion)
720 0 : - 1] = 0;
721 : /*printf("Option: %s=%s\n",token,value); */
722 : }
723 5 : else if (strcmp(token, "ECUSoftwareVersionFileField") == 0) {
724 : strncpy(daemon_local->flags.ecuSoftwareVersionFileField, value,
725 : sizeof(daemon_local->flags.ecuSoftwareVersionFileField) - 1);
726 : daemon_local->flags.ecuSoftwareVersionFileField[sizeof(daemon_local->flags.ecuSoftwareVersionFileField)
727 0 : - 1] = 0;
728 : /*printf("Option: %s=%s\n",token,value); */
729 : }
730 5 : else if (strcmp(token, "SendTimezone") == 0)
731 : {
732 0 : daemon_local->flags.sendTimezone = atoi(value);
733 : /*printf("Option: %s=%s\n",token,value); */
734 : }
735 5 : else if (strcmp(token, "OfflineLogstorageMaxDevices") == 0)
736 : {
737 1 : daemon_local->flags.offlineLogstorageMaxDevices = (int)strtoul(value, NULL, 10);
738 : }
739 4 : else if (strcmp(token, "OfflineLogstorageDirPath") == 0)
740 : {
741 1 : strncpy(daemon_local->flags.offlineLogstorageDirPath,
742 : value,
743 : sizeof(daemon_local->flags.offlineLogstorageDirPath) - 1);
744 : }
745 3 : else if (strcmp(token, "OfflineLogstorageTimestamp") == 0)
746 : {
747 : /* Check if set to 0, default otherwise */
748 1 : if (atoi(value) == 0)
749 1 : daemon_local->flags.offlineLogstorageTimestamp = 0;
750 : }
751 2 : else if (strcmp(token, "OfflineLogstorageDelimiter") == 0)
752 : {
753 : /* Check if valid punctuation, default otherwise*/
754 0 : if (ispunct((char)value[0]))
755 0 : daemon_local->flags.offlineLogstorageDelimiter = (char)value[0];
756 : }
757 2 : else if (strcmp(token, "OfflineLogstorageMaxCounter") == 0)
758 : {
759 0 : daemon_local->flags.offlineLogstorageMaxCounter = (unsigned int) atoi(value);
760 0 : daemon_local->flags.offlineLogstorageMaxCounterIdx = (unsigned int) strlen(value);
761 2 : } else if (strcmp(token, "OfflineLogstorageOptionalIndex") == 0) {
762 1 : daemon_local->flags.offlineLogstorageOptionalCounter = atoi(value);
763 : }
764 1 : else if (strcmp(token, "OfflineLogstorageCacheSize") == 0)
765 : {
766 0 : daemon_local->flags.offlineLogstorageCacheSize =
767 0 : (unsigned int)atoi(value);
768 0 : dlt_daemon_logstorage_set_logstorage_cache_size(
769 : daemon_local->flags.offlineLogstorageCacheSize);
770 : }
771 1 : else if (strcmp(token, "ControlSocketPath") == 0)
772 : {
773 : memset(
774 : daemon_local->flags.ctrlSockPath,
775 : 0,
776 : DLT_DAEMON_FLAG_MAX);
777 : strncpy(
778 : daemon_local->flags.ctrlSockPath,
779 : value,
780 : DLT_DAEMON_FLAG_MAX - 1);
781 : }
782 0 : else if (strcmp(token, "GatewayMode") == 0)
783 : {
784 0 : daemon_local->flags.gatewayMode = atoi(value);
785 : /*printf("Option: %s=%s\n",token,value); */
786 : }
787 0 : else if (strcmp(token, "GatewayConfigFile") == 0)
788 : {
789 : memset(
790 : daemon_local->flags.gatewayConfigFile,
791 : 0,
792 : DLT_DAEMON_FLAG_MAX);
793 : strncpy(
794 : daemon_local->flags.gatewayConfigFile,
795 : value,
796 : DLT_DAEMON_FLAG_MAX - 1);
797 : }
798 0 : else if (strcmp(token, "ContextLogLevel") == 0)
799 : {
800 : int const intval = atoi(value);
801 :
802 0 : if ((intval >= DLT_LOG_OFF) && (intval <= DLT_LOG_VERBOSE)) {
803 0 : daemon_local->flags.contextLogLevel = intval;
804 : printf("Option: %s=%s\n", token, value);
805 : }
806 : else {
807 0 : fprintf(stderr,
808 : "Invalid value for ContextLogLevel: %i. Must be in range [%i..%i]\n",
809 : intval,
810 : DLT_LOG_OFF,
811 : DLT_LOG_VERBOSE);
812 : }
813 : }
814 0 : else if (strcmp(token, "ContextTraceStatus") == 0)
815 : {
816 : int const intval = atoi(value);
817 :
818 0 : if ((intval >= DLT_TRACE_STATUS_OFF) && (intval <= DLT_TRACE_STATUS_ON)) {
819 0 : daemon_local->flags.contextTraceStatus = intval;
820 : printf("Option: %s=%s\n", token, value);
821 : }
822 : else {
823 0 : fprintf(stderr,
824 : "Invalid value for ContextTraceStatus: %i. Must be in range [%i..%i]\n",
825 : intval,
826 : DLT_TRACE_STATUS_OFF,
827 : DLT_TRACE_STATUS_ON);
828 : }
829 : }
830 0 : else if (strcmp(token, "ForceContextLogLevelAndTraceStatus") == 0)
831 : {
832 : int const intval = atoi(value);
833 :
834 0 : if ((intval >= 0) && (intval <= 1)) {
835 0 : daemon_local->flags.enforceContextLLAndTS = intval;
836 : printf("Option: %s=%s\n", token, value);
837 : }
838 : else {
839 0 : fprintf(stderr,
840 : "Invalid value for ForceContextLogLevelAndTraceStatus: %i. Must be 0, 1\n",
841 : intval);
842 : }
843 : }
844 :
845 : #ifdef DLT_DAEMON_USE_FIFO_IPC
846 0 : else if (strcmp(token, "DaemonFIFOSize") == 0)
847 : {
848 0 : if (dlt_daemon_check_numeric_setting(token,
849 : value, &(daemon_local->daemonFifoSize)) < 0) {
850 0 : fclose (pFile);
851 0 : return -1;
852 : }
853 : #ifndef __linux__
854 : printf("Option DaemonFIFOSize is set but only supported on Linux. Ignored.\n");
855 : #endif
856 : }
857 0 : else if (strcmp(token, "DaemonFifoGroup") == 0)
858 : {
859 : strncpy(daemon_local->flags.daemonFifoGroup, value, NAME_MAX);
860 0 : daemon_local->flags.daemonFifoGroup[NAME_MAX] = 0;
861 : }
862 : #endif
863 : #ifdef UDP_CONNECTION_SUPPORT
864 : else if (strcmp(token, "UDPConnectionSetup") == 0)
865 : {
866 : const long longval = strtol(value, NULL, 10);
867 :
868 : if ((longval == MULTICAST_CONNECTION_DISABLED)
869 : || (longval == MULTICAST_CONNECTION_ENABLED)) {
870 : daemon_local->UDPConnectionSetup = (int)longval;
871 : printf("Option: %s=%s\n", token, value);
872 : }
873 : else {
874 : daemon_local->UDPConnectionSetup = MULTICAST_CONNECTION_DISABLED;
875 : fprintf(stderr,
876 : "Invalid value for UDPConnectionSetup set to default %ld\n",
877 : longval);
878 : }
879 : }
880 : else if (strcmp(token, "UDPMulticastIPAddress") == 0)
881 : {
882 : strncpy(daemon_local->UDPMulticastIPAddress, value,
883 : MULTICASTIP_MAX_SIZE - 1);
884 : }
885 : else if (strcmp(token, "UDPMulticastIPPort") == 0)
886 : {
887 : daemon_local->UDPMulticastIPPort = (int)strtol(value, NULL, 10);
888 : }
889 : #endif
890 0 : else if (strcmp(token, "BindAddress") == 0)
891 : {
892 : DltBindAddress_t *newNode = NULL;
893 : DltBindAddress_t *temp = NULL;
894 :
895 0 : char *tok = strtok(value, ",;");
896 :
897 0 : if (tok != NULL) {
898 0 : daemon_local->flags.ipNodes = calloc(1, sizeof(DltBindAddress_t));
899 :
900 0 : if (daemon_local->flags.ipNodes == NULL) {
901 0 : dlt_vlog(LOG_ERR, "Could not allocate for IP list\n");
902 0 : fclose(pFile);
903 0 : return -1;
904 : }
905 : else {
906 0 : strncpy(daemon_local->flags.ipNodes->ip,
907 : tok,
908 : sizeof(daemon_local->flags.ipNodes->ip) - 1);
909 0 : daemon_local->flags.ipNodes->next = NULL;
910 : temp = daemon_local->flags.ipNodes;
911 :
912 0 : tok = strtok(NULL, ",;");
913 :
914 0 : while (tok != NULL) {
915 0 : newNode = calloc(1, sizeof(DltBindAddress_t));
916 :
917 0 : if (newNode == NULL) {
918 0 : dlt_vlog(LOG_ERR, "Could not allocate for IP list\n");
919 0 : fclose(pFile);
920 0 : return -1;
921 : }
922 : else {
923 0 : strncpy(newNode->ip, tok, sizeof(newNode->ip) - 1);
924 : }
925 :
926 0 : temp->next = newNode;
927 : temp = temp->next;
928 0 : tok = strtok(NULL, ",;");
929 : }
930 : }
931 : }
932 : else {
933 0 : dlt_vlog(LOG_WARNING, "BindAddress option is empty\n");
934 : }
935 : }
936 0 : else if (strcmp(token, "InjectionMode") == 0) {
937 0 : daemon_local->flags.injectionMode = atoi(value);
938 : }
939 : else {
940 0 : fprintf(stderr, "Unknown option: %s=%s\n", token, value);
941 : }
942 : }
943 : }
944 : else {
945 : break;
946 : }
947 : }
948 :
949 1 : fclose (pFile);
950 : }
951 : else {
952 0 : fprintf(stderr, "Cannot open configuration file: %s\n", filename);
953 : }
954 :
955 : return 0;
956 : }
957 :
958 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
959 : /**
960 : * Load configuration file parser
961 : */
962 :
963 : static int compare_app_id_conf(const void *lhs, const void *rhs)
964 : {
965 : return strncmp(((DltDaemonContextLogSettings *)lhs)->apid,
966 : ((DltDaemonContextLogSettings *)rhs)->apid, DLT_ID_SIZE);
967 : }
968 :
969 : int app_id_default_log_level_config_parser(DltDaemon *daemon,
970 : DltDaemonLocal *daemon_local) {
971 : FILE *pFile;
972 : char line[value_length - 1];
973 : char app_id_value[value_length];
974 : char ctx_id_value[value_length];
975 : DltLogLevelType log_level_value;
976 :
977 : char *pch;
978 : const char *filename;
979 :
980 : /* open configuration file */
981 : filename = daemon_local->flags.avalue[0]
982 : ? daemon_local->flags.avalue
983 : : CONFIGURATION_FILES_DIR "/dlt-log-levels.conf";
984 :
985 : pFile = fopen(filename, "r");
986 : if (pFile == NULL) {
987 : dlt_vlog(LOG_WARNING, "Cannot open app log level configuration%s\n", filename);
988 : return -errno;
989 : }
990 :
991 : /* fetch lines from configuration file */
992 : while (fgets(line, value_length - 1, pFile) != NULL) {
993 : pch = strtok(line, " \t");
994 : memset(app_id_value, 0, value_length);
995 : memset(ctx_id_value, 0, value_length);
996 : log_level_value = DLT_LOG_MAX;
997 :
998 : /* ignore comments and new lines*/
999 : if (strncmp(pch, "#", 1) == 0 || strncmp(pch, "\n", 1) == 0
1000 : || strlen(pch) < 1)
1001 : continue;
1002 :
1003 : strncpy(app_id_value, pch, sizeof(app_id_value) - 1);
1004 : app_id_value[sizeof(app_id_value) - 1] = 0;
1005 : if (strlen(app_id_value) == 0 || strlen(app_id_value) > DLT_ID_SIZE) {
1006 : if (app_id_value[strlen(app_id_value) - 1] == '\n') {
1007 : dlt_vlog(LOG_WARNING, "Missing log level for apid %s in log settings\n",
1008 : app_id_value);
1009 : } else {
1010 : dlt_vlog(LOG_WARNING,
1011 : "Invalid apid for log settings settings: app id: %s\n",
1012 : app_id_value);
1013 : }
1014 :
1015 : continue;
1016 : }
1017 :
1018 : char *pch_next1 = strtok(NULL, " \t");
1019 : char *pch_next2 = strtok(NULL, " \t");
1020 : char *log_level;
1021 : /* no context id given, log level is next token */
1022 : if (pch_next2 == NULL || pch_next2[0] == '#') {
1023 : log_level = pch_next1;
1024 : } else {
1025 : /* context id is given, log level is second to next token */
1026 : log_level = pch_next2;
1027 :
1028 : /* next token is token id */
1029 : strncpy(ctx_id_value, pch_next1, sizeof(ctx_id_value) - 1);
1030 : if (strlen(ctx_id_value) == 0 || strlen(app_id_value) > DLT_ID_SIZE) {
1031 : dlt_vlog(LOG_WARNING,
1032 : "Invalid ctxid for log settings: app id: %s "
1033 : "(skipping line)\n",
1034 : app_id_value);
1035 : continue;
1036 : }
1037 :
1038 : ctx_id_value[sizeof(ctx_id_value) - 1] = 0;
1039 : }
1040 :
1041 : errno = 0;
1042 : log_level_value = strtol(log_level, NULL, 10);
1043 : if (errno != 0 || log_level_value >= DLT_LOG_MAX ||
1044 : log_level_value <= DLT_LOG_DEFAULT) {
1045 : dlt_vlog(LOG_WARNING,
1046 : "Invalid log level (%i), app id %s, conversion error: %s\n",
1047 : log_level_value, app_id_value, strerror(errno));
1048 : continue;
1049 : }
1050 :
1051 : DltDaemonContextLogSettings *settings =
1052 : dlt_daemon_find_configured_app_id_ctx_id_settings(
1053 : daemon, app_id_value, NULL);
1054 :
1055 : if (settings != NULL &&
1056 : strncmp(settings->ctid, ctx_id_value, DLT_ID_SIZE) == 0) {
1057 : if (strlen(ctx_id_value) > 0) {
1058 : dlt_vlog(LOG_WARNING,
1059 : "Appid %s with ctxid %s is already configured, skipping "
1060 : "duplicated entry\n",
1061 : app_id_value, ctx_id_value);
1062 : } else {
1063 : dlt_vlog(LOG_WARNING,
1064 : "Appid %s is already configured, skipping duplicated entry\n",
1065 : app_id_value);
1066 : }
1067 :
1068 : continue;
1069 : }
1070 :
1071 : /* allocate one more element in the trace load settings */
1072 : DltDaemonContextLogSettings *tmp =
1073 : realloc(daemon->app_id_log_level_settings,
1074 : (++daemon->num_app_id_log_level_settings) *
1075 : sizeof(DltDaemonContextLogSettings));
1076 :
1077 : if (tmp == NULL) {
1078 : dlt_log(LOG_CRIT, "Failed to allocate memory for app load settings\n");
1079 : continue;
1080 : }
1081 :
1082 : daemon->app_id_log_level_settings = tmp;
1083 :
1084 : /* update newly created entry */
1085 : settings = &daemon->app_id_log_level_settings
1086 : [daemon->num_app_id_log_level_settings -1];
1087 :
1088 : memset(settings, 0, sizeof(DltDaemonContextLogSettings));
1089 : memcpy(settings->apid, app_id_value, DLT_ID_SIZE);
1090 : memcpy(settings->ctid, ctx_id_value, DLT_ID_SIZE);
1091 : settings->log_level = log_level_value;
1092 :
1093 : /* make sure ids are 0 terminated for printing */
1094 : char apid_buf[DLT_ID_SIZE + 1] = {0};
1095 : char ctid_buf[DLT_ID_SIZE + 1] = {0};
1096 : memcpy(apid_buf, app_id_value, DLT_ID_SIZE);
1097 : memcpy(ctid_buf, ctx_id_value, DLT_ID_SIZE);
1098 :
1099 : /* log with or without context id */
1100 : if (strlen(ctid_buf) > 0) {
1101 : dlt_vlog(
1102 : LOG_INFO,
1103 : "Configured trace limits for app id %s, context id %s, level %u\n",
1104 : apid_buf, ctid_buf, log_level_value);
1105 : } else {
1106 : dlt_vlog(LOG_INFO, "Configured trace limits for app id %s, level %u\n",
1107 : apid_buf, log_level_value);
1108 : }
1109 :
1110 : } /* while */
1111 : fclose(pFile);
1112 :
1113 : /* list must be sorted to speed up dlt_daemon_find_configured_app_id_ctx_id_settings */
1114 : qsort(daemon->app_id_log_level_settings,
1115 : daemon->num_app_id_log_level_settings,
1116 : sizeof(DltDaemonContextLogSettings), compare_app_id_conf);
1117 :
1118 : return 0;
1119 : }
1120 : #endif
1121 :
1122 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
1123 : static bool is_ascii_only(const char *str) {
1124 : while (*str) {
1125 : if ((unsigned char)*str > 127) {
1126 : return false;
1127 : }
1128 : str++;
1129 : }
1130 : return true;
1131 : }
1132 :
1133 : /**
1134 : * Load configuration file parser
1135 : */
1136 : int trace_load_config_file_parser(DltDaemon *daemon, DltDaemonLocal *daemon_local)
1137 : {
1138 : FILE *pFile;
1139 : const int max_tokens = 4;
1140 : const int min_tokens = 3;
1141 : char tokens[max_tokens][value_length];
1142 : char line[value_length - 1];
1143 : char app_id_value[value_length];
1144 : char ctx_id_value[value_length];
1145 : char soft_limit_value[value_length];
1146 : char hard_limit_value[value_length];
1147 : int i;
1148 : uint32_t soft_limit;
1149 : uint32_t hard_limit;
1150 :
1151 : char *pch;
1152 : const char *filename;
1153 :
1154 : bool skipped;
1155 :
1156 : if (daemon->preconfigured_trace_load_settings != NULL) {
1157 : free(daemon->preconfigured_trace_load_settings);
1158 : daemon->preconfigured_trace_load_settings = NULL;
1159 : daemon->preconfigured_trace_load_settings_count = 0;
1160 : }
1161 : daemon->preconfigured_trace_load_settings = malloc(sizeof(DltTraceLoadSettings));
1162 : if (daemon->preconfigured_trace_load_settings == NULL) {
1163 : dlt_log(LOG_CRIT, "Failed to allocate memory for trace load settings\n");
1164 : return DLT_RETURN_ERROR;
1165 : }
1166 :
1167 : /* open configuration file */
1168 : filename = daemon_local->flags.lvalue[0]
1169 : ? daemon_local->flags.lvalue
1170 : : CONFIGURATION_FILES_DIR "/dlt-trace-load.conf";
1171 :
1172 : pFile = fopen (filename, "r");
1173 : if (pFile == NULL) {
1174 : dlt_vlog(LOG_WARNING, "Cannot open trace load configuration file: %s\n",
1175 : filename);
1176 : return -errno;
1177 : }
1178 :
1179 : while (1) {
1180 : /* fetch line from configuration file */
1181 : if (fgets(line, value_length - 1, pFile) == NULL) {
1182 : break;
1183 : }
1184 :
1185 : pch = strtok(line, " ");
1186 : app_id_value[0] = 0;
1187 : ctx_id_value[0] = 0;
1188 : soft_limit_value[0] = 0;
1189 : hard_limit_value[0] = 0;
1190 : soft_limit = 0U;
1191 : hard_limit = 0U;
1192 : memset(tokens, 0, sizeof(tokens));
1193 : i = 0;
1194 :
1195 : skipped = false;
1196 : while (pch != NULL && i < max_tokens) {
1197 : /* ignore comments, empty lines and new lines */
1198 : if (strncmp(pch, "#", 1) == 0 || strncmp(pch, "\n", 1) == 0 ||
1199 : strncmp(pch, "\r", 1) == 0 || strncmp(pch, " ", 1) == 0) {
1200 : skipped = true;
1201 : break;
1202 : }
1203 : strncpy(tokens[i], pch, sizeof(tokens[i]) - 1);
1204 : pch = strtok(NULL, " ");
1205 : ++i;
1206 : }
1207 :
1208 : if (skipped && i < min_tokens)
1209 : continue;
1210 :
1211 : if (pch != NULL
1212 : && (pch[0] != '\n')
1213 : && (pch[0] != '\t')
1214 : && (pch[0] != ' ')
1215 : && (pch[0] != '#')) {
1216 : dlt_vlog(LOG_WARNING,
1217 : "Invalid trace load settings: too many tokens in line '%s'\n", line);
1218 : continue;
1219 : }
1220 :
1221 : bool has_ctx_id = i == max_tokens;
1222 : int soft_limit_idx = has_ctx_id ? 2 : 1;
1223 : int hard_limit_idx = has_ctx_id ? 3 : 2;
1224 :
1225 : strncpy(app_id_value, tokens[0], sizeof(app_id_value) - 1);
1226 : if ((strlen(app_id_value) == 0)
1227 : || (strlen(app_id_value) > DLT_ID_SIZE)
1228 : || (!is_ascii_only(app_id_value))) {
1229 : dlt_vlog(LOG_WARNING,
1230 : "Invalid apid for trace load settings: app id: '%s'\n", app_id_value);
1231 : continue;
1232 : }
1233 :
1234 : if (has_ctx_id) {
1235 : strncpy(ctx_id_value, tokens[1], sizeof(ctx_id_value) - 1);
1236 : if ((strlen(ctx_id_value) == 0)
1237 : || (strlen(ctx_id_value) > DLT_ID_SIZE)
1238 : || (!is_ascii_only(ctx_id_value))) {
1239 : dlt_vlog(LOG_WARNING,
1240 : "Invalid ctid for trace load settings: context id: '%s'\n", ctx_id_value);
1241 : continue;
1242 : }
1243 : }
1244 :
1245 : if (strlen(tokens[soft_limit_idx]) == 0) {
1246 : dlt_vlog(LOG_WARNING,
1247 : "Invalid soft_limit for trace load settings: app id: '%.4s', '%s'\n",
1248 : app_id_value, tokens[soft_limit_idx]);
1249 : continue;
1250 : }
1251 :
1252 : if (strlen(tokens[hard_limit_idx]) == 0) {
1253 : dlt_vlog(LOG_WARNING,
1254 : "Invalid hard_limit for trace load settings: app id: '%.4s', '%s'\n",
1255 : app_id_value, tokens[hard_limit_idx]);
1256 : continue;
1257 : }
1258 :
1259 : strncpy(soft_limit_value, tokens[soft_limit_idx],
1260 : sizeof(soft_limit_value) - 1);
1261 : strncpy(hard_limit_value, tokens[hard_limit_idx],
1262 : sizeof(hard_limit_value) - 1);
1263 :
1264 : errno = 0;
1265 : char *endptr;
1266 : endptr = NULL;
1267 : soft_limit = strtoul(soft_limit_value, &endptr, 10);
1268 : if ((errno != 0)
1269 : || ((soft_limit == 0) && (soft_limit_value[0] != '0'))
1270 : || (soft_limit_value[0] == '-')
1271 : || ((*endptr != '\n') && (*endptr != '\0'))) {
1272 : dlt_vlog(LOG_WARNING,
1273 : "Invalid soft_limit for trace load settings: app id: '%.4s', soft_limit '%s'\n",
1274 : app_id_value, soft_limit_value);
1275 : continue;
1276 : }
1277 :
1278 : errno = 0;
1279 : endptr = NULL;
1280 : hard_limit = strtoul(hard_limit_value, &endptr, 10);
1281 : if ((errno != 0)
1282 : || ((hard_limit == 0) && (hard_limit_value[0] != '0'))
1283 : || (hard_limit_value[0] == '-')
1284 : || ((*endptr != '\n') && (*endptr != '\0'))) {
1285 : dlt_vlog(LOG_WARNING,
1286 : "Invalid hard_limit for trace load settings: app id: '%.4s', hard_limit '%s'\n",
1287 : app_id_value, hard_limit_value);
1288 : continue;
1289 : }
1290 :
1291 : if (soft_limit > hard_limit) {
1292 : dlt_vlog(LOG_WARNING,
1293 : "Invalid trace load settings: app id: '%.4s', soft limit %u is greater than hard limit %u\n",
1294 : app_id_value, soft_limit, hard_limit);
1295 : continue;
1296 : }
1297 :
1298 : DltTraceLoadSettings *settings = NULL;
1299 : int num_settings = 0;
1300 : DltReturnValue find_trace_settings_return_value = dlt_daemon_find_preconfigured_trace_load_settings(
1301 : daemon, app_id_value, ctx_id_value, &settings, &num_settings,
1302 : 0);
1303 : if (find_trace_settings_return_value != DLT_RETURN_OK || num_settings != 0) {
1304 : dlt_vlog(LOG_WARNING,
1305 : "App id '%.4s' is already configured, or an error occurred, skipping entry\n",
1306 : app_id_value);
1307 : if (settings != NULL) {
1308 : free(settings);
1309 : }
1310 : continue;
1311 : }
1312 :
1313 : /* allocate one more element in the trace load settings */
1314 : DltTraceLoadSettings *tmp =
1315 : realloc(daemon->preconfigured_trace_load_settings,
1316 : (++daemon->preconfigured_trace_load_settings_count) *
1317 : sizeof(DltTraceLoadSettings));
1318 :
1319 : if (tmp == NULL) {
1320 : dlt_log(LOG_CRIT,
1321 : "Failed to allocate memory for trace load settings\n");
1322 : return DLT_RETURN_ERROR;
1323 : }
1324 :
1325 : daemon->preconfigured_trace_load_settings = tmp;
1326 : settings = &daemon->preconfigured_trace_load_settings
1327 : [daemon->preconfigured_trace_load_settings_count - 1];
1328 : memset(settings, 0, sizeof(DltTraceLoadSettings));
1329 : settings->soft_limit = soft_limit;
1330 : settings->hard_limit = hard_limit;
1331 :
1332 : memcpy(settings->apid, app_id_value, DLT_ID_SIZE);
1333 : if (has_ctx_id) {
1334 : memcpy(settings->ctid, ctx_id_value, DLT_ID_SIZE);
1335 : dlt_vlog(LOG_INFO,
1336 : "Configured trace limits for app id '%.4s', ctx id '%.4s', soft limit: %u, hard_limit: %u\n",
1337 : app_id_value, ctx_id_value, soft_limit, hard_limit);
1338 : } else {
1339 : dlt_vlog(LOG_INFO,
1340 : "Configured trace limits for app id '%.4s', soft limit: %u, hard_limit: %u\n",
1341 : app_id_value, soft_limit, hard_limit);
1342 : }
1343 :
1344 :
1345 :
1346 : } /* while */
1347 : fclose(pFile);
1348 :
1349 : // sort limits to improve search performance
1350 : qsort(daemon->preconfigured_trace_load_settings, daemon->preconfigured_trace_load_settings_count,
1351 : sizeof(DltTraceLoadSettings),
1352 : dlt_daemon_compare_trace_load_settings);
1353 : return 0;
1354 : }
1355 : #endif
1356 :
1357 1 : static int dlt_mkdir_recursive(const char *dir)
1358 : {
1359 : int ret = 0;
1360 : char tmp[PATH_MAX + 1];
1361 : char *p = NULL;
1362 : char *end = NULL;
1363 : size_t len;
1364 :
1365 : strncpy(tmp, dir, PATH_MAX);
1366 1 : len = strlen(tmp);
1367 :
1368 1 : if (tmp[len - 1] == '/')
1369 0 : tmp[len - 1] = 0;
1370 :
1371 1 : end = tmp + len;
1372 :
1373 4 : for (p = tmp + 1; ((*p) && (ret == 0)) || ((ret == -1 && errno == EEXIST) && (p != end)); p++)
1374 3 : if (*p == '/') {
1375 0 : *p = 0;
1376 :
1377 0 : if (access(tmp, F_OK) != 0 && errno == ENOENT) {
1378 0 : ret = mkdir(tmp,
1379 : #ifdef DLT_DAEMON_USE_FIFO_IPC
1380 : S_IRWXU);
1381 : #else
1382 : S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH /*S_IRWXU*/);
1383 : #endif
1384 : }
1385 :
1386 0 : *p = '/';
1387 : }
1388 :
1389 :
1390 :
1391 1 : if ((ret == 0) || ((ret == -1) && (errno == EEXIST)))
1392 1 : ret = mkdir(tmp,
1393 : #ifdef DLT_DAEMON_USE_FIFO_IPC
1394 : S_IRWXU);
1395 : #else
1396 : S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH /*S_IRWXU*/);
1397 : #endif
1398 :
1399 1 : if ((ret == -1) && (errno == EEXIST))
1400 : ret = 0;
1401 :
1402 1 : return ret;
1403 : }
1404 :
1405 : #ifdef DLT_DAEMON_USE_FIFO_IPC
1406 1 : static DltReturnValue dlt_daemon_create_pipes_dir(char *dir)
1407 : {
1408 : int ret = DLT_RETURN_OK;
1409 :
1410 1 : if (dir == NULL) {
1411 0 : dlt_vlog(LOG_ERR, "%s: Invalid parameter\n", __func__);
1412 0 : return DLT_RETURN_WRONG_PARAMETER;
1413 : }
1414 :
1415 : /* create dlt pipes directory */
1416 1 : ret = mkdir(dir,
1417 : S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_ISVTX);
1418 :
1419 1 : if ((ret == -1) && (errno != EEXIST)) {
1420 0 : dlt_vlog(LOG_ERR,
1421 : "FIFO user dir %s cannot be created (%s)!\n",
1422 : dir,
1423 : strerror(errno));
1424 :
1425 0 : return DLT_RETURN_ERROR;
1426 : }
1427 :
1428 : /* S_ISGID cannot be set by mkdir, let's reassign right bits */
1429 1 : ret = chmod(dir,
1430 : S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH | S_ISGID |
1431 : S_ISVTX);
1432 :
1433 1 : if (ret == -1) {
1434 0 : dlt_vlog(LOG_ERR,
1435 : "FIFO user dir %s cannot be chmoded (%s)!\n",
1436 : dir,
1437 0 : strerror(errno));
1438 :
1439 0 : return DLT_RETURN_ERROR;
1440 : }
1441 :
1442 : return ret;
1443 : }
1444 : #endif
1445 : // This will be defined when unit testing, so functions
1446 : // from this file can be tested without defining main twice
1447 : #ifndef DLT_DAEMON_UNIT_TESTS_NO_MAIN
1448 : /**
1449 : * Main function of tool.
1450 : */
1451 1 : int main(int argc, char *argv[])
1452 : {
1453 : char version[DLT_DAEMON_TEXTBUFSIZE];
1454 : char local_str[DLT_DAEMON_TEXTBUFSIZE];
1455 : DltDaemonLocal daemon_local;
1456 : DltDaemon daemon;
1457 : int back = 0;
1458 :
1459 : memset(&daemon_local, 0, sizeof(DltDaemonLocal));
1460 : memset(&daemon, 0, sizeof(DltDaemon));
1461 :
1462 : /* Command line option handling */
1463 1 : if ((back = option_handling(&daemon_local, argc, argv)) < 0) {
1464 0 : if (back != -2)
1465 0 : fprintf (stderr, "option_handling() failed!\n");
1466 :
1467 0 : return -1;
1468 : }
1469 :
1470 : /* Set protocol version from option, fallback to env or default */
1471 1 : if (daemon_local.flags.protocolVersion == DLTProtocolV1 || daemon_local.flags.protocolVersion == DLTProtocolV2) {
1472 0 : daemon.daemon_version = daemon_local.flags.protocolVersion;
1473 : } else {
1474 1 : daemon.daemon_version = DLTProtocolV1;
1475 : }
1476 :
1477 : /* Configuration file option handling */
1478 1 : if ((back = option_file_parser(&daemon_local)) < 0) {
1479 0 : if (back != -2)
1480 0 : fprintf (stderr, "option_file_parser() failed!\n");
1481 :
1482 0 : return -1;
1483 : }
1484 :
1485 : /* Initialize internal logging facility */
1486 1 : dlt_log_set_filename(daemon_local.flags.loggingFilename);
1487 1 : dlt_log_set_level(daemon_local.flags.loggingLevel);
1488 : DltReturnValue log_init_result =
1489 1 : dlt_log_init_multiple_logfiles_support(daemon_local.flags.loggingMode,
1490 1 : daemon_local.flags.enableLoggingFileLimit,
1491 : daemon_local.flags.loggingFileSize,
1492 : daemon_local.flags.loggingFileMaxSize);
1493 :
1494 1 : if (log_init_result != DLT_RETURN_OK) {
1495 0 : fprintf(stderr, "Failed to init internal logging\n");
1496 :
1497 : #ifdef WITH_DLT_FILE_LOGGING_SYSLOG_FALLBACK
1498 : if (daemon_local.flags.loggingMode == DLT_LOG_TO_FILE) {
1499 : fprintf(stderr, "Falling back to syslog mode\n");
1500 :
1501 : daemon_local.flags.loggingMode = DLT_LOG_TO_SYSLOG;
1502 : log_init_result = dlt_log_init(daemon_local.flags.loggingMode);
1503 : if (log_init_result != DLT_RETURN_OK) {
1504 : fprintf(stderr, "Failed to setup syslog logging, internal logs will "
1505 : "not be available\n");
1506 : }
1507 : }
1508 : #endif
1509 : }
1510 :
1511 : /* Print version information */
1512 1 : dlt_get_version(version, DLT_DAEMON_TEXTBUFSIZE);
1513 :
1514 1 : dlt_vlog(LOG_NOTICE, "Starting DLT Daemon; %s\n", version);
1515 1 : if (daemon.daemon_version == 2) {
1516 0 : dlt_vlog(LOG_INFO, "DLT protocol version: 2 (DLTv2)\n");
1517 : } else {
1518 1 : dlt_vlog(LOG_INFO, "DLT protocol version: 1 (DLTv1)\n");
1519 : }
1520 :
1521 1 : PRINT_FUNCTION_VERBOSE(daemon_local.flags.vflag);
1522 :
1523 : /* Make sure the parent user directory is created */
1524 : const char *dir_to_create;
1525 : #ifdef DLT_DAEMON_USE_FIFO_IPC
1526 : dir_to_create = dltFifoBaseDir;
1527 : #else
1528 : dir_to_create = DLT_USER_IPC_PATH;
1529 : #endif
1530 1 : if (dlt_mkdir_recursive(dir_to_create) != 0) {
1531 0 : dlt_vlog(LOG_ERR, "Base dir %s cannot be created!\n", dir_to_create);
1532 0 : return -1;
1533 : }
1534 :
1535 : /* --- Daemon init phase 1 begin --- */
1536 1 : if (dlt_daemon_local_init_p1(&daemon, &daemon_local, daemon_local.flags.vflag) == -1) {
1537 0 : dlt_log(LOG_CRIT, "Initialization of phase 1 failed!\n");
1538 0 : return -1;
1539 : }
1540 :
1541 : /* --- Daemon init phase 1 end --- */
1542 :
1543 1 : if (dlt_daemon_prepare_event_handling(&daemon_local.pEvent)) {
1544 : /* TODO: Perform clean-up */
1545 0 : dlt_log(LOG_CRIT, "Initialization of event handling failed!\n");
1546 0 : return -1;
1547 : }
1548 :
1549 : /* --- Daemon connection init begin */
1550 1 : if (dlt_daemon_local_connection_init(&daemon, &daemon_local, daemon_local.flags.vflag) == -1) {
1551 0 : dlt_log(LOG_CRIT, "Initialization of local connections failed!\n");
1552 0 : return -1;
1553 : }
1554 :
1555 : /* --- Daemon connection init end */
1556 :
1557 1 : if (dlt_daemon_init_runtime_configuration(&daemon, daemon_local.flags.ivalue, daemon_local.flags.vflag) == -1) {
1558 0 : dlt_log(LOG_ERR, "Could not load runtime config\n");
1559 0 : return -1;
1560 : }
1561 :
1562 : /*
1563 : * Load dlt-runtime.cfg if available.
1564 : * This must be loaded before offline setup
1565 : */
1566 1 : dlt_daemon_configuration_load(&daemon, daemon.runtime_configuration, daemon_local.flags.vflag);
1567 :
1568 : /* --- Daemon init phase 2 begin --- */
1569 1 : if (dlt_daemon_local_init_p2(&daemon, &daemon_local, daemon_local.flags.vflag) == -1) {
1570 0 : dlt_log(LOG_CRIT, "Initialization of phase 2 failed!\n");
1571 0 : return -1;
1572 : }
1573 :
1574 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
1575 : /* Load control app id level configuration file without setting `back` to
1576 : * prevent exit if file is missing */
1577 : if (app_id_default_log_level_config_parser(&daemon, &daemon_local) < 0) {
1578 : dlt_vlog(LOG_WARNING, "app_id_default_log_level_config_parser() failed, "
1579 : "no app specific log levels will be configured\n");
1580 : }
1581 : #endif
1582 :
1583 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
1584 : /* Load control trace load configuration file without setting `back` to prevent exit if file is missing */
1585 : pthread_rwlock_init(&trace_load_rw_lock, NULL);
1586 : if (trace_load_config_file_parser(&daemon, &daemon_local) < 0) {
1587 : dlt_vlog(LOG_WARNING, "trace_load_config_file_parser() failed, using defaults for all app ids!\n");
1588 : }
1589 : #endif
1590 :
1591 : /* --- Daemon init phase 2 end --- */
1592 :
1593 1 : if (daemon_local.flags.offlineLogstorageDirPath[0])
1594 1 : if (dlt_daemon_logstorage_setup_internal_storage(
1595 : &daemon,
1596 : &daemon_local,
1597 : daemon_local.flags.offlineLogstorageDirPath,
1598 : daemon_local.flags.vflag) == -1)
1599 0 : dlt_log(LOG_INFO,
1600 : "Setting up internal offline log storage failed!\n");
1601 :
1602 : /* create fd for watchdog */
1603 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
1604 : {
1605 : char *watchdogUSec = getenv("WATCHDOG_USEC");
1606 : // set a sensible default, in case the environment variable is not set
1607 : int watchdogTimeoutSeconds = 30;
1608 :
1609 : dlt_log(LOG_DEBUG, "Systemd watchdog initialization\n");
1610 :
1611 : if (watchdogUSec) {
1612 : // WATCHDOG_USEC is the timeout in micrsoseconds
1613 : // divide this by 2*10^6 to get the interval in seconds
1614 : // 2 * because we notify systemd after half the timeout
1615 : watchdogTimeoutSeconds = atoi(watchdogUSec) / 2000000;
1616 : }
1617 :
1618 : if (watchdogTimeoutSeconds == 0) {
1619 : dlt_log(LOG_WARNING, "Watchdog timeout is too small, need at least 1s, setting 30s timeout\n");
1620 : watchdogTimeoutSeconds = 30;
1621 : }
1622 :
1623 : daemon.watchdog_trigger_interval = watchdogTimeoutSeconds;
1624 : daemon.watchdog_last_trigger_time = 0U;
1625 : create_timer_fd(&daemon_local,
1626 : watchdogTimeoutSeconds,
1627 : watchdogTimeoutSeconds,
1628 : DLT_TIMER_SYSTEMD);
1629 : }
1630 : #endif
1631 :
1632 : /* create fd for timer timing packets */
1633 1 : create_timer_fd(&daemon_local, 1, 1, DLT_TIMER_PACKET);
1634 :
1635 : /* create fd for timer ecu version */
1636 1 : if ((daemon_local.flags.sendECUSoftwareVersion > 0) ||
1637 1 : (daemon_local.flags.sendTimezone > 0))
1638 0 : create_timer_fd(&daemon_local, 60, 60, DLT_TIMER_ECU);
1639 :
1640 : /* initiate gateway */
1641 1 : if (daemon_local.flags.gatewayMode == 1) {
1642 0 : if (dlt_gateway_init(&daemon_local, daemon_local.flags.vflag) == -1) {
1643 0 : dlt_log(LOG_CRIT, "Failed to create gateway\n");
1644 0 : return -1;
1645 : }
1646 :
1647 : /* create gateway timer */
1648 0 : create_timer_fd(&daemon_local,
1649 : daemon_local.pGateway.interval,
1650 : daemon_local.pGateway.interval,
1651 : DLT_TIMER_GATEWAY);
1652 : }
1653 :
1654 : /* For offline tracing we still can use the same states */
1655 : /* as for socket sending. Using this trick we see the traces */
1656 : /* In the offline trace AND in the socket stream. */
1657 1 : if (daemon_local.flags.yvalue[0])
1658 0 : dlt_daemon_change_state(&daemon, DLT_DAEMON_STATE_SEND_DIRECT);
1659 : else
1660 1 : dlt_daemon_change_state(&daemon, DLT_DAEMON_STATE_BUFFER);
1661 :
1662 1 : dlt_daemon_init_user_information(&daemon,
1663 : &daemon_local.pGateway,
1664 : daemon_local.flags.gatewayMode,
1665 : daemon_local.flags.vflag);
1666 :
1667 : /*
1668 : * Check for app and ctx runtime cfg.
1669 : * These cfg must be loaded after ecuId and num_user_lists are available
1670 : */
1671 1 : if ((dlt_daemon_applications_load(&daemon, daemon.runtime_application_cfg,
1672 0 : daemon_local.flags.vflag) == 0) &&
1673 0 : (dlt_daemon_contexts_load(&daemon, daemon.runtime_context_cfg,
1674 : daemon_local.flags.vflag) == 0))
1675 0 : daemon.runtime_context_cfg_loaded = 1;
1676 :
1677 1 : dlt_daemon_log_internal(&daemon, &daemon_local,
1678 : "Daemon launched. Starting to output traces...",
1679 : DLT_LOG_INFO, DLT_DAEMON_APP_ID, DLT_DAEMON_CTX_ID,
1680 : daemon_local.flags.vflag);
1681 :
1682 : /* Even handling loop. */
1683 118 : while ((back >= 0) && (g_exit >= 0))
1684 117 : back = dlt_daemon_handle_event(&daemon_local.pEvent,
1685 : &daemon,
1686 : &daemon_local);
1687 :
1688 1 : snprintf(local_str, DLT_DAEMON_TEXTBUFSIZE, "Exiting DLT daemon... [%d]",
1689 : g_signo);
1690 1 : dlt_daemon_log_internal(&daemon, &daemon_local, local_str, DLT_LOG_INFO,
1691 : DLT_DAEMON_APP_ID, DLT_DAEMON_CTX_ID,
1692 : daemon_local.flags.vflag);
1693 1 : dlt_vlog(LOG_NOTICE, "%s%s", local_str, "\n");
1694 :
1695 1 : dlt_daemon_local_cleanup(&daemon, &daemon_local, daemon_local.flags.vflag);
1696 :
1697 : #ifdef UDP_CONNECTION_SUPPORT
1698 : dlt_daemon_udp_close_connection();
1699 : #endif
1700 :
1701 1 : dlt_gateway_deinit(&daemon_local.pGateway, daemon_local.flags.vflag);
1702 :
1703 1 : dlt_daemon_free(&daemon, daemon_local.flags.vflag);
1704 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
1705 : dlt_trace_load_free(&daemon);
1706 : #endif
1707 :
1708 1 : dlt_log(LOG_NOTICE, "Leaving DLT daemon\n");
1709 :
1710 1 : dlt_log_free();
1711 :
1712 1 : return 0;
1713 :
1714 : } /* main() */
1715 : #endif
1716 :
1717 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
1718 : void dlt_trace_load_free(DltDaemon* daemon)
1719 : {
1720 : if (daemon->preconfigured_trace_load_settings != NULL) {
1721 : free(daemon->preconfigured_trace_load_settings);
1722 : daemon->preconfigured_trace_load_settings = NULL;
1723 : }
1724 : pthread_rwlock_destroy(&trace_load_rw_lock);
1725 : }
1726 : #endif
1727 :
1728 :
1729 1 : int dlt_daemon_local_init_p1(DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
1730 : {
1731 1 : PRINT_FUNCTION_VERBOSE(verbose);
1732 : int ret = DLT_RETURN_OK;
1733 :
1734 1 : if ((daemon == 0) || (daemon_local == 0)) {
1735 0 : dlt_log(LOG_ERR, "Invalid function parameters used for function dlt_daemon_local_init_p1()\n");
1736 0 : return -1;
1737 : }
1738 :
1739 : #if defined(DLT_SYSTEMD_WATCHDOG_ENABLE) || defined(DLT_SYSTEMD_ENABLE)
1740 : ret = sd_booted();
1741 :
1742 : if (ret == 0) {
1743 : dlt_log(LOG_CRIT, "System not booted with systemd!\n");
1744 : }
1745 : else if (ret < 0)
1746 : {
1747 : dlt_log(LOG_CRIT, "sd_booted failed!\n");
1748 : return -1;
1749 : }
1750 : else {
1751 : dlt_log(LOG_INFO, "System booted with systemd\n");
1752 : }
1753 :
1754 : #endif
1755 :
1756 : #ifdef DLT_DAEMON_USE_FIFO_IPC
1757 :
1758 1 : if (dlt_daemon_create_pipes_dir(daemon_local->flags.userPipesDir) == DLT_RETURN_ERROR)
1759 : return DLT_RETURN_ERROR;
1760 :
1761 : #endif
1762 :
1763 : /* Check for daemon mode */
1764 1 : if (daemon_local->flags.dflag)
1765 0 : dlt_daemon_daemonize(daemon_local->flags.vflag);
1766 :
1767 : /* initialise structure to use DLT file */
1768 1 : ret = dlt_file_init(&(daemon_local->file), daemon_local->flags.vflag);
1769 :
1770 1 : if (ret == DLT_RETURN_ERROR) {
1771 0 : dlt_log(LOG_ERR, "Could not initialize file structure\n");
1772 : /* Return value ignored, dlt daemon will exit */
1773 0 : dlt_file_free(&(daemon_local->file), daemon_local->flags.vflag);
1774 0 : return ret;
1775 : }
1776 :
1777 1 : signal(SIGPIPE, SIG_IGN);
1778 :
1779 1 : signal(SIGTERM, dlt_daemon_signal_handler); /* software termination signal from kill */
1780 1 : signal(SIGHUP, dlt_daemon_signal_handler); /* hangup signal */
1781 1 : signal(SIGQUIT, dlt_daemon_signal_handler);
1782 1 : signal(SIGINT, dlt_daemon_signal_handler);
1783 : #ifdef __QNX__
1784 : signal(SIGUSR1, dlt_daemon_signal_handler); /* for timer threads */
1785 : #endif
1786 :
1787 1 : return DLT_RETURN_OK;
1788 : }
1789 :
1790 1 : int dlt_daemon_local_init_p2(DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
1791 : {
1792 1 : PRINT_FUNCTION_VERBOSE(verbose);
1793 :
1794 1 : if ((daemon == 0) || (daemon_local == 0)) {
1795 0 : dlt_log(LOG_ERR, "Invalid function parameters used for function dlt_daemon_local_init_p2()\n");
1796 0 : return -1;
1797 : }
1798 :
1799 : /* Daemon data */
1800 1 : if (dlt_daemon_init(daemon, daemon_local->RingbufferMinSize, daemon_local->RingbufferMaxSize,
1801 1 : daemon_local->RingbufferStepSize, daemon_local->flags.ivalue,
1802 : daemon_local->flags.contextLogLevel,
1803 : daemon_local->flags.contextTraceStatus, daemon_local->flags.enforceContextLLAndTS,
1804 : daemon_local->flags.vflag) == -1) {
1805 0 : dlt_log(LOG_ERR, "Could not initialize daemon data\n");
1806 0 : return -1;
1807 : }
1808 :
1809 : /* init offline trace */
1810 1 : if (((daemon->mode == DLT_USER_MODE_INTERNAL) || (daemon->mode == DLT_USER_MODE_BOTH)) &&
1811 0 : daemon_local->flags.offlineTraceDirectory[0]) {
1812 0 : if (multiple_files_buffer_init(&(daemon_local->offlineTrace),
1813 0 : daemon_local->flags.offlineTraceDirectory,
1814 : daemon_local->flags.offlineTraceFileSize,
1815 : daemon_local->flags.offlineTraceMaxSize,
1816 0 : daemon_local->flags.offlineTraceFilenameTimestampBased,
1817 : false,
1818 : DLT_OFFLINETRACE_FILENAME_BASE,
1819 : DLT_OFFLINETRACE_FILENAME_EXT) == -1) {
1820 0 : dlt_log(LOG_ERR, "Could not initialize offline trace\n");
1821 0 : return -1;
1822 : }
1823 : }
1824 :
1825 : /* Init offline logstorage for MAX devices */
1826 1 : if (daemon_local->flags.offlineLogstorageMaxDevices > 0) {
1827 1 : size_t max_devices = (size_t)daemon_local->flags.offlineLogstorageMaxDevices;
1828 1 : daemon->storage_handle = malloc(sizeof(DltLogStorage) * max_devices);
1829 :
1830 1 : if (daemon->storage_handle == NULL) {
1831 0 : dlt_log(LOG_ERR, "Could not initialize offline logstorage\n");
1832 0 : return -1;
1833 : }
1834 :
1835 : memset(daemon->storage_handle, 0, sizeof(DltLogStorage) * max_devices);
1836 : }
1837 :
1838 : /* Set ECU id of daemon */
1839 1 : if (daemon_local->flags.evalue[0]){
1840 0 : dlt_set_id(daemon->ecuid, daemon_local->flags.evalue);
1841 0 : daemon->ecuid2len = (uint8_t)strlen(daemon_local->flags.evalue);
1842 0 : dlt_set_id_v2(daemon->ecuid2, daemon_local->flags.evalue, daemon->ecuid2len);
1843 :
1844 : }
1845 : else{
1846 1 : dlt_set_id(daemon->ecuid, DLT_DAEMON_ECU_ID);
1847 1 : daemon->ecuid2len = (uint8_t)strlen(DLT_DAEMON_ECU_ID);
1848 1 : dlt_set_id_v2(daemon->ecuid2, DLT_DAEMON_ECU_ID, daemon->ecuid2len);
1849 : }
1850 :
1851 : /* Set flag for optional sending of serial header */
1852 1 : daemon->sendserialheader = daemon_local->flags.lflag;
1853 :
1854 : #ifdef DLT_SHM_ENABLE
1855 :
1856 : /* init shared memory */
1857 : if (dlt_shm_init_server(&(daemon_local->dlt_shm), daemon_local->flags.dltShmName,
1858 : daemon_local->flags.sharedMemorySize) == DLT_RETURN_ERROR) {
1859 : dlt_log(LOG_ERR, "Could not initialize shared memory\n");
1860 : return -1;
1861 : }
1862 :
1863 : daemon_local->recv_buf_shm = (unsigned char *)calloc(1, DLT_SHM_RCV_BUFFER_SIZE);
1864 :
1865 : if (NULL == daemon_local->recv_buf_shm) {
1866 : dlt_log(LOG_ERR, "failed to allocated the buffer to receive shm data\n");
1867 : return -1;
1868 : }
1869 :
1870 : #endif
1871 :
1872 : /* prepare main loop */
1873 1 : if (dlt_message_init(&(daemon_local->msg), daemon_local->flags.vflag) == DLT_RETURN_ERROR) {
1874 0 : dlt_log(LOG_ERR, "Could not initialize message\n");
1875 0 : return -1;
1876 : }
1877 :
1878 : /* configure sending timing packets */
1879 1 : if (daemon_local->flags.sendMessageTime)
1880 0 : daemon->timingpackets = 1;
1881 :
1882 1 : if (dlt_daemon_local_ecu_version_init(daemon, daemon_local, daemon_local->flags.vflag) < 0) {
1883 1 : daemon->ECUVersionString = malloc(DLT_DAEMON_TEXTBUFSIZE);
1884 :
1885 1 : if (daemon->ECUVersionString == 0) {
1886 0 : dlt_log(LOG_WARNING, "Could not allocate memory for version string\n");
1887 0 : return -1;
1888 : }
1889 :
1890 1 : dlt_get_version(daemon->ECUVersionString, DLT_DAEMON_TEXTBUFSIZE);
1891 : }
1892 :
1893 : /* Set to allows one to maintain logstorage loglevel as default */
1894 1 : daemon->maintain_logstorage_loglevel = DLT_MAINTAIN_LOGSTORAGE_LOGLEVEL_ON;
1895 :
1896 1 : return 0;
1897 : }
1898 :
1899 1 : static int dlt_daemon_init_serial(DltDaemonLocal *daemon_local)
1900 : {
1901 : /* create and open serial connection from/to client */
1902 : /* open serial connection */
1903 : int fd = -1;
1904 :
1905 1 : if (daemon_local->flags.yvalue[0] == '\0')
1906 : return 0;
1907 :
1908 0 : fd = open(daemon_local->flags.yvalue, O_RDWR);
1909 :
1910 0 : if (fd < 0) {
1911 0 : dlt_vlog(LOG_ERR, "Failed to open serial device %s\n",
1912 : daemon_local->flags.yvalue);
1913 :
1914 0 : daemon_local->flags.yvalue[0] = 0;
1915 0 : return -1;
1916 : }
1917 :
1918 0 : if (isatty(fd)) {
1919 : int speed = DLT_DAEMON_SERIAL_DEFAULT_BAUDRATE;
1920 :
1921 0 : if (daemon_local->flags.bvalue[0])
1922 0 : speed = atoi(daemon_local->flags.bvalue);
1923 :
1924 0 : daemon_local->baudrate = dlt_convert_serial_speed(speed);
1925 :
1926 0 : if (dlt_setup_serial(fd, (speed_t) daemon_local->baudrate) < 0) {
1927 0 : close(fd);
1928 0 : daemon_local->flags.yvalue[0] = 0;
1929 :
1930 0 : dlt_vlog(LOG_ERR, "Failed to configure serial device %s (%s) \n",
1931 0 : daemon_local->flags.yvalue, strerror(errno));
1932 :
1933 0 : return -1;
1934 : }
1935 :
1936 0 : if (daemon_local->flags.vflag)
1937 0 : dlt_log(LOG_DEBUG, "Serial init done\n");
1938 : }
1939 : else {
1940 0 : close(fd);
1941 0 : fprintf(stderr,
1942 : "Device is not a serial device, device = %s (%s) \n",
1943 : daemon_local->flags.yvalue,
1944 0 : strerror(errno));
1945 0 : daemon_local->flags.yvalue[0] = 0;
1946 0 : return -1;
1947 : }
1948 :
1949 0 : return dlt_connection_create(daemon_local,
1950 : &daemon_local->pEvent,
1951 : fd,
1952 : POLLIN,
1953 : DLT_CONNECTION_CLIENT_MSG_SERIAL);
1954 : }
1955 :
1956 : #ifdef DLT_DAEMON_USE_FIFO_IPC
1957 1 : static int dlt_daemon_init_fifo(DltDaemonLocal *daemon_local)
1958 : {
1959 : int ret;
1960 : int fd = -1;
1961 : int fifo_size;
1962 :
1963 : /* open named pipe(FIFO) to receive DLT messages from users */
1964 1 : umask(0);
1965 :
1966 : /* Try to delete existing pipe, ignore result of unlink */
1967 1 : const char *tmpFifo = daemon_local->flags.daemonFifoName;
1968 1 : unlink(tmpFifo);
1969 :
1970 1 : ret = mkfifo(tmpFifo, S_IRUSR | S_IWUSR | S_IWGRP);
1971 :
1972 1 : if (ret == -1) {
1973 0 : dlt_vlog(LOG_WARNING, "FIFO user %s cannot be created (%s)!\n",
1974 0 : tmpFifo, strerror(errno));
1975 0 : return -1;
1976 : } /* if */
1977 :
1978 : /* Set group of daemon FIFO */
1979 1 : if (daemon_local->flags.daemonFifoGroup[0] != 0) {
1980 0 : errno = 0;
1981 0 : struct group *group_dlt = getgrnam(daemon_local->flags.daemonFifoGroup);
1982 :
1983 0 : if (group_dlt) {
1984 0 : ret = chown(tmpFifo, (uid_t)-1, group_dlt->gr_gid);
1985 :
1986 0 : if (ret == -1)
1987 0 : dlt_vlog(LOG_ERR, "FIFO user %s cannot be chowned to group %s (%s)\n",
1988 : tmpFifo, daemon_local->flags.daemonFifoGroup,
1989 : strerror(errno));
1990 : }
1991 0 : else if ((errno == 0) || (errno == ENOENT) || (errno == EBADF) || (errno == EPERM))
1992 : {
1993 0 : dlt_vlog(LOG_ERR, "Group name %s is not found (%s)\n",
1994 : daemon_local->flags.daemonFifoGroup,
1995 : strerror(errno));
1996 : }
1997 : else {
1998 0 : dlt_vlog(LOG_ERR, "Failed to get group id of %s (%s)\n",
1999 : daemon_local->flags.daemonFifoGroup,
2000 : strerror(errno));
2001 : }
2002 : }
2003 :
2004 : fd = open(tmpFifo, O_RDWR);
2005 :
2006 1 : if (fd == -1) {
2007 0 : dlt_vlog(LOG_WARNING, "FIFO user %s cannot be opened (%s)!\n",
2008 0 : tmpFifo, strerror(errno));
2009 0 : return -1;
2010 : } /* if */
2011 :
2012 : #ifdef __linux__
2013 : /* F_SETPIPE_SZ and F_GETPIPE_SZ are only supported for Linux.
2014 : * For other OSes it depends on its system e.g. pipe manager.
2015 : */
2016 1 : if (daemon_local->daemonFifoSize != 0) {
2017 : /* Set Daemon FIFO size */
2018 0 : if (fcntl(fd, F_SETPIPE_SZ, daemon_local->daemonFifoSize) == -1)
2019 0 : dlt_vlog(LOG_ERR, "set FIFO size error: %s\n", strerror(errno));
2020 : }
2021 :
2022 : /* Get Daemon FIFO size */
2023 1 : if ((fifo_size = fcntl(fd, F_GETPIPE_SZ, 0)) == -1)
2024 0 : dlt_vlog(LOG_ERR, "get FIFO size error: %s\n", strerror(errno));
2025 : else
2026 1 : dlt_vlog(LOG_INFO, "FIFO size: %d\n", fifo_size);
2027 : #endif
2028 :
2029 : /* Early init, to be able to catch client (app) connections
2030 : * as soon as possible. This registration is automatically ignored
2031 : * during next execution.
2032 : */
2033 1 : return dlt_connection_create(daemon_local,
2034 : &daemon_local->pEvent,
2035 : fd,
2036 : POLLIN,
2037 : DLT_CONNECTION_APP_MSG);
2038 : }
2039 : #endif
2040 :
2041 : #ifdef DLT_DAEMON_VSOCK_IPC_ENABLE
2042 : static int dlt_daemon_init_vsock(DltDaemonLocal *daemon_local)
2043 : {
2044 : int fd;
2045 : struct sockaddr_vm addr;
2046 :
2047 : fd = socket(AF_VSOCK, SOCK_STREAM, 0);
2048 : if (fd == -1) {
2049 : dlt_vlog(LOG_ERR, "Failed to create VSOCK socket: %s\n", strerror(errno));
2050 : return -1;
2051 : }
2052 :
2053 : memset(&addr, 0, sizeof(addr));
2054 : addr.svm_family = AF_VSOCK;
2055 : addr.svm_port = DLT_VSOCK_PORT;
2056 : addr.svm_cid = VMADDR_CID_ANY;
2057 :
2058 : if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
2059 : dlt_vlog(LOG_ERR, "Failed to bind VSOCK socket: %s\n", strerror(errno));
2060 : close(fd);
2061 : return -1;
2062 : }
2063 :
2064 : if (listen(fd, 1) != 0) {
2065 : dlt_vlog(LOG_ERR, "Failed to listen on VSOCK socket: %s\n", strerror(errno));
2066 : close(fd);
2067 : return -1;
2068 : }
2069 :
2070 : return dlt_connection_create(daemon_local,
2071 : &daemon_local->pEvent,
2072 : fd,
2073 : POLLIN,
2074 : DLT_CONNECTION_APP_CONNECT);
2075 : }
2076 : #endif
2077 :
2078 : #ifdef DLT_DAEMON_USE_UNIX_SOCKET_IPC
2079 : static DltReturnValue dlt_daemon_init_app_socket(DltDaemonLocal *daemon_local)
2080 : {
2081 : /* socket access permission set to srw-rw-rw- (666) */
2082 : int mask = S_IXUSR | S_IXGRP | S_IXOTH;
2083 : DltReturnValue ret = DLT_RETURN_OK;
2084 : int fd = -1;
2085 :
2086 : if (daemon_local == NULL) {
2087 : dlt_vlog(LOG_ERR, "%s: Invalid function parameters\n", __func__);
2088 : return DLT_RETURN_ERROR;
2089 : }
2090 :
2091 : #ifdef ANDROID
2092 : /* on android if we want to use security contexts on Unix sockets,
2093 : * they should be created by init (see dlt-daemon.rc in src/daemon)
2094 : * and recovered through the below API */
2095 : ret = dlt_daemon_unix_android_get_socket(&fd, daemon_local->flags.appSockPath);
2096 : if (ret < DLT_RETURN_OK) {
2097 : /* we failed to get app socket created by init.
2098 : * To avoid blocking users to launch dlt-daemon only through
2099 : * init on android (e.g: by hand for debugging purpose), try to
2100 : * create app socket by ourselves */
2101 : ret = dlt_daemon_unix_socket_open(&fd,
2102 : daemon_local->flags.appSockPath,
2103 : SOCK_STREAM,
2104 : mask);
2105 : }
2106 : #else
2107 : ret = dlt_daemon_unix_socket_open(&fd,
2108 : daemon_local->flags.appSockPath,
2109 : SOCK_STREAM,
2110 : mask);
2111 : #endif
2112 : if (ret == DLT_RETURN_OK) {
2113 : if (dlt_connection_create(daemon_local,
2114 : &daemon_local->pEvent,
2115 : fd,
2116 : POLLIN,
2117 : DLT_CONNECTION_APP_CONNECT)) {
2118 : dlt_log(LOG_CRIT, "Could not create connection for app socket.\n");
2119 : return DLT_RETURN_ERROR;
2120 : }
2121 : }
2122 : else {
2123 : dlt_log(LOG_CRIT, "Could not create and open app socket.\n");
2124 : return DLT_RETURN_ERROR;
2125 : }
2126 :
2127 : return ret;
2128 : }
2129 : #endif
2130 :
2131 1 : static DltReturnValue dlt_daemon_initialize_control_socket(DltDaemonLocal *daemon_local)
2132 : {
2133 : /* socket access permission set to srw-rw---- (660) */
2134 : int mask = S_IXUSR | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH;
2135 : DltReturnValue ret = DLT_RETURN_OK;
2136 1 : int fd = -1;
2137 :
2138 1 : if (daemon_local == NULL) {
2139 0 : dlt_vlog(LOG_ERR, "%s: Invalid function parameters\n", __func__);
2140 0 : return -1;
2141 : }
2142 :
2143 : #ifdef ANDROID
2144 : /* on android if we want to use security contexts on Unix sockets,
2145 : * they should be created by init (see dlt-daemon.rc in src/daemon)
2146 : * and recovered through the below API */
2147 : ret = dlt_daemon_unix_android_get_socket(&fd, daemon_local->flags.ctrlSockPath);
2148 : if (ret < DLT_RETURN_OK) {
2149 : /* we failed to get app socket created by init.
2150 : * To avoid blocking users to launch dlt-daemon only through
2151 : * init on android (e.g by hand for debugging purpose), try to
2152 : * create app socket by ourselves */
2153 : ret = dlt_daemon_unix_socket_open(&fd,
2154 : daemon_local->flags.ctrlSockPath,
2155 : SOCK_STREAM,
2156 : mask);
2157 : }
2158 : #else
2159 1 : ret = dlt_daemon_unix_socket_open(&fd,
2160 1 : daemon_local->flags.ctrlSockPath,
2161 : SOCK_STREAM,
2162 : mask);
2163 : #endif
2164 1 : if (ret == DLT_RETURN_OK) {
2165 1 : if (dlt_connection_create(daemon_local,
2166 : &daemon_local->pEvent,
2167 : fd,
2168 : POLLIN,
2169 : DLT_CONNECTION_CONTROL_CONNECT) < DLT_RETURN_OK) {
2170 0 : dlt_log(LOG_ERR, "Could not initialize control socket.\n");
2171 : ret = DLT_RETURN_ERROR;
2172 : }
2173 : }
2174 :
2175 : return ret;
2176 : }
2177 :
2178 1 : int dlt_daemon_local_connection_init(DltDaemon *daemon,
2179 : DltDaemonLocal *daemon_local,
2180 : int verbose)
2181 : {
2182 1 : int fd = -1;
2183 :
2184 1 : PRINT_FUNCTION_VERBOSE(verbose);
2185 :
2186 1 : if ((daemon == NULL) || (daemon_local == NULL)) {
2187 0 : dlt_vlog(LOG_ERR, "%s: Invalid function parameters\n", __func__);
2188 0 : return -1;
2189 : }
2190 :
2191 1 : DltBindAddress_t *head = daemon_local->flags.ipNodes;
2192 :
2193 : #ifdef DLT_DAEMON_USE_UNIX_SOCKET_IPC
2194 : /* create and open socket to receive incoming connections from user application */
2195 : if (dlt_daemon_init_app_socket(daemon_local) < DLT_RETURN_OK) {
2196 : dlt_log(LOG_ERR, "Unable to initialize app socket.\n");
2197 : return DLT_RETURN_ERROR;
2198 : }
2199 :
2200 : #else /* DLT_DAEMON_USE_FIFO_IPC */
2201 :
2202 1 : if (dlt_daemon_init_fifo(daemon_local)) {
2203 0 : dlt_log(LOG_ERR, "Unable to initialize fifo.\n");
2204 0 : return DLT_RETURN_ERROR;
2205 : }
2206 :
2207 : #endif
2208 :
2209 : #ifdef DLT_DAEMON_VSOCK_IPC_ENABLE
2210 : if (dlt_daemon_init_vsock(daemon_local) != 0) {
2211 : dlt_log(LOG_ERR, "Unable to initialize app VSOCK socket.\n");
2212 : return DLT_RETURN_ERROR;
2213 : }
2214 : #endif
2215 :
2216 : /* create and open socket to receive incoming connections from client */
2217 1 : daemon_local->client_connections = 0;
2218 :
2219 1 : if (head == NULL) { /* no IP set in BindAddress option, will use "0.0.0.0" as default */
2220 :
2221 1 : if (dlt_daemon_socket_open(&fd, daemon_local->flags.port, "0.0.0.0") == DLT_RETURN_OK) {
2222 1 : if (dlt_connection_create(daemon_local,
2223 : &daemon_local->pEvent,
2224 : fd,
2225 : POLLIN,
2226 : DLT_CONNECTION_CLIENT_CONNECT)) {
2227 0 : dlt_log(LOG_ERR, "Could not initialize main socket.\n");
2228 0 : return DLT_RETURN_ERROR;
2229 : }
2230 : }
2231 : else {
2232 0 : dlt_log(LOG_ERR, "Could not initialize main socket.\n");
2233 0 : return DLT_RETURN_ERROR;
2234 : }
2235 : }
2236 : else {
2237 : bool any_open = false;
2238 0 : while (head != NULL) { /* open socket for each IP in the bindAddress list */
2239 :
2240 0 : if (dlt_daemon_socket_open(&fd, daemon_local->flags.port, head->ip) == DLT_RETURN_OK) {
2241 0 : if (dlt_connection_create(daemon_local,
2242 : &daemon_local->pEvent,
2243 : fd,
2244 : POLLIN,
2245 : DLT_CONNECTION_CLIENT_CONNECT)) {
2246 0 : dlt_vlog(LOG_ERR, "Could not create connection, for binding %s\n", head->ip);
2247 : } else {
2248 : any_open = true;
2249 : }
2250 : }
2251 : else {
2252 0 : dlt_vlog(LOG_ERR, "Could not open main socket, for binding %s\n", head->ip);
2253 : }
2254 :
2255 0 : head = head->next;
2256 : }
2257 :
2258 0 : if (!any_open) {
2259 0 : dlt_vlog(LOG_ERR, "Failed create main socket for any configured binding\n");
2260 0 : return DLT_RETURN_ERROR;
2261 : }
2262 : }
2263 :
2264 : #ifdef UDP_CONNECTION_SUPPORT
2265 :
2266 : if (daemon_local->UDPConnectionSetup == MULTICAST_CONNECTION_ENABLED) {
2267 : if (dlt_daemon_udp_connection_setup(daemon_local) < 0) {
2268 : dlt_log(LOG_ERR, "UDP fd creation failed\n");
2269 : return DLT_RETURN_ERROR;
2270 : }
2271 : else {
2272 : dlt_log(LOG_INFO, "UDP fd creation success\n");
2273 : }
2274 : }
2275 :
2276 : #endif
2277 :
2278 : /* create and open unix socket to receive incoming connections from
2279 : * control application */
2280 1 : if (dlt_daemon_initialize_control_socket(daemon_local) < DLT_RETURN_OK) {
2281 0 : dlt_log(LOG_ERR, "Could not initialize control socket.\n");
2282 0 : return DLT_RETURN_ERROR;
2283 : }
2284 :
2285 : /* Init serial */
2286 1 : if (dlt_daemon_init_serial(daemon_local) < 0) {
2287 0 : dlt_log(LOG_ERR, "Could not initialize daemon data\n");
2288 0 : return DLT_RETURN_ERROR;
2289 : }
2290 :
2291 : return 0;
2292 : }
2293 :
2294 0 : static char* file_read_everything(FILE* const file, const size_t sizeLimit)
2295 : {
2296 0 : if (!file) {
2297 : return NULL;
2298 : }
2299 :
2300 : /* Get the file size. Bail out if stat fails. */
2301 0 : const int fd = fileno(file);
2302 0 : struct stat s_buf = {0};
2303 0 : if (fstat(fd, &s_buf) < 0) {
2304 0 : dlt_log(LOG_WARNING, "failed to stat file size\n");
2305 0 : fclose(file);
2306 0 : return NULL;
2307 : }
2308 :
2309 : /* Size limit includes NULL terminator. */
2310 0 : const off_t size = s_buf.st_size;
2311 0 : if (size < 0 || (size_t)size >= sizeLimit) {
2312 0 : dlt_log(LOG_WARNING, "file size invalid\n");
2313 0 : fclose(file);
2314 0 : return NULL;
2315 : }
2316 :
2317 0 : char* const string = malloc((size_t)size + 1);
2318 0 : if (!string) {
2319 0 : dlt_log(LOG_WARNING, "failed to allocate string for file contents\n");
2320 0 : fclose(file);
2321 0 : return NULL;
2322 : }
2323 :
2324 : off_t offset = 0;
2325 0 : while (!feof(file)) {
2326 0 : offset += (off_t)fread(string + offset, 1, (size_t)size, file);
2327 :
2328 0 : if (ferror(file)) {
2329 0 : dlt_log(LOG_WARNING, "failed to read file\n");
2330 0 : free(string);
2331 0 : fclose(file);
2332 0 : return NULL;
2333 : }
2334 :
2335 0 : if (offset > size) {
2336 0 : dlt_log(LOG_WARNING, "file too long for buffer\n");
2337 0 : free(string);
2338 0 : fclose(file);
2339 0 : return NULL;
2340 : }
2341 : }
2342 :
2343 0 : string[offset] = '\0'; /* append null termination at end of string */
2344 :
2345 0 : return string;
2346 : }
2347 :
2348 0 : static char* file_read_field(FILE* const file, const char* const fieldName)
2349 : {
2350 0 : if (!file) {
2351 : return NULL;
2352 : }
2353 :
2354 : const char* const kDelimiters = "\r\n\"\'=";
2355 0 : const size_t fieldNameLen = strlen(fieldName);
2356 :
2357 : char* result = NULL;
2358 :
2359 0 : char* buffer = NULL;
2360 0 : size_t bufferSize = 0;
2361 :
2362 : while (true) {
2363 : ssize_t lineSize = getline(&buffer, &bufferSize, file);
2364 0 : if (lineSize < 0 || !buffer) {
2365 : /* end of file */
2366 : break;
2367 : }
2368 :
2369 : char* line = buffer;
2370 :
2371 : /* trim trailing delimiters */
2372 0 : while (lineSize >= 1 && strchr(kDelimiters, line[lineSize - 1]) != NULL) {
2373 0 : line[lineSize - 1] = '\0';
2374 0 : --lineSize;
2375 : }
2376 :
2377 : /* check fieldName */
2378 0 : if (strncmp(line, fieldName, fieldNameLen) == 0 &&
2379 0 : (size_t)lineSize >= (fieldNameLen + 1) &&
2380 0 : strchr(kDelimiters, line[fieldNameLen]) != NULL) {
2381 : /* trim fieldName */
2382 : line += fieldNameLen;
2383 :
2384 : /* trim delimiter */
2385 0 : ++line;
2386 :
2387 : /* trim leading delimiters */
2388 0 : while (*line != '\0' && strchr(kDelimiters, *line) != NULL) {
2389 0 : ++line;
2390 : --lineSize;
2391 : }
2392 :
2393 0 : result = strdup(line);
2394 0 : break;
2395 : }
2396 : }
2397 :
2398 0 : free(buffer);
2399 :
2400 0 : return result;
2401 : }
2402 :
2403 1 : int dlt_daemon_local_ecu_version_init(DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
2404 : {
2405 : FILE *f = NULL;
2406 :
2407 1 : PRINT_FUNCTION_VERBOSE(verbose);
2408 :
2409 : /* By default, version string is null. */
2410 1 : daemon->ECUVersionString = NULL;
2411 :
2412 : /* Open the file. Bail out if error occurs */
2413 1 : f = fopen(daemon_local->flags.pathToECUSoftwareVersion, "r");
2414 :
2415 1 : if (f == NULL) {
2416 : /* Error level notice, because this might be deliberate choice */
2417 1 : dlt_log(LOG_NOTICE, "Failed to open ECU Software version file.\n");
2418 1 : return -1;
2419 : }
2420 :
2421 0 : if (daemon_local->flags.ecuSoftwareVersionFileField[0] != '\0') {
2422 0 : daemon->ECUVersionString = file_read_field(f, daemon_local->flags.ecuSoftwareVersionFileField);
2423 : } else {
2424 0 : daemon->ECUVersionString = file_read_everything(f, DLT_DAEMON_TEXTBUFSIZE);
2425 : }
2426 :
2427 0 : fclose(f);
2428 :
2429 0 : return (daemon->ECUVersionString != NULL) ? 0 : -1;
2430 : }
2431 :
2432 1 : void dlt_daemon_local_cleanup(DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
2433 : {
2434 1 : PRINT_FUNCTION_VERBOSE(verbose);
2435 :
2436 1 : if ((daemon == 0) || (daemon_local == 0)) {
2437 0 : dlt_log(LOG_ERR, "Invalid function parameters used for function dlt_daemon_local_cleanup()\n");
2438 0 : return;
2439 : }
2440 :
2441 : /* Don't receive event anymore */
2442 1 : dlt_event_handler_cleanup_connections(&daemon_local->pEvent);
2443 :
2444 1 : dlt_message_free(&(daemon_local->msg), daemon_local->flags.vflag);
2445 1 : dlt_message_free_v2(&(daemon_local->msgv2), daemon_local->flags.vflag);
2446 :
2447 : /* free shared memory */
2448 1 : if (daemon_local->flags.offlineTraceDirectory[0])
2449 0 : multiple_files_buffer_free(&(daemon_local->offlineTrace));
2450 :
2451 : /* Ignore result */
2452 1 : dlt_file_free(&(daemon_local->file), daemon_local->flags.vflag);
2453 :
2454 : #ifdef DLT_DAEMON_USE_FIFO_IPC
2455 : /* Try to delete existing pipe, ignore result of unlink() */
2456 1 : unlink(daemon_local->flags.daemonFifoName);
2457 : #else /* DLT_DAEMON_USE_UNIX_SOCKET_IPC */
2458 : /* Try to delete existing pipe, ignore result of unlink() */
2459 : if (unlink(daemon_local->flags.appSockPath) != 0) {
2460 : dlt_vlog(LOG_WARNING, "%s: unlink() failed: %s\n",
2461 : __func__, strerror(errno));
2462 : }
2463 : #endif
2464 :
2465 : #ifdef DLT_SHM_ENABLE
2466 : /* free shared memory */
2467 : dlt_shm_free_server(&(daemon_local->dlt_shm), daemon_local->flags.dltShmName);
2468 : free(daemon_local->recv_buf_shm);
2469 : daemon_local->recv_buf_shm = NULL;
2470 : #endif
2471 :
2472 1 : if (daemon_local->flags.offlineLogstorageMaxDevices > 0) {
2473 : /* disconnect all logstorage devices */
2474 1 : dlt_daemon_logstorage_cleanup(daemon,
2475 : daemon_local,
2476 : daemon_local->flags.vflag);
2477 :
2478 1 : free(daemon->storage_handle);
2479 : }
2480 :
2481 1 : if (daemon->ECUVersionString != NULL)
2482 1 : free(daemon->ECUVersionString);
2483 :
2484 1 : if (unlink(daemon_local->flags.ctrlSockPath) != 0) {
2485 0 : dlt_vlog(LOG_WARNING, "%s: unlink() failed: %s\n",
2486 0 : __func__, strerror(errno));
2487 : }
2488 :
2489 : /* free IP list */
2490 1 : free(daemon_local->flags.ipNodes);
2491 : }
2492 :
2493 0 : void dlt_daemon_exit_trigger()
2494 : {
2495 : /* stop event loop */
2496 0 : g_exit = -1;
2497 :
2498 : #ifdef DLT_DAEMON_USE_FIFO_IPC
2499 0 : char tmp[DLT_PATH_MAX] = { 0 };
2500 :
2501 : ssize_t n;
2502 0 : n = snprintf(tmp, DLT_PATH_MAX, "%s/dlt", dltFifoBaseDir);
2503 0 : if (n < 0 || (size_t)n > DLT_PATH_MAX) {
2504 0 : dlt_vlog(LOG_WARNING, "%s: snprintf truncation/error(%ld) %s\n",
2505 : __func__, (long int)n, tmp);
2506 : }
2507 :
2508 0 : (void)unlink(tmp);
2509 : #endif
2510 :
2511 : #ifdef __QNX__
2512 : dlt_daemon_cleanup_timers();
2513 : #endif
2514 :
2515 0 : }
2516 :
2517 0 : void dlt_daemon_signal_handler(int sig)
2518 : {
2519 0 : g_signo = sig;
2520 :
2521 0 : switch (sig) {
2522 0 : case SIGHUP:
2523 : case SIGTERM:
2524 : case SIGINT:
2525 : case SIGQUIT:
2526 : {
2527 : /* finalize the server */
2528 0 : dlt_vlog(LOG_NOTICE, "Exiting DLT daemon due to signal: %s\n",
2529 : strsignal(sig));
2530 0 : dlt_daemon_exit_trigger();
2531 0 : break;
2532 : }
2533 : default:
2534 : {
2535 : /* This case should never happen! */
2536 : break;
2537 : }
2538 : } /* switch */
2539 :
2540 0 : } /* dlt_daemon_signal_handler() */
2541 :
2542 : #ifdef __QNX__
2543 : void dlt_daemon_cleanup_timers()
2544 : {
2545 : int i = 0;
2546 : while (i < DLT_TIMER_UNKNOWN) {
2547 : /* Remove FIFO of every timer and kill timer thread */
2548 : if (0 != timer_threads[i]) {
2549 : pthread_kill(timer_threads[i], SIGUSR1);
2550 : pthread_join(timer_threads[i], NULL);
2551 : timer_threads[i] = 0;
2552 :
2553 : close_pipes(dlt_timer_pipes[i]);
2554 :
2555 : /* Free data of every timer */
2556 : if (NULL != timer_data[i]) {
2557 : free(timer_data[i]);
2558 : timer_data[i] = NULL;
2559 : }
2560 : }
2561 : i++;
2562 : }
2563 : }
2564 : #endif
2565 :
2566 0 : void dlt_daemon_daemonize(int verbose)
2567 : {
2568 : int i;
2569 : int fd;
2570 :
2571 0 : PRINT_FUNCTION_VERBOSE(verbose);
2572 :
2573 0 : dlt_log(LOG_NOTICE, "Daemon mode\n");
2574 :
2575 : /* Daemonize */
2576 0 : i = fork();
2577 :
2578 0 : if (i < 0) {
2579 0 : dlt_log(LOG_CRIT, "Unable to fork(), exiting DLT daemon\n");
2580 0 : exit(-1); /* fork error */
2581 : }
2582 :
2583 0 : if (i > 0)
2584 0 : exit(0); /* parent exits */
2585 :
2586 : /* child (daemon) continues */
2587 :
2588 : /* Process independency */
2589 :
2590 : /* obtain a new process group */
2591 0 : if (setsid() == -1) {
2592 0 : dlt_log(LOG_CRIT, "setsid() failed, exiting DLT daemon\n");
2593 0 : exit(-1); /* fork error */
2594 : }
2595 :
2596 : /* Open standard descriptors stdin, stdout, stderr */
2597 : fd = open("/dev/null", O_RDWR);
2598 :
2599 0 : if (fd != -1) {
2600 : /* Redirect STDOUT to /dev/null */
2601 0 : if (dup2(fd, STDOUT_FILENO) < 0)
2602 0 : dlt_vlog(LOG_WARNING, "Failed to direct stdout to /dev/null. Error: %s\n", strerror(errno));
2603 :
2604 : /* Redirect STDERR to /dev/null */
2605 0 : if (dup2(fd, STDERR_FILENO) < 0)
2606 0 : dlt_vlog(LOG_WARNING, "Failed to direct stderr to /dev/null. Error: %s\n", strerror(errno));
2607 :
2608 0 : close(fd);
2609 : }
2610 : else {
2611 0 : dlt_log(LOG_CRIT, "Error opening /dev/null, exiting DLT daemon\n");
2612 0 : exit(-1); /* fork error */
2613 : }
2614 :
2615 : /* Set umask */
2616 0 : umask(DLT_DAEMON_UMASK);
2617 :
2618 : /* Change to root directory */
2619 0 : if (chdir("/") < 0)
2620 0 : dlt_log(LOG_WARNING, "Failed to chdir to root\n");
2621 :
2622 : /* Catch signals */
2623 0 : signal(SIGCHLD, SIG_IGN); /* ignore child */
2624 0 : signal(SIGTSTP, SIG_IGN); /* ignore tty signals */
2625 0 : signal(SIGTTOU, SIG_IGN);
2626 0 : signal(SIGTTIN, SIG_IGN);
2627 :
2628 0 : } /* dlt_daemon_daemonize() */
2629 :
2630 : /* This function logs str to the configured output sink (socket, serial, offline trace).
2631 : * To avoid recursion this function must be called only from DLT highlevel functions.
2632 : * E. g. calling it to output a failure when the open of the offline trace file fails
2633 : * would cause an endless loop because dlt_daemon_log_internal() would itself again try
2634 : * to open the offline trace file.
2635 : * This is a dlt-daemon only function. The libdlt has no equivalent function available. */
2636 : /* TODO: How to call when it doesn't has version info */
2637 3 : int dlt_daemon_log_internal(DltDaemon *daemon, DltDaemonLocal *daemon_local,
2638 : char *str, DltLogLevelType level,
2639 : const char *app_id, const char *ctx_id, int verbose)
2640 : {
2641 : static uint8_t uiMsgCount = 0;
2642 3 : if (daemon->daemon_version == 2) {
2643 : DltMessageV2 msg;
2644 0 : if (dlt_message_init_v2(&msg, 0) == DLT_RETURN_ERROR)
2645 0 : return DLT_RETURN_ERROR;
2646 :
2647 : uint32_t uiType;
2648 : uint16_t uiSize;
2649 : char ecid_buf[DLT_V2_ID_SIZE];
2650 : char apid_buf[DLT_V2_ID_SIZE];
2651 : char ctid_buf[DLT_V2_ID_SIZE];
2652 :
2653 : DltHtyp2ContentType msgcontent = DLT_VERBOSE_DATA_MSG;
2654 :
2655 0 : PRINT_FUNCTION_VERBOSE(verbose);
2656 :
2657 0 : msg.storageheadersizev2 = (uint32_t)(STORAGE_HEADER_V2_FIXED_SIZE + strlen(DLT_DAEMON_ECU_ID));
2658 0 : msg.baseheadersizev2 = BASE_HEADER_V2_FIXED_SIZE;
2659 0 : msg.baseheaderextrasizev2 = (int32_t)dlt_message_get_extraparameters_size_v2(msgcontent);
2660 : /* Ecu Id, App Id, Ctx Id and Session Id*/
2661 0 : msg.extendedheadersizev2 = (uint32_t)(1 + strlen(DLT_DAEMON_ECU_ID) + 1 + strlen(app_id) + 1 + strlen(ctx_id) + sizeof(uint32_t));
2662 :
2663 0 : msg.headersizev2 = (int32_t) (msg.storageheadersizev2 +
2664 0 : msg.baseheadersizev2 +
2665 0 : msg.baseheaderextrasizev2 +
2666 : msg.extendedheadersizev2);
2667 :
2668 0 : msg.headerbufferv2 = (uint8_t*)malloc((size_t)msg.headersizev2);
2669 :
2670 0 : if (dlt_set_storageheader_v2(&(msg.storageheaderv2), (uint8_t)strlen(DLT_DAEMON_ECU_ID), DLT_DAEMON_ECU_ID) != DLT_RETURN_OK)
2671 : return DLT_RETURN_ERROR;
2672 :
2673 0 : if (dlt_message_set_storageparameters_v2(&msg, 0) != DLT_RETURN_OK)
2674 : return DLT_RETURN_ERROR;
2675 :
2676 : /* Set standardheader */
2677 0 : msg.baseheaderv2 = (DltBaseHeaderV2 *)(msg.headerbufferv2 + msg.storageheadersizev2);
2678 : msg.baseheaderv2->htyp2 = DLT_HTYP2_PROTOCOL_VERSION2;
2679 : msg.baseheaderv2->htyp2 |= msgcontent;
2680 : msg.baseheaderv2->htyp2 |= DLT_HTYP2_WEID;
2681 : msg.baseheaderv2->htyp2 |= DLT_HTYP2_WACID;
2682 0 : msg.baseheaderv2->htyp2 |= DLT_HTYP2_WSID;
2683 :
2684 0 : msg.baseheaderv2->mcnt = uiMsgCount++;
2685 :
2686 : /* Fill base header conditional parameters */
2687 0 : msg.headerextrav2.msin = (uint8_t)(DLT_MSIN_VERB | (DLT_TYPE_LOG << DLT_MSIN_MSTP_SHIFT) |
2688 : ((level << DLT_MSIN_MTIN_SHIFT) & DLT_MSIN_MTIN));
2689 0 : msg.headerextrav2.noar = 1; /* number of arguments */
2690 : memset(msg.headerextrav2.seconds, 0, 5);
2691 0 : msg.headerextrav2.nanoseconds = 0;
2692 :
2693 : #if defined (__WIN32__) || defined(_MSC_VER)
2694 : time_t t = time(NULL);
2695 : if (t==-1){
2696 : uint32_t tcnt = (uint32_t)(GetTickCount()); /* GetTickCount() in 10 ms resolution */
2697 : tcnt_seconds = tcnt / 100;
2698 : tcnt_ns = (tcnt - (tcnt*100)) * 10000;
2699 : msg.headerextrav2.seconds[0]=(tcnt_seconds >> 32) & 0xFF;
2700 : msg.headerextrav2.seconds[1]=(uint8_t)((tcnt_seconds >> 24) & 0xFF);
2701 : msg.headerextrav2.seconds[2]=(uint8_t)((tcnt_seconds >> 16) & 0xFF);
2702 : msg.headerextrav2.seconds[3]=(uint8_t)((tcnt_seconds >> 8) & 0xFF);
2703 : msg.headerextrav2.seconds[4]= (uint8_t)(tcnt_seconds & 0xFF);
2704 : if (ts.tv_nsec < 0x3B9ACA00) {
2705 : msg.headerextrav2.nanoseconds = tcnt_ns;
2706 : }
2707 : }else{
2708 : msg.headerextrav2.seconds[0]=(uint8_t)((t >> 32) & 0xFF);
2709 : msg.headerextrav2.seconds[1]=(uint8_t)((t >> 24) & 0xFF);
2710 : msg.headerextrav2.seconds[2]=(uint8_t)((t >> 16) & 0xFF);
2711 : msg.headerextrav2.seconds[3]=(uint8_t)((t >> 8) & 0xFF);
2712 : msg.headerextrav2.seconds[4]= (uint8_t)(t & 0xFF);
2713 : msg.headerextrav2.nanoseconds |= 0x80000000;
2714 : }
2715 : #else
2716 : struct timespec ts;
2717 0 : if(clock_gettime(CLOCK_REALTIME, &ts) == 0) {
2718 0 : msg.headerextrav2.seconds[0]=(uint8_t)((ts.tv_sec >> 32) & 0xFF);
2719 0 : msg.headerextrav2.seconds[1]=(uint8_t)((ts.tv_sec >> 24) & 0xFF);
2720 0 : msg.headerextrav2.seconds[2]=(uint8_t)((ts.tv_sec >> 16) & 0xFF);
2721 0 : msg.headerextrav2.seconds[3]=(uint8_t)((ts.tv_sec >> 8) & 0xFF);
2722 0 : msg.headerextrav2.seconds[4]= (uint8_t)(ts.tv_sec & 0xFF);
2723 0 : if (ts.tv_nsec < 0x3B9ACA00) {
2724 0 : msg.headerextrav2.nanoseconds = (uint32_t) ts.tv_nsec; /* value is long */
2725 : }
2726 0 : }else if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
2727 0 : msg.headerextrav2.seconds[0]=(uint8_t)((ts.tv_sec >> 32) & 0xFF);
2728 0 : msg.headerextrav2.seconds[1]=(uint8_t)((ts.tv_sec >> 24) & 0xFF);
2729 0 : msg.headerextrav2.seconds[2]=(uint8_t)((ts.tv_sec >> 16) & 0xFF);
2730 0 : msg.headerextrav2.seconds[3]=(uint8_t)((ts.tv_sec >> 8) & 0xFF);
2731 0 : msg.headerextrav2.seconds[4]= (uint8_t)(ts.tv_sec & 0xFF);
2732 0 : if (ts.tv_nsec < 0x3B9ACA00) {
2733 0 : msg.headerextrav2.nanoseconds = (uint32_t) ts.tv_nsec; /* value is long */
2734 : }
2735 0 : msg.headerextrav2.nanoseconds |= 0x80000000;
2736 : }
2737 : #endif
2738 :
2739 : /* Copy header extra parameters to headerbuffer */
2740 0 : if (dlt_message_set_extraparameters_v2(&msg, 0) == DLT_RETURN_ERROR) {
2741 0 : dlt_message_free_v2(&msg, 0);
2742 0 : return DLT_RETURN_ERROR;
2743 : }
2744 :
2745 : /* Fill out extended header */
2746 0 : if (DLT_IS_HTYP2_WEID(msg.baseheaderv2->htyp2)) {
2747 0 : msg.extendedheaderv2.ecidlen = (uint8_t)strlen(DLT_DAEMON_ECU_ID);
2748 : if (msg.extendedheaderv2.ecidlen > 0) {
2749 0 : dlt_set_id_v2(ecid_buf, DLT_DAEMON_ECU_ID, msg.extendedheaderv2.ecidlen);
2750 0 : msg.extendedheaderv2.ecid = ecid_buf;
2751 : } else {
2752 : msg.extendedheaderv2.ecid = NULL;
2753 : }
2754 : }
2755 :
2756 0 : if (DLT_IS_HTYP2_WACID(msg.baseheaderv2->htyp2)) {
2757 0 : msg.extendedheaderv2.apidlen = (uint8_t)strlen(app_id);
2758 0 : if (msg.extendedheaderv2.apidlen > 0) {
2759 0 : dlt_set_id_v2(apid_buf, app_id, msg.extendedheaderv2.apidlen);
2760 0 : msg.extendedheaderv2.apid = apid_buf;
2761 : } else {
2762 0 : msg.extendedheaderv2.apid = NULL;
2763 : }
2764 :
2765 0 : msg.extendedheaderv2.ctidlen = (uint8_t)strlen(ctx_id);
2766 0 : if (msg.extendedheaderv2.ctidlen > 0) {
2767 0 : dlt_set_id_v2(ctid_buf, ctx_id, msg.extendedheaderv2.ctidlen);
2768 0 : msg.extendedheaderv2.ctid = ctid_buf;
2769 : } else {
2770 0 : msg.extendedheaderv2.ctid = NULL;
2771 : }
2772 : }
2773 :
2774 0 : if (DLT_IS_HTYP2_WSID(msg.baseheaderv2->htyp2)) {
2775 0 : msg.extendedheaderv2.seid = (uint32_t)getpid();
2776 : }
2777 :
2778 0 : if (dlt_message_set_extendedparameters_v2(&msg) != DLT_RETURN_OK) {
2779 0 : dlt_message_free_v2(&msg, 0);
2780 0 : return DLT_RETURN_ERROR;
2781 : }
2782 :
2783 : /* Set payload data... */
2784 0 : uiType = DLT_TYPE_INFO_STRG;
2785 0 : uiSize = (uint16_t) (strlen(str) + 1);
2786 0 : msg.datasize = (int32_t) (sizeof(uint32_t) + sizeof(uint16_t) + uiSize);
2787 :
2788 0 : msg.databuffer = (uint8_t *)malloc((size_t) msg.datasize);
2789 0 : msg.databuffersize = msg.datasize;
2790 :
2791 0 : if (msg.databuffer == 0) {
2792 0 : dlt_log(LOG_WARNING, "Can't allocate buffer for get log info message\n");
2793 0 : return -1;
2794 : }
2795 :
2796 0 : msg.datasize = 0;
2797 : memcpy((uint8_t *)(msg.databuffer + msg.datasize), (uint8_t *)(&uiType), sizeof(uint32_t));
2798 0 : msg.datasize += (int32_t) sizeof(uint32_t);
2799 0 : memcpy((uint8_t *)(msg.databuffer + msg.datasize), (uint8_t *)(&uiSize), sizeof(uint16_t));
2800 0 : msg.datasize += (int32_t) sizeof(uint16_t);
2801 0 : memcpy((uint8_t *)(msg.databuffer + msg.datasize), str, uiSize);
2802 0 : msg.datasize += uiSize;
2803 :
2804 : /* Calc length */
2805 0 : msg.baseheaderv2->len = DLT_HTOBE_16((uint16_t)(msg.headersizev2 - (int32_t)msg.storageheadersizev2 + msg.datasize));
2806 :
2807 0 : dlt_daemon_client_send_v2(DLT_DAEMON_SEND_TO_ALL, daemon,daemon_local,
2808 : msg.headerbufferv2, (int)msg.storageheadersizev2,
2809 0 : msg.headerbufferv2 + msg.storageheadersizev2,
2810 0 : (int) (msg.headersizev2 - (int32_t)msg.storageheadersizev2),
2811 0 : msg.databuffer, (int) msg.datasize, verbose);
2812 :
2813 0 : dlt_message_free_v2(&msg, 0);
2814 3 : } else if (daemon->daemon_version == 1) {
2815 3 : DltMessage msg = { 0 };
2816 :
2817 : DltStandardHeaderExtra *pStandardExtra = NULL;
2818 : uint32_t uiType;
2819 : uint16_t uiSize;
2820 : uint32_t uiExtraSize;
2821 :
2822 3 : PRINT_FUNCTION_VERBOSE(verbose);
2823 :
2824 : /* Set storageheader */
2825 3 : msg.storageheader = (DltStorageHeader *)(msg.headerbuffer);
2826 3 : dlt_set_storageheader(msg.storageheader, daemon->ecuid);
2827 :
2828 : /* Set standardheader */
2829 3 : msg.standardheader = (DltStandardHeader *)(msg.headerbuffer + sizeof(DltStorageHeader));
2830 3 : msg.standardheader->htyp = DLT_HTYP_UEH | DLT_HTYP_WEID | DLT_HTYP_WSID | DLT_HTYP_WTMS |
2831 : DLT_HTYP_PROTOCOL_VERSION1;
2832 3 : msg.standardheader->mcnt = uiMsgCount++;
2833 :
2834 : uiExtraSize = (uint32_t) (DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp) +
2835 : (DLT_IS_HTYP_UEH(msg.standardheader->htyp) ? sizeof(DltExtendedHeader) : 0));
2836 3 : msg.headersize = (int32_t)((size_t)sizeof(DltStorageHeader) + (size_t)sizeof(DltStandardHeader) + (size_t)uiExtraSize);
2837 :
2838 : /* Set extraheader */
2839 : pStandardExtra =
2840 : (DltStandardHeaderExtra *)(msg.headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader));
2841 3 : dlt_set_id(pStandardExtra->ecu, daemon->ecuid);
2842 3 : pStandardExtra->tmsp = DLT_HTOBE_32(dlt_uptime());
2843 3 : pStandardExtra->seid = DLT_HTOBE_32((uint32_t)getpid());
2844 :
2845 : /* Set extendedheader */
2846 3 : msg.extendedheader =
2847 3 : (DltExtendedHeader *)(msg.headerbuffer + sizeof(DltStorageHeader) + sizeof(DltStandardHeader) +
2848 3 : DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp));
2849 3 : msg.extendedheader->msin = (uint8_t)(DLT_MSIN_VERB | (DLT_TYPE_LOG << DLT_MSIN_MSTP_SHIFT) |
2850 : ((level << DLT_MSIN_MTIN_SHIFT) & DLT_MSIN_MTIN));
2851 3 : msg.extendedheader->noar = 1;
2852 3 : dlt_set_id(msg.extendedheader->apid, app_id);
2853 3 : dlt_set_id(msg.extendedheader->ctid, ctx_id);
2854 :
2855 : /* Set payload data... */
2856 3 : uiType = DLT_TYPE_INFO_STRG;
2857 3 : uiSize = (uint16_t) (strlen(str) + 1);
2858 3 : msg.datasize = (int32_t)((size_t)sizeof(uint32_t) + (size_t)sizeof(uint16_t) + (size_t)uiSize);
2859 :
2860 3 : msg.databuffer = (uint8_t *)malloc((size_t) msg.datasize);
2861 3 : msg.databuffersize = (int32_t)msg.datasize;
2862 :
2863 3 : if (msg.databuffer == 0) {
2864 0 : dlt_log(LOG_WARNING, "Can't allocate buffer for get log info message\n");
2865 0 : return -1;
2866 : }
2867 :
2868 3 : msg.datasize = 0;
2869 : memcpy((uint8_t *)(msg.databuffer + msg.datasize), (uint8_t *)(&uiType), sizeof(uint32_t));
2870 3 : msg.datasize += (int32_t)sizeof(uint32_t);
2871 3 : memcpy((uint8_t *)(msg.databuffer + msg.datasize), (uint8_t *)(&uiSize), sizeof(uint16_t));
2872 3 : msg.datasize += (int32_t)sizeof(uint16_t);
2873 3 : memcpy((uint8_t *)(msg.databuffer + msg.datasize), str, uiSize);
2874 3 : msg.datasize += (int32_t)uiSize;
2875 :
2876 : /* Calc length */
2877 3 : msg.standardheader->len = DLT_HTOBE_16((size_t)msg.headersize - sizeof(DltStorageHeader) + (size_t)msg.datasize);
2878 :
2879 3 : dlt_daemon_client_send(DLT_DAEMON_SEND_TO_ALL, daemon,daemon_local,
2880 : msg.headerbuffer, sizeof(DltStorageHeader),
2881 : msg.headerbuffer + sizeof(DltStorageHeader),
2882 : (int)(msg.headersize - (int32_t)sizeof(DltStorageHeader)),
2883 3 : msg.databuffer, (int)msg.datasize, verbose);
2884 :
2885 3 : free(msg.databuffer);
2886 : }else {
2887 : return -1;
2888 : }
2889 :
2890 : return 0;
2891 : }
2892 :
2893 0 : int dlt_daemon_check_numeric_setting(char *token,
2894 : char *value,
2895 : unsigned long *data)
2896 0 : {
2897 0 : char value_check[value_length];
2898 0 : value_check[0] = 0;
2899 0 : sscanf(value, "%lu%s", data, value_check);
2900 0 : if (value_check[0] || !isdigit(value[0])) {
2901 0 : fprintf(stderr, "Invalid input [%s] detected in option %s\n",
2902 : value,
2903 : token);
2904 0 : return -1;
2905 : }
2906 : return 0;
2907 : }
2908 :
2909 0 : int dlt_daemon_process_client_connect(DltDaemon *daemon,
2910 : DltDaemonLocal *daemon_local,
2911 : DltReceiver *receiver,
2912 : int verbose)
2913 : {
2914 : socklen_t cli_size;
2915 : struct sockaddr_un cli;
2916 :
2917 : int in_sock = -1;
2918 0 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
2919 :
2920 0 : PRINT_FUNCTION_VERBOSE(verbose);
2921 :
2922 0 : if ((daemon == NULL) || (daemon_local == NULL) || (receiver == NULL)) {
2923 0 : dlt_log(LOG_ERR,
2924 : "Invalid function parameters used for function "
2925 : "dlt_daemon_process_client_connect()\n");
2926 0 : return -1;
2927 : }
2928 :
2929 : /* event from TCP server socket, new connection */
2930 0 : cli_size = sizeof(cli);
2931 :
2932 0 : if ((in_sock = accept(receiver->fd, (struct sockaddr *)&cli, &cli_size)) < 0) {
2933 0 : if (errno == ECONNABORTED) // Caused by nmap -v -p 3490 -Pn <IP of dlt-daemon>
2934 : return 0;
2935 0 : dlt_vlog(LOG_ERR, "accept() for socket %d failed: %s\n", receiver->fd, strerror(errno));
2936 0 : return -1;
2937 : }
2938 :
2939 0 : if (daemon->daemon_version == 2) {
2940 : /* check if file file descriptor was already used, and make it invalid if it
2941 : * is reused. */
2942 : /* This prevents sending messages to wrong file descriptor */
2943 0 : dlt_daemon_applications_invalidate_fd_v2(daemon, daemon->ecuid, in_sock, verbose);
2944 0 : dlt_daemon_contexts_invalidate_fd_v2(daemon, daemon->ecuid, in_sock, verbose);
2945 :
2946 : /* Set socket timeout in reception */
2947 : struct timeval timeout_send;
2948 0 : timeout_send.tv_sec = daemon_local->timeoutOnSend;
2949 0 : timeout_send.tv_usec = 0;
2950 :
2951 0 : if (setsockopt (in_sock,
2952 : SOL_SOCKET,
2953 : SO_SNDTIMEO,
2954 : (char *)&timeout_send,
2955 : sizeof(timeout_send)) < 0)
2956 0 : dlt_log(LOG_WARNING, "setsockopt failed\n");
2957 :
2958 0 : if (dlt_connection_create(daemon_local,
2959 : &daemon_local->pEvent,
2960 : in_sock,
2961 : POLLIN,
2962 : DLT_CONNECTION_CLIENT_MSG_TCP)) {
2963 0 : dlt_log(LOG_ERR, "Failed to register new client. \n");
2964 0 : close(in_sock);
2965 0 : return -1;
2966 : }
2967 :
2968 : /* send connection info about connected */
2969 0 : dlt_daemon_control_message_connection_info_v2(in_sock,
2970 : daemon,
2971 : daemon_local,
2972 : DLT_CONNECTION_STATUS_CONNECTED,
2973 : "",
2974 : verbose);
2975 :
2976 : /* send ecu version string */
2977 0 : if (daemon_local->flags.sendECUSoftwareVersion > 0) {
2978 : if (daemon_local->flags.sendECUSoftwareVersion > 0)
2979 0 : dlt_daemon_control_get_software_version_v2(DLT_DAEMON_SEND_TO_ALL,
2980 : daemon,
2981 : daemon_local,
2982 : daemon_local->flags.vflag);
2983 :
2984 0 : if (daemon_local->flags.sendTimezone > 0)
2985 0 : dlt_daemon_control_message_timezone_v2(DLT_DAEMON_SEND_TO_ALL,
2986 : daemon,
2987 : daemon_local,
2988 : daemon_local->flags.vflag);
2989 : }
2990 :
2991 0 : snprintf(local_str, DLT_DAEMON_TEXTBUFSIZE,
2992 : "New client connection #%d established, Total Clients : %d",
2993 : in_sock, daemon_local->client_connections);
2994 :
2995 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str, DLT_LOG_INFO,
2996 : DLT_DAEMON_APP_ID, DLT_DAEMON_CTX_ID,
2997 : daemon_local->flags.vflag);
2998 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
2999 :
3000 0 : if (daemon_local->client_connections == 1) {
3001 0 : if (daemon_local->flags.vflag)
3002 0 : dlt_log(LOG_DEBUG, "Send ring-buffer to client\n");
3003 :
3004 0 : dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_SEND_BUFFER);
3005 0 : if (dlt_daemon_send_ringbuffer_to_client_v2(daemon, daemon_local, verbose) == -1) {
3006 0 : dlt_log(LOG_WARNING, "Can't send contents of ringbuffer to clients\n");
3007 0 : close(in_sock);
3008 : in_sock = -1;
3009 0 : return -1;
3010 : }
3011 :
3012 : /* send new log state to all applications */
3013 0 : daemon->connectionState = 1;
3014 :
3015 0 : dlt_daemon_user_send_all_log_state_v2(daemon, verbose);
3016 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
3017 : /* Reset number of received bytes from FIFO */
3018 : daemon->bytes_recv = 0;
3019 : #endif
3020 : }
3021 0 : } else if (daemon->daemon_version == 1) {
3022 : /* check if file file descriptor was already used, and make it invalid if it
3023 : * is reused. */
3024 : /* This prevents sending messages to wrong file descriptor */
3025 0 : dlt_daemon_applications_invalidate_fd(daemon, daemon->ecuid, in_sock, verbose);
3026 0 : dlt_daemon_contexts_invalidate_fd(daemon, daemon->ecuid, in_sock, verbose);
3027 :
3028 : /* Set socket timeout in reception */
3029 : struct timeval timeout_send;
3030 0 : timeout_send.tv_sec = daemon_local->timeoutOnSend;
3031 0 : timeout_send.tv_usec = 0;
3032 :
3033 0 : if (setsockopt (in_sock,
3034 : SOL_SOCKET,
3035 : SO_SNDTIMEO,
3036 : (char *)&timeout_send,
3037 : sizeof(timeout_send)) < 0)
3038 0 : dlt_log(LOG_WARNING, "setsockopt failed\n");
3039 :
3040 0 : if (dlt_connection_create(daemon_local,
3041 : &daemon_local->pEvent,
3042 : in_sock,
3043 : POLLIN,
3044 : DLT_CONNECTION_CLIENT_MSG_TCP)) {
3045 0 : dlt_log(LOG_ERR, "Failed to register new client. \n");
3046 0 : close(in_sock);
3047 0 : return -1;
3048 : }
3049 :
3050 : /* send connection info about connected */
3051 0 : dlt_daemon_control_message_connection_info(in_sock,
3052 : daemon,
3053 : daemon_local,
3054 : DLT_CONNECTION_STATUS_CONNECTED,
3055 : "",
3056 : verbose);
3057 :
3058 : /* send ecu version string */
3059 0 : if (daemon_local->flags.sendECUSoftwareVersion > 0) {
3060 : if (daemon_local->flags.sendECUSoftwareVersion > 0)
3061 0 : dlt_daemon_control_get_software_version(DLT_DAEMON_SEND_TO_ALL,
3062 : daemon,
3063 : daemon_local,
3064 : daemon_local->flags.vflag);
3065 :
3066 0 : if (daemon_local->flags.sendTimezone > 0)
3067 0 : dlt_daemon_control_message_timezone(DLT_DAEMON_SEND_TO_ALL,
3068 : daemon,
3069 : daemon_local,
3070 : daemon_local->flags.vflag);
3071 : }
3072 :
3073 0 : snprintf(local_str, DLT_DAEMON_TEXTBUFSIZE,
3074 : "New client connection #%d established, Total Clients : %d",
3075 : in_sock, daemon_local->client_connections);
3076 :
3077 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str, DLT_LOG_INFO,
3078 : DLT_DAEMON_APP_ID, DLT_DAEMON_CTX_ID,
3079 : daemon_local->flags.vflag);
3080 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
3081 :
3082 0 : if (daemon_local->client_connections == 1) {
3083 0 : if (daemon_local->flags.vflag)
3084 0 : dlt_log(LOG_DEBUG, "Send ring-buffer to client\n");
3085 :
3086 0 : dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_SEND_BUFFER);
3087 :
3088 0 : if (dlt_daemon_send_ringbuffer_to_client(daemon, daemon_local, verbose) == -1) {
3089 0 : dlt_log(LOG_WARNING, "Can't send contents of ringbuffer to clients\n");
3090 0 : close(in_sock);
3091 : in_sock = -1;
3092 0 : return -1;
3093 : }
3094 :
3095 : /* send new log state to all applications */
3096 0 : daemon->connectionState = 1;
3097 0 : dlt_daemon_user_send_all_log_state(daemon, verbose);
3098 :
3099 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
3100 : /* Reset number of received bytes from FIFO */
3101 : daemon->bytes_recv = 0;
3102 : #endif
3103 : }
3104 : }else {
3105 : return -1;
3106 : }
3107 : return 0;
3108 : }
3109 :
3110 : /**
3111 : * Create a sanitized copy of the message for control processing
3112 : * This breaks the taint chain by creating a new clean message structure
3113 : * Uses allow list validation for all path-containing messages
3114 : * @param tainted_msg The tainted message from receiver
3115 : * @param secure_msg The secure message to populate (output)
3116 : * @return 0 on success, -1 on validation failure
3117 : */
3118 1 : static int dlt_daemon_create_secure_message(DltMessage *tainted_msg, DltMessage *secure_msg)
3119 : {
3120 : uint32_t service_id;
3121 : DltServiceOfflineLogstorage *tainted_req;
3122 : DltServiceOfflineLogstorage *secure_req;
3123 : char secure_mount_point[DLT_MOUNT_PATH_MAX];
3124 : size_t i;
3125 :
3126 1 : if ((tainted_msg == NULL) || (secure_msg == NULL))
3127 : return -1;
3128 :
3129 : /* Initialize secure message with clean data */
3130 : memset(secure_msg, 0, sizeof(DltMessage));
3131 :
3132 : /* Copy safe, non-tainted fields from message structure */
3133 1 : secure_msg->headersize = tainted_msg->headersize;
3134 1 : secure_msg->datasize = tainted_msg->datasize;
3135 1 : secure_msg->found_serialheader = tainted_msg->found_serialheader;
3136 1 : secure_msg->resync_offset = tainted_msg->resync_offset;
3137 :
3138 : /* Copy header buffers (these don't contain user-controlled paths) */
3139 1 : if (tainted_msg->headersize > 0 && tainted_msg->headersize < (int32_t)sizeof(secure_msg->headerbuffer)) {
3140 39 : for (i = 0; i < (size_t)tainted_msg->headersize; i++) {
3141 38 : secure_msg->headerbuffer[i] = tainted_msg->headerbuffer[i];
3142 : }
3143 : }
3144 :
3145 : /* Set up header pointers */
3146 1 : secure_msg->storageheader = (DltStorageHeader *)(secure_msg->headerbuffer);
3147 1 : secure_msg->standardheader = (DltStandardHeader *)(secure_msg->headerbuffer + sizeof(DltStorageHeader));
3148 : if (DLT_IS_HTYP_UEH(secure_msg->standardheader->htyp)) {
3149 : secure_msg->extendedheader = (DltExtendedHeader *)(secure_msg->headerbuffer +
3150 : sizeof(DltStorageHeader) +
3151 : sizeof(DltStandardHeader) +
3152 : DLT_STANDARD_HEADER_EXTRA_SIZE(secure_msg->standardheader->htyp));
3153 : }
3154 :
3155 : /* Point to the same databuffer initially */
3156 1 : secure_msg->databuffer = tainted_msg->databuffer;
3157 1 : secure_msg->databuffersize = tainted_msg->databuffersize;
3158 :
3159 1 : if (tainted_msg->databuffer == NULL || tainted_msg->datasize < (int32_t)sizeof(uint32_t))
3160 : return 0;
3161 :
3162 : /* Get service ID */
3163 1 : service_id = DLT_ENDIAN_GET_32(tainted_msg->standardheader->htyp,
3164 : *((uint32_t *)(tainted_msg->databuffer)));
3165 :
3166 : /* Sanitize logstorage messages with path validation */
3167 1 : if (service_id == DLT_SERVICE_ID_OFFLINE_LOGSTORAGE) {
3168 : /* Cast to logstorage request structure */
3169 : tainted_req = (DltServiceOfflineLogstorage *)(tainted_msg->databuffer);
3170 : if (tainted_req == NULL)
3171 : return -1;
3172 :
3173 : /* Ensure null-termination */
3174 1 : tainted_req->mount_point[DLT_MOUNT_PATH_MAX - 1] = '\0';
3175 :
3176 : /* Get path length */
3177 1 : size_t path_len = strnlen(tainted_req->mount_point, DLT_MOUNT_PATH_MAX);
3178 :
3179 : /* Validate basic path requirements */
3180 1 : if (path_len == 0) {
3181 : /* Empty path only allowed for sync all caches operation */
3182 1 : if (tainted_req->connection_type != DLT_OFFLINE_LOGSTORAGE_SYNC_CACHES) {
3183 0 : dlt_vlog(LOG_WARNING, "Rejected logstorage message with empty path\n");
3184 0 : return -1;
3185 : }
3186 : } else {
3187 : /* Path must be absolute (start with /) */
3188 0 : if (tainted_req->mount_point[0] != '/') {
3189 0 : dlt_vlog(LOG_WARNING, "Rejected logstorage message with non-absolute path\n");
3190 0 : return -1;
3191 : }
3192 : }
3193 :
3194 : /* Now sanitize the databuffer */
3195 : secure_req = (DltServiceOfflineLogstorage *)(secure_msg->databuffer);
3196 :
3197 : /* Explicitly sanitize: copy path to secure local buffer using allow list */
3198 : memset(secure_mount_point, 0, sizeof(secure_mount_point));
3199 1 : for (i = 0; i < DLT_MOUNT_PATH_MAX - 1 && tainted_req->mount_point[i] != '\0'; i++) {
3200 : char c = tainted_req->mount_point[i];
3201 : /* Apply allow list - only safe characters allowed */
3202 0 : if ((c >= 'a' && c <= 'z') ||
3203 0 : (c >= 'A' && c <= 'Z') ||
3204 : (c >= '0' && c <= '9') ||
3205 : c == '/' || c == '-' || c == '_' || c == '.') {
3206 0 : secure_mount_point[i] = c;
3207 : } else {
3208 0 : dlt_vlog(LOG_WARNING, "Rejected logstorage message with invalid character in path at position %zu\n", i);
3209 0 : return -1;
3210 : }
3211 : }
3212 1 : secure_mount_point[DLT_MOUNT_PATH_MAX - 1] = '\0';
3213 :
3214 : /* Check for path traversal attempts (defense in depth) */
3215 1 : if (strstr(secure_mount_point, "..") != NULL) {
3216 0 : dlt_vlog(LOG_WARNING, "Rejected logstorage message with path traversal attempt\n");
3217 0 : return -1;
3218 : }
3219 :
3220 : /* Deny consecutive slashes */
3221 1 : if (strstr(secure_mount_point, "//") != NULL) {
3222 0 : dlt_vlog(LOG_WARNING, "Rejected logstorage message with consecutive slashes in path\n");
3223 0 : return -1;
3224 : }
3225 :
3226 : /* Copy secure data to the secure message's databuffer */
3227 1025 : for (i = 0; i < DLT_MOUNT_PATH_MAX; i++) {
3228 1024 : secure_req->mount_point[i] = secure_mount_point[i];
3229 : }
3230 : }
3231 :
3232 : return 0;
3233 : }
3234 :
3235 0 : int dlt_daemon_process_client_messages(DltDaemon *daemon,
3236 : DltDaemonLocal *daemon_local,
3237 : DltReceiver *receiver,
3238 : int verbose)
3239 : {
3240 : int bytes_to_be_removed = 0;
3241 : int must_close_socket = -1;
3242 :
3243 0 : PRINT_FUNCTION_VERBOSE(verbose);
3244 :
3245 0 : if ((daemon == NULL) || (daemon_local == NULL) || (receiver == NULL)) {
3246 0 : dlt_log(LOG_ERR,
3247 : "Invalid function parameters used for function "
3248 : "dlt_daemon_process_client_messages()\n");
3249 0 : return -1;
3250 : }
3251 :
3252 0 : must_close_socket = dlt_receiver_receive(receiver);
3253 :
3254 0 : if (must_close_socket < 0) {
3255 0 : dlt_daemon_close_socket(receiver->fd,
3256 : daemon,
3257 : daemon_local,
3258 : verbose);
3259 0 : return -1;
3260 : }
3261 :
3262 0 : if(daemon->daemon_version == DLTProtocolV2) {
3263 : /* Process all received messages */
3264 0 : while (dlt_message_read_v2(&(daemon_local->msgv2),
3265 0 : (uint8_t *)receiver->buf,
3266 0 : (unsigned int) receiver->bytesRcvd,
3267 : daemon_local->flags.nflag,
3268 0 : daemon_local->flags.vflag) == DLT_MESSAGE_ERROR_OK) {
3269 :
3270 : /* Check for control message */
3271 0 : if ((0 < receiver->fd) &&
3272 0 : DLT_MSG_IS_CONTROL_REQUEST_V2(&(daemon_local->msgv2)))
3273 0 : dlt_daemon_client_process_control_v2(receiver->fd,
3274 : daemon,
3275 : daemon_local,
3276 : &(daemon_local->msgv2),
3277 : daemon_local->flags.vflag);
3278 0 : bytes_to_be_removed = (int) (daemon_local->msgv2.headersizev2 +
3279 0 : daemon_local->msgv2.datasize - (int32_t)daemon_local->msgv2.storageheadersizev2);
3280 :
3281 0 : if (daemon_local->msg.found_serialheader)
3282 0 : bytes_to_be_removed += (int) sizeof(dltSerialHeader);
3283 :
3284 0 : if (daemon_local->msg.resync_offset)
3285 0 : bytes_to_be_removed += daemon_local->msg.resync_offset;
3286 :
3287 0 : if (dlt_receiver_remove(receiver, bytes_to_be_removed) == -1) {
3288 0 : dlt_log(LOG_WARNING,
3289 : "Can't remove bytes from receiver for sockets\n");
3290 0 : return -1;
3291 : }
3292 : } /* while */
3293 0 : } else if (daemon->daemon_version == DLTProtocolV1) {
3294 : /* Process all received messages */
3295 0 : while (dlt_message_read(&(daemon_local->msg),
3296 0 : (uint8_t *)receiver->buf,
3297 0 : (unsigned int) receiver->bytesRcvd,
3298 : daemon_local->flags.nflag,
3299 0 : daemon_local->flags.vflag) == DLT_MESSAGE_ERROR_OK) {
3300 :
3301 : /* Check for control message */
3302 0 : if ((0 < receiver->fd) &&
3303 0 : DLT_MSG_IS_CONTROL_REQUEST(&(daemon_local->msg)))
3304 0 : dlt_daemon_client_process_control(receiver->fd,
3305 : daemon,
3306 : daemon_local,
3307 : &(daemon_local->msg),
3308 : daemon_local->flags.vflag);
3309 :
3310 0 : bytes_to_be_removed = (int)((size_t)daemon_local->msg.headersize +
3311 0 : (size_t)daemon_local->msg.datasize -
3312 : (size_t)sizeof(DltStorageHeader));
3313 :
3314 0 : if (daemon_local->msg.found_serialheader)
3315 : bytes_to_be_removed += (int) sizeof(dltSerialHeader);
3316 :
3317 0 : if (daemon_local->msg.resync_offset)
3318 0 : bytes_to_be_removed += daemon_local->msg.resync_offset;
3319 :
3320 0 : if (dlt_receiver_remove(receiver, bytes_to_be_removed) == -1) {
3321 0 : dlt_log(LOG_WARNING,
3322 : "Can't remove bytes from receiver for sockets\n");
3323 0 : return -1;
3324 : }
3325 : } /* while */
3326 : } else {
3327 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
3328 0 : return -1;
3329 : }
3330 :
3331 0 : if (dlt_receiver_move_to_begin(receiver) == -1) {
3332 0 : dlt_log(LOG_WARNING,
3333 : "Can't move bytes to beginning of receiver buffer for sockets\n");
3334 0 : return -1;
3335 : }
3336 :
3337 0 : if (must_close_socket == 0)
3338 : /* FIXME: Why the hell do we need to close the socket
3339 : * on control message reception ??
3340 : */
3341 0 : dlt_daemon_close_socket(receiver->fd,
3342 : daemon,
3343 : daemon_local,
3344 : verbose);
3345 :
3346 : return 0;
3347 : }
3348 :
3349 0 : int dlt_daemon_process_client_messages_serial(DltDaemon *daemon,
3350 : DltDaemonLocal *daemon_local,
3351 : DltReceiver *receiver,
3352 : int verbose)
3353 : {
3354 : int bytes_to_be_removed = 0;
3355 :
3356 0 : PRINT_FUNCTION_VERBOSE(verbose);
3357 :
3358 0 : if ((daemon == NULL) || (daemon_local == NULL) || (receiver == NULL)) {
3359 0 : dlt_log(LOG_ERR,
3360 : "Invalid function parameters used for function "
3361 : "dlt_daemon_process_client_messages_serial()\n");
3362 0 : return -1;
3363 : }
3364 :
3365 0 : if (dlt_receiver_receive(receiver) <= 0) {
3366 0 : dlt_log(LOG_WARNING,
3367 : "dlt_receiver_receive_fd() for messages from serial interface "
3368 : "failed!\n");
3369 0 : return -1;
3370 : }
3371 :
3372 : /* Process all received messages */
3373 0 : while (dlt_message_read(&(daemon_local->msg),
3374 0 : (uint8_t *)receiver->buf,
3375 0 : (unsigned int) receiver->bytesRcvd,
3376 : daemon_local->flags.mflag,
3377 0 : daemon_local->flags.vflag) == DLT_MESSAGE_ERROR_OK) {
3378 :
3379 : /* Check for control message */
3380 0 : if (DLT_MSG_IS_CONTROL_REQUEST(&(daemon_local->msg))) {
3381 0 : DltMessage secure_msg = {0};
3382 : /* Create sanitized message to break taint chain */
3383 0 : if (dlt_daemon_create_secure_message(&(daemon_local->msg), &secure_msg) < 0)
3384 0 : continue;
3385 :
3386 0 : if (dlt_daemon_client_process_control(receiver->fd,
3387 : daemon,
3388 : daemon_local,
3389 : &(daemon_local->msg),
3390 : daemon_local->flags.vflag)
3391 : == -1) {
3392 0 : dlt_log(LOG_WARNING, "Can't process control messages\n");
3393 0 : return -1;
3394 : }
3395 : }
3396 :
3397 0 : bytes_to_be_removed = (int)((size_t)daemon_local->msg.headersize +
3398 0 : (size_t)daemon_local->msg.datasize -
3399 : sizeof(DltStorageHeader));
3400 :
3401 0 : if (daemon_local->msg.found_serialheader)
3402 : bytes_to_be_removed += (int) sizeof(dltSerialHeader);
3403 :
3404 0 : if (daemon_local->msg.resync_offset)
3405 0 : bytes_to_be_removed += daemon_local->msg.resync_offset;
3406 :
3407 0 : if (dlt_receiver_remove(receiver, bytes_to_be_removed) == -1) {
3408 0 : dlt_log(LOG_WARNING,
3409 : "Can't remove bytes from receiver for serial connection\n");
3410 0 : return -1;
3411 : }
3412 : } /* while */
3413 :
3414 0 : if (dlt_receiver_move_to_begin(receiver) == -1) {
3415 0 : dlt_log(LOG_WARNING,
3416 : "Can't move bytes to beginning of receiver buffer for serial "
3417 : "connection\n");
3418 0 : return -1;
3419 : }
3420 :
3421 : return 0;
3422 : }
3423 :
3424 1 : int dlt_daemon_process_control_connect(
3425 : DltDaemon *daemon,
3426 : DltDaemonLocal *daemon_local,
3427 : DltReceiver *receiver,
3428 : int verbose)
3429 : {
3430 : socklen_t ctrl_size;
3431 : struct sockaddr_un ctrl;
3432 : int in_sock = -1;
3433 :
3434 1 : PRINT_FUNCTION_VERBOSE(verbose);
3435 :
3436 1 : if ((daemon == NULL) || (daemon_local == NULL) || (receiver == NULL)) {
3437 0 : dlt_log(LOG_ERR,
3438 : "Invalid function parameters used for function "
3439 : "dlt_daemon_process_control_connect()\n");
3440 0 : return -1;
3441 : }
3442 :
3443 : /* event from UNIX server socket, new connection */
3444 1 : ctrl_size = sizeof(ctrl);
3445 :
3446 1 : if ((in_sock = accept(receiver->fd, (struct sockaddr *)&ctrl, &ctrl_size)) < 0) {
3447 0 : dlt_vlog(LOG_ERR, "accept() on UNIX control socket %d failed: %s\n", receiver->fd, strerror(errno));
3448 0 : return -1;
3449 : }
3450 :
3451 : /* check if file file descriptor was already used, and make it invalid if it
3452 : * is reused */
3453 : /* This prevents sending messages to wrong file descriptor */
3454 1 : if (daemon->daemon_version == 2) {
3455 0 : dlt_daemon_applications_invalidate_fd_v2(daemon, daemon->ecuid, in_sock, verbose);
3456 0 : dlt_daemon_contexts_invalidate_fd_v2(daemon, daemon->ecuid, in_sock, verbose);
3457 1 : }else if (daemon->daemon_version == 1) {
3458 1 : dlt_daemon_applications_invalidate_fd(daemon, daemon->ecuid, in_sock, verbose);
3459 1 : dlt_daemon_contexts_invalidate_fd(daemon, daemon->ecuid, in_sock, verbose);
3460 : }else {
3461 : return -1;
3462 : }
3463 1 : dlt_daemon_applications_invalidate_fd(daemon, daemon->ecuid, in_sock, verbose);
3464 1 : dlt_daemon_contexts_invalidate_fd(daemon, daemon->ecuid, in_sock, verbose);
3465 :
3466 1 : if (dlt_connection_create(daemon_local,
3467 : &daemon_local->pEvent,
3468 : in_sock,
3469 : POLLIN,
3470 : DLT_CONNECTION_CONTROL_MSG)) {
3471 0 : dlt_log(LOG_ERR, "Failed to register new client. \n");
3472 : /* TODO: Perform clean-up */
3473 0 : return -1;
3474 : }
3475 :
3476 1 : if (verbose)
3477 0 : dlt_vlog(LOG_INFO, "New connection to control client established\n");
3478 :
3479 : return 0;
3480 : }
3481 :
3482 : #if defined DLT_DAEMON_USE_UNIX_SOCKET_IPC || defined DLT_DAEMON_VSOCK_IPC_ENABLE
3483 : int dlt_daemon_process_app_connect(
3484 : DltDaemon *daemon,
3485 : DltDaemonLocal *daemon_local,
3486 : DltReceiver *receiver,
3487 : int verbose)
3488 : {
3489 : int in_sock = -1;
3490 :
3491 : PRINT_FUNCTION_VERBOSE(verbose);
3492 :
3493 : if ((daemon == NULL) || (daemon_local == NULL) || (receiver == NULL)) {
3494 : dlt_vlog(LOG_ERR,
3495 : "%s: Invalid parameters\n",
3496 : __func__);
3497 : return DLT_RETURN_WRONG_PARAMETER;
3498 : }
3499 :
3500 : /* event from server socket, new connection */
3501 :
3502 : if ((in_sock = accept(receiver->fd, NULL, NULL)) < 0) {
3503 : dlt_vlog(LOG_ERR, "accept() on UNIX socket %d failed: %s\n", receiver->fd, strerror(errno));
3504 : return -1;
3505 : }
3506 :
3507 : /* check if file file descriptor was already used, and make it invalid if it
3508 : * is reused. This prevents sending messages to wrong file descriptor */
3509 : dlt_daemon_applications_invalidate_fd(daemon, daemon->ecuid, in_sock, verbose);
3510 : dlt_daemon_contexts_invalidate_fd(daemon, daemon->ecuid, in_sock, verbose);
3511 :
3512 : if (dlt_connection_create(daemon_local,
3513 : &daemon_local->pEvent,
3514 : in_sock,
3515 : POLLIN,
3516 : DLT_CONNECTION_APP_MSG)) {
3517 : dlt_log(LOG_ERR, "Failed to register new application. \n");
3518 : close(in_sock);
3519 : return -1;
3520 : }
3521 :
3522 : if (verbose)
3523 : dlt_vlog(LOG_INFO, "New connection to application established\n");
3524 :
3525 : return 0;
3526 : }
3527 : #endif
3528 :
3529 2 : int dlt_daemon_process_control_messages(
3530 : DltDaemon *daemon,
3531 : DltDaemonLocal *daemon_local,
3532 : DltReceiver *receiver,
3533 : int verbose)
3534 : {
3535 : int bytes_to_be_removed = 0;
3536 :
3537 2 : PRINT_FUNCTION_VERBOSE(verbose);
3538 :
3539 2 : if ((daemon == NULL) || (daemon_local == NULL) || (receiver == NULL)) {
3540 0 : dlt_log(LOG_ERR,
3541 : "Invalid function parameters used for function "
3542 : "dlt_daemon_process_control_messages()\n");
3543 0 : return -1;
3544 : }
3545 :
3546 2 : if (dlt_receiver_receive(receiver) <= 0) {
3547 1 : dlt_daemon_close_socket(receiver->fd,
3548 : daemon,
3549 : daemon_local,
3550 : verbose);
3551 1 : return -1;
3552 : }
3553 :
3554 1 : if(daemon->daemon_version == DLTProtocolV2) {
3555 : /* Process all received messages */
3556 0 : while (dlt_message_read_v2(&(daemon_local->msgv2),
3557 0 : (uint8_t *)receiver->buf,
3558 0 : (unsigned int) receiver->bytesRcvd,
3559 : daemon_local->flags.nflag,
3560 0 : daemon_local->flags.vflag) == DLT_MESSAGE_ERROR_OK) {
3561 :
3562 : /* Check for control message */
3563 0 : if ((0 < receiver->fd) &&
3564 0 : DLT_MSG_IS_CONTROL_REQUEST_V2(&(daemon_local->msgv2)))
3565 0 : dlt_daemon_client_process_control_v2(receiver->fd,
3566 : daemon,
3567 : daemon_local,
3568 : &(daemon_local->msgv2),
3569 : daemon_local->flags.vflag);
3570 0 : bytes_to_be_removed = (int) (daemon_local->msgv2.headersizev2
3571 0 : + daemon_local->msgv2.datasize
3572 0 : - (int32_t)daemon_local->msgv2.storageheadersizev2);
3573 :
3574 0 : if (daemon_local->msg.found_serialheader)
3575 0 : bytes_to_be_removed += (int) sizeof(dltSerialHeader);
3576 :
3577 0 : if (daemon_local->msg.resync_offset)
3578 0 : bytes_to_be_removed += daemon_local->msg.resync_offset;
3579 :
3580 0 : if (dlt_receiver_remove(receiver, bytes_to_be_removed) == -1) {
3581 0 : dlt_log(LOG_WARNING,
3582 : "Can't remove bytes from receiver for sockets\n");
3583 0 : return -1;
3584 : }
3585 : } /* while */
3586 : }
3587 1 : else if (daemon->daemon_version == DLTProtocolV1) {
3588 : /* Process all received messages */
3589 2 : while (dlt_message_read(
3590 : &(daemon_local->msg),
3591 2 : (uint8_t *)receiver->buf,
3592 2 : (unsigned int) receiver->bytesRcvd,
3593 : daemon_local->flags.nflag,
3594 2 : daemon_local->flags.vflag) == DLT_MESSAGE_ERROR_OK) {
3595 :
3596 : /* Check for control message */
3597 1 : if ((receiver->fd > 0) &&
3598 1 : DLT_MSG_IS_CONTROL_REQUEST(&(daemon_local->msg))) {
3599 1 : DltMessage secure_msg = {0};
3600 : /* Create sanitized message to break taint chain */
3601 1 : if (dlt_daemon_create_secure_message(&(daemon_local->msg), &secure_msg) < 0)
3602 0 : continue;
3603 :
3604 1 : if (dlt_daemon_client_process_control(receiver->fd,
3605 : daemon,
3606 : daemon_local,
3607 : &secure_msg,
3608 : daemon_local->flags.vflag)
3609 : == -1) {
3610 0 : dlt_log(LOG_WARNING, "Can't process control messages\n");
3611 0 : return -1;
3612 : }
3613 : }
3614 :
3615 1 : bytes_to_be_removed = (int)((size_t)daemon_local->msg.headersize +
3616 1 : (size_t)daemon_local->msg.datasize -
3617 : sizeof(DltStorageHeader));
3618 :
3619 1 : if (daemon_local->msg.found_serialheader)
3620 : bytes_to_be_removed += (int) sizeof(dltSerialHeader);
3621 :
3622 1 : if (daemon_local->msg.resync_offset)
3623 0 : bytes_to_be_removed += daemon_local->msg.resync_offset;
3624 :
3625 1 : if (dlt_receiver_remove(receiver, bytes_to_be_removed) == -1) {
3626 0 : dlt_log(LOG_WARNING,
3627 : "Can't remove bytes from receiver for sockets\n");
3628 0 : return -1;
3629 : }
3630 : } /* while */
3631 : } else {
3632 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
3633 0 : return -1;
3634 : }
3635 :
3636 1 : if (dlt_receiver_move_to_begin(receiver) == -1) {
3637 0 : dlt_log(LOG_WARNING, "Can't move bytes to beginning of receiver buffer for sockets\n");
3638 0 : return -1;
3639 : }
3640 :
3641 : return 0;
3642 : }
3643 :
3644 0 : static int dlt_daemon_process_user_message_not_sup(DltDaemon *daemon,
3645 : DltDaemonLocal *daemon_local,
3646 : DltReceiver *receiver,
3647 : int verbose)
3648 : {
3649 0 : DltUserHeader *userheader = (DltUserHeader *)(receiver->buf);
3650 : (void)daemon;
3651 : (void)daemon_local;
3652 :
3653 0 : PRINT_FUNCTION_VERBOSE(verbose);
3654 :
3655 0 : dlt_vlog(LOG_ERR, "Invalid user message type received: %u!\n",
3656 : userheader->message);
3657 :
3658 : /* remove user header */
3659 0 : if (dlt_receiver_remove(receiver, sizeof(DltUserHeader)) == -1)
3660 0 : dlt_log(LOG_WARNING,
3661 : "Can't remove bytes from receiver for user messages\n");
3662 :
3663 0 : return -1;
3664 : }
3665 :
3666 : static dlt_daemon_process_user_message_func process_user_func[DLT_USER_MESSAGE_NOT_SUPPORTED] = {
3667 : dlt_daemon_process_user_message_not_sup,
3668 : dlt_daemon_process_user_message_log,
3669 : dlt_daemon_process_user_message_register_application,
3670 : dlt_daemon_process_user_message_unregister_application,
3671 : dlt_daemon_process_user_message_register_context,
3672 : dlt_daemon_process_user_message_unregister_context,
3673 : dlt_daemon_process_user_message_not_sup,
3674 : dlt_daemon_process_user_message_not_sup,
3675 : dlt_daemon_process_user_message_overflow,
3676 : dlt_daemon_process_user_message_set_app_ll_ts,
3677 : dlt_daemon_process_user_message_not_sup,
3678 : dlt_daemon_process_user_message_not_sup,
3679 : dlt_daemon_process_user_message_not_sup,
3680 : dlt_daemon_process_user_message_marker,
3681 : dlt_daemon_process_user_message_not_sup,
3682 : dlt_daemon_process_user_message_not_sup
3683 : };
3684 :
3685 115 : int dlt_daemon_process_user_messages(DltDaemon *daemon,
3686 : DltDaemonLocal *daemon_local,
3687 : DltReceiver *receiver,
3688 : int verbose)
3689 : {
3690 : int offset = 0;
3691 : int run_loop = 1;
3692 : int32_t min_size = (int32_t) sizeof(DltUserHeader);
3693 : DltUserHeader *userheader;
3694 : int recv;
3695 :
3696 115 : PRINT_FUNCTION_VERBOSE(verbose);
3697 :
3698 115 : if ((daemon == NULL) || (daemon_local == NULL) || (receiver == NULL)) {
3699 0 : dlt_log(LOG_ERR,
3700 : "Invalid function parameters used for function "
3701 : "dlt_daemon_process_user_messages()\n");
3702 0 : return -1;
3703 : }
3704 :
3705 115 : recv = dlt_receiver_receive(receiver);
3706 :
3707 115 : if (recv <= 0 && receiver->type == DLT_RECEIVE_SOCKET) {
3708 0 : dlt_daemon_close_socket(receiver->fd,
3709 : daemon,
3710 : daemon_local,
3711 : verbose);
3712 0 : return 0;
3713 : }
3714 0 : else if (recv < 0) {
3715 0 : dlt_log(LOG_WARNING,
3716 : "dlt_receiver_receive_fd() for user messages failed!\n");
3717 0 : return -1;
3718 : }
3719 :
3720 :
3721 115 : if (daemon->daemon_version == DLTProtocolV2) {
3722 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
3723 : /* Count up number of received bytes from FIFO */
3724 : if (receiver->bytesRcvd > receiver->lastBytesRcvd)
3725 : {
3726 : daemon->bytes_recv += receiver->bytesRcvd - receiver->lastBytesRcvd;
3727 : }
3728 : #endif
3729 :
3730 : /* look through buffer as long as data is in there */
3731 0 : while ((receiver->bytesRcvd >= min_size) && run_loop) {
3732 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
3733 : /* this loop may be running long, so we have to exit it at some point to be able to
3734 : * to process other events, like feeding the watchdog
3735 : */
3736 : bool watchdog_triggered= dlt_daemon_trigger_systemd_watchdog_if_necessary(daemon);
3737 : if (watchdog_triggered) {
3738 : dlt_vlog(LOG_WARNING, "%s yields due to watchdog.\n", __func__);
3739 : run_loop = 0; // exit loop in next iteration
3740 : }
3741 : #endif
3742 : dlt_daemon_process_user_message_func func = NULL;
3743 :
3744 : offset = 0;
3745 0 : userheader = (DltUserHeader *)(receiver->buf + offset);
3746 :
3747 0 : while (!dlt_user_check_userheader(userheader) &&
3748 0 : (offset + min_size <= receiver->bytesRcvd)) {
3749 : /* resync if necessary */
3750 0 : offset++;
3751 0 : userheader = (DltUserHeader *)(receiver->buf + offset);
3752 : }
3753 :
3754 : /* Check for user header pattern */
3755 0 : if (!dlt_user_check_userheader(userheader))
3756 : break;
3757 :
3758 : /* Set new start offset */
3759 0 : if (offset > 0) {
3760 0 : if (dlt_receiver_remove(receiver, offset) == -1) {
3761 0 : dlt_log(LOG_WARNING,
3762 : "Can't remove offset from receiver\n");
3763 0 : return -1;
3764 : }
3765 : }
3766 0 : if (userheader->message >= DLT_USER_MESSAGE_NOT_SUPPORTED)
3767 : func = dlt_daemon_process_user_message_not_sup;
3768 : else
3769 0 : func = process_user_func[userheader->message];
3770 :
3771 0 : if (func(daemon,
3772 : daemon_local,
3773 : receiver,
3774 : daemon_local->flags.vflag) == -1)
3775 : run_loop = 0;
3776 : }
3777 :
3778 : /* keep not read data in buffer */
3779 0 : if (dlt_receiver_move_to_begin(receiver) == -1) {
3780 0 : dlt_log(LOG_WARNING,
3781 : "Can't move bytes to beginning of receiver buffer for user "
3782 : "messages\n");
3783 0 : return -1;
3784 : }
3785 115 : } else if (daemon->daemon_version == DLTProtocolV1) {
3786 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
3787 : /* Count up number of received bytes from FIFO */
3788 : if (receiver->bytesRcvd > receiver->lastBytesRcvd)
3789 : {
3790 : daemon->bytes_recv += receiver->bytesRcvd - receiver->lastBytesRcvd;
3791 : }
3792 : #endif
3793 :
3794 : /* look through buffer as long as data is in there */
3795 384 : while ((receiver->bytesRcvd >= min_size) && run_loop) {
3796 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
3797 : /* this loop may be running long, so we have to exit it at some point to be able to
3798 : * to process other events, like feeding the watchdog
3799 : */
3800 : bool watchdog_triggered= dlt_daemon_trigger_systemd_watchdog_if_necessary(daemon);
3801 : if (watchdog_triggered) {
3802 : dlt_vlog(LOG_WARNING, "%s yields due to watchdog.\n", __func__);
3803 : run_loop = 0; // exit loop in next iteration
3804 : }
3805 : #endif
3806 : dlt_daemon_process_user_message_func func = NULL;
3807 :
3808 : offset = 0;
3809 269 : userheader = (DltUserHeader *)(receiver->buf + offset);
3810 :
3811 269 : while (!dlt_user_check_userheader(userheader) &&
3812 0 : (offset + min_size <= receiver->bytesRcvd)) {
3813 : /* resync if necessary */
3814 0 : offset++;
3815 0 : userheader = (DltUserHeader *)(receiver->buf + offset);
3816 : }
3817 :
3818 : /* Check for user header pattern */
3819 269 : if (!dlt_user_check_userheader(userheader))
3820 : break;
3821 :
3822 : /* Set new start offset */
3823 269 : if (offset > 0) {
3824 0 : if (dlt_receiver_remove(receiver, offset) == -1) {
3825 0 : dlt_log(LOG_WARNING,
3826 : "Can't remove offset from receiver\n");
3827 0 : return -1;
3828 : }
3829 : }
3830 :
3831 269 : if (userheader->message >= DLT_USER_MESSAGE_NOT_SUPPORTED)
3832 : func = dlt_daemon_process_user_message_not_sup;
3833 : else
3834 269 : func = process_user_func[userheader->message];
3835 :
3836 269 : if (func(daemon,
3837 : daemon_local,
3838 : receiver,
3839 : daemon_local->flags.vflag) == -1)
3840 : run_loop = 0;
3841 : }
3842 :
3843 : /* keep not read data in buffer */
3844 115 : if (dlt_receiver_move_to_begin(receiver) == -1) {
3845 0 : dlt_log(LOG_WARNING,
3846 : "Can't move bytes to beginning of receiver buffer for user "
3847 : "messages\n");
3848 0 : return -1;
3849 : }
3850 : } else {
3851 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
3852 0 : return -1;
3853 : }
3854 :
3855 : return 0;
3856 : }
3857 :
3858 0 : int dlt_daemon_process_user_message_overflow(DltDaemon *daemon,
3859 : DltDaemonLocal *daemon_local,
3860 : DltReceiver *rec,
3861 : int verbose)
3862 : {
3863 : uint32_t len = sizeof(DltUserControlMsgBufferOverflow);
3864 : DltUserControlMsgBufferOverflow userpayload;
3865 :
3866 0 : PRINT_FUNCTION_VERBOSE(verbose);
3867 :
3868 0 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
3869 0 : dlt_vlog(LOG_ERR, "Invalid function parameters used for %s\n",
3870 : __func__);
3871 0 : return -1;
3872 : }
3873 :
3874 0 : if (dlt_receiver_check_and_get(rec,
3875 : &userpayload,
3876 : len,
3877 : DLT_RCV_SKIP_HEADER | DLT_RCV_REMOVE) < 0)
3878 : /* Not enough bytes received */
3879 : return -1;
3880 :
3881 : /* Store in daemon, that a message buffer overflow has occured */
3882 : /* look if TCP connection to client is available or it least message can be put into buffer */
3883 0 : if (dlt_daemon_control_message_buffer_overflow(DLT_DAEMON_SEND_TO_ALL,
3884 : daemon,
3885 : daemon_local,
3886 : userpayload.overflow_counter,
3887 : userpayload.apid,
3888 : verbose))
3889 : /* there was an error when storing message */
3890 : /* add the counter of lost messages to the daemon counter */
3891 0 : daemon->overflow_counter += userpayload.overflow_counter;
3892 :
3893 : return 0;
3894 : }
3895 :
3896 0 : int dlt_daemon_send_message_overflow(DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
3897 : {
3898 : int ret;
3899 0 : PRINT_FUNCTION_VERBOSE(verbose);
3900 :
3901 0 : if ((daemon == 0) || (daemon_local == 0)) {
3902 0 : dlt_log(LOG_ERR, "Invalid function parameters used for function dlt_daemon_process_user_message_overflow()\n");
3903 0 : return DLT_DAEMON_ERROR_UNKNOWN;
3904 : }
3905 :
3906 : /* Store in daemon, that a message buffer overflow has occured */
3907 : if ((ret =
3908 0 : dlt_daemon_control_message_buffer_overflow(DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local,
3909 : daemon->overflow_counter,
3910 : "", verbose)))
3911 : return ret;
3912 :
3913 : return DLT_DAEMON_ERROR_OK;
3914 : }
3915 :
3916 0 : int dlt_daemon_send_message_overflow_v2(DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
3917 : {
3918 : int ret;
3919 0 : PRINT_FUNCTION_VERBOSE(verbose);
3920 :
3921 0 : if ((daemon == 0) || (daemon_local == 0)) {
3922 0 : dlt_log(LOG_ERR, "Invalid function parameters used for function dlt_daemon_process_user_message_overflow()\n");
3923 0 : return DLT_DAEMON_ERROR_UNKNOWN;
3924 : }
3925 :
3926 : /* Store in daemon, that a message buffer overflow has occured */
3927 : if ((ret =
3928 0 : dlt_daemon_control_message_buffer_overflow_v2(DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local,
3929 : daemon->overflow_counter,
3930 : NULL, verbose)))
3931 : return ret;
3932 :
3933 : return DLT_DAEMON_ERROR_OK;
3934 : }
3935 :
3936 0 : int dlt_daemon_process_user_message_register_application(DltDaemon *daemon,
3937 : DltDaemonLocal *daemon_local,
3938 : DltReceiver *rec,
3939 : int verbose)
3940 : {
3941 : uint32_t to_remove = 0;
3942 : DltDaemonApplication *application = NULL;
3943 0 : DltDaemonApplication *old_application = NULL;
3944 : pid_t old_pid = 0;
3945 0 : char description[DLT_DAEMON_DESCSIZE + 1] = { '\0' };
3946 : char *origin;
3947 : int fd = -1;
3948 :
3949 0 : PRINT_FUNCTION_VERBOSE(verbose);
3950 :
3951 0 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
3952 0 : dlt_vlog(LOG_ERR, "Invalid function parameters used for %s\n",
3953 : __func__);
3954 0 : return -1;
3955 : }
3956 :
3957 0 : if(daemon->daemon_version == DLTProtocolV2) {
3958 : uint32_t len = sizeof(DltUserControlMsgRegisterApplicationV2);
3959 : DltUserControlMsgRegisterApplicationV2 usercontext;
3960 : memset(&usercontext, 0, sizeof(DltUserControlMsgRegisterApplicationV2));
3961 : usercontext.apid = NULL;
3962 : int usercontextSize;
3963 : uint8_t *buffer;
3964 :
3965 : int offset = 0;
3966 :
3967 0 : usercontext.apidlen = (uint8_t)rec->buf[8]; // TBD: write function to get apidlen from received buffer
3968 0 : usercontextSize = (int) (sizeof(uint8_t) + usercontext.apidlen + sizeof(pid_t) + sizeof(uint32_t));
3969 :
3970 0 : buffer = (uint8_t*)calloc((size_t)usercontextSize, 1);
3971 :
3972 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
3973 : dlt_vlog(LOG_ERR, "Invalid function parameters used for %s\n",
3974 : __func__);
3975 0 : return -1;
3976 : }
3977 :
3978 : origin = rec->buf;
3979 :
3980 : /* Adding temp variable to check the return value */
3981 : int temp = 0;
3982 :
3983 : /* We shall not remove data before checking that everything is there. */
3984 0 : temp = dlt_receiver_check_and_get(rec,
3985 : buffer,
3986 : (unsigned int)usercontextSize,
3987 : DLT_RCV_SKIP_HEADER);
3988 :
3989 0 : if (temp < 0) {
3990 : /* Not enough bytes received */
3991 : return -1;
3992 : }
3993 : else {
3994 0 : to_remove = (uint32_t) temp;
3995 : }
3996 :
3997 : offset = 0;
3998 : memcpy(&usercontext.apidlen, buffer, 1);
3999 : offset += 1;
4000 0 : char apid_buf[DLT_V2_ID_SIZE + 1] = {0};
4001 0 : memcpy(apid_buf, (buffer + offset), usercontext.apidlen);
4002 0 : apid_buf[usercontext.apidlen] = '\0';
4003 0 : usercontext.apid = apid_buf;
4004 0 : offset += usercontext.apidlen;
4005 0 : memcpy(&(usercontext.pid), (buffer + offset), sizeof(pid_t));
4006 0 : offset += (int)sizeof(pid_t);
4007 0 : memcpy(&(usercontext.description_length), (buffer + offset), 4);
4008 : offset = 0;
4009 :
4010 : len = usercontext.description_length;
4011 :
4012 : // if (len > DLT_DAEMON_DESCSIZE) {
4013 : // len = DLT_DAEMON_DESCSIZE;
4014 : // dlt_log(LOG_WARNING, "Application description exceeds limit\n");
4015 : // }
4016 :
4017 : /* adjust buffer pointer */
4018 0 : rec->buf += to_remove + sizeof(DltUserHeader);
4019 :
4020 0 : if (dlt_receiver_check_and_get(rec, description, len, DLT_RCV_NONE) < 0) {
4021 0 : dlt_log(LOG_ERR, "Unable to get application description\n");
4022 : /* in case description was not readable, set dummy description */
4023 : memcpy(description, "Unknown", sizeof("Unknown"));
4024 : /* unknown len of original description, set to 0 to not remove in next step. Because message buffer is re-adjusted the corrupted description is ignored. */
4025 : len = 0;
4026 : }
4027 :
4028 : /* adjust to_remove */
4029 0 : to_remove += (uint32_t) sizeof(DltUserHeader) + len;
4030 :
4031 : /* point to begin of message */
4032 0 : rec->buf = origin;
4033 :
4034 : //TBD: Need init_v2 ?
4035 :
4036 : /* We can now remove data. */
4037 0 : if (dlt_receiver_remove(rec, (int) to_remove) != DLT_RETURN_OK) {
4038 0 : dlt_log(LOG_WARNING, "Can't remove bytes from receiver\n");
4039 0 : return -1;
4040 : }
4041 :
4042 0 : dlt_daemon_application_find_v2(daemon, usercontext.apidlen, usercontext.apid, daemon->ecuid2len, daemon->ecuid2, verbose, &old_application);
4043 :
4044 0 : if (old_application != NULL)
4045 0 : old_pid = old_application->pid;
4046 :
4047 0 : if (rec->type == DLT_RECEIVE_SOCKET)
4048 0 : fd = rec->fd; /* For sockets, an app specific fd has already been created with accept(). */
4049 :
4050 0 : application = dlt_daemon_application_add_v2(daemon,
4051 : usercontext.apidlen,
4052 : usercontext.apid,
4053 : usercontext.pid,
4054 : description,
4055 : fd,
4056 0 : daemon->ecuid2len,
4057 : daemon->ecuid2,
4058 : verbose);
4059 :
4060 :
4061 : /* send log state to new application */
4062 0 : dlt_daemon_user_send_log_state_v2(daemon, application, verbose);
4063 :
4064 0 : if (application == NULL) {
4065 0 : dlt_vlog(LOG_WARNING, "Can't add ApplicationID '%s' for PID %d\n",
4066 : usercontext.apid, usercontext.pid);
4067 0 : return -1;
4068 : }
4069 0 : else if (old_pid != application->pid)
4070 : {
4071 0 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
4072 :
4073 0 : snprintf(local_str,
4074 : DLT_DAEMON_TEXTBUFSIZE,
4075 : "ApplicationID '%s' registered for PID %d, Description=%s",
4076 0 : application->apid2,
4077 : application->pid,
4078 : application->application_description);
4079 :
4080 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str, DLT_LOG_INFO,
4081 : DLT_DAEMON_APP_ID, DLT_DAEMON_CTX_ID,
4082 : daemon_local->flags.vflag);
4083 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
4084 : }
4085 :
4086 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
4087 : if (dlt_daemon_user_send_trace_load_config(daemon, application, verbose) != DLT_RETURN_OK)
4088 : dlt_vlog(LOG_WARNING, "Cannot send trace config to Apid: %.4s, PID: %d\n",
4089 : application->apid, application->pid);
4090 : #endif
4091 0 : free(buffer);
4092 0 : } else if(daemon->daemon_version == DLTProtocolV1) {
4093 : DltUserControlMsgRegisterApplication userapp;
4094 : uint32_t len = sizeof(DltUserControlMsgRegisterApplication);
4095 : memset(&userapp, 0, sizeof(DltUserControlMsgRegisterApplication));
4096 0 : origin = rec->buf;
4097 :
4098 : /* Adding temp variable to check the return value */
4099 : int temp = 0;
4100 :
4101 : /* We shall not remove data before checking that everything is there. */
4102 0 : temp = dlt_receiver_check_and_get(rec,
4103 : &userapp,
4104 : len,
4105 : DLT_RCV_SKIP_HEADER);
4106 :
4107 0 : if (temp < 0)
4108 : /* Not enough bytes received */
4109 0 : return -1;
4110 : else {
4111 0 : to_remove = (uint32_t) temp;
4112 : }
4113 :
4114 0 : len = userapp.description_length;
4115 :
4116 0 : if (len > DLT_DAEMON_DESCSIZE) {
4117 : len = DLT_DAEMON_DESCSIZE;
4118 0 : dlt_log(LOG_WARNING, "Application description exceeds limit\n");
4119 : }
4120 :
4121 : /* adjust buffer pointer */
4122 0 : rec->buf += to_remove + sizeof(DltUserHeader);
4123 :
4124 0 : if (dlt_receiver_check_and_get(rec, description, len, DLT_RCV_NONE) < 0) {
4125 0 : dlt_log(LOG_ERR, "Unable to get application description\n");
4126 : /* in case description was not readable, set dummy description */
4127 : memcpy(description, "Unknown", sizeof("Unknown"));
4128 :
4129 : /* unknown len of original description, set to 0 to not remove in next
4130 : * step. Because message buffer is re-adjusted the corrupted description
4131 : * is ignored. */
4132 : len = 0;
4133 : }
4134 :
4135 : /* adjust to_remove */
4136 0 : to_remove += (uint32_t) sizeof(DltUserHeader) + len;
4137 : /* point to begin of message */
4138 0 : rec->buf = origin;
4139 :
4140 : /* We can now remove data. */
4141 0 : if (dlt_receiver_remove(rec, (int) to_remove) != DLT_RETURN_OK) {
4142 0 : dlt_log(LOG_WARNING, "Can't remove bytes from receiver\n");
4143 0 : return -1;
4144 : }
4145 :
4146 0 : old_application = dlt_daemon_application_find(daemon, userapp.apid, daemon->ecuid, verbose);
4147 :
4148 0 : if (old_application != NULL)
4149 0 : old_pid = old_application->pid;
4150 :
4151 0 : if (rec->type == DLT_RECEIVE_SOCKET)
4152 0 : fd = rec->fd; /* For sockets, an app specific fd has already been created with accept(). */
4153 :
4154 0 : application = dlt_daemon_application_add(daemon,
4155 : userapp.apid,
4156 : userapp.pid,
4157 : description,
4158 : fd,
4159 : daemon->ecuid,
4160 : verbose);
4161 :
4162 : /* send log state to new application */
4163 0 : dlt_daemon_user_send_log_state(daemon, application, verbose);
4164 :
4165 0 : if (application == NULL) {
4166 0 : dlt_vlog(LOG_WARNING, "Can't add ApplicationID '%.4s' for PID %d\n",
4167 : userapp.apid, userapp.pid);
4168 0 : return -1;
4169 : }
4170 0 : else if (old_pid != application->pid)
4171 : {
4172 0 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
4173 :
4174 0 : snprintf(local_str,
4175 : DLT_DAEMON_TEXTBUFSIZE,
4176 : "ApplicationID '%.4s' registered for PID %d, Description=%s",
4177 0 : application->apid,
4178 : application->pid,
4179 : application->application_description);
4180 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str, DLT_LOG_INFO,
4181 : DLT_DAEMON_APP_ID, DLT_DAEMON_CTX_ID,
4182 : daemon_local->flags.vflag);
4183 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
4184 : }
4185 :
4186 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
4187 : if (dlt_daemon_user_send_trace_load_config(daemon, application, verbose) != DLT_RETURN_OK)
4188 : dlt_vlog(LOG_WARNING, "Cannot send trace config to Apid: %.4s, PID: %d\n",
4189 : application->apid, application->pid);
4190 : #endif
4191 : } else {
4192 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
4193 0 : return -1;
4194 : }
4195 : return 0;
4196 : }
4197 :
4198 2 : int dlt_daemon_process_user_message_register_context(DltDaemon *daemon,
4199 : DltDaemonLocal *daemon_local,
4200 : DltReceiver *rec,
4201 : int verbose)
4202 : {
4203 : uint32_t to_remove = 0;
4204 : uint32_t len = (uint32_t)(sizeof(DltUserControlMsgRegisterContext));
4205 : DltUserControlMsgRegisterContext userctxt;
4206 2 : char description[DLT_DAEMON_DESCSIZE + 1] = { '\0' };
4207 2 : DltDaemonApplication *application = NULL;
4208 : DltDaemonContext *context = NULL;
4209 : char *origin;
4210 :
4211 2 : PRINT_FUNCTION_VERBOSE(verbose);
4212 :
4213 2 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
4214 0 : dlt_vlog(LOG_ERR, "Invalid function parameters used for %s\n",
4215 : __func__);
4216 0 : return -1;
4217 : }
4218 :
4219 2 : if (daemon->daemon_version == DLTProtocolV2) {
4220 : DltUserControlMsgRegisterContextV2 usercontext;
4221 : /* local request pointer removed to avoid overlaying small buffers */
4222 : memset(&usercontext, 0, sizeof(DltUserControlMsgRegisterContextV2));
4223 : usercontext.apid = NULL;
4224 : usercontext.ctid = NULL;
4225 : int usercontextSize;
4226 : uint8_t *buffer;
4227 : DltMessageV2 msg;
4228 : int offset = 0;
4229 :
4230 0 : PRINT_FUNCTION_VERBOSE(verbose);
4231 :
4232 0 : usercontext.apidlen = (uint8_t)rec->buf[8]; // TBD: write function to get apidlen from received buffer
4233 0 : usercontext.ctidlen = (uint8_t)rec->buf[9 + usercontext.apidlen]; // TBD: write function to get ctidlen from received buffer
4234 :
4235 0 : usercontextSize = (int)(sizeof(uint8_t) + usercontext.apidlen +
4236 0 : sizeof(uint8_t) + usercontext.ctidlen + 10 + sizeof(pid_t));
4237 : len = (uint32_t)usercontextSize;
4238 0 : buffer = (uint8_t*)malloc((size_t)usercontextSize);
4239 :
4240 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
4241 : dlt_vlog(LOG_ERR, "Invalid function parameters used for %s\n",
4242 : __func__);
4243 : free(buffer);
4244 0 : return -1;
4245 : }
4246 :
4247 : origin = rec->buf;
4248 :
4249 : /* Adding temp variable to check the return value */
4250 : int temp = 0;
4251 :
4252 0 : temp = dlt_receiver_check_and_get(rec,
4253 : buffer,
4254 : (unsigned int)len,
4255 : DLT_RCV_SKIP_HEADER);
4256 :
4257 0 : if (temp < 0) {
4258 : /* Not enough bytes received */
4259 0 : free(buffer);
4260 0 : return -1;
4261 : } else {
4262 0 : to_remove = (uint32_t) temp;
4263 : }
4264 :
4265 : memcpy(&(usercontext.apidlen), buffer, 1);
4266 : offset = 1;
4267 :
4268 0 : usercontext.apid = (char *)malloc((size_t)usercontext.apidlen + 1);
4269 0 : if (usercontext.apid == NULL) {
4270 0 : dlt_log(LOG_ERR, "Memory allocation failed for usercontext.apid\n");
4271 0 : free(buffer);
4272 0 : return -1;
4273 : }
4274 0 : memcpy(usercontext.apid, (buffer + offset), usercontext.apidlen);
4275 0 : memset((usercontext.apid + usercontext.apidlen), '\0', 1); // Null-terminate string
4276 0 : offset += usercontext.apidlen;
4277 0 : memcpy(&(usercontext.ctidlen), (buffer + offset), 1);
4278 0 : offset += 1;
4279 0 : usercontext.ctid = (char *)malloc((size_t)usercontext.ctidlen + 1);
4280 0 : if (usercontext.ctid == NULL) {
4281 0 : dlt_log(LOG_ERR, "Memory allocation failed for usercontext.ctid\n");
4282 0 : free(usercontext.apid);
4283 0 : free(buffer);
4284 0 : return -1;
4285 : }
4286 0 : memcpy(usercontext.ctid, (buffer + offset), usercontext.ctidlen);
4287 0 : memset((usercontext.ctid + usercontext.ctidlen), '\0', 1); // Null-terminate string
4288 0 : offset += usercontext.ctidlen;
4289 0 : memcpy(&(usercontext.log_level_pos), (buffer + offset), sizeof(int32_t));
4290 0 : offset += (int)sizeof(int32_t);
4291 0 : memcpy(&(usercontext.log_level), (buffer + offset), sizeof(int8_t));
4292 0 : offset += (int)sizeof(int8_t);
4293 0 : memcpy(&(usercontext.trace_status), (buffer + offset), sizeof(int8_t));
4294 0 : offset += (int)sizeof(int8_t);
4295 0 : memcpy(&(usercontext.pid), (buffer + offset), sizeof(pid_t));
4296 0 : offset += (int)sizeof(pid_t);
4297 0 : memcpy(&(usercontext.description_length), (buffer + offset), 4);
4298 :
4299 : len = usercontext.description_length;
4300 :
4301 : // if (len > DLT_DAEMON_DESCSIZE) {
4302 : // dlt_vlog(LOG_WARNING, "Context description exceeds limit: %u\n", len);
4303 : // len = DLT_DAEMON_DESCSIZE;
4304 : // }
4305 :
4306 : /* adjust buffer pointer */
4307 0 : rec->buf += to_remove + sizeof(DltUserHeader);
4308 :
4309 0 : if (dlt_receiver_check_and_get(rec, description, len, DLT_RCV_NONE) < 0) {
4310 0 : dlt_log(LOG_ERR, "Unable to get context description\n");
4311 : /* in case description was not readable, set dummy description */
4312 : memcpy(description, "Unknown", sizeof("Unknown"));
4313 :
4314 : /* unknown len of original description, set to 0 to not remove in next
4315 : * step. Because message buffer is re-adjusted the corrupted description
4316 : * is ignored. */
4317 : len = 0;
4318 : }
4319 :
4320 : /* adjust to_remove */
4321 0 : to_remove += (uint32_t) sizeof(DltUserHeader) + len;
4322 : /* point to begin of message */
4323 0 : rec->buf = origin;
4324 :
4325 : /* We can now remove data. */
4326 0 : if (dlt_receiver_remove(rec, (int) to_remove) != DLT_RETURN_OK) {
4327 0 : dlt_log(LOG_WARNING, "Can't remove bytes from receiver\n");
4328 0 : free(usercontext.apid);
4329 0 : free(usercontext.ctid);
4330 0 : free(buffer);
4331 0 : return -1;
4332 : }
4333 :
4334 0 : dlt_daemon_application_find_v2(daemon,
4335 : usercontext.apidlen,
4336 : usercontext.apid,
4337 0 : daemon->ecuid2len,
4338 0 : daemon->ecuid2,
4339 : verbose,
4340 : &application);
4341 :
4342 0 : if (application == NULL) {
4343 0 : dlt_vlog(LOG_WARNING,
4344 : "ApID '%s' not found for new ContextID '%s' in %s\n",
4345 : usercontext.apid,
4346 : usercontext.ctid,
4347 : __func__);
4348 :
4349 0 : free(usercontext.apid);
4350 0 : free(usercontext.ctid);
4351 0 : free(buffer);
4352 0 : return 0;
4353 : }
4354 :
4355 : /* Set log level */
4356 0 : if (usercontext.log_level == DLT_USER_LOG_LEVEL_NOT_SET) {
4357 0 : usercontext.log_level = DLT_LOG_DEFAULT;
4358 : } else {
4359 : /* Plausibility check */
4360 0 : if ((usercontext.log_level < DLT_LOG_DEFAULT) ||
4361 : (usercontext.log_level > DLT_LOG_VERBOSE)) {
4362 0 : free(usercontext.apid);
4363 0 : free(usercontext.ctid);
4364 0 : free(buffer);
4365 0 : return -1;
4366 : }
4367 : }
4368 :
4369 : /* Set trace status */
4370 0 : if (usercontext.trace_status == DLT_USER_TRACE_STATUS_NOT_SET) {
4371 0 : usercontext.trace_status = DLT_TRACE_STATUS_DEFAULT;
4372 : } else {
4373 : /* Plausibility check */
4374 0 : if ((usercontext.trace_status < DLT_TRACE_STATUS_DEFAULT) ||
4375 : (usercontext.trace_status > DLT_TRACE_STATUS_ON)) {
4376 0 : free(usercontext.apid);
4377 0 : free(usercontext.ctid);
4378 0 : free(buffer);
4379 0 : return -1;
4380 : }
4381 : }
4382 :
4383 0 : context = dlt_daemon_context_add_v2(daemon,
4384 : usercontext.apidlen,
4385 : usercontext.apid,
4386 : usercontext.ctidlen,
4387 : usercontext.ctid,
4388 0 : usercontext.log_level,
4389 0 : usercontext.trace_status,
4390 : usercontext.log_level_pos,
4391 : application->user_handle,
4392 : description,
4393 0 : daemon->ecuid2len,
4394 : daemon->ecuid2,
4395 : verbose);
4396 0 : if (context == NULL) {
4397 0 : dlt_vlog(LOG_WARNING,
4398 : "Can't add ContextID '%s' for ApID '%s'\n in %s",
4399 : usercontext.ctid, usercontext.apid, __func__);
4400 0 : free(usercontext.apid);
4401 0 : free(usercontext.ctid);
4402 0 : free(buffer);
4403 0 : return -1;
4404 : }
4405 : else {
4406 0 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
4407 :
4408 0 : snprintf(local_str,
4409 : DLT_DAEMON_TEXTBUFSIZE,
4410 : "ContextID '%s' registered for ApID '%s', Description=%s",
4411 : context->ctid2,
4412 : context->apid2,
4413 : context->context_description);
4414 :
4415 0 : if (verbose)
4416 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str,
4417 : DLT_LOG_INFO, DLT_DAEMON_APP_ID,
4418 : DLT_DAEMON_CTX_ID, verbose);
4419 :
4420 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
4421 : }
4422 0 : if (daemon_local->flags.offlineLogstorageMaxDevices) {
4423 : //TBD: update for DLT V2
4424 : /* Store log level set for offline logstorage into context structure*/
4425 0 : context->storage_log_level =
4426 0 : (int8_t) dlt_daemon_logstorage_get_loglevel(daemon,
4427 0 : (int8_t) daemon_local->flags.offlineLogstorageMaxDevices,
4428 : usercontext.apid,
4429 : usercontext.ctid);
4430 : }
4431 : else
4432 0 : context->storage_log_level = DLT_LOG_DEFAULT;
4433 :
4434 : /* Create automatic get log info response for registered context */
4435 0 : if (daemon_local->flags.rflag) {
4436 : /* Prepare request for get log info with one application and one context */
4437 0 : if (dlt_message_init_v2(&msg, verbose) == -1) {
4438 0 : dlt_log(LOG_WARNING, "Can't initialize message");
4439 0 : return -1;
4440 : }
4441 :
4442 0 : msg.datasize = (int)(sizeof(uint32_t) + sizeof(uint8_t) +
4443 0 : sizeof(uint8_t) + usercontext.apidlen +
4444 0 : sizeof(uint8_t) + usercontext.ctidlen +
4445 : DLT_ID_SIZE);
4446 :
4447 0 : if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
4448 0 : free(msg.databuffer);
4449 0 : msg.databuffer = 0;
4450 : }
4451 :
4452 0 : if (msg.databuffer == 0) {
4453 0 : msg.databuffer = (uint8_t *)malloc((size_t)msg.datasize);
4454 0 : msg.databuffersize = msg.datasize;
4455 : }
4456 :
4457 0 : if (msg.databuffer == 0) {
4458 0 : dlt_log(LOG_WARNING, "Can't allocate buffer for get log info message\n");
4459 0 : return -1;
4460 : }
4461 :
4462 : /* prepare local request values (do NOT overlay struct onto small buffer) */
4463 : DltServiceGetLogInfoRequestV2 req_local;
4464 : char apid_buf[DLT_V2_ID_SIZE];
4465 : char ctid_buf[DLT_V2_ID_SIZE];
4466 : char com_buf[DLT_ID_SIZE];
4467 :
4468 0 : req_local.service_id = DLT_SERVICE_ID_GET_LOG_INFO;
4469 0 : req_local.options = (uint8_t) daemon_local->flags.autoResponseGetLogInfoOption;
4470 0 : req_local.apidlen = usercontext.apidlen;
4471 0 : req_local.ctidlen = usercontext.ctidlen;
4472 : /* fill id buffers */
4473 0 : dlt_set_id_v2(apid_buf, usercontext.apid, req_local.apidlen);
4474 0 : dlt_set_id_v2(ctid_buf, usercontext.ctid, req_local.ctidlen);
4475 0 : dlt_set_id(com_buf, "remo");
4476 :
4477 : offset = 0;
4478 0 : memcpy(msg.databuffer + offset, &(req_local.service_id), sizeof(uint32_t));
4479 : offset += (int)sizeof(uint32_t);
4480 0 : memcpy(msg.databuffer + offset, &(req_local.options), sizeof(uint8_t));
4481 : offset += (int)sizeof(uint8_t);
4482 0 : memcpy(msg.databuffer + offset, &(req_local.apidlen), sizeof(uint8_t));
4483 : offset += (int)sizeof(uint8_t);
4484 0 : if (req_local.apidlen > 0)
4485 0 : memcpy(msg.databuffer + offset, apid_buf, req_local.apidlen);
4486 0 : offset += req_local.apidlen;
4487 0 : memcpy(msg.databuffer + offset, &(req_local.ctidlen), sizeof(uint8_t));
4488 0 : offset += (int)sizeof(uint8_t);
4489 0 : if (req_local.ctidlen > 0)
4490 0 : memcpy(msg.databuffer + offset, ctid_buf, req_local.ctidlen);
4491 0 : offset += req_local.ctidlen;
4492 0 : memcpy(msg.databuffer + offset, com_buf, DLT_ID_SIZE);
4493 : offset = 0;
4494 :
4495 0 : dlt_daemon_control_get_log_info_v2(DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, &msg, verbose);
4496 0 : dlt_message_free_v2(&msg, verbose);
4497 : }
4498 :
4499 0 : if (context->user_handle >= DLT_FD_MINIMUM) {
4500 0 : if ((usercontext.log_level == DLT_LOG_DEFAULT) || (usercontext.trace_status == DLT_TRACE_STATUS_DEFAULT)) {
4501 : /* This call also replaces the default values with the values defined for default */
4502 0 : if (dlt_daemon_user_send_log_level_v2(daemon, context, verbose) == -1) {
4503 0 : dlt_vlog(LOG_WARNING, "Can't send current log level as response to %s for (%s;%s)\n",
4504 : __func__,
4505 0 : context->apid,
4506 0 : context->ctid);
4507 0 : free(usercontext.apid);
4508 0 : free(usercontext.ctid);
4509 0 : free(buffer);
4510 0 : return -1;
4511 : }
4512 : }
4513 : }
4514 0 : free(usercontext.apid);
4515 0 : free(usercontext.ctid);
4516 0 : free(buffer);
4517 2 : } else if (daemon->daemon_version == DLTProtocolV1) {
4518 : DltMessage msg;
4519 : DltServiceGetLogInfoRequest *req = NULL;
4520 : memset(&userctxt, 0, sizeof(DltUserControlMsgRegisterContext));
4521 2 : origin = rec->buf;
4522 :
4523 : /* Adding temp variable to check the return value */
4524 : int temp = 0;
4525 :
4526 2 : temp = dlt_receiver_check_and_get(rec,
4527 : &userctxt,
4528 : len,
4529 : DLT_RCV_SKIP_HEADER);
4530 :
4531 2 : if (temp < 0)
4532 : /* Not enough bytes received */
4533 2 : return -1;
4534 : else {
4535 2 : to_remove = (uint32_t) temp;
4536 : }
4537 :
4538 2 : len = userctxt.description_length;
4539 :
4540 2 : if (len > DLT_DAEMON_DESCSIZE) {
4541 0 : dlt_vlog(LOG_WARNING, "Context description exceeds limit: %u\n", len);
4542 : len = DLT_DAEMON_DESCSIZE;
4543 : }
4544 :
4545 : /* adjust buffer pointer */
4546 2 : rec->buf += to_remove + sizeof(DltUserHeader);
4547 :
4548 2 : if (dlt_receiver_check_and_get(rec, description, len, DLT_RCV_NONE) < 0) {
4549 0 : dlt_log(LOG_ERR, "Unable to get context description\n");
4550 : /* in case description was not readable, set dummy description */
4551 : memcpy(description, "Unknown", sizeof("Unknown"));
4552 :
4553 : /* unknown len of original description, set to 0 to not remove in next
4554 : * step. Because message buffer is re-adjusted the corrupted description
4555 : * is ignored. */
4556 : len = 0;
4557 : }
4558 :
4559 : /* adjust to_remove */
4560 2 : to_remove += (uint32_t) sizeof(DltUserHeader) + len;
4561 : /* point to begin of message */
4562 2 : rec->buf = origin;
4563 :
4564 : /* We can now remove data. */
4565 2 : if (dlt_receiver_remove(rec, (int) to_remove) != DLT_RETURN_OK) {
4566 0 : dlt_log(LOG_WARNING, "Can't remove bytes from receiver\n");
4567 0 : return -1;
4568 : }
4569 :
4570 4 : application = dlt_daemon_application_find(daemon,
4571 : userctxt.apid,
4572 2 : daemon->ecuid,
4573 : verbose);
4574 :
4575 2 : if (application == 0) {
4576 2 : dlt_vlog(LOG_WARNING,
4577 : "ApID '%.4s' not found for new ContextID '%.4s' in %s\n",
4578 : userctxt.apid,
4579 : userctxt.ctid,
4580 : __func__);
4581 :
4582 2 : return 0;
4583 : }
4584 :
4585 : /* Set log level */
4586 0 : if (userctxt.log_level == DLT_USER_LOG_LEVEL_NOT_SET) {
4587 0 : userctxt.log_level = DLT_LOG_DEFAULT;
4588 : } else {
4589 : /* Plausibility check */
4590 0 : if ((userctxt.log_level < DLT_LOG_DEFAULT) ||
4591 : (userctxt.log_level > DLT_LOG_VERBOSE)) {
4592 : return -1;
4593 : }
4594 : }
4595 :
4596 : /* Set trace status */
4597 0 : if (userctxt.trace_status == DLT_USER_TRACE_STATUS_NOT_SET) {
4598 0 : userctxt.trace_status = DLT_TRACE_STATUS_DEFAULT;
4599 : } else {
4600 : /* Plausibility check */
4601 0 : if ((userctxt.trace_status < DLT_TRACE_STATUS_DEFAULT) ||
4602 : (userctxt.trace_status > DLT_TRACE_STATUS_ON)) {
4603 : return -1;
4604 : }
4605 : }
4606 :
4607 0 : context = dlt_daemon_context_add(daemon,
4608 : userctxt.apid,
4609 : userctxt.ctid,
4610 0 : userctxt.log_level,
4611 0 : userctxt.trace_status,
4612 : userctxt.log_level_pos,
4613 : application->user_handle,
4614 : description,
4615 : daemon->ecuid,
4616 : verbose);
4617 :
4618 0 : if (context == 0) {
4619 0 : dlt_vlog(LOG_WARNING,
4620 : "Can't add ContextID '%.4s' for ApID '%.4s'\n in %s",
4621 : userctxt.ctid, userctxt.apid, __func__);
4622 0 : return -1;
4623 : }
4624 : else {
4625 0 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
4626 :
4627 0 : snprintf(local_str,
4628 : DLT_DAEMON_TEXTBUFSIZE,
4629 : "ContextID '%.4s' registered for ApID '%.4s', Description=%s",
4630 0 : context->ctid,
4631 0 : context->apid,
4632 : context->context_description);
4633 :
4634 0 : if (verbose)
4635 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str,
4636 : DLT_LOG_INFO, DLT_DAEMON_APP_ID,
4637 : DLT_DAEMON_CTX_ID, verbose);
4638 :
4639 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
4640 : }
4641 :
4642 0 : if (daemon_local->flags.offlineLogstorageMaxDevices)
4643 : /* Store log level set for offline logstorage into context structure*/
4644 0 : context->storage_log_level =
4645 0 : (int8_t) dlt_daemon_logstorage_get_loglevel(daemon,
4646 0 : (int8_t) daemon_local->flags.offlineLogstorageMaxDevices,
4647 : userctxt.apid,
4648 : userctxt.ctid);
4649 : else
4650 0 : context->storage_log_level = DLT_LOG_DEFAULT;
4651 :
4652 : /* Create automatic get log info response for registered context */
4653 0 : if (daemon_local->flags.rflag) {
4654 : /* Prepare request for get log info with one application and one context */
4655 0 : if (dlt_message_init(&msg, verbose) == -1) {
4656 0 : dlt_log(LOG_WARNING, "Can't initialize message");
4657 0 : return -1;
4658 : }
4659 :
4660 0 : msg.datasize = sizeof(DltServiceGetLogInfoRequest);
4661 :
4662 0 : if (msg.databuffer && (msg.databuffersize < msg.datasize)) {
4663 0 : free(msg.databuffer);
4664 0 : msg.databuffer = 0;
4665 : }
4666 :
4667 0 : if (msg.databuffer == 0) {
4668 0 : msg.databuffer = (uint8_t *)malloc((size_t)msg.datasize);
4669 0 : msg.databuffersize = msg.datasize;
4670 : }
4671 :
4672 0 : if (msg.databuffer == 0) {
4673 0 : dlt_log(LOG_WARNING, "Can't allocate buffer for get log info message\n");
4674 0 : return -1;
4675 : }
4676 :
4677 : req = (DltServiceGetLogInfoRequest *)msg.databuffer;
4678 :
4679 0 : req->service_id = DLT_SERVICE_ID_GET_LOG_INFO;
4680 0 : req->options = (uint8_t) daemon_local->flags.autoResponseGetLogInfoOption;
4681 0 : dlt_set_id(req->apid, userctxt.apid);
4682 0 : dlt_set_id(req->ctid, userctxt.ctid);
4683 0 : dlt_set_id(req->com, "remo");
4684 :
4685 0 : dlt_daemon_control_get_log_info(DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, &msg, verbose);
4686 :
4687 0 : dlt_message_free(&msg, verbose);
4688 : }
4689 :
4690 0 : if (context->user_handle >= DLT_FD_MINIMUM) {
4691 0 : if ((userctxt.log_level == DLT_LOG_DEFAULT) || (userctxt.trace_status == DLT_TRACE_STATUS_DEFAULT)) {
4692 : /* This call also replaces the default values with the values defined for default */
4693 0 : if (dlt_daemon_user_send_log_level(daemon, context, verbose) == -1) {
4694 0 : dlt_vlog(LOG_WARNING, "Can't send current log level as response to %s for (%.4s;%.4s)\n",
4695 : __func__,
4696 : context->apid,
4697 : context->ctid);
4698 0 : return -1;
4699 : }
4700 : }
4701 : }
4702 : } else {
4703 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
4704 0 : return -1;
4705 : }
4706 :
4707 : return 0;
4708 : }
4709 :
4710 0 : int dlt_daemon_process_user_message_unregister_application(DltDaemon *daemon,
4711 : DltDaemonLocal *daemon_local,
4712 : DltReceiver *rec,
4713 : int verbose)
4714 : {
4715 0 : DltDaemonApplication *application = NULL;
4716 : DltDaemonContext *context;
4717 : int i, offset_base;
4718 : DltDaemonRegisteredUsers *user_list = NULL;
4719 :
4720 0 : PRINT_FUNCTION_VERBOSE(verbose);
4721 :
4722 0 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
4723 0 : dlt_vlog(LOG_ERR,
4724 : "Invalid function parameters used for %s\n",
4725 : __func__);
4726 0 : return -1;
4727 : }
4728 :
4729 0 : if (daemon->daemon_version == DLTProtocolV2) {
4730 : uint32_t len = sizeof(DltUserControlMsgUnregisterApplicationV2);
4731 : DltUserControlMsgUnregisterApplicationV2 userapp;
4732 : int userappSize;
4733 : uint8_t *buffer;
4734 0 : userapp.apidlen = (uint8_t)rec->buf[8]; // TBD: write function to get apidlen from received buffer
4735 0 : userapp.apid = NULL;
4736 0 : userappSize = (int)sizeof(uint8_t) + userapp.apidlen + (int)sizeof(pid_t);
4737 0 : len = (uint32_t)userappSize;
4738 0 : buffer = (uint8_t*)malloc((size_t)userappSize);
4739 :
4740 : int offset = 0;
4741 :
4742 0 : if (dlt_receiver_check_and_get(rec,
4743 : buffer,
4744 : len,
4745 : DLT_RCV_SKIP_HEADER | DLT_RCV_REMOVE) < 0)
4746 : /* Not enough bytes received */
4747 0 : return -1;
4748 :
4749 : memcpy(&(userapp.apidlen), buffer, 1);
4750 : offset = 1;
4751 : char apid_buf[DLT_V2_ID_SIZE];
4752 0 : dlt_set_id_v2(apid_buf, (const char *)(buffer + offset), userapp.apidlen);
4753 0 : userapp.apid = apid_buf;
4754 0 : offset += userapp.apidlen;
4755 0 : memcpy(&(userapp.pid), (buffer + offset), sizeof(pid_t));
4756 0 : user_list = dlt_daemon_find_users_list_v2(daemon, daemon->ecuid2len, daemon->ecuid2, verbose);
4757 :
4758 0 : if (user_list == NULL)
4759 : return -1;
4760 :
4761 0 : if (user_list->num_applications > 0) {
4762 : /* Delete this application and all corresponding contexts
4763 : * for this application from internal table.
4764 : */
4765 0 : dlt_daemon_application_find_v2(daemon,
4766 : userapp.apidlen,
4767 : userapp.apid,
4768 0 : daemon->ecuid2len,
4769 : daemon->ecuid2,
4770 : verbose,
4771 : &application);
4772 0 : if (application != NULL) {
4773 : /* Calculate start offset within contexts[] */
4774 : offset_base = 0;
4775 0 : for (i = 0; i < (application - (user_list->applications)); i++)
4776 0 : offset_base += user_list->applications[i].num_contexts;
4777 0 : for (i = (application->num_contexts) - 1; i >= 0; i--) {
4778 0 : context = &(user_list->contexts[offset_base + i]);
4779 0 : if (context) {
4780 : /* Delete context */
4781 0 : if (dlt_daemon_context_del_v2(daemon,
4782 : context,
4783 0 : daemon->ecuid2len,
4784 : daemon->ecuid2,
4785 : verbose) == -1) {
4786 0 : dlt_vlog(LOG_WARNING,
4787 : "Can't delete CtID '%s' for ApID '%s' in %s\n",
4788 0 : context->ctid,
4789 0 : context->apid,
4790 : __func__);
4791 0 : return -1;
4792 : }
4793 : }
4794 : }
4795 : /* Delete this application entry from internal table*/
4796 0 : if (dlt_daemon_application_del_v2(daemon,
4797 : application,
4798 0 : daemon->ecuid2len,
4799 : daemon->ecuid2,
4800 : verbose) == -1) {
4801 0 : dlt_vlog(LOG_WARNING,
4802 : "Can't delete ApID '%s' in %s\n",
4803 0 : application->apid,
4804 : __func__);
4805 0 : return -1;
4806 : }
4807 : else {
4808 0 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
4809 : snprintf(local_str,
4810 : DLT_DAEMON_TEXTBUFSIZE,
4811 : "Unregistered ApID '%s'",
4812 : userapp.apid);
4813 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str,
4814 : DLT_LOG_INFO, DLT_DAEMON_APP_ID,
4815 : DLT_DAEMON_CTX_ID, verbose);
4816 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
4817 : }
4818 : }
4819 : }
4820 0 : } else if (daemon->daemon_version == DLTProtocolV1) {
4821 : uint32_t len = sizeof(DltUserControlMsgUnregisterApplication);
4822 : DltUserControlMsgUnregisterApplication userapp;
4823 0 : if (dlt_receiver_check_and_get(rec,
4824 : &userapp,
4825 : len,
4826 : DLT_RCV_SKIP_HEADER | DLT_RCV_REMOVE) < 0)
4827 : /* Not enough bytes received */
4828 0 : return -1;
4829 :
4830 0 : user_list = dlt_daemon_find_users_list(daemon, daemon->ecuid, verbose);
4831 :
4832 0 : if (user_list == NULL)
4833 : return -1;
4834 :
4835 0 : if (user_list->num_applications > 0) {
4836 : /* Delete this application and all corresponding contexts
4837 : * for this application from internal table.
4838 : */
4839 0 : application = dlt_daemon_application_find(daemon,
4840 : userapp.apid,
4841 : daemon->ecuid,
4842 : verbose);
4843 :
4844 0 : if (application) {
4845 : /* Calculate start offset within contexts[] */
4846 : offset_base = 0;
4847 :
4848 0 : for (i = 0; i < (application - (user_list->applications)); i++)
4849 0 : offset_base += user_list->applications[i].num_contexts;
4850 :
4851 0 : for (i = (application->num_contexts) - 1; i >= 0; i--) {
4852 0 : context = &(user_list->contexts[offset_base + i]);
4853 :
4854 0 : if (context) {
4855 : /* Delete context */
4856 0 : if (dlt_daemon_context_del(daemon,
4857 : context,
4858 : daemon->ecuid,
4859 : verbose) == -1) {
4860 0 : dlt_vlog(LOG_WARNING,
4861 : "Can't delete CtID '%.4s' for ApID '%.4s' in %s\n",
4862 0 : context->ctid,
4863 0 : context->apid,
4864 : __func__);
4865 0 : return -1;
4866 : }
4867 : }
4868 : }
4869 :
4870 : /* Delete this application entry from internal table*/
4871 0 : if (dlt_daemon_application_del(daemon,
4872 : application,
4873 : daemon->ecuid,
4874 : verbose) == -1) {
4875 0 : dlt_vlog(LOG_WARNING,
4876 : "Can't delete ApID '%.4s' in %s\n",
4877 0 : application->apid,
4878 : __func__);
4879 0 : return -1;
4880 : }
4881 : else {
4882 0 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
4883 :
4884 : snprintf(local_str,
4885 : DLT_DAEMON_TEXTBUFSIZE,
4886 : "Unregistered ApID '%.4s'",
4887 : userapp.apid);
4888 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str,
4889 : DLT_LOG_INFO, DLT_DAEMON_APP_ID,
4890 : DLT_DAEMON_CTX_ID, verbose);
4891 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
4892 : }
4893 : }
4894 : }
4895 : } else {
4896 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
4897 0 : return -1;
4898 : }
4899 :
4900 : return 0;
4901 : }
4902 :
4903 0 : int dlt_daemon_process_user_message_unregister_context(DltDaemon *daemon,
4904 : DltDaemonLocal *daemon_local,
4905 : DltReceiver *rec,
4906 : int verbose)
4907 : {
4908 : DltUserControlMsgUnregisterContext userctxt;
4909 : DltDaemonContext *context;
4910 :
4911 0 : PRINT_FUNCTION_VERBOSE(verbose);
4912 :
4913 0 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
4914 0 : dlt_vlog(LOG_ERR,
4915 : "Invalid function parameters used for %s\n",
4916 : __func__);
4917 :
4918 0 : return -1;
4919 : }
4920 :
4921 0 : if (daemon->daemon_version == DLTProtocolV2) {
4922 : uint32_t len = sizeof(DltUserControlMsgUnregisterContextV2);
4923 : DltUserControlMsgUnregisterContextV2 usercontext;
4924 : int usercontextSize;
4925 : uint8_t *buffer;
4926 0 : usercontext.apid = NULL;
4927 0 : usercontext.ctid = NULL;
4928 0 : usercontext.apidlen = (uint8_t)rec->buf[8]; // TBD: write function to get apidlen from received buffer
4929 0 : usercontext.ctidlen = (uint8_t)rec->buf[9 + usercontext.apidlen]; // TBD: write function to get ctidlen from received buffer
4930 0 : usercontextSize = (int)(sizeof(uint8_t) + usercontext.apidlen +
4931 0 : sizeof(uint8_t) + usercontext.ctidlen + sizeof(pid_t));
4932 : len = (uint32_t)usercontextSize;
4933 0 : buffer = (uint8_t*)malloc((size_t)usercontextSize);
4934 :
4935 : int offset = 0;
4936 :
4937 0 : if (dlt_receiver_check_and_get(rec,
4938 : buffer,
4939 : len,
4940 : DLT_RCV_SKIP_HEADER | DLT_RCV_REMOVE) < 0){
4941 : /* Not enough bytes received */
4942 0 : return -1;
4943 : }
4944 :
4945 : memcpy(&(usercontext.apidlen), buffer, 1);
4946 : offset = 1;
4947 0 : dlt_set_id_v2(usercontext.apid, (const char *)(buffer + offset), usercontext.apidlen);
4948 0 : offset += usercontext.apidlen;
4949 0 : memcpy(&(usercontext.ctidlen), (buffer + offset), 1);
4950 0 : offset += 1;
4951 0 : dlt_set_id_v2(usercontext.ctid, (const char *)(buffer + offset), usercontext.ctidlen);
4952 0 : offset += usercontext.ctidlen;
4953 0 : memcpy(&(usercontext.pid), (buffer + offset), sizeof(pid_t));
4954 : offset += (int)sizeof(pid_t);
4955 :
4956 0 : context = dlt_daemon_context_find_v2(daemon,
4957 : usercontext.apidlen,
4958 : usercontext.apid,
4959 : usercontext.ctidlen,
4960 : usercontext.ctid,
4961 0 : daemon->ecuid2len,
4962 0 : daemon->ecuid2,
4963 : verbose);
4964 :
4965 :
4966 : /* In case the daemon is loaded with predefined contexts and its context
4967 : * unregisters, the context information will not be deleted from daemon's
4968 : * table until its parent application is unregistered.
4969 : */
4970 0 : if (context && (context->predefined == false)) {
4971 : /* Delete this connection entry from internal table*/
4972 0 : if (dlt_daemon_context_del_v2(daemon, context, daemon->ecuid2len, daemon->ecuid2, verbose) == -1) {
4973 0 : dlt_vlog(LOG_WARNING,
4974 : "Can't delete CtID '%s' for ApID '%s' in %s\n",
4975 : usercontext.ctid ? usercontext.ctid : "<NULL>",
4976 : usercontext.apid ? usercontext.apid : "<NULL>",
4977 : __func__);
4978 0 : return -1;
4979 : }
4980 : else {
4981 0 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
4982 :
4983 : snprintf(local_str,
4984 : DLT_DAEMON_TEXTBUFSIZE,
4985 : "Unregistered CtID '%s' for ApID '%s'",
4986 : usercontext.ctid ? usercontext.ctid : "<NULL>",
4987 : usercontext.apid ? usercontext.apid : "<NULL>");
4988 :
4989 0 : if (verbose){
4990 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str,
4991 : DLT_LOG_INFO, DLT_DAEMON_APP_ID,
4992 : DLT_DAEMON_CTX_ID, verbose);
4993 : }
4994 :
4995 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
4996 : }
4997 : }
4998 :
4999 : /* Create automatic unregister context response for unregistered context */
5000 0 : if (daemon_local->flags.rflag)
5001 0 : dlt_daemon_control_message_unregister_context_v2(DLT_DAEMON_SEND_TO_ALL,
5002 : daemon,
5003 : daemon_local,
5004 : usercontext.apidlen,
5005 : usercontext.apid,
5006 : usercontext.ctidlen,
5007 : usercontext.ctid,
5008 : "remo",
5009 : verbose);
5010 : }
5011 0 : else if (daemon->daemon_version == DLTProtocolV1) {
5012 : uint32_t len = sizeof(DltUserControlMsgUnregisterContext);
5013 0 : if (dlt_receiver_check_and_get(rec,
5014 : &userctxt,
5015 : len,
5016 : DLT_RCV_SKIP_HEADER | DLT_RCV_REMOVE) < 0)
5017 : /* Not enough bytes received */
5018 : return -1;
5019 :
5020 0 : context = dlt_daemon_context_find(daemon,
5021 : userctxt.apid,
5022 : userctxt.ctid,
5023 0 : daemon->ecuid,
5024 : verbose);
5025 :
5026 : /* In case the daemon is loaded with predefined contexts and its context
5027 : * unregisters, the context information will not be deleted from daemon's
5028 : * table until its parent application is unregistered.
5029 : */
5030 0 : if (context && (context->predefined == false)) {
5031 : /* Delete this connection entry from internal table*/
5032 0 : if (dlt_daemon_context_del(daemon, context, daemon->ecuid, verbose) == -1) {
5033 0 : dlt_vlog(LOG_WARNING,
5034 : "Can't delete CtID '%.4s' for ApID '%.4s' in %s\n",
5035 : userctxt.ctid,
5036 : userctxt.apid,
5037 : __func__);
5038 0 : return -1;
5039 : }
5040 : else {
5041 0 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
5042 :
5043 : snprintf(local_str,
5044 : DLT_DAEMON_TEXTBUFSIZE,
5045 : "Unregistered CtID '%.4s' for ApID '%.4s'",
5046 : userctxt.ctid,
5047 : userctxt.apid);
5048 :
5049 0 : if (verbose)
5050 0 : dlt_daemon_log_internal(daemon, daemon_local, local_str,
5051 : DLT_LOG_INFO, DLT_DAEMON_APP_ID,
5052 : DLT_DAEMON_CTX_ID, verbose);
5053 :
5054 0 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
5055 : }
5056 : }
5057 :
5058 : /* Create automatic unregister context response for unregistered context */
5059 0 : if (daemon_local->flags.rflag)
5060 0 : dlt_daemon_control_message_unregister_context(DLT_DAEMON_SEND_TO_ALL,
5061 : daemon,
5062 : daemon_local,
5063 : userctxt.apid,
5064 : userctxt.ctid,
5065 : "remo",
5066 : verbose);
5067 : } else {
5068 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
5069 0 : return -1;
5070 : }
5071 : return 0;
5072 : }
5073 :
5074 267 : int dlt_daemon_process_user_message_log(DltDaemon *daemon,
5075 : DltDaemonLocal *daemon_local,
5076 : DltReceiver *rec,
5077 : int verbose)
5078 : {
5079 : int ret = 0;
5080 : int size = 0;
5081 : bool keep_message = true;
5082 267 : PRINT_FUNCTION_VERBOSE(verbose);
5083 :
5084 267 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
5085 0 : dlt_vlog(LOG_ERR, "%s: invalid function parameters.\n", __func__);
5086 0 : return DLT_DAEMON_ERROR_UNKNOWN;
5087 : }
5088 :
5089 : #ifdef DLT_SYSTEMD_WATCHDOG_ENFORCE_MSG_RX_ENABLE
5090 : daemon->received_message_since_last_watchdog_interval = 1;
5091 : #endif
5092 :
5093 : /* TODO: Add support for DLTv2 SHM messages */
5094 : #ifdef DLT_SHM_ENABLE
5095 : /** In case of SHM, the header still received via fifo/unix_socket receiver,
5096 : * so we need to remove header from the receiver.
5097 : */
5098 : if (dlt_receiver_remove(rec, sizeof(DltUserHeader)) < 0)
5099 : /* Not enough bytes received to remove*/
5100 : return DLT_DAEMON_ERROR_UNKNOWN;
5101 :
5102 : while (1) {
5103 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
5104 : bool watchdog_triggered = dlt_daemon_trigger_systemd_watchdog_if_necessary(daemon);
5105 : if (watchdog_triggered) {
5106 : dlt_vlog(LOG_WARNING, "%s yields due to watchdog.\n", __func__);
5107 : break;
5108 : }
5109 : #endif
5110 : /* get log message from SHM then store into receiver buffer */
5111 : size = dlt_shm_pull(&(daemon_local->dlt_shm),
5112 : daemon_local->recv_buf_shm,
5113 : DLT_SHM_RCV_BUFFER_SIZE);
5114 :
5115 : if (size <= 0)
5116 : break;
5117 :
5118 : ret = dlt_message_read(&(daemon_local->msg),
5119 : daemon_local->recv_buf_shm, size, 0, verbose);
5120 :
5121 : if (DLT_MESSAGE_ERROR_OK != ret) {
5122 : dlt_shm_remove(&(daemon_local->dlt_shm));
5123 : dlt_log(LOG_WARNING, "failed to read messages from shm.\n");
5124 : return DLT_DAEMON_ERROR_UNKNOWN;
5125 : }
5126 :
5127 : #if defined(DLT_LOG_LEVEL_APP_CONFIG) || defined(DLT_TRACE_LOAD_CTRL_ENABLE)
5128 : DltDaemonApplication *app = dlt_daemon_application_find(
5129 : daemon, daemon_local->msg.extendedheader->apid, daemon->ecuid, verbose);
5130 : #endif
5131 :
5132 : /* discard non-allowed levels if enforcement is on */
5133 : keep_message = enforce_context_ll_and_ts_keep_message(
5134 : daemon_local
5135 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
5136 : , app
5137 : #endif
5138 : );
5139 :
5140 : // check trace_load
5141 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
5142 : keep_message &= trace_load_keep_message(app, size, daemon, daemon_local, verbose);
5143 : #endif
5144 :
5145 : if (keep_message)
5146 : dlt_daemon_client_send_message_to_all_client(daemon, daemon_local, verbose);
5147 :
5148 : if (DLT_DAEMON_ERROR_OK != ret)
5149 : dlt_log(LOG_ERR, "failed to send message to client.\n");
5150 : }
5151 :
5152 : #else /* DLT_SHM_ENABLE */
5153 267 : if (daemon->daemon_version == DLTProtocolV2) {
5154 0 : ret = dlt_message_read_v2(&(daemon_local->msgv2),
5155 0 : (unsigned char *)rec->buf + sizeof(DltUserHeader),
5156 0 : (unsigned int) ((unsigned int) rec->bytesRcvd - sizeof(DltUserHeader)),
5157 : 0,
5158 : verbose);
5159 0 : if (ret != DLT_MESSAGE_ERROR_OK) {
5160 0 : if (ret != DLT_MESSAGE_ERROR_SIZE)
5161 : /* This is a normal usecase: The daemon reads the data in 10kb chunks.
5162 : * Thus the last trace in this chunk is probably not complete and will be completed
5163 : * with the next chunk read. This happens always when the FIFO is filled with more than 10kb before
5164 : * the daemon is able to read from the FIFO.
5165 : * Thus the loglevel of this message is set to DEBUG.
5166 : * A cleaner solution would be to check more in detail whether the message is not complete (normal usecase)
5167 : * or the headers are corrupted (error case). */
5168 0 : dlt_log(LOG_DEBUG, "Can't read messages from receiver\n");
5169 :
5170 0 : return DLT_DAEMON_ERROR_UNKNOWN;
5171 : }
5172 :
5173 : #if defined(DLT_LOG_LEVEL_APP_CONFIG) || defined(DLT_TRACE_LOAD_CTRL_ENABLE)
5174 : DltDaemonApplication *app = (DltDaemonApplication *)malloc(sizeof(DltDaemonApplication));
5175 : dlt_daemon_application_find_v2(
5176 : daemon, daemon_local->msgv2.extendedheaderv2->apidlen,
5177 : daemon_local->msgv2.extendedheaderv2->apid, daemon->ecuid2len, daemon->ecuid2, verbose, &app);
5178 : #endif
5179 :
5180 : /* discard non-allowed levels if enforcement is on */
5181 0 : keep_message = enforce_context_ll_and_ts_keep_message_v2(
5182 : daemon_local
5183 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
5184 : , app
5185 : #endif
5186 : );
5187 :
5188 : // check trace_load
5189 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
5190 : keep_message &=
5191 : trace_load_keep_message_v2(app, size, daemon, daemon_local, verbose);
5192 : #endif
5193 0 : if (keep_message){
5194 0 : dlt_daemon_client_send_message_to_all_client_v2(daemon, daemon_local, verbose);
5195 : }
5196 : /* keep not read data in buffer */
5197 0 : size = (int) (daemon_local->msgv2.headersizev2 +
5198 0 : daemon_local->msgv2.datasize +
5199 : (int32_t)sizeof(DltUserHeader) -
5200 0 : (int32_t) daemon_local->msgv2.storageheadersizev2);
5201 :
5202 0 : if (daemon_local->msgv2.found_serialheader)
5203 : size += (int) sizeof(dltSerialHeader);
5204 :
5205 0 : if (dlt_receiver_remove(rec, size) != DLT_RETURN_OK) {
5206 0 : dlt_log(LOG_WARNING, "failed to remove bytes from receiver.\n");
5207 0 : return DLT_DAEMON_ERROR_UNKNOWN;
5208 : }
5209 267 : } else if (daemon->daemon_version == DLTProtocolV1) {
5210 267 : ret = dlt_message_read(&(daemon_local->msg),
5211 267 : (unsigned char *)rec->buf + sizeof(DltUserHeader),
5212 267 : (unsigned int) ((unsigned int) rec->bytesRcvd - sizeof(DltUserHeader)),
5213 : 0,
5214 : verbose);
5215 :
5216 267 : if (ret != DLT_MESSAGE_ERROR_OK) {
5217 0 : if (ret != DLT_MESSAGE_ERROR_SIZE)
5218 : /* This is a normal usecase: The daemon reads the data in 10kb chunks.
5219 : * Thus the last trace in this chunk is probably not complete and will be completed
5220 : * with the next chunk read. This happens always when the FIFO is filled with more than 10kb before
5221 : * the daemon is able to read from the FIFO.
5222 : * Thus the loglevel of this message is set to DEBUG.
5223 : * A cleaner solution would be to check more in detail whether the message is not complete (normal usecase)
5224 : * or the headers are corrupted (error case). */
5225 0 : dlt_log(LOG_DEBUG, "Can't read messages from receiver\n");
5226 :
5227 0 : return DLT_DAEMON_ERROR_UNKNOWN;
5228 : }
5229 :
5230 : #if defined(DLT_LOG_LEVEL_APP_CONFIG) || defined(DLT_TRACE_LOAD_CTRL_ENABLE)
5231 : DltDaemonApplication *app = dlt_daemon_application_find(
5232 : daemon, daemon_local->msg.extendedheader->apid, daemon->ecuid, verbose);
5233 : #endif
5234 :
5235 : /* discard non-allowed levels if enforcement is on */
5236 0 : keep_message = enforce_context_ll_and_ts_keep_message(
5237 : daemon_local
5238 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
5239 : , app
5240 : #endif
5241 : );
5242 :
5243 : // check trace_load
5244 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
5245 : keep_message &=
5246 : trace_load_keep_message(app, size, daemon, daemon_local, verbose);
5247 : #endif
5248 0 : if (keep_message){
5249 267 : dlt_daemon_client_send_message_to_all_client(daemon, daemon_local, verbose);
5250 : }
5251 :
5252 : /* keep not read data in buffer */
5253 267 : size = (int) ((size_t)daemon_local->msg.headersize +
5254 267 : (size_t)daemon_local->msg.datasize - sizeof(DltStorageHeader) +
5255 : sizeof(DltUserHeader));
5256 :
5257 267 : if (daemon_local->msg.found_serialheader)
5258 : size += (int) sizeof(dltSerialHeader);
5259 :
5260 267 : if (dlt_receiver_remove(rec, size) != DLT_RETURN_OK) {
5261 0 : dlt_log(LOG_WARNING, "failed to remove bytes from receiver.\n");
5262 0 : return DLT_DAEMON_ERROR_UNKNOWN;
5263 : }
5264 : } else {
5265 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
5266 0 : return -1;
5267 : }
5268 : #endif /* DLT_SHM_ENABLE */
5269 :
5270 : return DLT_DAEMON_ERROR_OK;
5271 : }
5272 :
5273 0 : bool enforce_context_ll_and_ts_keep_message(DltDaemonLocal *daemon_local
5274 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
5275 : , DltDaemonApplication *app
5276 : #endif
5277 : )
5278 : {
5279 267 : if (!daemon_local->flags.enforceContextLLAndTS ||
5280 0 : !daemon_local->msg.extendedheader) {
5281 : return true;
5282 : }
5283 :
5284 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
5285 : if (app == NULL) {
5286 : return true;
5287 : }
5288 : #endif
5289 :
5290 0 : const int mtin = DLT_GET_MSIN_MTIN(daemon_local->msg.extendedheader->msin);
5291 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
5292 : if (app->num_context_log_level_settings > 0) {
5293 : DltDaemonContextLogSettings *log_settings =
5294 : dlt_daemon_find_app_log_level_config(app, daemon_local->msg.extendedheader->ctid);
5295 :
5296 : if (log_settings != NULL) {
5297 : return mtin <= log_settings->log_level;
5298 : }
5299 : }
5300 : #endif
5301 0 : return mtin <= daemon_local->flags.contextLogLevel;
5302 : }
5303 :
5304 0 : bool enforce_context_ll_and_ts_keep_message_v2(DltDaemonLocal *daemon_local
5305 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
5306 : , DltDaemonApplication *app
5307 : #endif
5308 : )
5309 : {
5310 0 : if (!daemon_local->flags.enforceContextLLAndTS) {
5311 : return true;
5312 : }
5313 :
5314 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
5315 : if (app == NULL) {
5316 : return true;
5317 : }
5318 : #endif
5319 :
5320 0 : const int mtin = DLT_GET_MSIN_MTIN(daemon_local->msgv2.headerextrav2.msin);
5321 : #ifdef DLT_LOG_LEVEL_APP_CONFIG
5322 : if (app->num_context_log_level_settings > 0) {
5323 : /* TODO: Call dlt_daemon_find_app_log_level_config_v2 for DLTv2 */
5324 : DltDaemonContextLogSettings *log_settings =
5325 : dlt_daemon_find_app_log_level_config(app, daemon_local->msgv2.extendedheaderv2->ctid);
5326 :
5327 : if (log_settings != NULL) {
5328 : return mtin <= log_settings->log_level;
5329 : }
5330 : }
5331 : #endif
5332 0 : return mtin <= daemon_local->flags.contextLogLevel;
5333 : }
5334 :
5335 :
5336 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
5337 : bool trace_load_keep_message(DltDaemonApplication *app,
5338 : const int size, DltDaemon *const daemon,
5339 : DltDaemonLocal *const daemon_local,
5340 : int verbose)
5341 : {
5342 : bool keep_message = true;
5343 : if (app == NULL || !daemon_local->msg.extendedheader) {
5344 : return keep_message;
5345 : }
5346 :
5347 : DltMessage* msg = &daemon_local->msg;
5348 : const int mtin = DLT_GET_MSIN_MTIN(msg->extendedheader->msin);
5349 :
5350 : struct DltTraceLoadLogParams params = {
5351 : daemon,
5352 : daemon_local,
5353 : verbose,
5354 : app->apid,
5355 : };
5356 :
5357 : DltDaemonContext *context = dlt_daemon_context_find(
5358 : daemon,
5359 : app->apid,
5360 : msg->extendedheader->ctid,
5361 : daemon->ecuid,
5362 : verbose);
5363 :
5364 :
5365 : if (context == NULL) {
5366 : context = dlt_daemon_context_add(
5367 : daemon,
5368 : app->apid,
5369 : msg->extendedheader->ctid,
5370 : daemon->default_log_level,
5371 : daemon->default_trace_status,
5372 : 0,
5373 : app->user_handle,
5374 : "",
5375 : daemon->ecuid,
5376 : verbose);
5377 : if (context == NULL) {
5378 : dlt_vlog(LOG_WARNING,
5379 : "Can't add ContextID '%.4s' for ApID '%.4s' in %s\n",
5380 : msg->extendedheader->ctid, app->apid, __func__);
5381 : return false;
5382 : }
5383 : }
5384 :
5385 : pthread_rwlock_wrlock(&trace_load_rw_lock);
5386 : keep_message = dlt_check_trace_load(
5387 : context->trace_load_settings, mtin, msg->headerextra.tmsp, size,
5388 : dlt_daemon_output_internal_msg, (void *)(¶ms));
5389 : pthread_rwlock_unlock(&trace_load_rw_lock);
5390 :
5391 : return keep_message;
5392 : }
5393 : #endif
5394 :
5395 0 : int dlt_daemon_process_user_message_set_app_ll_ts(DltDaemon *daemon,
5396 : DltDaemonLocal *daemon_local,
5397 : DltReceiver *rec,
5398 : int verbose)
5399 : {
5400 : uint32_t len = sizeof(DltUserControlMsgAppLogLevelTraceStatus);
5401 : DltUserControlMsgAppLogLevelTraceStatus userctxt;
5402 : DltDaemonApplication *application;
5403 : DltDaemonContext *context;
5404 : int i, offset_base;
5405 : int8_t old_log_level, old_trace_status;
5406 : DltDaemonRegisteredUsers *user_list = NULL;
5407 :
5408 0 : PRINT_FUNCTION_VERBOSE(verbose);
5409 :
5410 0 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
5411 0 : dlt_vlog(LOG_ERR,
5412 : "Invalid function parameters used for %s\n",
5413 : __func__);
5414 0 : return DLT_RETURN_ERROR;
5415 : }
5416 :
5417 0 : user_list = dlt_daemon_find_users_list(daemon, daemon->ecuid, verbose);
5418 :
5419 0 : if (user_list == NULL)
5420 : return DLT_RETURN_ERROR;
5421 :
5422 : memset(&userctxt, 0, len);
5423 :
5424 0 : if (dlt_receiver_check_and_get(rec,
5425 : &userctxt,
5426 : len,
5427 : DLT_RCV_SKIP_HEADER | DLT_RCV_REMOVE) < 0)
5428 : /* Not enough bytes received */
5429 : return DLT_RETURN_ERROR;
5430 :
5431 0 : if (user_list->num_applications > 0) {
5432 : /* Get all contexts with application id matching the received application id */
5433 0 : application = dlt_daemon_application_find(daemon,
5434 : userctxt.apid,
5435 : daemon->ecuid,
5436 : verbose);
5437 :
5438 0 : if (application) {
5439 : /* Calculate start offset within contexts[] */
5440 : offset_base = 0;
5441 :
5442 0 : for (i = 0; i < (application - (user_list->applications)); i++)
5443 0 : offset_base += user_list->applications[i].num_contexts;
5444 :
5445 0 : for (i = 0; i < application->num_contexts; i++) {
5446 0 : context = &(user_list->contexts[offset_base + i]);
5447 :
5448 0 : if (context) {
5449 0 : old_log_level = context->log_level;
5450 0 : context->log_level = (int8_t) userctxt.log_level; /* No endianess conversion necessary*/
5451 :
5452 0 : old_trace_status = context->trace_status;
5453 0 : context->trace_status = (int8_t) userctxt.trace_status; /* No endianess conversion necessary */
5454 :
5455 : /* The following function sends also the trace status */
5456 0 : if ((context->user_handle >= DLT_FD_MINIMUM) &&
5457 0 : (dlt_daemon_user_send_log_level(daemon,
5458 : context,
5459 : verbose) != 0)) {
5460 0 : context->log_level = old_log_level;
5461 0 : context->trace_status = old_trace_status;
5462 : }
5463 : }
5464 : }
5465 : }
5466 : }
5467 :
5468 : return DLT_RETURN_OK;
5469 : }
5470 :
5471 0 : int dlt_daemon_process_user_message_log_mode(DltDaemon *daemon,
5472 : DltDaemonLocal *daemon_local,
5473 : DltReceiver *rec,
5474 : int verbose)
5475 : {
5476 : DltUserControlMsgLogMode userctxt;
5477 : uint32_t len = sizeof(DltUserControlMsgLogMode);
5478 :
5479 0 : PRINT_FUNCTION_VERBOSE(verbose);
5480 :
5481 0 : if ((daemon == 0) || (daemon_local == 0)) {
5482 0 : dlt_log(LOG_ERR, "Invalid function parameters used for function dlt_daemon_process_log_mode()\n");
5483 0 : return -1;
5484 : }
5485 :
5486 : memset(&userctxt, 0, len);
5487 :
5488 0 : if (dlt_receiver_check_and_get(rec,
5489 : &userctxt,
5490 : len,
5491 : DLT_RCV_SKIP_HEADER | DLT_RCV_REMOVE) < 0)
5492 : /* Not enough bytes received */
5493 : return -1;
5494 :
5495 : /* set the new log mode */
5496 0 : daemon->mode = userctxt.log_mode;
5497 :
5498 : /* write configuration persistantly */
5499 0 : dlt_daemon_configuration_save(daemon, daemon->runtime_configuration, verbose);
5500 :
5501 0 : return 0;
5502 : }
5503 :
5504 0 : int dlt_daemon_process_user_message_marker(DltDaemon *daemon,
5505 : DltDaemonLocal *daemon_local,
5506 : DltReceiver *rec,
5507 : int verbose)
5508 : {
5509 : uint32_t len = sizeof(DltUserControlMsgLogMode);
5510 : DltUserControlMsgLogMode userctxt;
5511 0 : PRINT_FUNCTION_VERBOSE(verbose);
5512 :
5513 0 : if ((daemon == NULL) || (daemon_local == NULL) || (rec == NULL)) {
5514 0 : dlt_vlog(LOG_ERR, "Invalid function parameters used for %s\n",
5515 : __func__);
5516 0 : return -1;
5517 : }
5518 :
5519 : memset(&userctxt, 0, len);
5520 :
5521 0 : if (dlt_receiver_check_and_get(rec,
5522 : &userctxt,
5523 : len,
5524 : DLT_RCV_SKIP_HEADER | DLT_RCV_REMOVE) < 0)
5525 : /* Not enough bytes received */
5526 : return -1;
5527 :
5528 : /* Create automatic unregister context response for unregistered context */
5529 0 : dlt_daemon_control_message_marker(DLT_DAEMON_SEND_TO_ALL, daemon, daemon_local, verbose);
5530 :
5531 0 : return 0;
5532 : }
5533 :
5534 0 : int dlt_daemon_send_ringbuffer_to_client(DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
5535 : {
5536 : int ret;
5537 : static uint8_t data[DLT_DAEMON_RCVBUFSIZE];
5538 : int length;
5539 :
5540 0 : PRINT_FUNCTION_VERBOSE(verbose);
5541 :
5542 0 : if ((daemon == 0) || (daemon_local == 0)) {
5543 0 : dlt_log(LOG_ERR, "Invalid function parameters used for function dlt_daemon_send_ringbuffer_to_client()\n");
5544 0 : return DLT_DAEMON_ERROR_UNKNOWN;
5545 : }
5546 :
5547 0 : if (dlt_buffer_get_message_count(&(daemon->client_ringbuffer)) <= 0) {
5548 0 : dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_SEND_DIRECT);
5549 0 : return DLT_DAEMON_ERROR_OK;
5550 : }
5551 :
5552 0 : while ((length = dlt_buffer_copy(&(daemon->client_ringbuffer), data, sizeof(data))) > 0) {
5553 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
5554 : dlt_daemon_trigger_systemd_watchdog_if_necessary(daemon);
5555 : #endif
5556 :
5557 0 : if ((ret =
5558 0 : dlt_daemon_client_send(DLT_DAEMON_SEND_FORCE, daemon, daemon_local, 0, 0, data, length, 0, 0,
5559 : verbose)))
5560 0 : return ret;
5561 :
5562 0 : dlt_buffer_remove(&(daemon->client_ringbuffer));
5563 :
5564 0 : if (daemon->state != DLT_DAEMON_STATE_SEND_BUFFER)
5565 0 : dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_SEND_BUFFER);
5566 :
5567 0 : if (dlt_buffer_get_message_count(&(daemon->client_ringbuffer)) <= 0) {
5568 0 : dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_SEND_DIRECT);
5569 0 : return DLT_DAEMON_ERROR_OK;
5570 : }
5571 : }
5572 :
5573 : return DLT_DAEMON_ERROR_OK;
5574 : }
5575 :
5576 0 : int dlt_daemon_send_ringbuffer_to_client_v2(DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
5577 : {
5578 : int ret;
5579 : static uint8_t data[DLT_DAEMON_RCVBUFSIZE];
5580 : int length;
5581 :
5582 0 : PRINT_FUNCTION_VERBOSE(verbose);
5583 :
5584 0 : if ((daemon == 0) || (daemon_local == 0)) {
5585 0 : dlt_log(LOG_ERR, "Invalid function parameters used for function dlt_daemon_send_ringbuffer_to_client()\n");
5586 0 : return DLT_DAEMON_ERROR_UNKNOWN;
5587 : }
5588 :
5589 0 : if (dlt_buffer_get_message_count(&(daemon->client_ringbuffer)) <= 0) {
5590 0 : dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_SEND_DIRECT);
5591 0 : return DLT_DAEMON_ERROR_OK;
5592 : }
5593 :
5594 0 : while ((length = dlt_buffer_copy(&(daemon->client_ringbuffer), data, sizeof(data))) > 0) {
5595 : #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
5596 : dlt_daemon_trigger_systemd_watchdog_if_necessary(daemon);
5597 : #endif
5598 :
5599 0 : if ((ret =
5600 0 : dlt_daemon_client_send_v2(DLT_DAEMON_SEND_FORCE, daemon, daemon_local, 0, 0, data, length, 0, 0,
5601 : verbose)))
5602 0 : return ret;
5603 :
5604 0 : dlt_buffer_remove(&(daemon->client_ringbuffer));
5605 :
5606 0 : if (daemon->state != DLT_DAEMON_STATE_SEND_BUFFER)
5607 0 : dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_SEND_BUFFER);
5608 :
5609 0 : if (dlt_buffer_get_message_count(&(daemon->client_ringbuffer)) <= 0) {
5610 0 : dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_SEND_DIRECT);
5611 0 : return DLT_DAEMON_ERROR_OK;
5612 : }
5613 : }
5614 :
5615 : return DLT_DAEMON_ERROR_OK;
5616 : }
5617 :
5618 : #ifdef __QNX__
5619 : static void *timer_thread(void *data)
5620 : {
5621 : int pexit = 0;
5622 : unsigned int sleep_ret = 0;
5623 :
5624 : DltDaemonPeriodicData* timer_thread_data = (DltDaemonPeriodicData*) data;
5625 :
5626 : /* Timer will start in starts_in sec*/
5627 : if ((sleep_ret = sleep(timer_thread_data->starts_in))) {
5628 : dlt_vlog(LOG_NOTICE, "Sleep remains [%u] for starting!"
5629 : "Stop thread of timer [%d]\n",
5630 : sleep_ret, timer_thread_data->timer_id);
5631 : close_pipes(dlt_timer_pipes[timer_thread_data->timer_id]);
5632 : return NULL;
5633 : }
5634 :
5635 : while (1) {
5636 : if ((dlt_timer_pipes[timer_thread_data->timer_id][1] > 0) &&
5637 : (0 > write(dlt_timer_pipes[timer_thread_data->timer_id][1], "1", 1))) {
5638 : dlt_vlog(LOG_ERR, "Failed to send notification for timer [%s]!\n",
5639 : dlt_timer_names[timer_thread_data->timer_id]);
5640 : pexit = 1;
5641 : }
5642 :
5643 : if (pexit || g_exit) {
5644 : dlt_vlog(LOG_NOTICE, "Received signal!"
5645 : "Stop thread of timer [%d]\n",
5646 : timer_thread_data->timer_id);
5647 : close_pipes(dlt_timer_pipes[timer_thread_data->timer_id]);
5648 : return NULL;
5649 : }
5650 :
5651 : if ((sleep_ret = sleep(timer_thread_data->period_sec))) {
5652 : dlt_vlog(LOG_NOTICE, "Sleep remains [%u] for interval!"
5653 : "Stop thread of timer [%d]\n",
5654 : sleep_ret, timer_thread_data->timer_id);
5655 : close_pipes(dlt_timer_pipes[timer_thread_data->timer_id]);
5656 : return NULL;
5657 : }
5658 : }
5659 : }
5660 : #endif
5661 :
5662 1 : int create_timer_fd(DltDaemonLocal *daemon_local,
5663 : int period_sec,
5664 : int starts_in,
5665 : DltTimers timer_id)
5666 : {
5667 : int local_fd = DLT_FD_INIT;
5668 : char *timer_name = NULL;
5669 :
5670 1 : if (timer_id >= DLT_TIMER_UNKNOWN) {
5671 0 : dlt_log(DLT_LOG_ERROR, "Unknown timer.");
5672 0 : return -1;
5673 : }
5674 :
5675 1 : timer_name = dlt_timer_names[timer_id];
5676 :
5677 1 : if (daemon_local == NULL) {
5678 0 : dlt_log(DLT_LOG_ERROR, "Daemon local structure is NULL");
5679 0 : return -1;
5680 : }
5681 :
5682 1 : if ((period_sec <= 0) || (starts_in <= 0)) {
5683 : /* timer not activated via the service file */
5684 0 : dlt_vlog(LOG_INFO, "<%s> not set: period=0\n", timer_name);
5685 : local_fd = DLT_FD_INIT;
5686 : }
5687 : else {
5688 : #ifdef linux
5689 : struct itimerspec l_timer_spec;
5690 1 : local_fd = timerfd_create(CLOCK_MONOTONIC, 0);
5691 :
5692 1 : if (local_fd < 0)
5693 0 : dlt_vlog(LOG_WARNING, "<%s> timerfd_create failed: %s\n",
5694 0 : timer_name, strerror(errno));
5695 :
5696 1 : l_timer_spec.it_interval.tv_sec = period_sec;
5697 1 : l_timer_spec.it_interval.tv_nsec = 0;
5698 1 : l_timer_spec.it_value.tv_sec = starts_in;
5699 1 : l_timer_spec.it_value.tv_nsec = 0;
5700 :
5701 1 : if (timerfd_settime(local_fd, 0, &l_timer_spec, NULL) < 0) {
5702 0 : dlt_vlog(LOG_WARNING, "<%s> timerfd_settime failed: %s\n",
5703 0 : timer_name, strerror(errno));
5704 : local_fd = DLT_FD_INIT;
5705 : }
5706 : #elif __QNX__
5707 : /*
5708 : * Since timerfd is not valid in QNX, new threads are introduced
5709 : * to manage timers and communicate with main thread when timer expires.
5710 : */
5711 : if(0 != pipe(dlt_timer_pipes[timer_id])) {
5712 : dlt_vlog(LOG_ERR, "Failed to create pipe for timer [%s]",
5713 : dlt_timer_names[timer_id]);
5714 : return -1;
5715 : }
5716 : if (NULL == timer_data[timer_id]) {
5717 : timer_data[timer_id] = calloc(1, sizeof(DltDaemonPeriodicData));
5718 : if (NULL == timer_data[timer_id]) {
5719 : dlt_vlog(LOG_ERR, "Failed to allocate memory for timer_data [%s]!\n",
5720 : dlt_timer_names[timer_id]);
5721 : close_pipes(dlt_timer_pipes[timer_id]);
5722 : return -1;
5723 : }
5724 : }
5725 :
5726 : timer_data[timer_id]->timer_id = timer_id;
5727 : timer_data[timer_id]->period_sec = period_sec;
5728 : timer_data[timer_id]->starts_in = starts_in;
5729 : timer_data[timer_id]->wakeups_missed = 0;
5730 :
5731 : if (0 != pthread_create(&timer_threads[timer_id], NULL,
5732 : &timer_thread, (void*)timer_data[timer_id])) {
5733 : dlt_vlog(LOG_ERR, "Failed to create new thread for timer [%s]!\n",
5734 : dlt_timer_names[timer_id]);
5735 : /* Clean up timer before returning */
5736 : close_pipes(dlt_timer_pipes[timer_id]);
5737 : free(timer_data[timer_id]);
5738 : timer_data[timer_id] = NULL;
5739 :
5740 : return -1;
5741 : }
5742 : local_fd = dlt_timer_pipes[timer_id][0];
5743 : #endif
5744 : }
5745 :
5746 1 : return dlt_connection_create(daemon_local,
5747 : &daemon_local->pEvent,
5748 : local_fd,
5749 : POLLIN,
5750 1 : dlt_timer_conn_types[timer_id]);
5751 : }
5752 :
5753 : /* Close connection function */
5754 1 : int dlt_daemon_close_socket(int sock, DltDaemon *daemon, DltDaemonLocal *daemon_local, int verbose)
5755 : {
5756 1 : char local_str[DLT_DAEMON_TEXTBUFSIZE] = { '\0' };
5757 :
5758 1 : PRINT_FUNCTION_VERBOSE(verbose);
5759 :
5760 1 : if ((daemon_local == NULL) || (daemon == NULL)) {
5761 0 : dlt_log(LOG_ERR, "dlt_daemon_close_socket: Invalid input parmeters\n");
5762 0 : return -1;
5763 : }
5764 :
5765 : /* Closure is done while unregistering has for any connection */
5766 1 : dlt_event_handler_unregister_connection(&daemon_local->pEvent,
5767 : daemon_local,
5768 : sock);
5769 :
5770 1 : if (daemon_local->client_connections == 0) {
5771 : /* send new log state to all applications */
5772 1 : daemon->connectionState = 0;
5773 :
5774 1 : if (daemon->daemon_version == DLTProtocolV2) {
5775 0 : dlt_daemon_user_send_all_log_state_v2(daemon, verbose);
5776 1 : } else if (daemon->daemon_version == DLTProtocolV1) {
5777 1 : dlt_daemon_user_send_all_log_state(daemon, verbose);
5778 : } else {
5779 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
5780 0 : return -1;
5781 : }
5782 :
5783 : /* For offline tracing we still can use the same states */
5784 : /* as for socket sending. Using this trick we see the traces */
5785 : /* In the offline trace AND in the socket stream. */
5786 1 : if (daemon_local->flags.yvalue[0] == 0)
5787 1 : dlt_daemon_change_state(daemon, DLT_DAEMON_STATE_BUFFER);
5788 : }
5789 :
5790 1 : if (daemon->daemon_version == DLTProtocolV2) {
5791 0 : dlt_daemon_control_message_connection_info_v2(DLT_DAEMON_SEND_TO_ALL,
5792 : daemon,
5793 : daemon_local,
5794 : DLT_CONNECTION_STATUS_DISCONNECTED,
5795 : "",
5796 : verbose);
5797 1 : } else if (daemon->daemon_version == DLTProtocolV1) {
5798 1 : dlt_daemon_control_message_connection_info(DLT_DAEMON_SEND_TO_ALL,
5799 : daemon,
5800 : daemon_local,
5801 : DLT_CONNECTION_STATUS_DISCONNECTED,
5802 : "",
5803 : verbose);
5804 : } else {
5805 0 : dlt_vlog(LOG_ERR, "Unsupported DLT version %u in %s\n", daemon->daemon_version, __func__);
5806 0 : return -1;
5807 : }
5808 :
5809 1 : snprintf(local_str, DLT_DAEMON_TEXTBUFSIZE,
5810 : "Client connection #%d closed. Total Clients : %d",
5811 : sock,
5812 : daemon_local->client_connections);
5813 1 : dlt_daemon_log_internal(daemon, daemon_local, local_str, DLT_LOG_INFO,
5814 : DLT_DAEMON_APP_ID, DLT_DAEMON_CTX_ID,
5815 : daemon_local->flags.vflag);
5816 1 : dlt_vlog(LOG_DEBUG, "%s%s", local_str, "\n");
5817 :
5818 1 : return 0;
5819 : }
5820 :
5821 : #ifdef DLT_TRACE_LOAD_CTRL_ENABLE
5822 :
5823 : static DltReturnValue dlt_daemon_output_internal_msg(
5824 : const DltLogLevelType loglevel, const char *const text, void* const params) {
5825 : struct DltTraceLoadLogParams* log_params = (struct DltTraceLoadLogParams*)params;
5826 : return dlt_daemon_log_internal(
5827 : log_params->daemon, log_params->daemon_local, (char *)text, loglevel,
5828 : log_params->app_id, DLT_TRACE_LOAD_CONTEXT_ID, log_params->verbose);
5829 : }
5830 : #endif
5831 :
5832 :
5833 : /**
5834 : \}
5835 : */
|