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