Skip to content

Commit dd11e3b

Browse files
notrojrpluem
authored andcommitted
Remove libsystemd dependency from main httpd binary
Until this change httpd was linking libsystemd to the main httpd binary. If you want to run lightweight version of httpd in container, sometimes you just want to install httpd binary with as little dependencies as possible to make container small in size and do not pull uncencessary dependencies and libraries. This change will move all systemd library calls from listen.c to mod_systemd module and remove systemd linking from the main httpd bin. Fixed mixed declaration and wrongly declared variable. Submitted by: Luboš Uhliarik <luhliari redhat.com> Github: closes #312 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1899784 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit db0631e)
1 parent 32cafa3 commit dd11e3b

File tree

4 files changed

+80
-32
lines changed

4 files changed

+80
-32
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*) mod_systemd: Systemd socket activation can now be enabled at
2+
build time but disabled at run time, if mod_systemd is not
3+
loaded. [Lubos Uhliarik <luhliari redhat.com>]

include/ap_listen.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "apr_network_io.h"
3030
#include "httpd.h"
3131
#include "http_config.h"
32+
#include "apr_optional.h"
3233

3334
#ifdef __cplusplus
3435
extern "C" {
@@ -143,6 +144,15 @@ AP_DECLARE_NONSTD(const char *) ap_set_receive_buffer_size(cmd_parms *cmd,
143144
void *dummy,
144145
const char *arg);
145146

147+
#ifdef HAVE_SYSTEMD
148+
APR_DECLARE_OPTIONAL_FN(int,
149+
ap_find_systemd_socket, (process_rec *, apr_port_t));
150+
151+
APR_DECLARE_OPTIONAL_FN(int,
152+
ap_systemd_listen_fds, (int));
153+
#endif
154+
155+
146156
#define LISTEN_COMMANDS \
147157
AP_INIT_TAKE1("ListenBacklog", ap_set_listenbacklog, NULL, RSRC_CONF, \
148158
"Maximum length of the queue of pending connections, as used by listen(2)"), \

modules/arch/unix/mod_systemd.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
#include <unistd.h>
3535
#endif
3636

37+
APR_DECLARE_OPTIONAL_FN(int,
38+
ap_find_systemd_socket, (process_rec *, apr_port_t));
39+
40+
APR_DECLARE_OPTIONAL_FN(int,
41+
ap_systemd_listen_fds, (int));
42+
3743
static int systemd_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
3844
apr_pool_t *ptemp)
3945
{
@@ -96,8 +102,42 @@ static int systemd_monitor(apr_pool_t *p, server_rec *s)
96102
return DECLINED;
97103
}
98104

105+
static int ap_find_systemd_socket(process_rec * process, apr_port_t port) {
106+
int fdcount, fd;
107+
int sdc = sd_listen_fds(0);
108+
109+
if (sdc < 0) {
110+
ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
111+
"find_systemd_socket: Error parsing enviroment, sd_listen_fds returned %d",
112+
sdc);
113+
return -1;
114+
}
115+
116+
if (sdc == 0) {
117+
ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
118+
"find_systemd_socket: At least one socket must be set.");
119+
return -1;
120+
}
121+
122+
fdcount = atoi(getenv("LISTEN_FDS"));
123+
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
124+
if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
125+
return fd;
126+
}
127+
}
128+
129+
return -1;
130+
}
131+
132+
static int ap_systemd_listen_fds(int unset_environment){
133+
return sd_listen_fds(unset_environment);
134+
}
135+
99136
static void systemd_register_hooks(apr_pool_t *p)
100137
{
138+
APR_REGISTER_OPTIONAL_FN(ap_systemd_listen_fds);
139+
APR_REGISTER_OPTIONAL_FN(ap_find_systemd_socket);
140+
101141
/* Enable ap_extended_status. */
102142
ap_hook_pre_config(systemd_pre_config, NULL, NULL, APR_HOOK_LAST);
103143
/* Signal service is ready. */

server/listen.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -283,34 +283,7 @@ static apr_status_t close_listeners_on_exec(void *v)
283283
return APR_SUCCESS;
284284
}
285285

286-
#ifdef AP_SYSTEMD_SUPPORT
287-
288-
static int find_systemd_socket(process_rec * process, apr_port_t port) {
289-
int fdcount, fd;
290-
int sdc = sd_listen_fds(0);
291-
292-
if (sdc < 0) {
293-
ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
294-
"find_systemd_socket: Error parsing enviroment, sd_listen_fds returned %d",
295-
sdc);
296-
return 1;
297-
}
298-
299-
if (sdc == 0) {
300-
ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
301-
"find_systemd_socket: At least one socket must be set.");
302-
return 1;
303-
}
304-
305-
fdcount = atoi(getenv("LISTEN_FDS"));
306-
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
307-
if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
308-
return fd;
309-
}
310-
}
311-
312-
return -1;
313-
}
286+
#ifdef HAVE_SYSTEMD
314287

315288
static apr_status_t alloc_systemd_listener(process_rec * process,
316289
int fd, const char *proto,
@@ -371,7 +344,16 @@ static const char *set_systemd_listener(process_rec *process, apr_port_t port,
371344
{
372345
ap_listen_rec *last, *new;
373346
apr_status_t rv;
374-
int fd = find_systemd_socket(process, port);
347+
APR_OPTIONAL_FN_TYPE(ap_find_systemd_socket) *find_systemd_socket;
348+
int fd;
349+
350+
find_systemd_socket = APR_RETRIEVE_OPTIONAL_FN(ap_find_systemd_socket);
351+
352+
if (!find_systemd_socket)
353+
return "Systemd socket activation is used, but mod_systemd is probably "
354+
"not loaded";
355+
356+
fd = find_systemd_socket(process, port);
375357
if (fd < 0) {
376358
return "Systemd socket activation is used, but this port is not "
377359
"configured in systemd";
@@ -397,7 +379,6 @@ static const char *set_systemd_listener(process_rec *process, apr_port_t port,
397379

398380
return NULL;
399381
}
400-
401382
#endif /* HAVE_SYSTEMD */
402383

403384
/* Returns non-zero if socket address SA matches hostname, port and
@@ -737,6 +718,9 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
737718
int num_listeners = 0;
738719
const char* proto;
739720
int found;
721+
#ifdef HAVE_SYSTEMD
722+
APR_OPTIONAL_FN_TYPE(ap_systemd_listen_fds) *systemd_listen_fds;
723+
#endif
740724

741725
for (ls = s; ls; ls = ls->next) {
742726
proto = ap_get_server_protocol(ls);
@@ -777,7 +761,10 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
777761
apr_pool_cleanup_null, s->process->pool);
778762
}
779763
else {
780-
sd_listen_fds(1);
764+
systemd_listen_fds = APR_RETRIEVE_OPTIONAL_FN(ap_systemd_listen_fds);
765+
if (systemd_listen_fds != NULL) {
766+
systemd_listen_fds(1);
767+
}
781768
}
782769
}
783770
else
@@ -1006,6 +993,9 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
1006993
apr_port_t port;
1007994
apr_status_t rv;
1008995
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
996+
#ifdef HAVE_SYSTEMD
997+
APR_OPTIONAL_FN_TYPE(ap_systemd_listen_fds) *systemd_listen_fds;
998+
#endif
1009999

10101000
if (err != NULL) {
10111001
return err;
@@ -1016,7 +1006,12 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
10161006
}
10171007
#ifdef HAVE_SYSTEMD
10181008
if (use_systemd == -1) {
1019-
use_systemd = sd_listen_fds(0) > 0;
1009+
systemd_listen_fds = APR_RETRIEVE_OPTIONAL_FN(ap_systemd_listen_fds);
1010+
if (systemd_listen_fds != NULL) {
1011+
use_systemd = systemd_listen_fds(0) > 0;
1012+
} else {
1013+
use_systemd = 0;
1014+
}
10201015
}
10211016
#endif
10221017

0 commit comments

Comments
 (0)