Mercurial > dovecot > core-2.2
annotate src/log/doveadm-connection.c @ 22715:20415dd0b85a
dsync: Add per-mailbox sync lock that is always used.
Both importing and exporting gets the lock before they even sync the
mailbox. The lock is kept until the import/export finishes. This guarantees
that no matter how dsync is run, two dsyncs can't be working on the same
mailbox at the same time.
This lock is in addition to the optional per-user lock enabled by the -l
parameter. If the -l parameter is used, the same lock timeout is used for
the per-mailbox lock. Otherwise 30s timeout is used.
This should help to avoid email duplication when replication is enabled for
public namespaces, and maybe in some other rare situations as well.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 28 Dec 2017 14:10:23 +0200 |
parents | cb108f786fb4 |
children |
rev | line source |
---|---|
22713
cb108f786fb4
Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21390
diff
changeset
|
1 /* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */ |
14067
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 #include "lib.h" |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 #include "ostream.h" |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 #include "str.h" |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 #include "strescape.h" |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 #include "master-service.h" |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 #include "log-error-buffer.h" |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 #include "doveadm-connection.h" |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
10 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
11 #include <unistd.h> |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
13 struct doveadm_connection { |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 struct log_error_buffer *errorbuf; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 int fd; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 struct ostream *output; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 }; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 static void doveadm_connection_destroy(struct doveadm_connection **_conn); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
21 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
22 static int doveadm_connection_send_errors(struct doveadm_connection *conn) |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 { |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
24 struct log_error_buffer_iter *iter; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
25 const struct log_error *error; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
26 string_t *str = t_str_new(256); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
27 int ret = 0; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
28 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
29 iter = log_error_buffer_iter_init(conn->errorbuf); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
30 while ((error = log_error_buffer_iter_next(iter)) != NULL) { |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
31 str_truncate(str, 0); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
32 str_printfa(str, "%s\t%ld\t", |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
33 failure_log_type_names[error->type], |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
34 (long)error->timestamp); |
15068
002e0a120c2a
Renamed str_tabescape_write() to str_append_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
14067
diff
changeset
|
35 str_append_tabescaped(str, error->prefix); |
14067
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 str_append_c(str, '\t'); |
15068
002e0a120c2a
Renamed str_tabescape_write() to str_append_tabescaped()
Timo Sirainen <tss@iki.fi>
parents:
14067
diff
changeset
|
37 str_append_tabescaped(str, error->text); |
14067
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
38 str_append_c(str, '\n'); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
39 if (o_stream_send(conn->output, |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
40 str_data(str), str_len(str)) < 0) { |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
41 ret = -1; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
42 break; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 } |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 } |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
45 log_error_buffer_iter_deinit(&iter); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
46 return ret; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
47 } |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
48 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
49 static int doveadm_output(struct doveadm_connection *conn) |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
50 { |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
51 int ret; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
52 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 if ((ret = o_stream_flush(conn->output)) != 0) { |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
54 /* error / finished */ |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
55 doveadm_connection_destroy(&conn); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
56 } |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
57 return 1; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
58 } |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
60 void doveadm_connection_create(struct log_error_buffer *errorbuf, int fd) |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
61 { |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
62 struct doveadm_connection *conn; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
63 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
64 conn = i_new(struct doveadm_connection, 1); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
65 conn->errorbuf = errorbuf; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
66 conn->fd = fd; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
67 conn->output = o_stream_create_fd(conn->fd, (size_t)-1, FALSE); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
68 if (doveadm_connection_send_errors(conn) < 0) |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
69 doveadm_connection_destroy(&conn); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
70 else { |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
71 o_stream_set_flush_callback(conn->output, doveadm_output, conn); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
72 o_stream_set_flush_pending(conn->output, TRUE); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
73 } |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
74 } |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
75 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
76 static void doveadm_connection_destroy(struct doveadm_connection **_conn) |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
77 { |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
78 struct doveadm_connection *conn = *_conn; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
79 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
80 *_conn = NULL; |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
81 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
82 o_stream_destroy(&conn->output); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
83 if (close(conn->fd) < 0) |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
84 i_error("close(doveadm connection) failed: %m"); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
85 i_free(conn); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
86 |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
87 master_service_client_connection_destroyed(master_service); |
6cd1b564fcc2
log: Forgot to add new files to recent commit.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
88 } |