~ubuntu-branches/ubuntu/wily/dovecot/wily

« back to all changes in this revision

Viewing changes to src/replication/replicator/dsync-client.c

  • Committer: Package Import Robot
  • Author(s): Jaldhar H. Vyas
  • Date: 2013-09-09 00:57:32 UTC
  • mfrom: (1.13.11)
  • mto: (4.8.5 experimental) (1.16.1)
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: package-import@ubuntu.com-20130909005732-dn1eell8srqbhh0e
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
 
2
 
 
3
#include "lib.h"
 
4
#include "ioloop.h"
 
5
#include "net.h"
 
6
#include "istream.h"
 
7
#include "ostream.h"
 
8
#include "str.h"
 
9
#include "strescape.h"
 
10
#include "dsync-client.h"
 
11
 
 
12
#include <unistd.h>
 
13
 
 
14
#define DSYNC_FAIL_TIMEOUT_MSECS (1000*5)
 
15
#define DOVEADM_HANDSHAKE "VERSION\tdoveadm-server\t1\t0\n"
 
16
 
 
17
/* normally there shouldn't be any need for locking, since replicator doesn't
 
18
   start dsync in parallel for the same user. we'll do locking just in case
 
19
   anyway */
 
20
#define DSYNC_LOCK_TIMEOUT_SECS 30
 
21
 
 
22
struct dsync_client {
 
23
        char *path;
 
24
        int fd;
 
25
        struct io *io;
 
26
        struct istream *input;
 
27
        struct ostream *output;
 
28
        struct timeout *to;
 
29
 
 
30
        char *state;
 
31
        dsync_callback_t *callback;
 
32
        void *context;
 
33
 
 
34
        time_t last_connect_failure;
 
35
        unsigned int handshaked:1;
 
36
        unsigned int cmd_sent:1;
 
37
};
 
38
 
 
39
struct dsync_client *dsync_client_init(const char *path)
 
40
{
 
41
        struct dsync_client *client;
 
42
 
 
43
        client = i_new(struct dsync_client, 1);
 
44
        client->path = i_strdup(path);
 
45
        client->fd = -1;
 
46
        return client;
 
47
}
 
48
 
 
49
static void dsync_callback(struct dsync_client *client,
 
50
                           const char *state, enum dsync_reply reply)
 
51
{
 
52
        dsync_callback_t *callback = client->callback;
 
53
        void *context = client->context;
 
54
 
 
55
        if (client->to != NULL)
 
56
                timeout_remove(&client->to);
 
57
 
 
58
        client->callback = NULL;
 
59
        client->context = NULL;
 
60
 
 
61
        /* make sure callback doesn't try to reuse this connection, since
 
62
           we can't currently handle it */
 
63
        i_assert(!client->cmd_sent);
 
64
        client->cmd_sent = TRUE;
 
65
        callback(reply, state, context);
 
66
        client->cmd_sent = FALSE;
 
67
}
 
68
 
 
69
static void dsync_close(struct dsync_client *client)
 
70
{
 
71
        if (client->fd == -1)
 
72
                return;
 
73
 
 
74
        io_remove(&client->io);
 
75
        o_stream_destroy(&client->output);
 
76
        i_stream_destroy(&client->input);
 
77
        if (close(client->fd) < 0)
 
78
                i_error("close(dsync) failed: %m");
 
79
        client->fd = -1;
 
80
        i_free_and_null(client->state);
 
81
        client->cmd_sent = FALSE;
 
82
        client->handshaked = FALSE;
 
83
}
 
84
 
 
85
static void dsync_disconnect(struct dsync_client *client)
 
86
{
 
87
        dsync_close(client);
 
88
        if (client->callback != NULL)
 
89
                dsync_callback(client, "", DSYNC_REPLY_FAIL);
 
90
}
 
91
 
 
92
void dsync_client_deinit(struct dsync_client **_client)
 
93
{
 
94
        struct dsync_client *client = *_client;
 
95
 
 
96
        *_client = NULL;
 
97
 
 
98
        dsync_disconnect(client);
 
99
        i_free(client->path);
 
100
        i_free(client);
 
101
}
 
102
 
 
103
static int dsync_input_line(struct dsync_client *client, const char *line)
 
104
{
 
105
        const char *state;
 
106
 
 
107
        if (!client->handshaked) {
 
108
                if (strcmp(line, "+") != 0) {
 
109
                        i_error("%s: Unexpected handshake: %s",
 
110
                                client->path, line);
 
111
                        return -1;
 
112
                }
 
113
                client->handshaked = TRUE;
 
114
                return 0;
 
115
        }
 
116
        if (client->callback == NULL) {
 
117
                i_error("%s: Unexpected input: %s", client->path, line);
 
118
                return -1;
 
119
        }
 
120
        if (client->state == NULL) {
 
121
                client->state = i_strdup(t_strcut(line, '\t'));
 
122
                return 0;
 
123
        }
 
124
        state = t_strdup(client->state);
 
125
        line = t_strdup(line);
 
126
        dsync_close(client);
 
127
 
 
128
        if (line[0] == '+')
 
129
                dsync_callback(client, state, DSYNC_REPLY_OK);
 
130
        else if (line[0] == '-') {
 
131
                if (strcmp(line+1, "NOUSER") == 0)
 
132
                        dsync_callback(client, "", DSYNC_REPLY_NOUSER);
 
133
                else
 
134
                        dsync_callback(client, "", DSYNC_REPLY_FAIL);
 
135
        } else {
 
136
                i_error("%s: Invalid input: %s", client->path, line);
 
137
                return -1;
 
138
        }
 
139
        /* FIXME: disconnect after each request for now.
 
140
           doveadm server's getopt() handling seems to break otherwise.
 
141
           also with multiple UIDs doveadm-server fails because setid() fails */
 
142
        return -1;
 
143
}
 
144
 
 
145
static void dsync_input(struct dsync_client *client)
 
146
{
 
147
        const char *line;
 
148
 
 
149
        while ((line = i_stream_read_next_line(client->input)) != NULL) {
 
150
                if (dsync_input_line(client, line) < 0) {
 
151
                        dsync_disconnect(client);
 
152
                        return;
 
153
                }
 
154
        }
 
155
        if (client->input->eof)
 
156
                dsync_disconnect(client);
 
157
}
 
158
 
 
159
static int dsync_connect(struct dsync_client *client)
 
160
{
 
161
        if (client->fd != -1)
 
162
                return 0;
 
163
 
 
164
        if (client->last_connect_failure == ioloop_time)
 
165
                return -1;
 
166
 
 
167
        client->fd = net_connect_unix(client->path);
 
168
        if (client->fd == -1) {
 
169
                i_error("net_connect_unix(%s) failed: %m", client->path);
 
170
                client->last_connect_failure = ioloop_time;
 
171
                return -1;
 
172
        }
 
173
        client->last_connect_failure = 0;
 
174
        client->io = io_add(client->fd, IO_READ, dsync_input, client);
 
175
        client->input = i_stream_create_fd(client->fd, (size_t)-1, FALSE);
 
176
        client->output = o_stream_create_fd(client->fd, (size_t)-1, FALSE);
 
177
        o_stream_set_no_error_handling(client->output, TRUE);
 
178
        o_stream_nsend_str(client->output, DOVEADM_HANDSHAKE);
 
179
        return 0;
 
180
}
 
181
 
 
182
static void dsync_fail_timeout(struct dsync_client *client)
 
183
{
 
184
        dsync_disconnect(client);
 
185
}
 
186
 
 
187
void dsync_client_sync(struct dsync_client *client,
 
188
                       const char *username, const char *state, bool full,
 
189
                       dsync_callback_t *callback, void *context)
 
190
{
 
191
        string_t *cmd;
 
192
 
 
193
        i_assert(callback != NULL);
 
194
        i_assert(!dsync_client_is_busy(client));
 
195
 
 
196
        client->cmd_sent = TRUE;
 
197
        client->callback = callback;
 
198
        client->context = context;
 
199
 
 
200
        if (dsync_connect(client) < 0) {
 
201
                i_assert(client->to == NULL);
 
202
                client->to = timeout_add(DSYNC_FAIL_TIMEOUT_MSECS,
 
203
                                       dsync_fail_timeout, client);
 
204
        } else {
 
205
                /* <flags> <username> <command> [<args>] */
 
206
                cmd = t_str_new(256);
 
207
                str_append_c(cmd, '\t');
 
208
                str_append_tabescaped(cmd, username);
 
209
                str_printfa(cmd, "\tsync\t-d\t-N\t-l\t%u", DSYNC_LOCK_TIMEOUT_SECS);
 
210
                if (full)
 
211
                        str_append(cmd, "\t-f");
 
212
                str_append(cmd, "\t-U\t-s\t");
 
213
                if (state != NULL)
 
214
                        str_append(cmd, state);
 
215
                str_append_c(cmd, '\n');
 
216
                o_stream_nsend(client->output, str_data(cmd), str_len(cmd));
 
217
        }
 
218
}
 
219
 
 
220
bool dsync_client_is_busy(struct dsync_client *client)
 
221
{
 
222
        return client->cmd_sent;
 
223
}