|
|
52592b |
diff --git a/server/listen.c b/server/listen.c
|
|
|
52592b |
index a8e9e6f..1a6c1d3 100644
|
|
|
52592b |
--- a/server/listen.c
|
|
|
52592b |
+++ b/server/listen.c
|
|
|
52592b |
@@ -34,6 +34,10 @@
|
|
|
52592b |
#include <unistd.h>
|
|
|
52592b |
#endif
|
|
|
52592b |
|
|
|
52592b |
+#ifdef HAVE_SYSTEMD
|
|
|
52592b |
+#include <systemd/sd-daemon.h>
|
|
|
52592b |
+#endif
|
|
|
52592b |
+
|
|
|
52592b |
/* we know core's module_index is 0 */
|
|
|
52592b |
#undef APLOG_MODULE_INDEX
|
|
|
52592b |
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
|
|
|
52592b |
@@ -59,9 +63,12 @@ static int ap_listenbacklog;
|
|
|
52592b |
static int ap_listencbratio;
|
|
|
52592b |
static int send_buffer_size;
|
|
|
52592b |
static int receive_buffer_size;
|
|
|
52592b |
+#ifdef HAVE_SYSTEMD
|
|
|
52592b |
+static int use_systemd = -1;
|
|
|
52592b |
+#endif
|
|
|
52592b |
|
|
|
52592b |
/* TODO: make_sock is just begging and screaming for APR abstraction */
|
|
|
52592b |
-static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
|
|
|
52592b |
+static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server, int do_bind_listen)
|
|
|
52592b |
{
|
|
|
52592b |
apr_socket_t *s = server->sd;
|
|
|
52592b |
int one = 1;
|
|
|
52592b |
@@ -94,20 +101,6 @@ static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
|
|
|
52592b |
return stat;
|
|
|
52592b |
}
|
|
|
52592b |
|
|
|
52592b |
-#if APR_HAVE_IPV6
|
|
|
52592b |
- if (server->bind_addr->family == APR_INET6) {
|
|
|
52592b |
- stat = apr_socket_opt_set(s, APR_IPV6_V6ONLY, v6only_setting);
|
|
|
52592b |
- if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
|
|
|
52592b |
- ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00069)
|
|
|
52592b |
- "make_sock: for address %pI, apr_socket_opt_set: "
|
|
|
52592b |
- "(IPV6_V6ONLY)",
|
|
|
52592b |
- server->bind_addr);
|
|
|
52592b |
- apr_socket_close(s);
|
|
|
52592b |
- return stat;
|
|
|
52592b |
- }
|
|
|
52592b |
- }
|
|
|
52592b |
-#endif
|
|
|
52592b |
-
|
|
|
52592b |
/*
|
|
|
52592b |
* To send data over high bandwidth-delay connections at full
|
|
|
52592b |
* speed we must force the TCP window to open wide enough to keep the
|
|
|
52592b |
@@ -169,21 +162,37 @@ static apr_status_t make_sock(apr_pool_t *p, ap_listen_rec *server)
|
|
|
52592b |
}
|
|
|
52592b |
#endif
|
|
|
52592b |
|
|
|
52592b |
- if ((stat = apr_socket_bind(s, server->bind_addr)) != APR_SUCCESS) {
|
|
|
52592b |
- ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p, APLOGNO(00072)
|
|
|
52592b |
- "make_sock: could not bind to address %pI",
|
|
|
52592b |
- server->bind_addr);
|
|
|
52592b |
- apr_socket_close(s);
|
|
|
52592b |
- return stat;
|
|
|
52592b |
- }
|
|
|
52592b |
+ if (do_bind_listen) {
|
|
|
52592b |
+#if APR_HAVE_IPV6
|
|
|
52592b |
+ if (server->bind_addr->family == APR_INET6) {
|
|
|
52592b |
+ stat = apr_socket_opt_set(s, APR_IPV6_V6ONLY, v6only_setting);
|
|
|
52592b |
+ if (stat != APR_SUCCESS && stat != APR_ENOTIMPL) {
|
|
|
52592b |
+ ap_log_perror(APLOG_MARK, APLOG_CRIT, stat, p, APLOGNO(00069)
|
|
|
52592b |
+ "make_sock: for address %pI, apr_socket_opt_set: "
|
|
|
52592b |
+ "(IPV6_V6ONLY)",
|
|
|
52592b |
+ server->bind_addr);
|
|
|
52592b |
+ apr_socket_close(s);
|
|
|
52592b |
+ return stat;
|
|
|
52592b |
+ }
|
|
|
52592b |
+ }
|
|
|
52592b |
+#endif
|
|
|
52592b |
|
|
|
52592b |
- if ((stat = apr_socket_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
|
|
|
52592b |
- ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p, APLOGNO(00073)
|
|
|
52592b |
- "make_sock: unable to listen for connections "
|
|
|
52592b |
- "on address %pI",
|
|
|
52592b |
- server->bind_addr);
|
|
|
52592b |
- apr_socket_close(s);
|
|
|
52592b |
- return stat;
|
|
|
52592b |
+ if ((stat = apr_socket_bind(s, server->bind_addr)) != APR_SUCCESS) {
|
|
|
52592b |
+ ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, stat, p, APLOGNO(00072)
|
|
|
52592b |
+ "make_sock: could not bind to address %pI",
|
|
|
52592b |
+ server->bind_addr);
|
|
|
52592b |
+ apr_socket_close(s);
|
|
|
52592b |
+ return stat;
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ if ((stat = apr_socket_listen(s, ap_listenbacklog)) != APR_SUCCESS) {
|
|
|
52592b |
+ ap_log_perror(APLOG_MARK, APLOG_STARTUP|APLOG_ERR, stat, p, APLOGNO(00073)
|
|
|
52592b |
+ "make_sock: unable to listen for connections "
|
|
|
52592b |
+ "on address %pI",
|
|
|
52592b |
+ server->bind_addr);
|
|
|
52592b |
+ apr_socket_close(s);
|
|
|
52592b |
+ return stat;
|
|
|
52592b |
+ }
|
|
|
52592b |
}
|
|
|
52592b |
|
|
|
52592b |
#ifdef WIN32
|
|
|
52592b |
@@ -315,6 +324,123 @@ static int find_listeners(ap_listen_rec **from, ap_listen_rec **to,
|
|
|
52592b |
return found;
|
|
|
52592b |
}
|
|
|
52592b |
|
|
|
52592b |
+#ifdef HAVE_SYSTEMD
|
|
|
52592b |
+
|
|
|
52592b |
+static int find_systemd_socket(process_rec * process, apr_port_t port) {
|
|
|
52592b |
+ int fdcount, fd;
|
|
|
52592b |
+ int sdc = sd_listen_fds(0);
|
|
|
52592b |
+
|
|
|
52592b |
+ if (sdc < 0) {
|
|
|
52592b |
+ ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
|
|
|
52592b |
+ "find_systemd_socket: Error parsing enviroment, sd_listen_fds returned %d",
|
|
|
52592b |
+ sdc);
|
|
|
52592b |
+ return -1;
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ if (sdc == 0) {
|
|
|
52592b |
+ ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
|
|
|
52592b |
+ "find_systemd_socket: At least one socket must be set.");
|
|
|
52592b |
+ return -1;
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ fdcount = atoi(getenv("LISTEN_FDS"));
|
|
|
52592b |
+ for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
|
|
|
52592b |
+ if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
|
|
|
52592b |
+ return fd;
|
|
|
52592b |
+ }
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ return -1;
|
|
|
52592b |
+}
|
|
|
52592b |
+
|
|
|
52592b |
+static apr_status_t alloc_systemd_listener(process_rec * process,
|
|
|
52592b |
+ int fd, const char *proto,
|
|
|
52592b |
+ ap_listen_rec **out_rec)
|
|
|
52592b |
+{
|
|
|
52592b |
+ apr_status_t rv;
|
|
|
52592b |
+ struct sockaddr sa;
|
|
|
52592b |
+ socklen_t len = sizeof(struct sockaddr);
|
|
|
52592b |
+ apr_os_sock_info_t si;
|
|
|
52592b |
+ ap_listen_rec *rec;
|
|
|
52592b |
+ *out_rec = NULL;
|
|
|
52592b |
+
|
|
|
52592b |
+ memset(&si, 0, sizeof(si));
|
|
|
52592b |
+
|
|
|
52592b |
+ rv = getsockname(fd, &sa, &len;;
|
|
|
52592b |
+
|
|
|
52592b |
+ if (rv != 0) {
|
|
|
52592b |
+ rv = apr_get_netos_error();
|
|
|
52592b |
+ ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02489)
|
|
|
52592b |
+ "getsockname on %d failed.", fd);
|
|
|
52592b |
+ return rv;
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ si.os_sock = &fd;
|
|
|
52592b |
+ si.family = sa.sa_family;
|
|
|
52592b |
+ si.local = &sa;
|
|
|
52592b |
+ si.type = SOCK_STREAM;
|
|
|
52592b |
+ si.protocol = APR_PROTO_TCP;
|
|
|
52592b |
+
|
|
|
52592b |
+ rec = apr_palloc(process->pool, sizeof(ap_listen_rec));
|
|
|
52592b |
+ rec->active = 0;
|
|
|
52592b |
+ rec->next = 0;
|
|
|
52592b |
+
|
|
|
52592b |
+
|
|
|
52592b |
+ rv = apr_os_sock_make(&rec->sd, &si, process->pool);
|
|
|
52592b |
+ if (rv != APR_SUCCESS) {
|
|
|
52592b |
+ ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02490)
|
|
|
52592b |
+ "apr_os_sock_make on %d failed.", fd);
|
|
|
52592b |
+ return rv;
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ rv = apr_socket_addr_get(&rec->bind_addr, APR_LOCAL, rec->sd);
|
|
|
52592b |
+ if (rv != APR_SUCCESS) {
|
|
|
52592b |
+ ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02491)
|
|
|
52592b |
+ "apr_socket_addr_get on %d failed.", fd);
|
|
|
52592b |
+ return rv;
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ rec->protocol = apr_pstrdup(process->pool, proto);
|
|
|
52592b |
+
|
|
|
52592b |
+ *out_rec = rec;
|
|
|
52592b |
+
|
|
|
52592b |
+ return make_sock(process->pool, rec, 0);
|
|
|
52592b |
+}
|
|
|
52592b |
+
|
|
|
52592b |
+static const char *set_systemd_listener(process_rec *process, apr_port_t port,
|
|
|
52592b |
+ const char *proto)
|
|
|
52592b |
+{
|
|
|
52592b |
+ ap_listen_rec *last, *new;
|
|
|
52592b |
+ apr_status_t rv;
|
|
|
52592b |
+ int fd = find_systemd_socket(process, port);
|
|
|
52592b |
+ if (fd < 0) {
|
|
|
52592b |
+ return "Systemd socket activation is used, but this port is not "
|
|
|
52592b |
+ "configured in systemd";
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ last = ap_listeners;
|
|
|
52592b |
+ while (last && last->next) {
|
|
|
52592b |
+ last = last->next;
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ rv = alloc_systemd_listener(process, fd, proto, &new;;
|
|
|
52592b |
+ if (rv != APR_SUCCESS) {
|
|
|
52592b |
+ return "Failed to setup socket passed by systemd using socket activation";
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ if (last == NULL) {
|
|
|
52592b |
+ ap_listeners = last = new;
|
|
|
52592b |
+ }
|
|
|
52592b |
+ else {
|
|
|
52592b |
+ last->next = new;
|
|
|
52592b |
+ last = new;
|
|
|
52592b |
+ }
|
|
|
52592b |
+
|
|
|
52592b |
+ return NULL;
|
|
|
52592b |
+}
|
|
|
52592b |
+
|
|
|
52592b |
+#endif /* HAVE_SYSTEMD */
|
|
|
52592b |
+
|
|
|
52592b |
static const char *alloc_listener(process_rec *process, const char *addr,
|
|
|
52592b |
apr_port_t port, const char* proto,
|
|
|
52592b |
void *slave)
|
|
|
52592b |
@@ -495,7 +621,7 @@ static int open_listeners(apr_pool_t *pool)
|
|
|
52592b |
}
|
|
|
52592b |
}
|
|
|
52592b |
#endif
|
|
|
52592b |
- if (make_sock(pool, lr) == APR_SUCCESS) {
|
|
|
52592b |
+ if (make_sock(pool, lr, 1) == APR_SUCCESS) {
|
|
|
52592b |
++num_open;
|
|
|
52592b |
}
|
|
|
52592b |
else {
|
|
|
52592b |
@@ -607,8 +733,28 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
|
|
|
52592b |
}
|
|
|
52592b |
}
|
|
|
52592b |
|
|
|
52592b |
- if (open_listeners(s->process->pool)) {
|
|
|
52592b |
- return 0;
|
|
|
52592b |
+#ifdef HAVE_SYSTEMD
|
|
|
52592b |
+ if (use_systemd) {
|
|
|
52592b |
+ const char *userdata_key = "ap_open_systemd_listeners";
|
|
|
52592b |
+ void *data;
|
|
|
52592b |
+ /* clear the enviroment on our second run
|
|
|
52592b |
+ * so that none of our future children get confused.
|
|
|
52592b |
+ */
|
|
|
52592b |
+ apr_pool_userdata_get(&data, userdata_key, s->process->pool);
|
|
|
52592b |
+ if (!data) {
|
|
|
52592b |
+ apr_pool_userdata_set((const void *)1, userdata_key,
|
|
|
52592b |
+ apr_pool_cleanup_null, s->process->pool);
|
|
|
52592b |
+ }
|
|
|
52592b |
+ else {
|
|
|
52592b |
+ sd_listen_fds(1);
|
|
|
52592b |
+ }
|
|
|
52592b |
+ }
|
|
|
52592b |
+ else
|
|
|
52592b |
+#endif
|
|
|
52592b |
+ {
|
|
|
52592b |
+ if (open_listeners(s->process->pool)) {
|
|
|
52592b |
+ return 0;
|
|
|
52592b |
+ }
|
|
|
52592b |
}
|
|
|
52592b |
|
|
|
52592b |
for (lr = ap_listeners; lr; lr = lr->next) {
|
|
|
52592b |
@@ -698,7 +844,7 @@ AP_DECLARE(apr_status_t) ap_duplicate_listeners(apr_pool_t *p, server_rec *s,
|
|
|
52592b |
duplr->bind_addr);
|
|
|
52592b |
return stat;
|
|
|
52592b |
}
|
|
|
52592b |
- make_sock(p, duplr);
|
|
|
52592b |
+ make_sock(p, duplr, 1);
|
|
|
52592b |
#if AP_NONBLOCK_WHEN_MULTI_LISTEN
|
|
|
52592b |
use_nonblock = (ap_listeners && ap_listeners->next);
|
|
|
52592b |
stat = apr_socket_opt_set(duplr->sd, APR_SO_NONBLOCK, use_nonblock);
|
|
|
52592b |
@@ -825,6 +971,11 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
|
|
|
52592b |
if (argc < 1 || argc > 2) {
|
|
|
52592b |
return "Listen requires 1 or 2 arguments.";
|
|
|
52592b |
}
|
|
|
52592b |
+#ifdef HAVE_SYSTEMD
|
|
|
52592b |
+ if (use_systemd == -1) {
|
|
|
52592b |
+ use_systemd = sd_listen_fds(0) > 0;
|
|
|
52592b |
+ }
|
|
|
52592b |
+#endif
|
|
|
52592b |
|
|
|
52592b |
rv = apr_parse_addr_port(&host, &scope_id, &port, argv[0], cmd->pool);
|
|
|
52592b |
if (rv != APR_SUCCESS) {
|
|
|
52592b |
@@ -856,6 +1007,12 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
|
|
|
52592b |
ap_str_tolower(proto);
|
|
|
52592b |
}
|
|
|
52592b |
|
|
|
52592b |
+#ifdef HAVE_SYSTEMD
|
|
|
52592b |
+ if (use_systemd) {
|
|
|
52592b |
+ return set_systemd_listener(cmd->server->process, port, proto);
|
|
|
52592b |
+ }
|
|
|
52592b |
+#endif
|
|
|
52592b |
+
|
|
|
52592b |
return alloc_listener(cmd->server->process, host, port, proto, NULL);
|
|
|
52592b |
}
|
|
|
52592b |
|