annotate src/lib/iostream-rawlog.c @ 22664:fea53c2725c0

director: Fix director_max_parallel_moves/kicks type Should be uint, not time.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 09 Nov 2017 12:24:16 +0200
parents 2e2563132d5f
children cb108f786fb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21390
2e2563132d5f Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents: 19681
diff changeset
1 /* Copyright (c) 2011-2017 Dovecot authors, see the included COPYING file */
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "hostpid.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "ioloop.h"
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
6 #include "buffer.h"
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
7 #include "str.h"
17520
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
8 #include "net.h"
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "write-full.h"
13929
19700d683144 Compiler warning fix.
Timo Sirainen <tss@iki.fi>
parents: 13924
diff changeset
10 #include "time-util.h"
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 #include "istream.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 #include "ostream.h"
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
13 #include "iostream-private.h"
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
14 #include "iostream-rawlog-private.h"
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 #include "istream-rawlog.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 #include "ostream-rawlog.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 #include "iostream-rawlog.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 #include <unistd.h>
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 #include <fcntl.h>
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
22 #define RAWLOG_MAX_LINE_LEN 8192
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
23
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
24 static void
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
25 rawlog_write_timestamp(struct rawlog_iostream *rstream, bool line_ends)
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 {
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
27 unsigned char data[MAX_INT_STRLEN + 6 + 1 + 3];
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
28 buffer_t buf;
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
29
17001
cf38c8eb8493 iostream-rawlog: Timestamp prefix is now optional.
Timo Sirainen <tss@iki.fi>
parents: 15814
diff changeset
30 if ((rstream->flags & IOSTREAM_RAWLOG_FLAG_TIMESTAMP) == 0)
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
31 return;
17001
cf38c8eb8493 iostream-rawlog: Timestamp prefix is now optional.
Timo Sirainen <tss@iki.fi>
parents: 15814
diff changeset
32
15034
7efef678bca8 Renamed buffer_create_*data() to buffer_create_from_*data() for consistency.
Timo Sirainen <tss@iki.fi>
parents: 14968
diff changeset
33 buffer_create_from_data(&buf, data, sizeof(data));
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
34 str_printfa(&buf, "%lu.%06u ",
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
35 (unsigned long)ioloop_timeval.tv_sec,
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
36 (unsigned int)ioloop_timeval.tv_usec);
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
37 if ((rstream->flags & IOSTREAM_RAWLOG_FLAG_BUFFERED) != 0) {
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
38 str_append_c(&buf, rstream->input ? 'I' : 'O');
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
39 str_append_c(&buf, line_ends ? ':' : '>');
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
40 str_append_c(&buf, ' ');
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
41 }
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
42 o_stream_nsend(rstream->rawlog_output, buf.data, buf.used);
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 }
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
45 void iostream_rawlog_init(struct rawlog_iostream *rstream,
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
46 enum iostream_rawlog_flags flags, bool input)
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 {
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
48 rstream->flags = flags;
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
49 rstream->input = input;
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
50 if ((rstream->flags & IOSTREAM_RAWLOG_FLAG_BUFFERED) != 0)
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
51 rstream->buffer = buffer_create_dynamic(default_pool, 1024);
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
52 }
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
53
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
54 static void
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
55 iostream_rawlog_write_buffered(struct rawlog_iostream *rstream,
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
56 const unsigned char *data, size_t size)
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
57 {
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
58 const unsigned char *p;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
59 size_t pos;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
60 bool line_ends;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
61
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
62 while (size > 0) {
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
63 p = memchr(data, '\n', size);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
64 if (p != NULL) {
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
65 line_ends = TRUE;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
66 pos = p-data + 1;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
67 } else if (rstream->buffer->used + size < RAWLOG_MAX_LINE_LEN) {
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
68 buffer_append(rstream->buffer, data, size);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
69 break;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
70 } else {
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
71 line_ends = FALSE;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
72 pos = size;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
73 }
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
74
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
75 rawlog_write_timestamp(rstream, line_ends);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
76 if (rstream->buffer->used > 0) {
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
77 o_stream_nsend(rstream->rawlog_output,
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
78 rstream->buffer->data,
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
79 rstream->buffer->used);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
80 buffer_set_used_size(rstream->buffer, 0);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
81 }
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
82 o_stream_nsend(rstream->rawlog_output, data, pos);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
83
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
84 data += pos;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
85 size -= pos;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
86 }
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
87 }
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
88
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
89 static void
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
90 iostream_rawlog_write_unbuffered(struct rawlog_iostream *rstream,
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
91 const unsigned char *data, size_t size)
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
92 {
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
93 size_t i, start;
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
95 if (!rstream->line_continued)
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
96 rawlog_write_timestamp(rstream, TRUE);
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
97
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
98 for (start = 0, i = 1; i < size; i++) {
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
99 if (data[i-1] == '\n') {
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
100 o_stream_nsend(rstream->rawlog_output,
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
101 data + start, i - start);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
102 rawlog_write_timestamp(rstream, TRUE);
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
103 start = i;
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
104 }
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
105 }
15351
ce342fd3cae7 iostream-rawlog: Avoid crashing more if write() to rawlog fails.
Timo Sirainen <tss@iki.fi>
parents: 15347
diff changeset
106 if (start != size) {
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
107 o_stream_nsend(rstream->rawlog_output,
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
108 data + start, size - start);
15351
ce342fd3cae7 iostream-rawlog: Avoid crashing more if write() to rawlog fails.
Timo Sirainen <tss@iki.fi>
parents: 15347
diff changeset
109 }
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
110 rstream->line_continued = data[size-1] != '\n';
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111 }
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 void iostream_rawlog_write(struct rawlog_iostream *rstream,
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 const unsigned char *data, size_t size)
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 {
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
116 if (size == 0 || rstream->rawlog_output == NULL)
15798
f35a8649634c iostream-rawlog: Ignore 0 byte writes instead of assert-crashing.
Timo Sirainen <tss@iki.fi>
parents: 14133
diff changeset
117 return;
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 io_loop_time_refresh();
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
121 o_stream_cork(rstream->rawlog_output);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
122 if ((rstream->flags & IOSTREAM_RAWLOG_FLAG_BUFFERED) != 0)
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
123 iostream_rawlog_write_buffered(rstream, data, size);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
124 else
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
125 iostream_rawlog_write_unbuffered(rstream, data, size);
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
126 o_stream_uncork(rstream->rawlog_output);
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
127
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
128 if (o_stream_nfinish(rstream->rawlog_output) < 0) {
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
129 i_error("write(%s) failed: %s",
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
130 o_stream_get_name(rstream->rawlog_output),
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
131 o_stream_get_error(rstream->rawlog_output));
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
132 iostream_rawlog_close(rstream);
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 }
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 }
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 void iostream_rawlog_close(struct rawlog_iostream *rstream)
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137 {
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
138 if (rstream->rawlog_output != NULL)
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
139 o_stream_unref(&rstream->rawlog_output);
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
140 if (rstream->buffer != NULL)
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
141 buffer_free(&rstream->buffer);
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142 }
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143
17520
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
144 static void
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
145 iostream_rawlog_create_fd(int fd, const char *path, struct istream **input,
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
146 struct ostream **output)
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
147 {
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
148 struct istream *old_input;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
149 struct ostream *old_output;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
150
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
151 old_input = *input;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
152 old_output = *output;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
153 *input = i_stream_create_rawlog(old_input, path, fd,
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
154 IOSTREAM_RAWLOG_FLAG_BUFFERED |
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
155 IOSTREAM_RAWLOG_FLAG_TIMESTAMP);
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
156 *output = o_stream_create_rawlog(old_output, path, fd,
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
157 IOSTREAM_RAWLOG_FLAG_AUTOCLOSE |
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
158 IOSTREAM_RAWLOG_FLAG_BUFFERED |
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
159 IOSTREAM_RAWLOG_FLAG_TIMESTAMP);
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
160 i_stream_unref(&old_input);
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
161 o_stream_unref(&old_output);
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
162 }
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
163
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
164 static int
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
165 iostream_rawlog_try_create_tcp(const char *path,
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
166 struct istream **input, struct ostream **output)
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
167 {
19681
5a3aac8f8ee1 Replace host:port parsers with net_str2hostport().
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 19552
diff changeset
168 const char *host;
17520
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
169 struct ip_addr *ips;
19035
aabfe48db1cf Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents: 18450
diff changeset
170 unsigned int ips_count;
aabfe48db1cf Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents: 18450
diff changeset
171 in_port_t port;
17520
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
172 int ret, fd;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
173
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
174 /* tcp:host:port */
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
175 if (strncmp(path, "tcp:", 4) != 0)
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
176 return 0;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
177 path += 4;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
178
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
179 if (strchr(path, '/') != NULL)
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
180 return 0;
19681
5a3aac8f8ee1 Replace host:port parsers with net_str2hostport().
Timo Sirainen <timo.sirainen@dovecot.fi>
parents: 19552
diff changeset
181 if (net_str2hostport(path, 0, &host, &port) < 0 || port == 0)
17520
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
182 return 0;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
183
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
184 ret = net_gethostbyname(host, &ips, &ips_count);
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
185 if (ret != 0) {
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
186 i_error("net_gethostbyname(%s) failed: %s", host,
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
187 net_gethosterror(ret));
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
188 return -1;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
189 }
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
190 fd = net_connect_ip_blocking(&ips[0], port, NULL);
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
191 if (fd == -1) {
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
192 i_error("connect(%s:%u) failed: %m", net_ip2addr(&ips[0]), port);
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
193 return -1;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
194 }
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
195 iostream_rawlog_create_fd(fd, path, input, output);
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
196 return 1;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
197 }
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
198
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 int iostream_rawlog_create(const char *dir, struct istream **input,
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200 struct ostream **output)
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 {
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202 static unsigned int counter = 0;
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
203 const char *timestamp, *prefix;
17521
4bc7c1ad21a8 lib: Moved rawlog dir stat()s to iostream_rawlog_create() internally.
Timo Sirainen <tss@iki.fi>
parents: 17520
diff changeset
204 struct stat st;
17520
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
205 int ret;
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
206
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
207 if ((ret = iostream_rawlog_try_create_tcp(dir, input, output)) != 0)
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
208 return ret < 0 ? -1 : 0;
17521
4bc7c1ad21a8 lib: Moved rawlog dir stat()s to iostream_rawlog_create() internally.
Timo Sirainen <tss@iki.fi>
parents: 17520
diff changeset
209 if (stat(dir, &st) < 0) {
4bc7c1ad21a8 lib: Moved rawlog dir stat()s to iostream_rawlog_create() internally.
Timo Sirainen <tss@iki.fi>
parents: 17520
diff changeset
210 if (errno != ENOENT && errno != EACCES)
4bc7c1ad21a8 lib: Moved rawlog dir stat()s to iostream_rawlog_create() internally.
Timo Sirainen <tss@iki.fi>
parents: 17520
diff changeset
211 i_error("rawlog: stat(%s) failed: %m", dir);
4bc7c1ad21a8 lib: Moved rawlog dir stat()s to iostream_rawlog_create() internally.
Timo Sirainen <tss@iki.fi>
parents: 17520
diff changeset
212 return -1;
4bc7c1ad21a8 lib: Moved rawlog dir stat()s to iostream_rawlog_create() internally.
Timo Sirainen <tss@iki.fi>
parents: 17520
diff changeset
213 }
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
214
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
215 timestamp = t_strflocaltime("%Y%m%d-%H%M%S", ioloop_time);
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
216
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
217 counter++;
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
218 prefix = t_strdup_printf("%s/%s.%s.%u", dir, timestamp, my_pid, counter);
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
219 return iostream_rawlog_create_prefix(prefix, input, output);
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
220 }
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
221
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
222 int iostream_rawlog_create_prefix(const char *prefix, struct istream **input,
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
223 struct ostream **output)
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
224 {
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
225 const char *in_path, *out_path;
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
226 struct istream *old_input;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
227 struct ostream *old_output;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
228 int in_fd, out_fd;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
230 in_path = t_strdup_printf("%s.in", prefix);
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
231 in_fd = open(in_path, O_CREAT | O_APPEND | O_WRONLY, 0600);
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
232 if (in_fd == -1) {
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
233 i_error("creat(%s) failed: %m", in_path);
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
234 return -1;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
235 }
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
236
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
237 out_path = t_strdup_printf("%s.out", prefix);
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238 out_fd = open(out_path, O_CREAT | O_APPEND | O_WRONLY, 0600);
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
239 if (out_fd == -1) {
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
240 i_error("creat(%s) failed: %m", out_path);
14691
3945a3646c67 Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents: 14687
diff changeset
241 i_close_fd(&in_fd);
19136
fefaa6d09a81 Replaced unlink() calls with i_unlink*() wherever possible.
Timo Sirainen <tss@iki.fi>
parents: 19035
diff changeset
242 i_unlink(in_path);
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
243 return -1;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
244 }
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
245
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
246 old_input = *input;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
247 old_output = *output;
17001
cf38c8eb8493 iostream-rawlog: Timestamp prefix is now optional.
Timo Sirainen <tss@iki.fi>
parents: 15814
diff changeset
248
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
249 *input = i_stream_create_rawlog(old_input, in_path, in_fd,
17001
cf38c8eb8493 iostream-rawlog: Timestamp prefix is now optional.
Timo Sirainen <tss@iki.fi>
parents: 15814
diff changeset
250 IOSTREAM_RAWLOG_FLAG_AUTOCLOSE |
cf38c8eb8493 iostream-rawlog: Timestamp prefix is now optional.
Timo Sirainen <tss@iki.fi>
parents: 15814
diff changeset
251 IOSTREAM_RAWLOG_FLAG_TIMESTAMP);
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
252 *output = o_stream_create_rawlog(old_output, out_path, out_fd,
17001
cf38c8eb8493 iostream-rawlog: Timestamp prefix is now optional.
Timo Sirainen <tss@iki.fi>
parents: 15814
diff changeset
253 IOSTREAM_RAWLOG_FLAG_AUTOCLOSE |
cf38c8eb8493 iostream-rawlog: Timestamp prefix is now optional.
Timo Sirainen <tss@iki.fi>
parents: 15814
diff changeset
254 IOSTREAM_RAWLOG_FLAG_TIMESTAMP);
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
255 i_stream_unref(&old_input);
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
256 o_stream_unref(&old_output);
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257 return 0;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
258 }
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
259
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
260 int iostream_rawlog_create_path(const char *path, struct istream **input,
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
261 struct ostream **output)
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
262 {
17520
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
263 int ret, fd;
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
264
17520
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
265 if ((ret = iostream_rawlog_try_create_tcp(path, input, output)) != 0)
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
266 return ret < 0 ? -1 : 0;
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
267 fd = open(path, O_CREAT | O_APPEND | O_WRONLY, 0600);
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
268 if (fd == -1) {
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
269 i_error("creat(%s) failed: %m", path);
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
270 return -1;
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
271 }
17520
f3134d3a7ff1 lib: iostream-rawlog now supports TCP target with "tcp:host:port" as the path.
Timo Sirainen <tss@iki.fi>
parents: 17130
diff changeset
272 iostream_rawlog_create_fd(fd, path, input, output);
14967
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
273 return 0;
8ce6d41d2d41 iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents: 14691
diff changeset
274 }
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
275
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
276 void iostream_rawlog_create_from_stream(struct ostream *rawlog_output,
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
277 struct istream **input,
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
278 struct ostream **output)
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
279 {
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
280 const enum iostream_rawlog_flags rawlog_flags =
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
281 IOSTREAM_RAWLOG_FLAG_BUFFERED |
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
282 IOSTREAM_RAWLOG_FLAG_TIMESTAMP;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
283 struct istream *old_input;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
284 struct ostream *old_output;
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
285
18449
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
286 if (input != NULL) {
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
287 old_input = *input;
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
288 *input = i_stream_create_rawlog_from_stream(old_input,
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
289 rawlog_output, rawlog_flags);
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
290 i_stream_unref(&old_input);
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
291 }
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
292 if (output != NULL) {
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
293 old_output = *output;
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
294 *output = o_stream_create_rawlog_from_stream(old_output,
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
295 rawlog_output, rawlog_flags);
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
296 o_stream_unref(&old_output);
49dd781d8f26 lib: iostream_rawlog_create_from_stream() now allows input or output to be NULL.
Timo Sirainen <tss@iki.fi>
parents: 18166
diff changeset
297 }
18450
c3acdfc64a0b lib: Fixed reference counting in iostream-rawlog if either i/ostream wasn't used.
Timo Sirainen <tss@iki.fi>
parents: 18449
diff changeset
298 if (input != NULL && output != NULL)
c3acdfc64a0b lib: Fixed reference counting in iostream-rawlog if either i/ostream wasn't used.
Timo Sirainen <tss@iki.fi>
parents: 18449
diff changeset
299 o_stream_ref(rawlog_output);
18166
c02f29ca104d lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents: 18137
diff changeset
300 }