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