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
|
/*
* 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 "pseudofile.h"
#include "messages.h"
#include "scratch-buffers.h"
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
typedef struct _PseudoFileDestDriver
{
LogDestDriver super;
LogTemplateOptions template_options;
LogTemplate *template;
gchar *pseudofile_name;
time_t suspend_until;
time_t time_reopen;
} PseudoFileDestDriver;
/*
* Locking
* =======
*
* The pseudofile driver currently has a single, global lock that protects
* writing the data into the procfile.
*
* We could use a per-destination lock, however in that case nothing would
* prevent races when two pseudofile drivers open the same proc file.
*
* Also this driver shouldn't be that performance intensive (e.g. pushing
* data to a procfile more than a dozen times per second is really sick),
* I've just measured pseudofile to be able to do 25-26k/sec easily in my
* debug build.
*/
G_LOCK_DEFINE_STATIC(pseudofile_lock);
LogTemplateOptions *
pseudofile_dd_get_template_options(LogDriver *s)
{
PseudoFileDestDriver *self = (PseudoFileDestDriver *) s;
return &self->template_options;
}
void
pseudofile_dd_set_template(LogDriver *s, LogTemplate *template)
{
PseudoFileDestDriver *self = (PseudoFileDestDriver *) s;
log_template_unref(self->template);
self->template = template;
}
void
pseudofile_dd_set_time_reopen(LogDriver *s, time_t time_reopen)
{
PseudoFileDestDriver *self = (PseudoFileDestDriver *) s;
self->time_reopen = time_reopen;
}
static void
_format_message(PseudoFileDestDriver *self, LogMessage *msg, GString *formatted_message)
{
LogTemplateEvalOptions options = {&self->template_options, LTZ_LOCAL, 0, NULL, LM_VT_STRING};
log_template_format(self->template, msg, &options, formatted_message);
}
static EVTTAG *
_evt_tag_message(const GString *msg)
{
const int max_len = 30;
return evt_tag_printf("message", "%.*s%s",
(int) MIN(max_len, msg->len), msg->str,
msg->len > max_len ? "..." : "");
}
static gboolean
_write_message(PseudoFileDestDriver *self, const GString *msg)
{
int fd;
gboolean success = FALSE;
gint rc;
msg_debug("Posting message to pseudo file",
evt_tag_str("pseudofile", self->pseudofile_name),
evt_tag_str("driver", self->super.super.id),
_evt_tag_message(msg));
fd = open(self->pseudofile_name, O_NOCTTY | O_WRONLY | O_NONBLOCK);
if (fd < 0)
{
msg_error("Error opening pseudo file",
evt_tag_str("pseudofile", self->pseudofile_name),
evt_tag_str("driver", self->super.super.id),
evt_tag_error("error"),
_evt_tag_message(msg));
goto exit;
}
rc = write(fd, msg->str, msg->len);
if (rc < 0)
{
msg_error("Error writing to pseudo file",
evt_tag_str("pseudofile", self->pseudofile_name),
evt_tag_str("driver", self->super.super.id),
evt_tag_error("error"),
_evt_tag_message(msg));
goto exit;
}
else if (rc != msg->len)
{
msg_error("Partial write to pseudofile, probably the output is too much for the kernel to consume",
evt_tag_str("pseudofile", self->pseudofile_name),
evt_tag_str("driver", self->super.super.id),
_evt_tag_message(msg));
goto exit;
}
success = TRUE;
exit:
if (fd >= 0)
close(fd);
return success;
}
static gboolean
_is_output_suspended(PseudoFileDestDriver *self, time_t now)
{
if (self->suspend_until && self->suspend_until > now)
return TRUE;
return FALSE;
}
static void
_suspend_output(PseudoFileDestDriver *self, time_t now)
{
self->suspend_until = now + self->time_reopen;
}
static void
pseudofile_dd_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options)
{
PseudoFileDestDriver *self = (PseudoFileDestDriver *) s;
GString *formatted_message = scratch_buffers_alloc();
gboolean success;
time_t now = msg->timestamps[LM_TS_RECVD].ut_sec;
if (_is_output_suspended(self, now))
goto finish;
_format_message(self, msg, formatted_message);
G_LOCK(pseudofile_lock);
success = _write_message(self, formatted_message);
G_UNLOCK(pseudofile_lock);
if (!success)
_suspend_output(self, now);
finish:
log_dest_driver_queue_method(s, msg, path_options);
}
static gboolean
pseudofile_dd_init(LogPipe *s)
{
PseudoFileDestDriver *self = (PseudoFileDestDriver *) s;
GlobalConfig *cfg = log_pipe_get_config(s);
log_template_options_init(&self->template_options, cfg);
if (self->time_reopen == -1)
self->time_reopen = cfg->time_reopen;
if (!self->template)
{
msg_error("The template() option for pseudofile() is mandatory", log_pipe_location_tag(s));
return FALSE;
}
return log_dest_driver_init_method(s);
}
static void
pseudofile_dd_free(LogPipe *s)
{
PseudoFileDestDriver *self = (PseudoFileDestDriver *) s;
log_template_options_destroy(&self->template_options);
g_free(self->pseudofile_name);
log_template_unref(self->template);
log_dest_driver_free(s);
}
LogDriver *
pseudofile_dd_new(gchar *pseudofile_name, GlobalConfig *cfg)
{
PseudoFileDestDriver *self = g_new0(PseudoFileDestDriver, 1);
log_dest_driver_init_instance(&self->super, cfg);
log_template_options_defaults(&self->template_options);
self->super.super.super.init = pseudofile_dd_init;
self->super.super.super.queue = pseudofile_dd_queue;
self->super.super.super.free_fn = pseudofile_dd_free;
self->pseudofile_name = g_strdup(pseudofile_name);
self->time_reopen = -1;
return &self->super.super;
}
|