Auto-detect and link libsystemd at compile-time

This adds Makefile/build-system support for USE_SYSTEMD=(yes|no|*). This
variable's value determines whether or not libsystemd will be linked at
build-time.

If USE_SYSTEMD is set to "yes", make will use PKG_CONFIG to check for
libsystemd's presence, and fail the build early if it isn't
installed/detected properly.

If USE_SYSTEM is set to "no", libsystemd will *not* be linked, even if
support for it is available on the system redis is being built on.

For any other value that USE_SYSTEM might assume (e.g. "auto"),
PKG_CONFIG will try to determine libsystemd's presence, and set up the
build process to link against it, if it was indicated as being
installed/available.

This approach has a number of repercussions of its own, most importantly
the following: If you build redis on a system that actually has systemd
support, but no libsystemd-dev package(s) installed, you'll end up
*without* support for systemd notification/status reporting support in
redis-server. This changes established runtime behaviour.

I'm not sure if the build system and/or the server binary should
indicate this. I'm also wondering if not actually having
systemd-notify-support, but requesting it via the server's config,
should result in a fatal error now.
This commit is contained in:
Johannes Truschnigg
2019-05-30 18:44:17 +02:00
committed by max ulidtko
parent ec5681f0f1
commit 129d14e143
3 changed files with 34 additions and 27 deletions

View File

@ -55,7 +55,6 @@
#include <sys/utsname.h>
#include <locale.h>
#include <sys/socket.h>
#include <dlfcn.h>
/* Our shared "common" objects */
@ -4876,37 +4875,16 @@ int redisSupervisedUpstart(void) {
int redisCommunicateSystemd(const char *sd_notify_msg) {
const char *notify_socket = getenv("NOTIFY_SOCKET");
int (*dl_sd_notify)(int unset_environment, const char *state);
char *error;
void *handle;
if (!notify_socket) {
serverLog(LL_WARNING,
"systemd supervision requested, but NOTIFY_SOCKET not found");
return 0;
}
handle = dlopen("libsystemd.so.0", RTLD_LAZY);
if (!handle) {
serverLog(LL_WARNING,
"systemd supervision requested, but could not dlopen() libsystemd.so");
(void) dlerror();
return 0;
}
(void) dlerror();
*(void **)(&dl_sd_notify) = dlsym(handle, "sd_notify");
error = dlerror();
if (error != NULL) {
serverLog(LL_WARNING,
"systemd supervision requested, but could not load sd_notify(3) from libsystemd.so");
dlclose(handle);
return 0;
}
(void) (*dl_sd_notify)(0, sd_notify_msg);
dlclose(handle);
#ifdef HAVE_LIBSYSTEMD
(void) sd_notify(0, sd_notify_msg);
#else
UNUSED(sd_notify_msg);
#endif
return 0;
}