summaryrefslogtreecommitdiff
path: root/src/system/dlt-system-syslog.c
blob: fc597747c42df798230865fa15b0c08b5063ed30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2011-2015, BMW AG
 *
 * This file is part of GENIVI 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.genivi.org/.
 */

/*!
 * \author Lassi Marttala <lassi.lm.marttala@partner.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-system-syslog.c
 */

/*******************************************************************************
**                                                                            **
**  SRC-MODULE: dlt-system-syslog.c                                                  **
**                                                                            **
**  TARGET    : linux                                                         **
**                                                                            **
**  PROJECT   : DLT                                                           **
**                                                                            **
**  AUTHOR    : Lassi Marttala <lassi.lm.marttala@partner.bmw.de>             **
**              Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
**                                                                            **
**  PURPOSE   :                                                               **
**                                                                            **
**  REMARKS   :                                                               **
**                                                                            **
**  PLATFORM DEPENDANT [yes/no]: yes                                          **
**                                                                            **
**  TO BE CHANGED BY USER [yes/no]: no                                        **
**                                                                            **
*******************************************************************************/


#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>

#include "dlt-system.h"

extern DltSystemThreads threads;

DLT_IMPORT_CONTEXT(dltsystem)
DLT_DECLARE_CONTEXT(syslogContext)
#define RECV_BUF_SZ 1024

int init_socket(SyslogOptions opts)
{
    DLT_LOG(dltsystem, DLT_LOG_DEBUG,
            DLT_STRING("dlt-system-syslog, init socket, port: "),
            DLT_INT(opts.Port));

    int sock = -1;
    struct sockaddr_in syslog_addr;

#ifdef DLT_USE_IPv6
    sock = socket(AF_INET6, SOCK_DGRAM, 0);
#else
    sock = socket(AF_INET, SOCK_DGRAM, 0);
#endif

    if (sock < 0) {
        DLT_LOG(syslogContext, DLT_LOG_FATAL,
                DLT_STRING("Unable to create socket for SYSLOG."));
        return -1;
    }

#ifdef DLT_USE_IPv6
    syslog_addr.sin_family = AF_INET6;
#else
    syslog_addr.sin_family = AF_INET;
#endif
    syslog_addr.sin_port = htons(opts.Port);
    syslog_addr.sin_addr.s_addr = INADDR_ANY;
    memset(&(syslog_addr.sin_zero), 0, 8);

    if (bind(sock, (struct sockaddr *)&syslog_addr,
             sizeof(struct sockaddr)) == -1) {
        DLT_LOG(syslogContext, DLT_LOG_FATAL,
                DLT_STRING("Unable to bind socket for SYSLOG."));
        close(sock);
        return -1;
    }

    return sock;
}

int read_socket(int sock)
{
    DLT_LOG(dltsystem, DLT_LOG_DEBUG,
            DLT_STRING("dlt-system-syslog, read socket"));
    char recv_data[RECV_BUF_SZ];
    struct sockaddr_in client_addr;
    socklen_t addr_len = sizeof(struct sockaddr_in);

    int bytes_read = recvfrom(sock, recv_data, RECV_BUF_SZ, 0,
                              (struct sockaddr *)&client_addr, &addr_len);

    if (bytes_read == -1) {
        if (errno == EINTR) {
            return 0;
        }
        else {
            DLT_LOG(syslogContext, DLT_LOG_FATAL,
                    DLT_STRING("Read from socket failed in SYSLOG."));
            return -1;
        }
    }

    recv_data[bytes_read] = '\0';

    if (bytes_read != 0)
        DLT_LOG(syslogContext, DLT_LOG_INFO, DLT_STRING(recv_data));

    return bytes_read;
}

void syslog_thread(void *v_conf)
{
    DLT_LOG(dltsystem, DLT_LOG_DEBUG,
            DLT_STRING("dlt-system-syslog, in thread."));

    DltSystemConfiguration *conf = (DltSystemConfiguration *)v_conf;
    DLT_REGISTER_CONTEXT(syslogContext, conf->Syslog.ContextId, "SYSLOG Adapter");

    int sock = init_socket(conf->Syslog);

    if (sock < 0)
        return;

    while (!threads.shutdown)
        if (read_socket(sock) < 0) {
            close(sock);
            return;
        }

    close (sock);
}

void start_syslog(DltSystemConfiguration *conf)
{
    DLT_LOG(dltsystem, DLT_LOG_DEBUG,
            DLT_STRING("dlt-system-syslog, start syslog"));
    static pthread_attr_t t_attr;
    static pthread_t pt;
    pthread_create(&pt, &t_attr, (void *)syslog_thread, conf);
    threads.threads[threads.count++] = pt;
}