automotive-dlt
dlt_daemon_unix_socket.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2015, Advanced Driver Information Technology
6  * Copyright of Advanced Driver Information Technology, Bosch and Denso
7  *
8  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
9  *
10  * This Source Code Form is subject to the terms of the
11  * Mozilla Public License (MPL), v. 2.0.
12  * If a copy of the MPL was not distributed with this file,
13  * You can obtain one at http://mozilla.org/MPL/2.0/.
14  *
15  * For further information see http://www.genivi.org/.
16  * @licence end@
17  */
18 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/un.h>
32 #include <sys/socket.h>
33 #include <syslog.h>
34 #include <errno.h>
35 #include "dlt-daemon.h"
36 #include "dlt_common.h"
37 #include "dlt-daemon_cfg.h"
38 #include "dlt_daemon_socket.h"
39 #include "dlt_daemon_unix_socket.h"
40 
42 
43 int dlt_daemon_unix_socket_open(int *sock, char *sock_path)
44 {
45  struct sockaddr_un addr;
46 
47  if (sock == NULL || sock_path == NULL)
48  {
49  dlt_log(LOG_ERR, "dlt_daemon_unix_socket_open: arguments invalid");
50  return -1;
51  }
52 
53  if ((*sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
54  {
55  dlt_log(LOG_WARNING, "unix socket: socket() error");
56  return -1;
57  }
58 
59  memset(&addr, 0, sizeof(addr));
60  addr.sun_family = AF_UNIX;
61  memcpy(addr.sun_path, sock_path, sizeof(addr.sun_path));
62 
63  unlink(sock_path);
64 
65  if (bind(*sock, (struct sockaddr *) &addr, sizeof(addr)) == -1)
66  {
67  dlt_log(LOG_WARNING, "unix socket: bind() error");
68  return -1;
69  }
70 
71  if (listen(*sock, 1) == -1)
72  {
73  dlt_log(LOG_WARNING, "unix socket: listen error");
74  return -1;
75  }
76 
77  return 0;
78 }
79 
81 {
82  int ret = close(sock);
83 
84  if (ret != 0)
85  {
86  sprintf(err_string, "unix socket close failed: %s", strerror(errno));
87  dlt_log(LOG_WARNING, err_string);
88  }
89 
90  return ret;
91 }
92 
94  int sock,
95  void *data1,
96  int size1,
97  void *data2,
98  int size2,
99  char serialheader)
100 {
101  /* re-use socket send function */
102  return dlt_daemon_socket_send(
103  sock,
104  data1,
105  size1,
106  data2,
107  size2,
108  serialheader);
109 }
DltReturnValue dlt_log(int prio, char *s)
Definition: dlt_common.c:2029
int dlt_daemon_unix_socket_send(int sock, void *data1, int size1, void *data2, int size2, char serialheader)
char err_string[DLT_DAEMON_TEXTBUFSIZE]
int dlt_daemon_unix_socket_close(int sock)
int dlt_daemon_socket_send(int sock, void *data1, int size1, void *data2, int size2, char serialheader)
int dlt_daemon_unix_socket_open(int *sock, char *sock_path)
#define DLT_DAEMON_TEXTBUFSIZE
#define NULL
Definition: dlt_common.h:232