2
* Copyright (c) 2002-2010 BalaBit IT Ltd, Budapest, Hungary
3
* Copyright (c) 1998-2010 Balázs Scheidler
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* As an additional exemption you are allowed to compile & link against the
20
* OpenSSL libraries as published by the OpenSSL project. See the file
21
* COPYING for details.
25
#include "logtransport.h"
33
log_transport_free_method(LogTransport *s)
35
if (((s->flags & LTF_DONTCLOSE) == 0) && s->fd != -1)
37
msg_verbose("Closing log transport fd",
38
evt_tag_int("fd", s->fd),
40
if (s->flags & LTF_SHUTDOWN)
41
shutdown(s->fd, SHUT_RDWR);
47
log_transport_free(LogTransport *self)
53
/* log transport that simply sends messages to an fd */
54
typedef struct _LogTransportPlain LogTransportPlain;
56
struct _LogTransportPlain
62
log_transport_plain_read_method(LogTransport *s, gpointer buf, gsize buflen, GSockAddr **sa)
64
LogTransportPlain *self = (LogTransportPlain *) s;
67
if ((self->super.flags & LTF_RECV) == 0)
74
if (self->super.timeout)
75
alarm_set(self->super.timeout);
76
rc = read(self->super.fd, buf, buflen);
78
if (self->super.timeout > 0 && rc == -1 && errno == EINTR && alarm_has_fired())
80
msg_notice("Nonblocking read has blocked, returning with an error",
81
evt_tag_int("fd", self->super.fd),
82
evt_tag_int("timeout", self->super.timeout),
87
if (self->super.timeout)
90
while (rc == -1 && errno == EINTR);
96
#if HAVE_STRUCT_SOCKADDR_STORAGE
97
struct sockaddr_storage __sas;
102
socklen_t salen = sizeof(sas);
106
rc = recvfrom(self->super.fd, buf, buflen, 0,
107
(struct sockaddr *) &sas, &salen);
109
while (rc == -1 && errno == EINTR);
110
if (rc != -1 && salen && sa)
111
(*sa) = g_sockaddr_new((struct sockaddr *) &sas, salen);
117
log_transport_plain_write_method(LogTransport *s, const gpointer buf, gsize buflen)
119
LogTransportPlain *self = (LogTransportPlain *) s;
124
if (self->super.timeout)
125
alarm_set(self->super.timeout);
126
if (self->super.flags & LTF_APPEND)
127
lseek(self->super.fd, 0, SEEK_END);
128
rc = write(self->super.fd, buf, buflen);
129
if (self->super.timeout > 0 && rc == -1 && errno == EINTR && alarm_has_fired())
131
msg_notice("Nonblocking write has blocked, returning with an error",
132
evt_tag_int("fd", self->super.fd),
133
evt_tag_int("timeout", self->super.timeout),
138
if (self->super.timeout)
140
if (self->super.flags & LTF_FSYNC)
141
fsync(self->super.fd);
143
while (rc == -1 && errno == EINTR);
149
log_transport_plain_new(gint fd, guint flags)
151
LogTransportPlain *self = g_new0(LogTransportPlain, 1);
154
self->super.cond = 0;
155
self->super.flags = flags;
156
self->super.read = log_transport_plain_read_method;
157
self->super.write = log_transport_plain_write_method;
158
self->super.free_fn = log_transport_free_method;