iostream-rawlog.c revision 949b368882333eb707399386526e47436ee2c390
163N/A/* Copyright (c) 2011-2012 Dovecot authors, see the included COPYING file */
163N/A
163N/A#include "lib.h"
163N/A#include "hostpid.h"
163N/A#include "ioloop.h"
163N/A#include "buffer.h"
163N/A#include "str.h"
163N/A#include "write-full.h"
163N/A#include "time-util.h"
163N/A#include "istream.h"
163N/A#include "ostream.h"
163N/A#include "iostream-private.h"
163N/A#include "iostream-rawlog-private.h"
163N/A#include "istream-rawlog.h"
163N/A#include "ostream-rawlog.h"
163N/A#include "iostream-rawlog.h"
163N/A
163N/A#include <unistd.h>
163N/A#include <fcntl.h>
163N/A
163N/A#define RAWLOG_MAX_LINE_LEN 8192
163N/A
163N/Astatic int
163N/Arawlog_write(struct rawlog_iostream *rstream, const void *data, size_t size)
163N/A{
163N/A if (rstream->rawlog_fd == -1)
163N/A return -1;
163N/A
163N/A if (write_full(rstream->rawlog_fd, data, size) < 0) {
163N/A i_error("rawlog_istream.write(%s) failed: %m",
163N/A rstream->rawlog_path);
163N/A iostream_rawlog_close(rstream);
163N/A return -1;
163N/A }
163N/A return 0;
163N/A}
163N/A
163N/Astatic int
163N/Arawlog_write_timestamp(struct rawlog_iostream *rstream, bool line_ends)
163N/A{
163N/A unsigned char data[MAX_INT_STRLEN + 6 + 1 + 3];
163N/A buffer_t buf;
163N/A
163N/A buffer_create_from_data(&buf, data, sizeof(data));
163N/A str_printfa(&buf, "%lu.%06u ",
163N/A (unsigned long)ioloop_timeval.tv_sec,
163N/A (unsigned int)ioloop_timeval.tv_usec);
163N/A if ((rstream->flags & IOSTREAM_RAWLOG_FLAG_BUFFERED) != 0) {
163N/A str_append_c(&buf, rstream->input ? 'I' : 'O');
163N/A str_append_c(&buf, line_ends ? ':' : '>');
163N/A str_append_c(&buf, ' ');
163N/A }
163N/A return rawlog_write(rstream, buf.data, buf.used);
163N/A}
163N/A
163N/Avoid iostream_rawlog_init(struct rawlog_iostream *rstream,
163N/A enum iostream_rawlog_flags flags, bool input)
163N/A{
163N/A rstream->flags = flags;
163N/A rstream->input = input;
163N/A if ((rstream->flags & IOSTREAM_RAWLOG_FLAG_BUFFERED) != 0)
163N/A rstream->buffer = buffer_create_dynamic(default_pool, 1024);
163N/A}
163N/A
163N/Astatic void
163N/Aiostream_rawlog_write_unbuffered(struct rawlog_iostream *rstream,
163N/A const unsigned char *data, size_t size)
163N/A{
163N/A size_t i, start;
163N/A
163N/A if (!rstream->line_continued) {
163N/A if (rawlog_write_timestamp(rstream, TRUE) < 0)
163N/A return;
163N/A }
163N/A
163N/A for (start = 0, i = 1; i < size; i++) {
163N/A if (data[i-1] == '\n') {
163N/A if (rawlog_write(rstream, data + start, i - start) < 0 ||
163N/A rawlog_write_timestamp(rstream, TRUE) < 0)
163N/A return;
163N/A start = i;
163N/A }
163N/A }
163N/A if (start != size) {
163N/A if (rawlog_write(rstream, data + start, size - start) < 0)
163N/A return;
163N/A }
163N/A rstream->line_continued = data[size-1] != '\n';
163N/A}
163N/A
163N/Avoid iostream_rawlog_write(struct rawlog_iostream *rstream,
163N/A const unsigned char *data, size_t size)
163N/A{
163N/A const unsigned char *p;
163N/A size_t pos;
163N/A bool line_ends;
163N/A
163N/A if (size == 0)
163N/A return;
163N/A
163N/A io_loop_time_refresh();
163N/A if ((rstream->flags & IOSTREAM_RAWLOG_FLAG_BUFFERED) == 0) {
163N/A iostream_rawlog_write_unbuffered(rstream, data, size);
163N/A return;
163N/A }
163N/A
163N/A while (rstream->rawlog_fd != -1 && size > 0) {
163N/A p = memchr(data, '\n', size);
163N/A if (p != NULL) {
163N/A line_ends = TRUE;
163N/A pos = p-data + 1;
163N/A } else if (rstream->buffer->used + size < RAWLOG_MAX_LINE_LEN) {
163N/A buffer_append(rstream->buffer, data, size);
163N/A return;
163N/A } else {
163N/A line_ends = FALSE;
163N/A pos = size;
163N/A }
163N/A
163N/A if (rawlog_write_timestamp(rstream, line_ends) < 0)
163N/A break;
163N/A if (rstream->buffer->used > 0) {
163N/A if (rawlog_write(rstream, rstream->buffer->data,
163N/A rstream->buffer->used) < 0)
163N/A break;
163N/A buffer_set_used_size(rstream->buffer, 0);
163N/A }
163N/A if (rawlog_write(rstream, data, pos) < 0)
163N/A break;
163N/A
163N/A data += pos;
163N/A size -= pos;
}
}
void iostream_rawlog_close(struct rawlog_iostream *rstream)
{
if ((rstream->flags & IOSTREAM_RAWLOG_FLAG_AUTOCLOSE) != 0 &&
rstream->rawlog_fd != -1) {
if (close(rstream->rawlog_fd) < 0) {
i_error("rawlog_istream.close(%s) failed: %m",
rstream->rawlog_path);
}
}
rstream->rawlog_fd = -1;
i_free_and_null(rstream->rawlog_path);
if (rstream->buffer != NULL)
buffer_free(&rstream->buffer);
}
int iostream_rawlog_create(const char *dir, struct istream **input,
struct ostream **output)
{
static unsigned int counter = 0;
const char *timestamp, *prefix;
timestamp = t_strflocaltime("%Y%m%d-%H%M%S", ioloop_time);
counter++;
prefix = t_strdup_printf("%s/%s.%s.%u", dir, timestamp, my_pid, counter);
return iostream_rawlog_create_prefix(prefix, input, output);
}
int iostream_rawlog_create_prefix(const char *prefix, struct istream **input,
struct ostream **output)
{
const char *in_path, *out_path;
struct istream *old_input;
struct ostream *old_output;
int in_fd, out_fd;
in_path = t_strdup_printf("%s.in", prefix);
in_fd = open(in_path, O_CREAT | O_APPEND | O_WRONLY, 0600);
if (in_fd == -1) {
i_error("creat(%s) failed: %m", in_path);
return -1;
}
out_path = t_strdup_printf("%s.out", prefix);
out_fd = open(out_path, O_CREAT | O_APPEND | O_WRONLY, 0600);
if (out_fd == -1) {
i_error("creat(%s) failed: %m", out_path);
i_close_fd(&in_fd);
(void)unlink(in_path);
return -1;
}
old_input = *input;
old_output = *output;
*input = i_stream_create_rawlog(old_input, in_path, in_fd,
IOSTREAM_RAWLOG_FLAG_AUTOCLOSE);
*output = o_stream_create_rawlog(old_output, out_path, out_fd,
IOSTREAM_RAWLOG_FLAG_AUTOCLOSE);
i_stream_unref(&old_input);
o_stream_unref(&old_output);
return 0;
}
int iostream_rawlog_create_path(const char *path, struct istream **input,
struct ostream **output)
{
struct istream *old_input;
struct ostream *old_output;
int fd;
fd = open(path, O_CREAT | O_APPEND | O_WRONLY, 0600);
if (fd == -1) {
i_error("creat(%s) failed: %m", path);
return -1;
}
old_input = *input;
old_output = *output;
*input = i_stream_create_rawlog(old_input, path, fd,
IOSTREAM_RAWLOG_FLAG_BUFFERED);
*output = o_stream_create_rawlog(old_output, path, fd,
IOSTREAM_RAWLOG_FLAG_AUTOCLOSE |
IOSTREAM_RAWLOG_FLAG_BUFFERED);
i_stream_unref(&old_input);
o_stream_unref(&old_output);
return 0;
}