-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathdlt_user.c
5480 lines (4432 loc) · 190 KB
/
dlt_user.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX license identifier: MPL-2.0
*
* Copyright (C) 2011-2015, BMW AG
*
* This file is part of COVESA Project DLT - Diagnostic Log and Trace.
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* For further information see http://www.covesa.org/.
*/
/*!
* \author
* Alexander Wenzel <alexander.aw.wenzel@bmw.de>
* Markus Klein <Markus.Klein@esk.fraunhofer.de>
* Mikko Rapeli <mikko.rapeli@bmw.de>
*
* \copyright Copyright © 2011-2015 BMW AG. \n
* License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
*
* \file dlt_user.c
*/
#include <stdlib.h> /* for getenv(), free(), atexit() */
#include <string.h> /* for strcmp(), strncmp(), strlen(), memset(), memcpy() */
#include <signal.h> /* for signal(), SIGPIPE, SIG_IGN */
#if !defined (__WIN32__)
# include <syslog.h> /* for LOG_... */
# include <semaphore.h>
# include <pthread.h> /* POSIX Threads */
#endif
#include <sys/time.h>
#include <math.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/uio.h> /* writev() */
#include <poll.h>
#include <limits.h>
#ifdef linux
# include <sys/prctl.h> /* for PR_SET_NAME */
#endif
#include <sys/types.h> /* needed for getpid() */
#include <unistd.h>
#include <stdbool.h>
#include <stdatomic.h>
#if defined DLT_LIB_USE_UNIX_SOCKET_IPC || defined DLT_LIB_USE_VSOCK_IPC
# include <sys/socket.h>
#endif
#ifdef DLT_LIB_USE_UNIX_SOCKET_IPC
# include <sys/un.h>
#endif
#ifdef DLT_LIB_USE_VSOCK_IPC
# ifdef linux
# include <linux/vm_sockets.h>
# endif
# ifdef __QNX__
# include <vm_sockets.h>
# endif
#endif
#include "dlt_user.h"
#include "dlt_common.h"
#include "dlt_log.h"
#include "dlt_user_shared.h"
#include "dlt_user_shared_cfg.h"
#include "dlt_user_cfg.h"
#ifdef DLT_FATAL_LOG_RESET_ENABLE
# define DLT_LOG_FATAL_RESET_TRAP(LOGLEVEL) \
do { \
if (LOGLEVEL == DLT_LOG_FATAL) { \
int *p = NULL; \
*p = 0; \
} \
} while (false)
#else /* DLT_FATAL_LOG_RESET_ENABLE */
# define DLT_LOG_FATAL_RESET_TRAP(LOGLEVEL)
#endif /* DLT_FATAL_LOG_RESET_ENABLE */
enum InitState {
INIT_UNITIALIZED,
INIT_IN_PROGRESS,
INIT_ERROR,
INIT_DONE
};
static DltUser dlt_user;
static _Atomic enum InitState dlt_user_init_state = INIT_UNITIALIZED;
#define DLT_USER_INITALIZED (dlt_user_init_state == INIT_DONE)
#define DLT_USER_INIT_ERROR (dlt_user_init_state == INIT_ERROR)
#define DLT_USER_INITALIZED_NOT_FREEING (DLT_USER_INITALIZED && (dlt_user_freeing == 0))
static _Atomic int dlt_user_freeing = 0;
static bool dlt_user_file_reach_max = false;
#ifdef DLT_LIB_USE_FIFO_IPC
static char dlt_user_dir[DLT_PATH_MAX];
static char dlt_daemon_fifo[DLT_PATH_MAX];
#endif
static pthread_mutex_t dlt_mutex;
static pthread_mutexattr_t dlt_mutex_attr;
static pthread_t dlt_housekeeperthread_handle;
/* Sync housekeeper thread start */
pthread_mutex_t dlt_housekeeper_running_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t dlt_housekeeper_running_cond;
/* calling dlt_user_atexit_handler() second time fails with error message */
static int atexit_registered = 0;
/* used to disallow DLT usage in fork() child */
static int g_dlt_is_child = 0;
/* String truncate message */
static const char STR_TRUNCATED_MESSAGE[] = "... <<Message truncated, too long>>";
/* Enum for type of string */
enum StringType
{
ASCII_STRING = 0,
UTF8_STRING = 1
};
/* Data type holding "Variable Info" (VARI) properties
* Some of the supported data types (eg. bool, string, raw) have only "name", but not "unit".
*/
typedef struct VarInfo
{
const char *name; // the "name" attribute (can be NULL)
const char *unit; // the "unit" attribute (can be NULL)
bool with_unit; // true if the "unit" field is to be considered
} VarInfo;
#define DLT_UNUSED(x) (void)(x)
/* Network trace */
#ifdef DLT_NETWORK_TRACE_ENABLE
#define DLT_USER_SEGMENTED_THREAD (1<<2)
/* Segmented Network Trace */
#define DLT_MAX_TRACE_SEGMENT_SIZE 1024
#define DLT_MESSAGE_QUEUE_NAME "/dlt_message_queue"
#define DLT_DELAYED_RESEND_INDICATOR_PATTERN 0xFFFF
/* Mutex to wait on while message queue is not initialized */
pthread_mutex_t mq_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t mq_init_condition;
#endif /* DLT_NETWORK_TRACE_ENABLE */
void dlt_lock_mutex(pthread_mutex_t *mutex)
{
int32_t lock_mutex_result = pthread_mutex_lock(mutex);
if (lock_mutex_result != 0)
dlt_vlog(LOG_ERR,
"Mutex lock failed unexpected pid=%i with result %i!\n",
getpid(), lock_mutex_result);
}
void dlt_unlock_mutex(pthread_mutex_t *mutex)
{
pthread_mutex_unlock(mutex);
}
/* Structure to pass data to segmented thread */
typedef struct
{
DltContext *handle;
uint32_t id;
DltNetworkTraceType nw_trace_type;
uint32_t header_len;
void *header;
uint32_t payload_len;
void *payload;
} s_segmented_data;
/* Function prototypes for internally used functions */
static void dlt_user_housekeeperthread_function(void *ptr);
static void dlt_user_atexit_handler(void);
static DltReturnValue dlt_user_log_init(DltContext *handle, DltContextData *log);
static DltReturnValue dlt_user_log_send_log(DltContextData *log, int mtype, int *sent_size);
static DltReturnValue dlt_user_log_send_register_application(void);
static DltReturnValue dlt_user_log_send_unregister_application(void);
static DltReturnValue dlt_user_log_send_register_context(DltContextData *log);
static DltReturnValue dlt_user_log_send_unregister_context(DltContextData *log);
static DltReturnValue dlt_send_app_ll_ts_limit(const char *apid,
DltLogLevelType loglevel,
DltTraceStatusType tracestatus);
static DltReturnValue dlt_user_log_send_log_mode(DltUserLogMode mode);
static DltReturnValue dlt_user_log_send_marker();
static DltReturnValue dlt_user_print_msg(DltMessage *msg, DltContextData *log);
static DltReturnValue dlt_user_log_check_user_message(void);
static void dlt_user_log_reattach_to_daemon(void);
static DltReturnValue dlt_user_log_send_overflow(void);
static DltReturnValue dlt_user_log_out_error_handling(void *ptr1,
size_t len1,
void *ptr2,
size_t len2,
void *ptr3,
size_t len3);
static void dlt_user_cleanup_handler(void *arg);
static int dlt_start_threads();
static void dlt_stop_threads();
static void dlt_fork_child_fork_handler();
#ifdef DLT_NETWORK_TRACE_ENABLE
static void dlt_user_trace_network_segmented_thread(void *unused);
static void dlt_user_trace_network_segmented_thread_segmenter(s_segmented_data *data);
#endif
static DltReturnValue dlt_user_log_write_string_utils_attr(DltContextData *log, const char *text, const enum StringType type, const char *name, bool with_var_info);
static DltReturnValue dlt_user_log_write_sized_string_utils_attr(DltContextData *log, const char *text, size_t length, const enum StringType type, const char *name, bool with_var_info);
static DltReturnValue dlt_unregister_app_util(bool force_sending_messages);
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
/* For trace load control feature */
static DltReturnValue dlt_user_output_internal_msg(DltLogLevelType loglevel, const char *text, void* params);
DltTraceLoadSettings* trace_load_settings = NULL;
uint32_t trace_load_settings_count = 0;
pthread_rwlock_t trace_load_rw_lock = PTHREAD_RWLOCK_INITIALIZER;
#endif
DltReturnValue dlt_user_check_library_version(const char *user_major_version, const char *user_minor_version)
{
char lib_major_version[DLT_USER_MAX_LIB_VERSION_LENGTH];
char lib_minor_version[DLT_USER_MAX_LIB_VERSION_LENGTH];
dlt_get_major_version(lib_major_version, DLT_USER_MAX_LIB_VERSION_LENGTH);
dlt_get_minor_version(lib_minor_version, DLT_USER_MAX_LIB_VERSION_LENGTH);
if ((strcmp(lib_major_version, user_major_version) != 0) || (strcmp(lib_minor_version, user_minor_version) != 0)) {
dlt_vnlog(LOG_WARNING,
DLT_USER_BUFFER_LENGTH,
"DLT Library version check failed! Installed DLT library version is %s.%s - Application using DLT library version %s.%s\n",
lib_major_version,
lib_minor_version,
user_major_version,
user_minor_version);
return DLT_RETURN_ERROR;
}
return DLT_RETURN_OK;
}
#if defined DLT_LIB_USE_UNIX_SOCKET_IPC || defined DLT_LIB_USE_VSOCK_IPC
static DltReturnValue dlt_socket_set_nonblock_and_linger(int sockfd)
{
int status;
struct linger l_opt;
status = fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK);
if (status == -1) {
dlt_log(LOG_INFO, "Socket cannot be changed to NON BLOCK\n");
return DLT_RETURN_ERROR;
}
l_opt.l_onoff = 1;
l_opt.l_linger = 10;
if (setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &l_opt, sizeof l_opt) < 0)
dlt_log(LOG_WARNING, "Failed to set socket linger option\n");
return DLT_RETURN_OK;
}
#endif
#ifdef DLT_LIB_USE_UNIX_SOCKET_IPC
static DltReturnValue dlt_initialize_socket_connection(void)
{
struct sockaddr_un remote;
char dltSockBaseDir[DLT_IPC_PATH_MAX];
DLT_SEM_LOCK();
int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (sockfd == DLT_FD_INIT) {
dlt_log(LOG_CRIT, "Failed to create socket\n");
DLT_SEM_FREE();
return DLT_RETURN_ERROR;
}
if (dlt_socket_set_nonblock_and_linger(sockfd) != DLT_RETURN_OK) {
close(sockfd);
DLT_SEM_FREE();
return DLT_RETURN_ERROR;
}
remote.sun_family = AF_UNIX;
snprintf(dltSockBaseDir, DLT_IPC_PATH_MAX, "%s/dlt", DLT_USER_IPC_PATH);
strncpy(remote.sun_path, dltSockBaseDir, sizeof(remote.sun_path));
if (strlen(DLT_USER_IPC_PATH) > DLT_IPC_PATH_MAX)
dlt_vlog(LOG_INFO,
"Provided path too long...trimming it to path[%s]\n",
dltSockBaseDir);
if (connect(sockfd, (struct sockaddr *)&remote, sizeof(remote)) == -1) {
if (dlt_user.connection_state != DLT_USER_RETRY_CONNECT) {
dlt_vlog(LOG_INFO,
"Socket %s cannot be opened (errno=%d). Retrying later...\n",
dltSockBaseDir, errno);
dlt_user.connection_state = DLT_USER_RETRY_CONNECT;
}
close(sockfd);
dlt_user.dlt_log_handle = -1;
}
else {
dlt_user.dlt_log_handle = sockfd;
dlt_user.connection_state = DLT_USER_CONNECTED;
if (dlt_receiver_init(&(dlt_user.receiver),
sockfd,
DLT_RECEIVE_SOCKET,
DLT_USER_RCVBUF_MAX_SIZE) == DLT_RETURN_ERROR) {
dlt_user_init_state = INIT_UNITIALIZED;
close(sockfd);
DLT_SEM_FREE();
return DLT_RETURN_ERROR;
}
}
DLT_SEM_FREE();
return DLT_RETURN_OK;
}
#elif defined DLT_LIB_USE_VSOCK_IPC
static DltReturnValue dlt_initialize_vsock_connection()
{
struct sockaddr_vm remote;
DLT_SEM_LOCK();
int sockfd = socket(AF_VSOCK, SOCK_STREAM, 0);
if (sockfd == DLT_FD_INIT) {
dlt_log(LOG_CRIT, "Failed to create VSOCK socket\n");
DLT_SEM_FREE();
return DLT_RETURN_ERROR;
}
memset(&remote, 0, sizeof(remote));
remote.svm_family = AF_VSOCK;
remote.svm_port = DLT_VSOCK_PORT;
remote.svm_cid = VMADDR_CID_HOST;
if (connect(sockfd, (struct sockaddr *)&remote, sizeof(remote)) == -1) {
if (dlt_user.connection_state != DLT_USER_RETRY_CONNECT) {
dlt_vlog(LOG_INFO, "VSOCK socket cannot be opened. Retrying later...\n");
dlt_user.connection_state = DLT_USER_RETRY_CONNECT;
}
close(sockfd);
dlt_user.dlt_log_handle = -1;
}
else {
/* Set to non-blocking after connect() to avoid EINPROGRESS. DltUserConntextionState
needs "connecting" state if connect() should be non-blocking. */
if (dlt_socket_set_nonblock_and_linger(sockfd) != DLT_RETURN_OK) {
close(sockfd);
DLT_SEM_FREE();
return DLT_RETURN_ERROR;
}
dlt_user.dlt_log_handle = sockfd;
dlt_user.connection_state = DLT_USER_CONNECTED;
if (dlt_receiver_init(&(dlt_user.receiver),
sockfd,
DLT_RECEIVE_SOCKET,
DLT_USER_RCVBUF_MAX_SIZE) == DLT_RETURN_ERROR) {
dlt_user_init_state = INIT_UNITIALIZED;
close(sockfd);
DLT_SEM_FREE();
return DLT_RETURN_ERROR;
}
}
DLT_SEM_FREE();
return DLT_RETURN_OK;
}
#else /* DLT_LIB_USE_FIFO_IPC */
static DltReturnValue dlt_initialize_fifo_connection(void)
{
char filename[DLT_PATH_MAX];
int ret;
snprintf(dlt_user_dir, DLT_PATH_MAX, "%s/dltpipes", dltFifoBaseDir);
snprintf(dlt_daemon_fifo, DLT_PATH_MAX, "%s/dlt", dltFifoBaseDir);
ret = mkdir(dlt_user_dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_ISVTX);
if ((ret == -1) && (errno != EEXIST)) {
dlt_vnlog(LOG_ERR, DLT_USER_BUFFER_LENGTH, "FIFO user dir %s cannot be created!\n", dlt_user_dir);
return DLT_RETURN_ERROR;
}
/* if dlt pipes directory is created by the application also chmod the directory */
if (ret == 0) {
/* S_ISGID cannot be set by mkdir, let's reassign right bits */
ret = chmod(dlt_user_dir,
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH | S_ISGID |
S_ISVTX);
if (ret == -1) {
dlt_vnlog(LOG_ERR, DLT_USER_BUFFER_LENGTH, "FIFO user dir %s cannot be chmoded!\n", dlt_user_dir);
return DLT_RETURN_ERROR;
}
}
/* create and open DLT user FIFO */
snprintf(filename, DLT_PATH_MAX, "%s/dlt%d", dlt_user_dir, getpid());
/* Try to delete existing pipe, ignore result of unlink */
unlink(filename);
ret = mkfifo(filename, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP);
if (ret == -1)
dlt_vnlog(LOG_WARNING, DLT_USER_BUFFER_LENGTH, "Loging disabled, FIFO user %s cannot be created!\n", filename);
/* S_IWGRP cannot be set by mkfifo (???), let's reassign right bits */
ret = chmod(filename, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP);
if (ret == -1) {
dlt_vnlog(LOG_WARNING, DLT_USER_BUFFER_LENGTH, "FIFO user %s cannot be chmoded!\n", dlt_user_dir);
return DLT_RETURN_ERROR;
}
dlt_user.dlt_user_handle = open(filename, O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (dlt_user.dlt_user_handle == DLT_FD_INIT) {
dlt_vnlog(LOG_WARNING, DLT_USER_BUFFER_LENGTH, "Logging disabled, FIFO user %s cannot be opened!\n", filename);
unlink(filename);
return DLT_RETURN_OK;
}
/* open DLT output FIFO */
dlt_user.dlt_log_handle = open(dlt_daemon_fifo, O_WRONLY | O_NONBLOCK | O_CLOEXEC);
if (dlt_user.dlt_log_handle == -1)
/* This is a normal usecase. It is OK that the daemon (and thus the FIFO /tmp/dlt)
* starts later and some DLT users have already been started before.
* Thus it is OK if the FIFO can't be opened. */
dlt_vnlog(LOG_INFO, DLT_USER_BUFFER_LENGTH, "FIFO %s cannot be opened. Retrying later...\n",
dlt_daemon_fifo);
return DLT_RETURN_OK;
}
#endif
DltReturnValue dlt_init(void)
{
/* process is exiting. Do not allocate new resources. */
if (dlt_user_freeing != 0) {
#ifndef DLT_UNIT_TESTS
dlt_vlog(LOG_INFO, "%s logging disabled, process is exiting\n", __func__);
#endif
/* return negative value, to stop the current log */
return DLT_RETURN_LOGGING_DISABLED;
}
/* Compare dlt_user_init_state to INIT_UNITIALIZED. If equal it will be set to INIT_IN_PROGRESS.
* Call returns DLT_RETURN_OK init state != INIT_UNITIALIZED
* That way it's no problem, if two threads enter this function, because only the very first one will
* pass fully. The other one will immediately return, because when it executes the atomic function
* dlt_user_init_state won't be INIT_UNITIALIZED anymore.
* This is not handled via a simple boolean to prevent issues with shutting down while the init is still running.
* Furthermore, this makes sure we enter some function only when dlt_init is fully done.
* */
enum InitState expectedInitState = INIT_UNITIALIZED;
if (!(atomic_compare_exchange_strong(&dlt_user_init_state, &expectedInitState, INIT_IN_PROGRESS))) {
return DLT_RETURN_OK;
}
/* check environment variables */
dlt_check_envvar();
/* Check logging mode and internal log file is opened or not*/
if (logging_mode == DLT_LOG_TO_FILE && logging_handle == NULL) {
dlt_log_init(logging_mode);
}
/* Initialize common part of dlt_init()/dlt_init_file() */
if (dlt_init_common() == DLT_RETURN_ERROR) {
dlt_user_init_state = INIT_ERROR;
dlt_free();
return DLT_RETURN_ERROR;
}
dlt_user.dlt_is_file = 0;
dlt_user.filesize_max = UINT_MAX;
dlt_user_file_reach_max = false;
dlt_user.overflow = 0;
dlt_user.overflow_counter = 0;
#ifdef DLT_SHM_ENABLE
memset(&(dlt_user.dlt_shm), 0, sizeof(DltShm));
/* init shared memory */
if (dlt_shm_init_client(&(dlt_user.dlt_shm), dltShmName) < DLT_RETURN_OK)
dlt_vnlog(LOG_WARNING, DLT_USER_BUFFER_LENGTH, "Logging disabled,"
" Shared memory %s cannot be created!\n", dltShmName);
#endif
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
pthread_rwlock_wrlock(&trace_load_rw_lock);
trace_load_settings = malloc(sizeof(DltTraceLoadSettings));
if (trace_load_settings == NULL) {
dlt_vlog(LOG_ERR, "Failed to allocate memory for trace load settings\n");
dlt_user_init_state = INIT_UNITIALIZED;
return DLT_RETURN_ERROR;
}
memset(trace_load_settings, 0, sizeof(DltTraceLoadSettings));
trace_load_settings[0].soft_limit = DLT_TRACE_LOAD_CLIENT_SOFT_LIMIT_DEFAULT;
trace_load_settings[0].hard_limit = DLT_TRACE_LOAD_CLIENT_HARD_LIMIT_DEFAULT;
strncpy(trace_load_settings[0].apid, dlt_user.appID, DLT_ID_SIZE);
trace_load_settings_count = 1;
pthread_rwlock_unlock(&trace_load_rw_lock);
#endif
#ifdef DLT_LIB_USE_UNIX_SOCKET_IPC
if (dlt_initialize_socket_connection() != DLT_RETURN_OK) {
/* We could connect to the pipe, but not to the socket, which is normally */
/* open before by the DLT daemon => bad failure => return error code */
/* in case application is started before daemon, it is expected behaviour */
dlt_user_init_state = INIT_ERROR;
dlt_free();
return DLT_RETURN_ERROR;
}
#elif defined DLT_LIB_USE_VSOCK_IPC
if (dlt_initialize_vsock_connection() != DLT_RETURN_OK) {
dlt_user_init_state = INIT_ERROR;
dlt_free();
return DLT_RETURN_ERROR;
}
#else /* DLT_LIB_USE_FIFO_IPC */
if (dlt_initialize_fifo_connection() != DLT_RETURN_OK) {
dlt_user_init_state = INIT_ERROR;
dlt_free();
return DLT_RETURN_ERROR;
}
if (dlt_receiver_init(&(dlt_user.receiver),
dlt_user.dlt_user_handle,
DLT_RECEIVE_FD,
DLT_USER_RCVBUF_MAX_SIZE) == DLT_RETURN_ERROR) {
dlt_user_init_state = INIT_ERROR;
dlt_free();
return DLT_RETURN_ERROR;
}
#endif
#ifdef DLT_NETWORK_TRACE_ENABLE
/* These will be lazy initialized only when needed */
dlt_user.dlt_segmented_queue_read_handle = -1;
dlt_user.dlt_segmented_queue_write_handle = -1;
pthread_cond_init(&mq_init_condition, NULL);
#endif
if (dlt_start_threads() < 0) {
dlt_user_init_state = INIT_ERROR;
dlt_free();
return DLT_RETURN_ERROR;
}
/* prepare for fork() call */
pthread_atfork(NULL, NULL, &dlt_fork_child_fork_handler);
dlt_user_init_state = INIT_DONE;
return DLT_RETURN_OK;
}
DltReturnValue dlt_get_appid(char *appid)
{
if (appid != NULL) {
strncpy(appid, dlt_user.appID, 4);
return DLT_RETURN_OK;
} else {
dlt_log(LOG_ERR, "Invalid parameter.\n");
return DLT_RETURN_WRONG_PARAMETER;
}
}
DltReturnValue dlt_init_file(const char *name)
{
/* check null pointer */
if (!name)
return DLT_RETURN_WRONG_PARAMETER;
/* Compare dlt_user_init_state to INIT_UNITIALIZED. If equal it will be set to INIT_IN_PROGRESS.
* Call returns DLT_RETURN_OK init state != INIT_UNITIALIZED
* That way it's no problem, if two threads enter this function, because only the very first one will
* pass fully. The other one will immediately return, because when it executes the atomic function
* dlt_user_init_state won't be INIT_UNITIALIZED anymore.
* This is not handled via a simple boolean to prevent issues with shutting down while the init is still running.
* Furthermore, this makes sure we enter some function only when dlt_init is fully done.
* */
enum InitState expectedInitState = INIT_UNITIALIZED;
if (!(atomic_compare_exchange_strong(&dlt_user_init_state, &expectedInitState, INIT_IN_PROGRESS)))
return DLT_RETURN_OK;
/* Initialize common part of dlt_init()/dlt_init_file() */
if (dlt_init_common() == DLT_RETURN_ERROR) {
expectedInitState = INIT_UNITIALIZED;
return DLT_RETURN_ERROR;
}
dlt_user.dlt_is_file = 1;
/* open DLT output file */
dlt_user.dlt_log_handle = open(name, O_WRONLY | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
if (dlt_user.dlt_log_handle == -1) {
dlt_vnlog(LOG_ERR, DLT_USER_BUFFER_LENGTH, "Log file %s cannot be opened!\n", name);
dlt_user.dlt_is_file = 0;
return DLT_RETURN_ERROR;
}
return DLT_RETURN_OK;
}
DltReturnValue dlt_set_filesize_max(unsigned int filesize)
{
if (dlt_user.dlt_is_file == 0)
{
dlt_vlog(LOG_ERR, "%s: Library is not configured to log to file\n",
__func__);
return DLT_RETURN_ERROR;
}
if (filesize == 0) {
dlt_user.filesize_max = UINT_MAX;
}
else {
dlt_user.filesize_max = filesize;
}
dlt_vlog(LOG_DEBUG, "%s: Defined filesize_max is [%d]\n", __func__,
dlt_user.filesize_max);
return DLT_RETURN_OK;
}
#ifdef DLT_NETWORK_TRACE_ENABLE
DltReturnValue dlt_init_message_queue(void)
{
dlt_lock_mutex(&mq_mutex);
if ((dlt_user.dlt_segmented_queue_read_handle >= 0) &&
(dlt_user.dlt_segmented_queue_write_handle >= 0)) {
/* Already intialized */
dlt_unlock_mutex(&mq_mutex);
return DLT_RETURN_OK;
}
/* Generate per process name for queue */
char queue_name[NAME_MAX];
snprintf(queue_name, NAME_MAX, "%s.%d", DLT_MESSAGE_QUEUE_NAME, getpid());
/* Maximum queue size is 10, limit to size of pointers */
struct mq_attr mqatr;
mqatr.mq_flags = 0;
mqatr.mq_maxmsg = 10;
mqatr.mq_msgsize = sizeof(s_segmented_data *);
mqatr.mq_curmsgs = 0;
/**
* Create the message queue. It must be newly created
* if old one was left by a crashing process.
* */
dlt_user.dlt_segmented_queue_read_handle = mq_open(queue_name, O_CREAT | O_RDONLY | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,
&mqatr);
if (dlt_user.dlt_segmented_queue_read_handle < 0) {
if (errno == EEXIST) {
dlt_log(LOG_WARNING, "Old message queue exists, trying to delete.\n");
if (mq_unlink(queue_name) < 0)
dlt_vnlog(LOG_CRIT, 256, "Could not delete existing message queue!: %s \n", strerror(errno));
else /* Retry */
dlt_user.dlt_segmented_queue_read_handle = mq_open(queue_name,
O_CREAT | O_RDONLY | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,
&mqatr);
}
if (dlt_user.dlt_segmented_queue_read_handle < 0) {
dlt_vnlog(LOG_CRIT, 256, "Can't create message queue read handle!: %s \n", strerror(errno));
dlt_unlock_mutex(&mq_mutex);
return DLT_RETURN_ERROR;
}
}
dlt_user.dlt_segmented_queue_write_handle = mq_open(queue_name, O_WRONLY | O_NONBLOCK);
if (dlt_user.dlt_segmented_queue_write_handle < 0) {
dlt_vnlog(LOG_CRIT, 256, "Can't open message queue write handle!: %s \n", strerror(errno));
dlt_unlock_mutex(&mq_mutex);
return DLT_RETURN_ERROR;
}
pthread_cond_signal(&mq_init_condition);
dlt_unlock_mutex(&mq_mutex);
return DLT_RETURN_OK;
}
#endif /* DLT_NETWORK_TRACE_ENABLE */
/* Return true if verbose mode is to be used for this DltContextData */
static inline bool is_verbose_mode(int8_t dltuser_verbose_mode, const DltContextData* log)
{
return (dltuser_verbose_mode == 1) || (log != NULL && log->verbose_mode);
}
DltReturnValue dlt_init_common(void)
{
char *env_local_print;
char *env_initial_log_level;
char *env_buffer_min;
uint32_t buffer_min = DLT_USER_RINGBUFFER_MIN_SIZE;
char *env_buffer_max;
uint32_t buffer_max = DLT_USER_RINGBUFFER_MAX_SIZE;
char *env_buffer_step;
uint32_t buffer_step = DLT_USER_RINGBUFFER_STEP_SIZE;
char *env_disable_extended_header_for_nonverbose;
char *env_log_buffer_len;
uint32_t buffer_max_configured = 0;
uint32_t header_size = 0;
// already initialized, nothing to do
if (DLT_USER_INITALIZED) {
return DLT_RETURN_OK;
}
/* Binary semaphore for threads */
if ((pthread_attr_init(&dlt_mutex_attr) != 0) ||
(pthread_mutexattr_settype(&dlt_mutex_attr, PTHREAD_MUTEX_ERRORCHECK) != 0) ||
(pthread_mutex_init(&dlt_mutex, &dlt_mutex_attr) != 0)) {
dlt_user_init_state = INIT_UNITIALIZED;
return DLT_RETURN_ERROR;
}
/* set to unknown state of connected client */
dlt_user.log_state = -1;
dlt_user.dlt_log_handle = -1;
dlt_user.dlt_user_handle = DLT_FD_INIT;
dlt_set_id(dlt_user.ecuID, DLT_USER_DEFAULT_ECU_ID);
dlt_set_id(dlt_user.appID, "");
dlt_user.application_description = NULL;
/* Verbose mode is enabled by default */
dlt_user.verbose_mode = 1;
/* header_size is used for resend buffer
* so it won't include DltStorageHeader
*/
header_size = sizeof(DltUserHeader) + sizeof(DltStandardHeader) +
sizeof(DltStandardHeaderExtra);
/* Use extended header for non verbose is enabled by default */
dlt_user.use_extended_header_for_non_verbose =
DLT_USER_USE_EXTENDED_HEADER_FOR_NONVERBOSE;
/* Use extended header for non verbose is modified as per environment variable */
env_disable_extended_header_for_nonverbose =
getenv(DLT_USER_ENV_DISABLE_EXTENDED_HEADER_FOR_NONVERBOSE);
if (env_disable_extended_header_for_nonverbose) {
if (strcmp(env_disable_extended_header_for_nonverbose, "1") == 0)
dlt_user.use_extended_header_for_non_verbose =
DLT_USER_NO_USE_EXTENDED_HEADER_FOR_NONVERBOSE;
}
if (dlt_user.use_extended_header_for_non_verbose ==
DLT_USER_USE_EXTENDED_HEADER_FOR_NONVERBOSE)
header_size += (uint32_t) sizeof(DltExtendedHeader);
/* With session id is enabled by default */
dlt_user.with_session_id = DLT_USER_WITH_SESSION_ID;
/* With timestamp is enabled by default */
dlt_user.with_timestamp = DLT_USER_WITH_TIMESTAMP;
/* With timestamp is enabled by default */
dlt_user.with_ecu_id = DLT_USER_WITH_ECU_ID;
/* Local print is disabled by default */
dlt_user.enable_local_print = 0;
dlt_user.local_print_mode = DLT_PM_UNSET;
dlt_user.timeout_at_exit_handler = DLT_USER_ATEXIT_RESEND_BUFFER_EXIT_TIMEOUT;
env_local_print = getenv(DLT_USER_ENV_LOCAL_PRINT_MODE);
if (env_local_print) {
if (strcmp(env_local_print, "AUTOMATIC") == 0)
dlt_user.local_print_mode = DLT_PM_AUTOMATIC;
else if (strcmp(env_local_print, "FORCE_ON") == 0)
dlt_user.local_print_mode = DLT_PM_FORCE_ON;
else if (strcmp(env_local_print, "FORCE_OFF") == 0)
dlt_user.local_print_mode = DLT_PM_FORCE_OFF;
}
env_initial_log_level = getenv("DLT_INITIAL_LOG_LEVEL");
if (env_initial_log_level != NULL) {
if (dlt_env_extract_ll_set(&env_initial_log_level, &dlt_user.initial_ll_set) != 0)
dlt_vlog(LOG_WARNING,
"Unable to parse initial set of log-levels from environment! Env:\n%s\n",
getenv("DLT_INITIAL_LOG_LEVEL"));
}
/* Initialize LogLevel/TraceStatus field */
DLT_SEM_LOCK();
dlt_user.dlt_ll_ts = NULL;
dlt_user.dlt_ll_ts_max_num_entries = 0;
dlt_user.dlt_ll_ts_num_entries = 0;
env_buffer_min = getenv(DLT_USER_ENV_BUFFER_MIN_SIZE);
env_buffer_max = getenv(DLT_USER_ENV_BUFFER_MAX_SIZE);
env_buffer_step = getenv(DLT_USER_ENV_BUFFER_STEP_SIZE);
if (env_buffer_min != NULL) {
buffer_min = (uint32_t)strtol(env_buffer_min, NULL, 10);
if ((errno == EINVAL) || (errno == ERANGE)) {
dlt_vlog(LOG_ERR,
"Wrong value specified for %s. Using default\n",
DLT_USER_ENV_BUFFER_MIN_SIZE);
buffer_min = DLT_USER_RINGBUFFER_MIN_SIZE;
}
}
if (env_buffer_max != NULL) {
buffer_max = (uint32_t)strtol(env_buffer_max, NULL, 10);
if ((errno == EINVAL) || (errno == ERANGE)) {
dlt_vlog(LOG_ERR,
"Wrong value specified for %s. Using default\n",
DLT_USER_ENV_BUFFER_MAX_SIZE);
buffer_max = DLT_USER_RINGBUFFER_MAX_SIZE;
}
}
if (env_buffer_step != NULL) {
buffer_step = (uint32_t)strtol(env_buffer_step, NULL, 10);
if ((errno == EINVAL) || (errno == ERANGE)) {
dlt_vlog(LOG_ERR,
"Wrong value specified for %s. Using default\n",
DLT_USER_ENV_BUFFER_STEP_SIZE);
buffer_step = DLT_USER_RINGBUFFER_STEP_SIZE;
}
}
/* init log buffer size */
dlt_user.log_buf_len = DLT_USER_BUF_MAX_SIZE;
env_log_buffer_len = getenv(DLT_USER_ENV_LOG_MSG_BUF_LEN);
if (env_log_buffer_len != NULL) {
buffer_max_configured = (uint32_t)strtol(env_log_buffer_len, NULL, 10);
if (buffer_max_configured > DLT_LOG_MSG_BUF_MAX_SIZE) {
dlt_user.log_buf_len = DLT_LOG_MSG_BUF_MAX_SIZE;
dlt_vlog(LOG_WARNING,
"Configured size exceeds maximum allowed size,restricting to max [65535 bytes]\n");
}
else {
dlt_user.log_buf_len = (uint16_t) buffer_max_configured;
dlt_vlog(LOG_INFO,
"Configured buffer size to [%u bytes]\n",
buffer_max_configured);
}
}
if (dlt_user.resend_buffer == NULL) {
dlt_user.resend_buffer = calloc(sizeof(unsigned char),
(dlt_user.log_buf_len + header_size));
if (dlt_user.resend_buffer == NULL) {
dlt_user_init_state = INIT_UNITIALIZED;
DLT_SEM_FREE();
dlt_vlog(LOG_ERR, "cannot allocate memory for resend buffer\n");
return DLT_RETURN_ERROR;
}
}
dlt_user.disable_injection_msg = 0;
if (getenv(DLT_USER_ENV_DISABLE_INJECTION_MSG)) {
dlt_log(LOG_WARNING, "Injection message is disabled\n");
dlt_user.disable_injection_msg = 1;
}
if (dlt_buffer_init_dynamic(&(dlt_user.startup_buffer),
buffer_min,
buffer_max,
buffer_step) == DLT_RETURN_ERROR) {
dlt_user_init_state = INIT_UNITIALIZED;
DLT_SEM_FREE();
return DLT_RETURN_ERROR;
}
DLT_SEM_FREE();
signal(SIGPIPE, SIG_IGN); /* ignore pipe signals */
if (atexit_registered == 0) {
atexit_registered = 1;
atexit(dlt_user_atexit_handler);
}
#ifdef DLT_TEST_ENABLE
dlt_user.corrupt_user_header = 0;
dlt_user.corrupt_message_size = 0;
dlt_user.corrupt_message_size_size = 0;
#endif
return DLT_RETURN_OK;
}
void dlt_user_atexit_handler(void)
{
/* parent will do clean-up */
if (g_dlt_is_child)
return;
if (!DLT_USER_INITALIZED) {
dlt_vlog(LOG_WARNING, "%s dlt_user_init_state=%i (expected INIT_DONE), dlt_user_freeing=%i\n", __FUNCTION__, dlt_user_init_state, dlt_user_freeing);
/* close file */
dlt_log_free();
return;
}
/* Try to resend potential log messages in the user buffer */
int count = dlt_user_atexit_blow_out_user_buffer();
if (count != 0)
dlt_vnlog(LOG_WARNING, 128, "Lost log messages in user buffer when exiting: %i\n", count);
/* Unregister app (this also unregisters all contexts in daemon) */
/* Ignore return value */
dlt_unregister_app_util(false);
/* Cleanup */
/* Ignore return value */
dlt_free();
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
pthread_rwlock_destroy(&trace_load_rw_lock);
#endif
}
int dlt_user_atexit_blow_out_user_buffer(void)
{
int count, ret;
struct timespec ts;
uint32_t exitTime = dlt_uptime() + dlt_user.timeout_at_exit_handler;
/* Send content of ringbuffer */
DLT_SEM_LOCK();
count = dlt_buffer_get_message_count(&(dlt_user.startup_buffer));
DLT_SEM_FREE();
if (count > 0) {
while (dlt_uptime() < exitTime) {