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
 
   9 +#include <systemd/sd-daemon.h>
 
  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;
 
  20 +static int use_systemd = -1;
 
  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)
 
  27      apr_socket_t *s = server->sd;
 
  29 @@ -94,20 +101,6 @@ static apr_status_t make_sock(apr_pool_t
 
  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: "
 
  41 -            apr_socket_close(s);
 
  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
 
  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",
 
  58 -        apr_socket_close(s);
 
  61 +    if (do_bind_listen) {
 
  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: "
 
  70 +                apr_socket_close(s);
 
  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 "
 
  81 -        apr_socket_close(s);
 
  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",
 
  87 +            apr_socket_close(s);
 
  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 "
 
  96 +            apr_socket_close(s);
 
 102 @@ -277,6 +286,123 @@ static apr_status_t close_listeners_on_e
 
 108 +static int find_systemd_socket(process_rec * process, apr_port_t port) {
 
 110 +    int sdc = sd_listen_fds(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",
 
 120 +        ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
 
 121 +                      "find_systemd_socket: At least one socket must be set.");
 
 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) {
 
 135 +static apr_status_t alloc_systemd_listener(process_rec * process,
 
 136 +                                           int fd, const char *proto,
 
 137 +                                           ap_listen_rec **out_rec)
 
 140 +    struct sockaddr sa;
 
 141 +    socklen_t len = sizeof(struct sockaddr);
 
 142 +    apr_os_sock_info_t si;
 
 143 +    ap_listen_rec *rec;
 
 146 +    memset(&si, 0, sizeof(si));
 
 148 +    rv = getsockname(fd, &sa, &len);
 
 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);
 
 158 +    si.family = sa.sa_family;
 
 160 +    si.type = SOCK_STREAM;
 
 161 +    si.protocol = APR_PROTO_TCP;
 
 163 +    rec = apr_palloc(process->pool, sizeof(ap_listen_rec));
 
 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);
 
 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);
 
 182 +    rec->protocol = apr_pstrdup(process->pool, proto);
 
 186 +    return make_sock(process->pool, rec, 0);
 
 189 +static const char *set_systemd_listener(process_rec *process, apr_port_t port,
 
 192 +    ap_listen_rec *last, *new;
 
 194 +    int fd = find_systemd_socket(process, port);
 
 196 +        return "Systemd socket activation is used, but this port is not "
 
 197 +                "configured in systemd";
 
 200 +    last = ap_listeners;
 
 201 +    while (last && last->next) {
 
 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";
 
 210 +    if (last == NULL) {
 
 211 +        ap_listeners = last = new;
 
 221 +#endif /* HAVE_SYSTEMD */
 
 223  static int find_listeners(ap_listen_rec **from, ap_listen_rec **to,
 
 224                            const char *addr, apr_port_t port)
 
 226 @@ -495,7 +621,7 @@ static int open_listeners(apr_pool_t *po
 
 230 -            if (make_sock(pool, lr) == APR_SUCCESS) {
 
 231 +            if (make_sock(pool, lr, 1) == APR_SUCCESS) {
 
 235 @@ -607,8 +733,28 @@ AP_DECLARE(int) ap_setup_listeners(serve
 
 239 -    if (open_listeners(s->process->pool)) {
 
 243 +        const char *userdata_key = "ap_open_systemd_listeners";
 
 245 +        /* clear the enviroment on our second run
 
 246 +        * so that none of our future children get confused.
 
 248 +        apr_pool_userdata_get(&data, userdata_key, s->process->pool);
 
 250 +            apr_pool_userdata_set((const void *)1, userdata_key,
 
 251 +                                apr_pool_cleanup_null, s->process->pool);
 
 260 +        if (open_listeners(s->process->pool)) {
 
 265      for (lr = ap_listeners; lr; lr = lr->next) {
 
 266 @@ -698,7 +844,7 @@ AP_DECLARE(apr_status_t) ap_duplicate_li
 
 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.";
 
 280 +    if (use_systemd == -1) {
 
 281 +        use_systemd = sd_listen_fds(0) > 0;
 
 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);
 
 293 +        return set_systemd_listener(cmd->server->process, port, proto);
 
 297      return alloc_listener(cmd->server->process, host, port, proto, NULL);