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