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
|
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "str.h"
#include "strescape.h"
#include "ostream.h"
#include "doveadm.h"
#include "doveadm-print-private.h"
#include "client-connection.h"
#define DOVEADM_PRINT_FLUSH_TIMEOUT_SECS 60
struct doveadm_print_server_context {
unsigned int header_idx, header_count;
string_t *str;
};
static struct doveadm_print_server_context ctx;
static void doveadm_print_server_flush(void);
static void doveadm_print_server_init(void)
{
ctx.str = str_new(default_pool, 256);
}
static void doveadm_print_server_deinit(void)
{
str_free(&ctx.str);
}
static void
doveadm_print_server_header(const struct doveadm_print_header *hdr ATTR_UNUSED)
{
/* no need to transfer these. the client should already know what
it's getting */
ctx.header_count++;
}
static void doveadm_print_server_print(const char *value)
{
str_append_tabescaped(ctx.str, value);
str_append_c(ctx.str, '\t');
if (++ctx.header_idx == ctx.header_count) {
ctx.header_idx = 0;
doveadm_print_server_flush();
}
}
static void
doveadm_print_server_print_stream(const unsigned char *value, size_t size)
{
if (size == 0) {
doveadm_print_server_print("");
return;
}
str_append_tabescaped_n(ctx.str, value, size);
if (str_len(ctx.str) >= IO_BLOCK_SIZE)
doveadm_print_server_flush();
}
static int flush_callback(struct doveadm_print_server_context *ctx ATTR_UNUSED)
{
int ret;
/* Keep flushing until everything is sent */
if ((ret = o_stream_flush(doveadm_print_ostream)) != 0)
io_loop_stop(current_ioloop);
return ret;
}
static void handle_flush_timeout(struct doveadm_print_server_context *ctx ATTR_UNUSED)
{
io_loop_stop(current_ioloop);
o_stream_close(doveadm_print_ostream);
i_error("write(%s) failed: Timed out after %u seconds",
o_stream_get_name(doveadm_print_ostream),
DOVEADM_PRINT_FLUSH_TIMEOUT_SECS);
}
static void doveadm_print_server_flush(void)
{
o_stream_nsend(doveadm_print_ostream,
str_data(ctx.str), str_len(ctx.str));
str_truncate(ctx.str, 0);
o_stream_uncork(doveadm_print_ostream);
if (o_stream_get_buffer_used_size(doveadm_print_ostream) < IO_BLOCK_SIZE ||
doveadm_print_ostream->stream_errno != 0)
return;
/* Wait until buffer is flushed to avoid it growing too large */
struct ioloop *prev_loop = current_ioloop;
struct ioloop *loop = io_loop_create();
/* Ensure we don't get stuck here forever */
struct timeout *to =
timeout_add(DOVEADM_PRINT_FLUSH_TIMEOUT_SECS*1000, handle_flush_timeout, &ctx);
o_stream_switch_ioloop_to(doveadm_print_ostream, loop);
o_stream_set_flush_callback(doveadm_print_ostream, flush_callback, &ctx);
io_loop_run(loop);
timeout_remove(&to);
o_stream_unset_flush_callback(doveadm_print_ostream);
o_stream_switch_ioloop_to(doveadm_print_ostream, prev_loop);
io_loop_destroy(&loop);
}
struct doveadm_print_vfuncs doveadm_print_server_vfuncs = {
DOVEADM_PRINT_TYPE_SERVER,
doveadm_print_server_init,
doveadm_print_server_deinit,
doveadm_print_server_header,
doveadm_print_server_print,
doveadm_print_server_print_stream,
doveadm_print_server_flush
};
|