File: mupdate-slave.c

package info (click to toggle)
cyrus-imapd 3.6.1-4%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,688 kB
  • sloc: ansic: 255,928; perl: 97,730; javascript: 9,266; sh: 5,537; yacc: 2,651; cpp: 2,128; makefile: 2,099; lex: 660; xml: 621; python: 388; awk: 303; asm: 262
file content (373 lines) | stat: -rw-r--r-- 11,335 bytes parent folder | download | duplicates (3)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* mupdate-slave.c -- cyrus murder database clients
 *
 * Copyright (c) 1994-2008 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any legal
 *    details, please contact
 *      Carnegie Mellon University
 *      Center for Technology Transfer and Enterprise Creation
 *      4615 Forbes Avenue
 *      Suite 302
 *      Pittsburgh, PA  15213
 *      (412) 268-7393, fax: (412) 268-7395
 *      innovation@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <config.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sasl/sasl.h>
#include <sasl/saslutil.h>
#include <sysexits.h>
#include <syslog.h>
#include <stdarg.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <netinet/in.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#include "prot.h"
#include "xmalloc.h"
#include "xstrlcpy.h"
#include "xstrlcat.h"
#include "global.h"
#include "mpool.h"
#include "mupdate.h"

/* Returns file descriptor of kick socket (or does not return) */
static int open_kick_socket(void)
{
    int r,s,len;
    char fnamebuf[2048];
    struct sockaddr_un srvaddr;
    mode_t oldumask;

    s = socket(AF_UNIX, SOCK_STREAM, 0);
    if (s == -1) {
        syslog(LOG_ERR, "socket: %m");
        fatal("socket failed", EX_OSERR);
    }

    strlcpy(fnamebuf, config_dir, sizeof(fnamebuf));
    strlcat(fnamebuf, FNAME_MUPDATE_TARGET_SOCK, sizeof(fnamebuf));

    (void) unlink(fnamebuf);
    memset((char *)&srvaddr, 0, sizeof(srvaddr));
    srvaddr.sun_family = AF_UNIX;
    strlcpy(srvaddr.sun_path, fnamebuf, sizeof(srvaddr.sun_path));
    len = strlen(srvaddr.sun_path) + sizeof(srvaddr.sun_family) + 1;
    oldumask = umask((mode_t) 0); /* for Linux */
    r = bind(s, (struct sockaddr *)&srvaddr, len);
    umask(oldumask); /* for Linux */
    chmod(fnamebuf, 0777); /* for DUX */
    if (r == -1) {
        syslog(LOG_ERR, "bind: %s: %m", fnamebuf);
        fatal("bind failed", EX_OSERR);
    }
    r = listen(s, 10);
    if (r == -1) {
        syslog(LOG_ERR, "listen: %m");
        fatal("listen failed", EX_OSERR);
    }

    return s;
}

/* Accept up to max_fds connections on kicksock, put the fds into
 * the array fd_list (atleast max_fds big), and the number of connections
 * into num_fds */
static int get_kick_fds(int kicksock,
                        int *fd_list, int *num_fds, int max_fds)
{
    fd_set read_set;
    int highest_fd = kicksock + 1;
    struct timeval tv;

    tv.tv_sec = 0;
    tv.tv_usec = 0;

    FD_ZERO(&read_set);
    FD_SET(kicksock, &read_set);

    *num_fds = 0;

    for (*num_fds = 0; *num_fds < max_fds; (*num_fds)++) {
        int gotdata;
        fd_set rset;

        rset = read_set;
        gotdata = select(highest_fd, &rset, NULL, NULL, &tv);

        if (gotdata == -1) {
          /* Select Error! */
          syslog(LOG_ERR, "kicksock select failed");
          return -1;
        } else if (gotdata != 0 && FD_ISSET(kicksock, &rset)) {
          struct sockaddr_un clientaddr;
          int len = sizeof(clientaddr);

          fd_list[*num_fds] = accept(kicksock,
                (struct sockaddr *)&clientaddr, (socklen_t *)&len);
          if (fd_list[*num_fds] == -1) {
            syslog(LOG_WARNING, "kicksock accept() failed: %m %d", kicksock);
            return -1;
          }
        } else {
          /* Timeout Expired, we're done! */
          break;
        }
    }

    return 0;
}

#define KICK_FDS_LEN 5

static void mupdate_listen(mupdate_handle *handle, int pingtimeout)
{
    int gotdata = 0;
    fd_set rset, read_set;
    int highest_fd, kicksock;
    int waiting_for_noop = 0;
    int kick_fds[KICK_FDS_LEN];
    int num_kick_fds = 0;
    struct mbent_queue remote_boxes;
    struct mpool *pool;
    int r;
    enum mupdate_cmd_response response;

    if (!handle || !handle->saslcompleted) return;

    pool = new_mpool(131072); /* Arbitrary, but large (128k) */

    /* first get the list of remote mailboxes from the mupdate master */
    r = mupdate_synchronize_remote(handle, &remote_boxes, pool);
    if (r) {
        free_mpool(pool);
        return;
    }

    /* don't handle connections (and drop current connections)
     * while we sync */
    mupdate_unready();

    /* Now, resync the database by comparing the remote mbox with our local*/
    r = mupdate_synchronize(&remote_boxes, pool);
    free_mpool(pool);
    if (r) return;

    mupdate_signal_db_synced();

    /* Okay, we're all set to go */
    mupdate_ready();

    kicksock = open_kick_socket();
    highest_fd = ((kicksock > handle->conn->sock) ? kicksock : handle->conn->sock) + 1;

    FD_ZERO(&read_set);
    FD_SET(handle->conn->sock, &read_set);
    FD_SET(kicksock, &read_set);

    /* Now just listen to the rest of the updates */
    while (1) {
        struct timeval tv;

        tv.tv_sec = pingtimeout;
        tv.tv_usec = 0;

        prot_flush(handle->conn->out);

        rset = read_set;

        gotdata = select(highest_fd, &rset, NULL, NULL, &tv);

        if (gotdata == -1) {
            /* Oops? */
            syslog(LOG_ERR, "select failed");
            break;
        } else if (gotdata != 0) {
            if (FD_ISSET(handle->conn->sock, &rset)) {
                /* If there is a fatal error, die, other errors ignore */
                response = MUPDATE_NONE;
                if ((r = mupdate_scarf(handle, cmd_change, NULL,
                                  waiting_for_noop, &response)) != 0) {
                    syslog(LOG_ERR, "mupdate_scarf: %d", r);
                    break;
                }

                /* If we were waiting on a noop, we no longer are.
                 * If we have been kicked, tell them we're done now */
                if (waiting_for_noop) {
                    if (response != MUPDATE_OK) {
                        syslog(LOG_ERR, "update/noop sync error %d", response);
                        break;
                    }
                    waiting_for_noop = 0;

                    for (; num_kick_fds; num_kick_fds--) {
                        if (write(kick_fds[num_kick_fds-1], "ok", 2) < 0) {
                            syslog(LOG_WARNING,
                                   "can't write to IPC socket (ignoring)");
                        }
                        (void)close(kick_fds[num_kick_fds-1]);
                    }
                }
            }

            if (waiting_for_noop == 0 && FD_ISSET(kicksock, &rset)) {
                /* We were kicked--collect outstanding kicks! */
                if (get_kick_fds(kicksock,
                                kick_fds, &num_kick_fds, KICK_FDS_LEN)) {
                    /* Nonzero return code -- Error */
                    break;
                }

                prot_printf(handle->conn->out, "N%u NOOP\r\n", handle->tagn++);
                if (prot_flush(handle->conn->out) == EOF) {
                    syslog(LOG_ERR, "connection to master failed.");
                    break;
                }
                waiting_for_noop = 1;
            }
        } else /* (gotdata == 0) */ {
            /* Timeout, send a NOOP */
            if (!waiting_for_noop) {
                prot_printf(handle->conn->out, "N%u NOOP\r\n", handle->tagn++);
                if (prot_flush(handle->conn->out) == EOF) {
                    syslog(LOG_ERR, "connection to master failed.");
                    break;
                }
                waiting_for_noop = 1;
            } else {
                /* We were already waiting on a noop! */
                syslog(LOG_ERR, "connection to master timed out.");
                break;
            }
        }
    } /* Loop */

    /* Don't leak the descriptors! */
    for (; num_kick_fds; num_kick_fds--) {
        (void)close(kick_fds[num_kick_fds-1]);
    }
    (void)close(kicksock);
    return;
}

void *mupdate_client_start(void *rock __attribute__((unused)))
{
    mupdate_handle *h = NULL;
    int retry_delay = 20, real_delay;
    int ret;

    srand(time(NULL) * getpid());

    if(!config_mupdate_server) {
        fatal("couldn't get mupdate server name", EX_UNAVAILABLE);
    }

    retry_delay = config_getint(IMAPOPT_MUPDATE_RETRY_DELAY);
    if(retry_delay < 0) {
        fatal("invalid value for mupdate_retry_delay", EX_UNAVAILABLE);
    }

    while(1) {
        ret = mupdate_connect(config_mupdate_server, NULL, &h, NULL);
        if(ret) {
            syslog(LOG_ERR,"couldn't connect to mupdate server");
            goto retry;
        }

        syslog(LOG_ERR, "successful mupdate connection to %s",
               config_mupdate_server);

        mupdate_listen(h, retry_delay);

    retry:
        /* Cleanup */
        mupdate_disconnect(&h);

        real_delay = retry_delay + (rand() % (retry_delay / 2));

        syslog(LOG_ERR,
               "retrying connection to mupdate server in %d seconds",
               real_delay);

        /* Wait before retrying */
        sleep(real_delay);
    }

    return NULL;
}

void *mupdate_placebo_kick_start(void *rock __attribute__((unused)))
{
    int kicksock, kickconn = -1;

    kicksock = open_kick_socket();

    /* Now just listen to the rest of the updates */
    while(1) {
        struct sockaddr_un clientaddr;
        int len;

        /* Only handle one kick at a time -- they're fast */
        len = sizeof(clientaddr);
        kickconn =
            accept(kicksock, (struct sockaddr *)&clientaddr, (socklen_t *)&len);

        if (kickconn == -1) {
          syslog(LOG_WARNING, "accept(): %m");
          break;
        } else {
          if (write(kickconn, "ok", 2) < 0) {
            syslog(LOG_WARNING, "can't write to IPC socket?");
          }
          close(kickconn);
          kickconn = -1;
        }
    } /* Loop */

    /* Don't leak the descriptor! */
    if(kickconn >= 0) close(kickconn);
    close(kicksock);

    return NULL;
}