Mercurial > dovecot > core-2.2
annotate src/config/config-connection.c @ 9159:6324a79d3ee1 HEAD
Initial commit for v2.0 master rewrite. Several features are still missing.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Thu, 23 Apr 2009 19:53:44 -0400 |
parents | 81ff88345441 |
children | f8460b27db00 |
rev | line source |
---|---|
9013
8b616cc6d848
Added dovecof --exec and made deliver use it instead of forking.
Timo Sirainen <tss@iki.fi>
parents:
9002
diff
changeset
|
1 /* Copyright (C) 2005-2009 Dovecot authors, see the included COPYING file */ |
9002 | 2 |
3 #include "common.h" | |
9087
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
4 #include "array.h" |
9002 | 5 #include "str.h" |
9159
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
6 #include "llist.h" |
9002 | 7 #include "ioloop.h" |
8 #include "network.h" | |
9 #include "istream.h" | |
10 #include "ostream.h" | |
9013
8b616cc6d848
Added dovecof --exec and made deliver use it instead of forking.
Timo Sirainen <tss@iki.fi>
parents:
9002
diff
changeset
|
11 #include "env-util.h" |
9002 | 12 #include "config-connection.h" |
13 | |
14 #include <stdlib.h> | |
15 #include <unistd.h> | |
16 #include <fcntl.h> | |
17 | |
18 #define MAX_INBUF_SIZE 1024 | |
19 | |
20 #define CONFIG_CLIENT_PROTOCOL_MAJOR_VERSION 1 | |
21 #define CONFIG_CLIENT_PROTOCOL_MINOR_VERSION 0 | |
22 | |
23 struct config_connection { | |
9159
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
24 struct config_connection *prev, *next; |
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
25 |
9002 | 26 int fd; |
27 struct istream *input; | |
28 struct ostream *output; | |
29 struct io *io; | |
30 | |
31 unsigned int version_received:1; | |
32 unsigned int handshaked:1; | |
33 }; | |
34 | |
9159
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
35 struct config_connection *config_connections = NULL; |
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
36 |
9002 | 37 static const char *const * |
38 config_connection_next_line(struct config_connection *conn) | |
39 { | |
40 const char *line; | |
41 | |
42 line = i_stream_next_line(conn->input); | |
43 if (line == NULL) | |
44 return NULL; | |
45 | |
46 return t_strsplit(line, "\t"); | |
47 } | |
48 | |
49 static void config_connection_request(struct config_connection *conn, | |
9087
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
50 const char *const *args, |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
51 enum config_dump_flags flags) |
9002 | 52 { |
9087
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
53 const char *const *strings; |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
54 unsigned int i, count; |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
55 string_t *str; |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
56 |
9002 | 57 /* <process> [<args>] */ |
9087
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
58 str = t_str_new(256); |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
59 strings = array_get(&config_strings, &count); |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
60 o_stream_cork(conn->output); |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
61 for (i = 0; i < count; i += 2) { |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
62 str_truncate(str, 0); |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
63 str_printfa(str, "%s=%s\n", strings[i], strings[i+1]); |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
64 o_stream_send(conn->output, str_data(str), str_len(str)); |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
65 } |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
66 o_stream_send_str(conn->output, "\n"); |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
67 o_stream_uncork(conn->output); |
9002 | 68 } |
69 | |
70 static void config_connection_input(void *context) | |
71 { | |
72 struct config_connection *conn = context; | |
73 const char *const *args, *line; | |
74 | |
75 switch (i_stream_read(conn->input)) { | |
76 case -2: | |
77 i_error("BUG: Config client connection sent too much data"); | |
78 config_connection_destroy(conn); | |
79 return; | |
80 case -1: | |
81 config_connection_destroy(conn); | |
82 return; | |
83 } | |
84 | |
85 if (!conn->version_received) { | |
86 line = i_stream_next_line(conn->input); | |
87 if (line == NULL) | |
88 return; | |
89 | |
90 if (strncmp(line, "VERSION\t", 8) != 0 || | |
91 atoi(t_strcut(line + 8, '\t')) != | |
92 CONFIG_CLIENT_PROTOCOL_MAJOR_VERSION) { | |
93 i_error("Config client not compatible with this server " | |
94 "(mixed old and new binaries?)"); | |
95 config_connection_destroy(conn); | |
96 return; | |
97 } | |
98 conn->version_received = TRUE; | |
99 } | |
100 | |
101 while ((args = config_connection_next_line(conn)) != NULL) { | |
102 if (args[0] == NULL) | |
103 continue; | |
104 if (strcmp(args[0], "REQ") == 0) | |
9087
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
105 config_connection_request(conn, args + 1, 0); |
9002 | 106 } |
107 } | |
108 | |
109 struct config_connection *config_connection_create(int fd) | |
110 { | |
111 struct config_connection *conn; | |
112 | |
113 conn = i_new(struct config_connection, 1); | |
114 conn->fd = fd; | |
115 conn->input = i_stream_create_fd(fd, MAX_INBUF_SIZE, FALSE); | |
116 conn->output = o_stream_create_fd(fd, (size_t)-1, FALSE); | |
117 conn->io = io_add(fd, IO_READ, config_connection_input, conn); | |
9159
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
118 DLLIST_PREPEND(&config_connections, conn); |
9002 | 119 return conn; |
120 } | |
121 | |
122 void config_connection_destroy(struct config_connection *conn) | |
123 { | |
9159
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
124 DLLIST_REMOVE(&config_connections, conn); |
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
125 |
9002 | 126 io_remove(&conn->io); |
127 i_stream_destroy(&conn->input); | |
128 o_stream_destroy(&conn->output); | |
129 if (close(conn->fd) < 0) | |
130 i_error("close(config conn) failed: %m"); | |
131 i_free(conn); | |
132 } | |
133 | |
9087
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
134 void config_connection_dump_request(int fd, const char *service, |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
135 enum config_dump_flags flags) |
9002 | 136 { |
137 struct config_connection *conn; | |
138 const char *args[2] = { service, NULL }; | |
139 | |
140 conn = config_connection_create(fd); | |
9087
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
141 config_connection_request(conn, args, flags); |
9002 | 142 config_connection_destroy(conn); |
143 } | |
9013
8b616cc6d848
Added dovecof --exec and made deliver use it instead of forking.
Timo Sirainen <tss@iki.fi>
parents:
9002
diff
changeset
|
144 |
8b616cc6d848
Added dovecof --exec and made deliver use it instead of forking.
Timo Sirainen <tss@iki.fi>
parents:
9002
diff
changeset
|
145 void config_connection_putenv(void) |
8b616cc6d848
Added dovecof --exec and made deliver use it instead of forking.
Timo Sirainen <tss@iki.fi>
parents:
9002
diff
changeset
|
146 { |
9087
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
147 const char *const *strings; |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
148 unsigned int i, count; |
9013
8b616cc6d848
Added dovecof --exec and made deliver use it instead of forking.
Timo Sirainen <tss@iki.fi>
parents:
9002
diff
changeset
|
149 |
9087
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
150 strings = array_get(&config_strings, &count); |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
151 for (i = 0; i < count; i += 2) T_BEGIN { |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
152 env_put(t_strconcat(t_str_ucase(strings[i]), "=", |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
153 strings[i+1], NULL)); |
81ff88345441
Some cleanups to the config parsing code.
Timo Sirainen <tss@iki.fi>
parents:
9013
diff
changeset
|
154 } T_END; |
9013
8b616cc6d848
Added dovecof --exec and made deliver use it instead of forking.
Timo Sirainen <tss@iki.fi>
parents:
9002
diff
changeset
|
155 } |
9159
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
156 |
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
157 void config_connections_destroy_all(void) |
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
158 { |
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
159 while (config_connections != NULL) |
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
160 config_connection_destroy(config_connections); |
6324a79d3ee1
Initial commit for v2.0 master rewrite. Several features are still missing.
Timo Sirainen <tss@iki.fi>
parents:
9087
diff
changeset
|
161 } |