]> Raphaƫl G. Git Repositories - ihttpd/blob - SOURCES/httpd-2.4.17-socket-activation.patch
Initial import
[ihttpd] / SOURCES / httpd-2.4.17-socket-activation.patch
1 diff --git a/server/listen.c b/server/listen.c
2 index 1d9be83..f5f7754 100644
3 --- a/server/listen.c
4 +++ b/server/listen.c
5 @@ -34,6 +34,10 @@
6 #include <unistd.h>
7 #endif
8
9 +#ifdef HAVE_SYSTEMD
10 +#include <systemd/sd-daemon.h>
11 +#endif
12 +
13 /* we know core's module_index is 0 */
14 #undef APLOG_MODULE_INDEX
15 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
16 @@ -59,9 +63,12 @@ static int ap_listenbacklog;
17 static int ap_listencbratio;
18 static int send_buffer_size;
19 static int receive_buffer_size;
20 +#ifdef HAVE_SYSTEMD
21 +static int use_systemd = -1;
22 +#endif
23
24 /* TODO: make_sock is just begging and screaming for APR abstraction */
25 -static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
26 +static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server, int do_bind_listen)
27 {
28 apr_socket_t *s = server->sd;
29 int one = 1;
30 @@ -94,20 +101,6 @@ static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
31 return stat;
32 }
33
34 -#if APR_HAVE_IPV6
35 - if (server->bind_addr->family == APR_INET6) {
36 - stat = apr_socket_opt_set(s, APR_IPV6_V6ONLY, v6only_setting);
37 - if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
38 - ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00069)
39 - "make_sock: for address %pI, apr_socket_opt_set: "
40 - "(IPV6_V6ONLY)",
41 - server->bind_addr);
42 - apr_socket_close(s);
43 - return stat;
44 - }
45 - }
46 -#endif
47 -
48 /*
49 * To send data over high bandwidth-delay connections at full
50 * speed we must force the TCP window to open wide enough to keep the
51 @@ -169,21 +162,37 @@ static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
52 }
53 #endif
54
55 - if ((stat = apr_socket_bind(s, server->bind_addr)) != APR_SUCCESS) {
56 - ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p, APLOGNO(00072)
57 - "make_sock: could not bind to address %pI",
58 - server->bind_addr);
59 - apr_socket_close(s);
60 - return stat;
61 - }
62 + if (do_bind_listen) {
63 +#if APR_HAVE_IPV6
64 + if (server->bind_addr->family == APR_INET6) {
65 + stat = apr_socket_opt_set(s, APR_IPV6_V6ONLY, v6only_setting);
66 + if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
67 + ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00069)
68 + "make_sock: for address %pI, apr_socket_opt_set: "
69 + "(IPV6_V6ONLY)",
70 + server->bind_addr);
71 + apr_socket_close(s);
72 + return stat;
73 + }
74 + }
75 +#endif
76
77 - if ((stat = apr_socket_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
78 - ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p, APLOGNO(00073)
79 - "make_sock: unable to listen for connections "
80 - "on address %pI",
81 - server->bind_addr);
82 - apr_socket_close(s);
83 - return stat;
84 + if ((stat = apr_socket_bind(s, server->bind_addr)) != APR_SUCCESS) {
85 + ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p, APLOGNO(00072)
86 + "make_sock: could not bind to address %pI",
87 + server->bind_addr);
88 + apr_socket_close(s);
89 + return stat;
90 + }
91 +
92 + if ((stat = apr_socket_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
93 + ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p, APLOGNO(00073)
94 + "make_sock: unable to listen for connections "
95 + "on address %pI",
96 + server->bind_addr);
97 + apr_socket_close(s);
98 + return stat;
99 + }
100 }
101
102 #ifdef WIN32
103 @@ -277,6 +286,124 @@ static apr_status_t close_listeners_on_exec(void *v)
104 return APR_SUCCESS;
105 }
106
107 +
108 +#ifdef HAVE_SYSTEMD
109 +
110 +static int find_systemd_socket(process_rec * process, apr_port_t port) {
111 + int fdcount, fd;
112 + int sdc = sd_listen_fds(0);
113 +
114 + if (sdc < 0) {
115 + ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
116 + "find_systemd_socket: Error parsing enviroment, sd_listen_fds returned %d",
117 + sdc);
118 + return -1;
119 + }
120 +
121 + if (sdc == 0) {
122 + ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
123 + "find_systemd_socket: At least one socket must be set.");
124 + return -1;
125 + }
126 +
127 + fdcount = atoi(getenv("LISTEN_FDS"));
128 + for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
129 + if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
130 + return fd;
131 + }
132 + }
133 +
134 + return -1;
135 +}
136 +
137 +static apr_status_t alloc_systemd_listener(process_rec * process,
138 + int fd, const char *proto,
139 + ap_listen_rec **out_rec)
140 +{
141 + apr_status_t rv;
142 + struct sockaddr sa;
143 + socklen_t len = sizeof(struct sockaddr);
144 + apr_os_sock_info_t si;
145 + ap_listen_rec *rec;
146 + *out_rec = NULL;
147 +
148 + memset(&si, 0, sizeof(si));
149 +
150 + rv = getsockname(fd, &sa, &len);
151 +
152 + if (rv != 0) {
153 + rv = apr_get_netos_error();
154 + ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02489)
155 + "getsockname on %d failed.", fd);
156 + return rv;
157 + }
158 +
159 + si.os_sock = &fd;
160 + si.family = sa.sa_family;
161 + si.local = &sa;
162 + si.type = SOCK_STREAM;
163 + si.protocol = APR_PROTO_TCP;
164 +
165 + rec = apr_palloc(process->pool, sizeof(ap_listen_rec));
166 + rec->active = 0;
167 + rec->next = 0;
168 +
169 +
170 + rv = apr_os_sock_make(&rec->sd, &si, process->pool);
171 + if (rv != APR_SUCCESS) {
172 + ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02490)
173 + "apr_os_sock_make on %d failed.", fd);
174 + return rv;
175 + }
176 +
177 + rv = apr_socket_addr_get(&rec->bind_addr, APR_LOCAL, rec->sd);
178 + if (rv != APR_SUCCESS) {
179 + ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02491)
180 + "apr_socket_addr_get on %d failed.", fd);
181 + return rv;
182 + }
183 +
184 + rec->protocol = apr_pstrdup(process->pool, proto);
185 +
186 + *out_rec = rec;
187 +
188 + return make_sock(process->pool, rec, 0);
189 +}
190 +
191 +static const char *set_systemd_listener(process_rec *process, apr_port_t port,
192 + const char *proto)
193 +{
194 + ap_listen_rec *last, *new;
195 + apr_status_t rv;
196 + int fd = find_systemd_socket(process, port);
197 + if (fd < 0) {
198 + return "Systemd socket activation is used, but this port is not "
199 + "configured in systemd";
200 + }
201 +
202 + last = ap_listeners;
203 + while (last && last->next) {
204 + last = last->next;
205 + }
206 +
207 + rv = alloc_systemd_listener(process, fd, proto, &new);
208 + if (rv != APR_SUCCESS) {
209 + return "Failed to setup socket passed by systemd using socket activation";
210 + }
211 +
212 + if (last == NULL) {
213 + ap_listeners = last = new;
214 + }
215 + else {
216 + last->next = new;
217 + last = new;
218 + }
219 +
220 + return NULL;
221 +}
222 +
223 +#endif /* HAVE_SYSTEMD */
224 +
225 static const char *alloc_listener(process_rec *process, char *addr,
226 apr_port_t port, const char* proto,
227 void *slave)
228 @@ -479,7 +606,7 @@ static int open_listeners(apr_pool_t *pool)
229 }
230 }
231 #endif
232 - if (make_sock(pool, lr) == APR_SUCCESS) {
233 + if (make_sock(pool, lr, 1) == APR_SUCCESS) {
234 ++num_open;
235 }
236 else {
237 @@ -591,8 +718,28 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
238 }
239 }
240
241 - if (open_listeners(s->process->pool)) {
242 - return 0;
243 +#ifdef HAVE_SYSTEMD
244 + if (use_systemd) {
245 + const char *userdata_key = "ap_open_systemd_listeners";
246 + void *data;
247 + /* clear the enviroment on our second run
248 + * so that none of our future children get confused.
249 + */
250 + apr_pool_userdata_get(&data, userdata_key, s->process->pool);
251 + if (!data) {
252 + apr_pool_userdata_set((const void *)1, userdata_key,
253 + apr_pool_cleanup_null, s->process->pool);
254 + }
255 + else {
256 + sd_listen_fds(1);
257 + }
258 + }
259 + else
260 +#endif
261 + {
262 + if (open_listeners(s->process->pool)) {
263 + return 0;
264 + }
265 }
266
267 for (lr = ap_listeners; lr; lr = lr->next) {
268 @@ -682,7 +829,7 @@ AP_DECLARE(apr_status_t) ap_duplicate_listeners(apr_pool_t *p, server_rec *s,
269 duplr->bind_addr);
270 return stat;
271 }
272 - make_sock(p, duplr);
273 + make_sock(p, duplr, 1);
274 #if AP_NONBLOCK_WHEN_MULTI_LISTEN
275 use_nonblock = (ap_listeners && ap_listeners->next);
276 stat = apr_socket_opt_set(duplr->sd, APR_SO_NONBLOCK, use_nonblock);
277 @@ -809,6 +956,11 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
278 if (argc < 1 || argc > 2) {
279 return "Listen requires 1 or 2 arguments.";
280 }
281 +#ifdef HAVE_SYSTEMD
282 + if (use_systemd == -1) {
283 + use_systemd = sd_listen_fds(0) > 0;
284 + }
285 +#endif
286
287 rv = apr_parse_addr_port(&host, &scope_id, &port, argv[0], cmd->pool);
288 if (rv != APR_SUCCESS) {
289 @@ -840,6 +992,12 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
290 ap_str_tolower(proto);
291 }
292
293 +#ifdef HAVE_SYSTEMD
294 + if (use_systemd) {
295 + return set_systemd_listener(cmd->server->process, port, proto);
296 + }
297 +#endif
298 +
299 return alloc_listener(cmd->server->process, host, port, proto, NULL);
300 }
301