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
|
/*
* Copyright (c) 2002-2013 Balabit
* Copyright (c) 1998-2013 Balázs Scheidler
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "afstreams.h"
#include "messages.h"
#include "logreader.h"
#include "fdhelpers.h"
#include "apphook.h"
#include "stats/stats-registry.h"
#include "poll-fd-events.h"
#include "logproto/logproto-dgram-server.h"
#include "stats/stats-cluster-key-builder.h"
typedef struct _AFStreamsSourceDriver
{
LogSrcDriver super;
GString *dev_filename;
GString *door_filename;
gint door_fd;
LogReader *reader;
LogReaderOptions reader_options;
} AFStreamsSourceDriver;
#include <sys/types.h>
#include <sys/stat.h>
#include <stropts.h>
#include <sys/strlog.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#if SYSLOG_NG_HAVE_DOOR_H
#include <door.h>
#endif
static gssize
log_transport_streams_read(LogTransport *self, void *buf, gsize buflen, GSockAddr **sa)
{
struct strbuf ctl, data;
struct log_ctl lc;
gint flags;
gint res;
gchar tmpbuf[buflen];
gint len;
ctl.maxlen = ctl.len = sizeof(lc);
ctl.buf = (char *) &lc;
data.maxlen = buflen;
data.len = 0;
data.buf = tmpbuf;
flags = 0;
res = getmsg(self->fd, &ctl, &data, &flags);
if (res == -1)
return -1;
else if ((res & (MORECTL+MOREDATA)) == 0)
{
len = g_snprintf(buf, buflen, "<%d>%.*s", lc.pri, data.len, data.buf);
return MIN(len, buflen);
}
else
{
msg_error("Insufficient buffer space for retrieving STREAMS log message",
evt_tag_printf("res", "%x", res));
}
return 0;
}
LogTransport *
log_transport_streams_new(gint fd)
{
LogTransport *self = g_new0(LogTransport, 1);
log_transport_init_instance(self, fd);
self->cond = G_IO_IN;
self->read = log_transport_streams_read;
self->free_fn = log_transport_free_method;
return self;
}
void
afstreams_sd_set_sundoor(LogDriver *s, gchar *filename)
{
AFStreamsSourceDriver *self = (AFStreamsSourceDriver *) s;
self->door_filename = g_string_new(filename);
}
static void
afstreams_sd_door_server_proc(void *cookie, char *argp, size_t arg_size, door_desc_t *dp, uint_t n_desc)
{
door_return(NULL, 0, NULL, 0);
return;
}
static void
afstreams_init_door(int hook_type G_GNUC_UNUSED, gpointer user_data)
{
AFStreamsSourceDriver *self = (AFStreamsSourceDriver *) user_data;
struct stat st;
gint fd;
if (stat(self->door_filename->str, &st) == -1)
{
/* file does not exist, create it */
fd = creat(self->door_filename->str, 0666);
if (fd == -1)
{
msg_error("Error creating syslog door file",
evt_tag_str(EVT_TAG_FILENAME, self->door_filename->str),
evt_tag_error(EVT_TAG_OSERROR));
close(fd);
return;
}
}
fdetach(self->door_filename->str);
self->door_fd = door_create(afstreams_sd_door_server_proc, NULL, 0);
if (self->door_fd == -1)
{
msg_error("Error creating syslog door",
evt_tag_str(EVT_TAG_FILENAME, self->door_filename->str),
evt_tag_error(EVT_TAG_OSERROR));
return;
}
g_fd_set_cloexec(self->door_fd, TRUE);
if (fattach(self->door_fd, self->door_filename->str) == -1)
{
msg_error("Error attaching syslog door",
evt_tag_str(EVT_TAG_FILENAME, self->door_filename->str),
evt_tag_error(EVT_TAG_OSERROR));
close(self->door_fd);
self->door_fd = -1;
return;
}
}
static void
afstreams_sd_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options)
{
log_msg_set_value_to_string(msg, LM_V_TRANSPORT, "local+sunstreams");
log_src_driver_queue_method(s, msg, path_options);
}
static gboolean
afstreams_sd_init(LogPipe *s)
{
AFStreamsSourceDriver *self = (AFStreamsSourceDriver *) s;
GlobalConfig *cfg = log_pipe_get_config(s);
gint fd;
if (!log_src_driver_init_method(s))
return FALSE;
log_reader_options_init(&self->reader_options, cfg, self->super.super.group);
fd = open(self->dev_filename->str, O_RDONLY | O_NOCTTY | O_NONBLOCK);
if (fd != -1)
{
struct strioctl ioc;
g_fd_set_cloexec(fd, TRUE);
memset(&ioc, 0, sizeof(ioc));
ioc.ic_cmd = I_CONSLOG;
if (ioctl(fd, I_STR, &ioc) < 0)
{
msg_error("Error in ioctl(I_STR, I_CONSLOG)",
evt_tag_str(EVT_TAG_FILENAME, self->dev_filename->str),
evt_tag_error(EVT_TAG_OSERROR));
close(fd);
return FALSE;
}
g_fd_set_nonblock(fd, TRUE);
self->reader = log_reader_new(cfg);
log_pipe_set_options(&self->reader->super.super, &self->super.super.super.options);
log_reader_open(self->reader, log_proto_dgram_server_new(log_transport_streams_new(fd),
&self->reader_options.proto_options.super), poll_fd_events_new(fd));
StatsClusterKeyBuilder *kb = stats_cluster_key_builder_new();
stats_cluster_key_builder_add_label(kb, stats_cluster_label("driver", "sun-streams"));
stats_cluster_key_builder_add_legacy_label(kb, stats_cluster_label("filename", self->dev_filename->str));
log_reader_set_options(self->reader,
s,
&self->reader_options,
self->super.super.id,
kb);
log_pipe_append((LogPipe *) self->reader, s);
if (self->door_filename)
{
/* door creation is deferred, because it creates threads which is
* not inherited through forks, and syslog-ng forks during
* startup, but _after_ the configuration was initialized */
register_application_hook(AH_POST_DAEMONIZED, afstreams_init_door, self, AHM_RUN_ONCE);
}
if (!log_pipe_init((LogPipe *) self->reader))
{
msg_error("Error initializing log_reader, closing fd",
evt_tag_int("fd", fd));
log_pipe_unref((LogPipe *) self->reader);
self->reader = NULL;
close(fd);
return FALSE;
}
}
else
{
msg_error("Error opening syslog device",
evt_tag_str(EVT_TAG_FILENAME, self->dev_filename->str),
evt_tag_error(EVT_TAG_OSERROR));
return FALSE;
}
return TRUE;
}
static gboolean
afstreams_sd_deinit(LogPipe *s)
{
AFStreamsSourceDriver *self = (AFStreamsSourceDriver *) s;
if (self->reader)
{
log_pipe_deinit((LogPipe *) self->reader);
log_pipe_unref((LogPipe *) self->reader);
self->reader = NULL;
}
if (self->door_fd != -1)
{
door_revoke(self->door_fd);
close(self->door_fd);
}
if (!log_src_driver_deinit_method(s))
return FALSE;
return TRUE;
}
static void
afstreams_sd_free(LogPipe *s)
{
AFStreamsSourceDriver *self = (AFStreamsSourceDriver *) s;
log_reader_options_destroy(&self->reader_options);
if (self->dev_filename)
g_string_free(self->dev_filename, TRUE);
if (self->door_filename)
g_string_free(self->door_filename, TRUE);
log_src_driver_free(s);
}
LogDriver *
afstreams_sd_new(gchar *filename, GlobalConfig *cfg)
{
AFStreamsSourceDriver *self = g_new0(AFStreamsSourceDriver, 1);
log_src_driver_init_instance(&self->super, cfg);
self->dev_filename = g_string_new(filename);
self->super.super.super.init = afstreams_sd_init;
self->super.super.super.deinit = afstreams_sd_deinit;
self->super.super.super.free_fn = afstreams_sd_free;
self->super.super.super.queue = afstreams_sd_queue;
log_reader_options_defaults(&self->reader_options);
self->reader_options.parse_options.flags |= LP_LOCAL;
self->reader_options.parse_options.flags &= ~LP_EXPECT_HOSTNAME;
self->reader_options.super.stats_level = STATS_LEVEL1;
self->reader_options.super.stats_source = stats_register_type("sun-streams");
return &self->super.super;
}
|