static int send_buffer_size;
static int receive_buffer_size;
#ifdef HAVE_SYSTEMD
-static int use_systemd;
+static int use_systemd = -1;
#endif
/* TODO: make_sock is just begging and screaming for APR abstraction */
#ifdef HAVE_SYSTEMD
+static int find_systemd_socket(process_rec * process, apr_port_t port)
+{
+ int fdcount, fd;
+ int sdc = sd_listen_fds(0);
+
+ if (sdc < 0) {
+ ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
+ "find_systemd_socket: Error parsing enviroment, sd_listen_fds returned %d",
+ sdc);
+ return -1;
+ }
+
+ if (sdc == 0) {
+ ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
+ "find_systemd_socket: At least one socket must be set.");
+ return -1;
+ }
+
+ fdcount = atoi(getenv("LISTEN_FDS"));
+ for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
+ if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
+ return fd;
+ }
+ }
+
+ return -1;
+}
+
static apr_status_t alloc_systemd_listener(process_rec * process,
- int fd,
+ int fd, const char *proto,
ap_listen_rec **out_rec)
{
apr_status_t rv;
rec->active = 0;
rec->next = 0;
-
rv = apr_os_sock_make(&rec->sd, &si, process->pool);
if (rv != APR_SUCCESS) {
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02490)
return rv;
}
- if (rec->bind_addr->port == 443) {
- rec->protocol = apr_pstrdup(process->pool, "https");
- } else {
- rec->protocol = apr_pstrdup(process->pool, "http");
- }
+ rec->protocol = apr_pstrdup(process->pool, proto);
*out_rec = rec;
return make_sock(process->pool, rec, 0);
}
-static int open_systemd_listeners(process_rec *process)
+static const char *set_systemd_listener(process_rec *process, apr_port_t port,
+ const char *proto)
{
ap_listen_rec *last, *new;
- int fdcount, fd;
apr_status_t rv;
- void *data;
- const char *userdata_key = "ap_systemd_listeners";
- int sdc = sd_listen_fds(0);
-
- if (sdc < 0) {
- ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
- "open_systemd_listeners: Error parsing enviroment, sd_listen_fds returned %d",
- sdc);
- return 1;
- }
-
- if (sdc == 0) {
- ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
- "open_systemd_listeners: At least one socket must be set.");
- return 1;
+ int fd = find_systemd_socket(process, port);
+ if (fd < 0) {
+ return "Systemd socket activation is used, but this port is not "
+ "configured in systemd";
}
last = ap_listeners;
last = last->next;
}
- fdcount = atoi(getenv("LISTEN_FDS"));
-
- for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
- rv = alloc_systemd_listener(process, fd, &new);
-
- if (rv != APR_SUCCESS) {
- ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, process->pool, APLOGNO(02488)
- "open_systemd_listeners: failed to setup socket %d.", fd);
- return 1;
- }
-
- if (last == NULL) {
- ap_listeners = last = new;
- }
- else {
- last->next = new;
- last = new;
- }
+ rv = alloc_systemd_listener(process, fd, proto, &new);
+ if (rv != APR_SUCCESS) {
+ return "Failed to setup socket passed by systemd using socket activation";
}
- /* clear the enviroment on our second run
- * so that none of our future children get confused.
- */
- apr_pool_userdata_get(&data, userdata_key, process->pool);
- if (!data) {
- apr_pool_userdata_set((const void *)1, userdata_key,
- apr_pool_cleanup_null, process->pool);
- }
- else {
- sd_listen_fds(1);
- }
-
+ if (last == NULL) {
+ ap_listeners = last = new;
+ }
+ else {
+ last->next = new;
+ last = new;
+ }
- return 0;
+ return NULL;
}
#endif /* HAVE_SYSTEMD */
#ifdef HAVE_SYSTEMD
if (use_systemd) {
- if (open_systemd_listeners(s->process) != 0) {
- return 0;
+ const char *userdata_key = "ap_open_systemd_listeners";
+ void *data;
+ /* clear the enviroment on our second run
+ * so that none of our future children get confused.
+ */
+ apr_pool_userdata_get(&data, userdata_key, s->process->pool);
+ if (!data) {
+ apr_pool_userdata_set((const void *)1, userdata_key,
+ apr_pool_cleanup_null, s->process->pool);
}
+ else {
+ sd_listen_fds(1);
+ }
}
else
#endif
if (use_systemd) {
int thesock;
apr_os_sock_get(&thesock, lr->sd);
- if ((stat = alloc_systemd_listener(p, thesock, &duplr))
- != APR_SUCCESS) {
+ if ((stat = alloc_systemd_listener(s->process, thesock,
+ lr->protocol, &duplr)) != APR_SUCCESS) {
return stat;
}
}
if (argc < 1 || argc > 2) {
return "Listen requires 1 or 2 arguments.";
}
-
- if (strcmp("systemd", argv[0]) == 0) {
#ifdef HAVE_SYSTEMD
- use_systemd = 1;
- if (ap_listeners != NULL) {
- return "systemd socket activation support must be used exclusive of normal listeners.";
- }
- return NULL;
-#else
- return "systemd support was not compiled in.";
-#endif
- }
-
-#ifdef HAVE_SYSTEMD
- if (use_systemd) {
- return "systemd socket activation support must be used exclusive of normal listeners.";
+ if (use_systemd == -1) {
+ use_systemd = sd_listen_fds(0) > 0;
}
#endif
ap_str_tolower(proto);
}
+#ifdef HAVE_SYSTEMD
+ if (use_systemd) {
+ return set_systemd_listener(cmd->server->process, port, proto);
+ }
+#endif
+
return alloc_listener(cmd->server->process, host, port, proto, NULL);
}