automotive-dlt
dlt-system-syslog.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
27 /*******************************************************************************
28 ** **
29 ** SRC-MODULE: dlt-system-syslog.c **
30 ** **
31 ** TARGET : linux **
32 ** **
33 ** PROJECT : DLT **
34 ** **
35 ** AUTHOR : Lassi Marttala <lassi.lm.marttala@partner.bmw.de> **
36 ** Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
37 ** **
38 ** PURPOSE : **
39 ** **
40 ** REMARKS : **
41 ** **
42 ** PLATFORM DEPENDANT [yes/no]: yes **
43 ** **
44 ** TO BE CHANGED BY USER [yes/no]: no **
45 ** **
46 *******************************************************************************/
47 
48 
49 #include <pthread.h>
50 #include <unistd.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <strings.h>
54 #include <errno.h>
55 
56 #include "dlt-system.h"
57 
59 
61 DLT_DECLARE_CONTEXT(syslogContext)
62 #define RECV_BUF_SZ 1024
63 
64 int init_socket(SyslogOptions opts)
65 {
66  DLT_LOG(dltsystem,DLT_LOG_DEBUG,
67  DLT_STRING("dlt-system-syslog, init socket, port: "),
68  DLT_INT(opts.Port));
69 
70  int sock = -1;
71  struct sockaddr_in syslog_addr;
72 
73 #ifdef DLT_USE_IPv6
74  sock = socket(AF_INET6, SOCK_DGRAM, 0);
75 #else
76  sock = socket(AF_INET, SOCK_DGRAM, 0);
77 #endif
78  if(sock < 0)
79  {
80  DLT_LOG(syslogContext, DLT_LOG_FATAL,
81  DLT_STRING("Unable to create socket for SYSLOG."));
82  return -1;
83  }
84 
85 #ifdef DLT_USE_IPv6
86  syslog_addr.sin_family = AF_INET6;
87 #else
88  syslog_addr.sin_family = AF_INET;
89 #endif
90  syslog_addr.sin_port = htons(opts.Port);
91  syslog_addr.sin_addr.s_addr = INADDR_ANY;
92  bzero(&(syslog_addr.sin_zero), 8);
93 
94  if (bind(sock, (struct sockaddr *)&syslog_addr,
95  sizeof(struct sockaddr)) == -1)
96  {
97  DLT_LOG(syslogContext, DLT_LOG_FATAL,
98  DLT_STRING("Unable to bind socket for SYSLOG."));
99  close(sock);
100  return -1;
101  }
102 
103  return sock;
104 }
105 
106 int read_socket(int sock)
107 {
108  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
109  DLT_STRING("dlt-system-syslog, read socket"));
110  char recv_data[RECV_BUF_SZ];
111  struct sockaddr_in client_addr;
112  socklen_t addr_len = sizeof(struct sockaddr_in);
113 
114  int bytes_read = recvfrom(sock, recv_data, RECV_BUF_SZ, 0,
115  (struct sockaddr *) &client_addr, &addr_len);
116  if(bytes_read == -1)
117  {
118  if(errno == EINTR)
119  {
120  return 0;
121  }
122  else
123  {
124  DLT_LOG(syslogContext, DLT_LOG_FATAL,
125  DLT_STRING("Read from socket failed in SYSLOG."));
126  return -1;
127  }
128  }
129 
130  recv_data[bytes_read] = '\0';
131 
132  if(bytes_read != 0)
133  {
134  DLT_LOG(syslogContext, DLT_LOG_INFO, DLT_STRING(recv_data));
135  }
136  return bytes_read;
137 }
138 
139 void syslog_thread(void *v_conf)
140 {
141  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
142  DLT_STRING("dlt-system-syslog, in thread."));
143 
145  DLT_REGISTER_CONTEXT(syslogContext, conf->Syslog.ContextId, "SYSLOG Adapter");
146 
147  int sock = init_socket(conf->Syslog);
148  if(sock < 0)
149  return;
150 
151  while(!threads.shutdown)
152  {
153  if(read_socket(sock) < 0)
154  {
155  close(sock);
156  return;
157  }
158  }
159  close (sock);
160 }
161 
163 {
164  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
165  DLT_STRING("dlt-system-syslog, start syslog"));
166  static pthread_attr_t t_attr;
167  static pthread_t pt;
168  pthread_create(&pt, &t_attr, (void *)syslog_thread, conf);
169  threads.threads[threads.count++] = pt;
170 }
SyslogOptions Syslog
Definition: dlt-system.h:155
#define DLT_INT(INT_VAR)
DLT_IMPORT_CONTEXT(dltsystem)
DltSystemThreads threads
int read_socket(int sock)
#define DLT_DECLARE_CONTEXT(CONTEXT)
#define DLT_STRING(TEXT)
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
char * ContextId
Definition: dlt-system.h:101
#define RECV_BUF_SZ
void start_syslog(DltSystemConfiguration *conf)
void syslog_thread(void *v_conf)
pthread_t threads[MAX_THREADS]
Definition: dlt-system.h:163