Skip to content

Commit db0631e

Browse files
committed
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 apache#312 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1899784 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4c8b180 commit db0631e

File tree

5 files changed

+80
-37
lines changed

5 files changed

+80
-37
lines changed

CHANGES

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
-*- coding: utf-8 -*-
22
Changes with Apache 2.5.1
33

4+
*) mod_systemd: Systemd socket activation can now be enabled at
5+
build time but disabled at run time, if mod_systemd is not
6+
loaded. [Lubos Uhliarik <luhliari redhat.com>]
7+
48
*) ab: Add an optional ramp delay when starting concurrent connections so
59
as to not trigger denial of service protection in the network. Report
610
levels of concurrency achieved in cases where the test completes before

acinclude.m4

-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,6 @@ case $host in
831831
if test "${ac_cv_header_systemd_sd_daemon_h}" = "no"; then
832832
AC_MSG_WARN([Your system does not support systemd.])
833833
else
834-
APR_ADDTO(HTTPD_LIBS, [$SYSTEMD_LIBS])
835834
AC_DEFINE(HAVE_SYSTEMD, 1, [Define if systemd is supported])
836835
fi
837836
fi

include/ap_listen.h

+10
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" {
@@ -161,6 +162,15 @@ AP_DECLARE_NONSTD(const char *) ap_set_accept_errors_nonfatal(cmd_parms *cmd,
161162
void *dummy,
162163
int flag);
163164

165+
#ifdef HAVE_SYSTEMD
166+
APR_DECLARE_OPTIONAL_FN(int,
167+
ap_find_systemd_socket, (process_rec *, apr_port_t));
168+
169+
APR_DECLARE_OPTIONAL_FN(int,
170+
ap_systemd_listen_fds, (int));
171+
#endif
172+
173+
164174
#define LISTEN_COMMANDS \
165175
AP_INIT_TAKE1("ListenBacklog", ap_set_listenbacklog, NULL, RSRC_CONF, \
166176
"Maximum length of the queue of pending connections, as used by listen(2)"), \

modules/arch/unix/mod_systemd.c

+40
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

+26-36
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@
3535
#include <unistd.h>
3636
#endif
3737

38-
#ifdef HAVE_SYSTEMD
39-
#include <systemd/sd-daemon.h>
40-
#endif
41-
4238
/* we know core's module_index is 0 */
4339
#undef APLOG_MODULE_INDEX
4440
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
@@ -309,34 +305,6 @@ static apr_status_t close_listeners_on_exec(void *v)
309305

310306
#ifdef HAVE_SYSTEMD
311307

312-
static int find_systemd_socket(process_rec * process, apr_port_t port)
313-
{
314-
int fdcount, fd;
315-
int sdc = sd_listen_fds(0);
316-
317-
if (sdc < 0) {
318-
ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02486)
319-
"find_systemd_socket: Error parsing environment, sd_listen_fds returned %d",
320-
sdc);
321-
return -1;
322-
}
323-
324-
if (sdc == 0) {
325-
ap_log_perror(APLOG_MARK, APLOG_CRIT, sdc, process->pool, APLOGNO(02487)
326-
"find_systemd_socket: At least one socket must be set.");
327-
return -1;
328-
}
329-
330-
fdcount = atoi(getenv("LISTEN_FDS"));
331-
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fdcount; fd++) {
332-
if (sd_is_socket_inet(fd, 0, 0, -1, port) > 0) {
333-
return fd;
334-
}
335-
}
336-
337-
return -1;
338-
}
339-
340308
static apr_status_t alloc_systemd_listener(process_rec * process,
341309
int fd, const char *proto,
342310
ap_listen_rec **out_rec)
@@ -395,7 +363,16 @@ static const char *set_systemd_listener(process_rec *process, apr_port_t port,
395363
{
396364
ap_listen_rec *last, *new;
397365
apr_status_t rv;
398-
int fd = find_systemd_socket(process, port);
366+
APR_OPTIONAL_FN_TYPE(ap_find_systemd_socket) *find_systemd_socket;
367+
int fd;
368+
369+
find_systemd_socket = APR_RETRIEVE_OPTIONAL_FN(ap_find_systemd_socket);
370+
371+
if (!find_systemd_socket)
372+
return "Systemd socket activation is used, but mod_systemd is probably "
373+
"not loaded";
374+
375+
fd = find_systemd_socket(process, port);
399376
if (fd < 0) {
400377
return "Systemd socket activation is used, but this port is not "
401378
"configured in systemd";
@@ -420,7 +397,6 @@ static const char *set_systemd_listener(process_rec *process, apr_port_t port,
420397

421398
return NULL;
422399
}
423-
424400
#endif /* HAVE_SYSTEMD */
425401

426402
/* Returns non-zero if socket address SA matches hostname, port and
@@ -761,6 +737,9 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
761737
int num_listeners = 0;
762738
const char* proto;
763739
int found;
740+
#ifdef HAVE_SYSTEMD
741+
APR_OPTIONAL_FN_TYPE(ap_systemd_listen_fds) *systemd_listen_fds;
742+
#endif
764743

765744
for (ls = s; ls; ls = ls->next) {
766745
proto = ap_get_server_protocol(ls);
@@ -800,7 +779,10 @@ AP_DECLARE(int) ap_setup_listeners(server_rec *s)
800779
apr_pool_cleanup_null, s->process->pool);
801780
}
802781
else {
803-
sd_listen_fds(1);
782+
systemd_listen_fds = APR_RETRIEVE_OPTIONAL_FN(ap_systemd_listen_fds);
783+
if (systemd_listen_fds != NULL) {
784+
systemd_listen_fds(1);
785+
}
804786
}
805787
}
806788
else
@@ -1070,6 +1052,9 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
10701052
apr_status_t rv;
10711053
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
10721054
apr_uint32_t flags = 0;
1055+
#ifdef HAVE_SYSTEMD
1056+
APR_OPTIONAL_FN_TYPE(ap_systemd_listen_fds) *systemd_listen_fds;
1057+
#endif
10731058

10741059
if (err != NULL) {
10751060
return err;
@@ -1080,7 +1065,12 @@ AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
10801065
}
10811066
#ifdef HAVE_SYSTEMD
10821067
if (use_systemd == -1) {
1083-
use_systemd = sd_listen_fds(0) > 0;
1068+
systemd_listen_fds = APR_RETRIEVE_OPTIONAL_FN(ap_systemd_listen_fds);
1069+
if (systemd_listen_fds != NULL) {
1070+
use_systemd = systemd_listen_fds(0) > 0;
1071+
} else {
1072+
use_systemd = 0;
1073+
}
10841074
}
10851075
#endif
10861076

0 commit comments

Comments
 (0)