1 diff --git a/server/listen.c b/server/listen.c
2 index 1d9be83..f5f7754 100644
10 +#include <systemd/sd-daemon.h>
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;
21 +static int use_systemd = -1;
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)
28 apr_socket_t *s = server->sd;
30 @@ -94,20 +101,6 @@ static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
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: "
42 - apr_socket_close(s);
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)
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",
59 - apr_socket_close(s);
62 + if (do_bind_listen) {
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: "
71 + apr_socket_close(s);
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 "
82 - apr_socket_close(s);
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",
88 + apr_socket_close(s);
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 "
97 + apr_socket_close(s);
103 @@ -277,6 +286,124 @@ static apr_status_t close_listeners_on_exec(void *v)
110 +static int find_systemd_socket(process_rec * process, apr_port_t port) {
112 + int sdc = sd_listen_fds(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",
122 + ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
123 + "find_systemd_socket: At least one socket must be set.");
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) {
137 +static apr_status_t alloc_systemd_listener(process_rec * process,
138 + int fd, const char *proto,
139 + ap_listen_rec **out_rec)
142 + struct sockaddr sa;
143 + socklen_t len = sizeof(struct sockaddr);
144 + apr_os_sock_info_t si;
145 + ap_listen_rec *rec;
148 + memset(&si, 0, sizeof(si));
150 + rv = getsockname(fd, &sa, &len);
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);
160 + si.family = sa.sa_family;
162 + si.type = SOCK_STREAM;
163 + si.protocol = APR_PROTO_TCP;
165 + rec = apr_palloc(process->pool, sizeof(ap_listen_rec));
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);
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);
184 + rec->protocol = apr_pstrdup(process->pool, proto);
188 + return make_sock(process->pool, rec, 0);
191 +static const char *set_systemd_listener(process_rec *process, apr_port_t port,
194 + ap_listen_rec *last, *new;
196 + int fd = find_systemd_socket(process, port);
198 + return "Systemd socket activation is used, but this port is not "
199 + "configured in systemd";
202 + last = ap_listeners;
203 + while (last && last->next) {
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";
212 + if (last == NULL) {
213 + ap_listeners = last = new;
223 +#endif /* HAVE_SYSTEMD */
225 static const char *alloc_listener(process_rec *process, char *addr,
226 apr_port_t port, const char* proto,
228 @@ -479,7 +606,7 @@ static int open_listeners(apr_pool_t *pool)
232 - if (make_sock(pool, lr) == APR_SUCCESS) {
233 + if (make_sock(pool, lr, 1) == APR_SUCCESS) {
237 @@ -591,8 +718,28 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
241 - if (open_listeners(s->process->pool)) {
245 + const char *userdata_key = "ap_open_systemd_listeners";
247 + /* clear the enviroment on our second run
248 + * so that none of our future children get confused.
250 + apr_pool_userdata_get(&data, userdata_key, s->process->pool);
252 + apr_pool_userdata_set((const void *)1, userdata_key,
253 + apr_pool_cleanup_null, s->process->pool);
262 + if (open_listeners(s->process->pool)) {
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,
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.";
282 + if (use_systemd == -1) {
283 + use_systemd = sd_listen_fds(0) > 0;
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);
295 + return set_systemd_listener(cmd->server->process, port, proto);
299 return alloc_listener(cmd->server->process, host, port, proto, NULL);