Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add systemd service notification support #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build/top.mk
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
sysconfdir = @sysconfdir@$(ldap_subdir)
schemadir = $(sysconfdir)/schema
systemdsystemunitdir = @systemdsystemunitdir@

PLAT = @PLAT@
EXEEXT = @EXEEXT@
Expand Down Expand Up @@ -192,6 +193,7 @@ TLS_LIBS = @TLS_LIBS@
AUTH_LIBS = @AUTH_LIBS@
SECURITY_LIBS = $(SASL_LIBS) $(KRB_LIBS) $(TLS_LIBS) $(AUTH_LIBS)
ICU_LIBS = @ICU_LIBS@
SYSTEMD_LIBS = @SYSTEMD_LIBS@

MODULES_CPPFLAGS = @SLAPD_MODULES_CPPFLAGS@
MODULES_LDFLAGS = @SLAPD_MODULES_LDFLAGS@
Expand All @@ -202,7 +204,7 @@ SLAPD_SQL_LDFLAGS = @SLAPD_SQL_LDFLAGS@
SLAPD_SQL_INCLUDES = @SLAPD_SQL_INCLUDES@
SLAPD_SQL_LIBS = @SLAPD_SQL_LIBS@

SLAPD_LIBS = @SLAPD_LIBS@ @SLAPD_PERL_LDFLAGS@ @SLAPD_SQL_LDFLAGS@ @SLAPD_SQL_LIBS@ @SLAPD_SLP_LIBS@ @SLAPD_GMP_LIBS@ $(ICU_LIBS)
SLAPD_LIBS = @SLAPD_LIBS@ @SLAPD_PERL_LDFLAGS@ @SLAPD_SQL_LDFLAGS@ @SLAPD_SQL_LIBS@ @SLAPD_SLP_LIBS@ @SLAPD_GMP_LIBS@ $(ICU_LIBS) $(SYSTEMD_LIBS)

# Our Defaults
CC = $(AC_CC)
Expand Down
57 changes: 52 additions & 5 deletions configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ dnl ----------------------------------------------------------------
dnl General "with" options
OL_ARG_WITH(cyrus_sasl,[ --with-cyrus-sasl with Cyrus SASL support],
auto, [auto yes no] )
OL_ARG_WITH(systemd,[ --with-systemd with systemd service notification support],
auto, [auto yes no] )
OL_ARG_WITH(fetch,[ --with-fetch with fetch(3) URL support],
auto, [auto yes no] )
OL_ARG_WITH(threads,[ --with-threads with threads],
Expand Down Expand Up @@ -615,6 +617,7 @@ SLAPI_LIBS=
LIBSLAPI=
AUTH_LIBS=
ICU_LIBS=
SYSTEMD_LIBS=

SLAPD_SLP_LIBS=
SLAPD_GMP_LIBS=
Expand Down Expand Up @@ -690,6 +693,7 @@ fi
AC_LIBTOOL_WIN32_DLL
AC_LIBTOOL_DLOPEN
AC_PROG_LIBTOOL
AC_PATH_PROG(PKG_CONFIG, pkg-config, no)

dnl ----------------------------------------------------------------
dnl Perl
Expand Down Expand Up @@ -2104,12 +2108,13 @@ dnl ----------------------------------------------------------------
dnl WiredTiger
ol_link_wt=no
if test $ol_enable_wt != no ; then
AC_CHECK_PROG(PKGCONFIG,pkg-config,yes)
if test "$PKGCONFIG" != yes ; then
AC_MSG_ERROR([could not locate pkg-config])
if test "$PKG_CONFIG" != no; then
WT_INCS=`$PKG_CONFIG --cflags wiredtiger`
WT_LIBS=`$PKG_CONFIG --libs wiredtiger`
else
WT_INCS=
WT_LIBS="-lwiredtiger"
fi
WT_INCS=`pkg-config --cflags wiredtiger`
WT_LIBS=`pkg-config --libs wiredtiger`

save_CFLAGS="$CFLAGS"
save_LDFLAGS="$LDFLAGS"
Expand Down Expand Up @@ -2192,6 +2197,46 @@ else
fi
fi

dnl ----------------------------------------------------------------
dnl
dnl Check for systemd
dnl
WITH_SYSTEMD=no
systemdsystemunitdir=
ol_link_systemd=no
if test $ol_with_systemd != no ; then
AC_CHECK_HEADERS(systemd/sd-daemon.h)

if test $ac_cv_header_systemd_sd_daemon_h = yes; then
AC_CHECK_LIB(systemd, sd_notify,
[ol_link_systemd="-lsystemd"])
fi

if test $ol_link_systemd = no ; then
if test $ol_with_systemd != auto ; then
AC_MSG_ERROR([Could not locate systemd])
else
AC_MSG_WARN([Could not locate systemd])
AC_MSG_WARN([systemd service notification not supported!])
fi
else
AC_DEFINE(HAVE_SYSTEMD,1,[define if you have systemd])
SYSTEMD_LIBS="$ol_link_systemd"
WITH_SYSTEMD=yes
fi
fi

if test $WITH_SYSTEMD = yes; then
if test "$PKG_CONFIG" != no ; then
systemdsystemunitdir=`$PKG_CONFIG --variable=systemdsystemunitdir systemd`
elif test -d /usr/lib/systemd/system; then
systemdsystemunitdir=/usr/lib/systemd/system
else
systemdsystemunitdir=/lib/systemd/system
fi
fi
AC_SUBST(systemdsystemunitdir)

dnl ----------------------------------------------------------------
dnl Check for entropy sources
if test $cross_compiling != yes && test "$ac_cv_mingw32" != yes ; then
Expand Down Expand Up @@ -3167,6 +3212,7 @@ AC_SUBST(WITH_SASL)
AC_SUBST(WITH_TLS)
AC_SUBST(WITH_MODULES_ENABLED)
AC_SUBST(WITH_ACI_ENABLED)
AC_SUBST(WITH_SYSTEMD)
AC_SUBST(BUILD_THREAD)
AC_SUBST(BUILD_LIBS_DYNAMIC)

Expand Down Expand Up @@ -3248,6 +3294,7 @@ AC_SUBST(SLAPI_LIBS)
AC_SUBST(LIBSLAPI)
AC_SUBST(AUTH_LIBS)
AC_SUBST(ICU_LIBS)
AC_SUBST(SYSTEMD_LIBS)

AC_SUBST(SLAPD_SLP_LIBS)
AC_SUBST(SLAPD_GMP_LIBS)
Expand Down
6 changes: 6 additions & 0 deletions include/portable.hin
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,12 @@
/* Define to 1 if you have the <syslog.h> header file. */
#undef HAVE_SYSLOG_H

/* define if you have systemd */
#undef HAVE_SYSTEMD

/* Define to 1 if you have the <systemd/sd-daemon.h> header file. */
#undef HAVE_SYSTEMD_SD_DAEMON_H

/* Define to 1 if you have the <sys/devpoll.h> header file. */
#undef HAVE_SYS_DEVPOLL_H

Expand Down
14 changes: 12 additions & 2 deletions servers/slapd/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,15 @@ all-cffiles: slapd $(SLAPD_DYNAMIC_BACKENDS) dynamic_overlays
$(SED) -e "s;%SYSCONFDIR%;$$sysconfdir;" \
-e "s;%LOCALSTATEDIR%;$$localstatedir;" \
-e "s;%MODULEDIR%;$$moduledir;" \
$(srcdir)/slapd.conf > slapd.conf.tmp ; \
$(srcdir)/slapd.conf > slapd.conf.tmp || exit $$?; \
$(SED) -e "s;%SYSCONFDIR%;$$sysconfdir;" \
-e "s;%LOCALSTATEDIR%;$$localstatedir;" \
-e "s;%MODULEDIR%;$$moduledir;" \
$(srcdir)/slapd.ldif > slapd.ldif.tmp ; \
$(srcdir)/slapd.ldif > slapd.ldif.tmp || exit $$?
@if test -n "$(systemdsystemunitdir)"; then \
$(SED) -e "s;%LIBEXECDIR%;$(libexecdir);" \
$(srcdir)/slapd.service > slapd.service.tmp ; \
fi
touch all-cffiles

install-schema: FORCE
Expand Down Expand Up @@ -442,6 +446,12 @@ install-conf: FORCE
else \
echo "PRESERVING EXISTING CONFIGURATION FILE $(DESTDIR)$(sysconfdir)/slapd.ldif" ; \
fi
if test -n "$(systemdsystemunitdir)" && test ! -f $(DESTDIR)$(systemdsystemunitdir)/slapd.service; then \
$(MKDIR) $(DESTDIR)$(systemdsystemunitdir); \
echo "installing slapd.service in $(systemdsystemunitdir)"; \
echo "$(INSTALL) $(INSTALLFLAGS) -m 644 slapd.service.tmp $(DESTDIR)$(systemdsystemunitdir)/slapd.service"; \
$(INSTALL) $(INSTALLFLAGS) -m 644 slapd.service.tmp $(DESTDIR)$(systemdsystemunitdir)/slapd.service; \
fi

install-db-config: FORCE
@-$(MKDIR) $(DESTDIR)$(localstatedir) $(DESTDIR)$(sysconfdir)
Expand Down
29 changes: 29 additions & 0 deletions servers/slapd/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

#include "ldap_rq.h"

#ifdef HAVE_SYSTEMD_SD_DAEMON_H
#include <systemd/sd-daemon.h>
#endif

#ifdef HAVE_POLL
#include <poll.h>
#endif
Expand Down Expand Up @@ -88,6 +92,9 @@ int slapd_tcp_rmem;
int slapd_tcp_wmem;
#endif /* LDAP_TCP_BUFFER */

static ldap_pvt_thread_mutex_t listener_init_mutex;
static ldap_pvt_thread_cond_t listener_init_cond;

Listener **slap_listeners = NULL;
static volatile sig_atomic_t listening = 1; /* 0 when slap_listeners closed */
static ldap_pvt_thread_t *listener_tid;
Expand Down Expand Up @@ -2303,6 +2310,8 @@ slapd_daemon_task(
"daemon: listen(%s, 5) failed errno=%d (%s)\n",
slap_listeners[l]->sl_url.bv_val, err,
sock_errstr(err) );
slapd_shutdown = 2;
ldap_pvt_thread_cond_signal( &listener_init_cond );
return (void*)-1;
}

Expand All @@ -2312,12 +2321,15 @@ slapd_daemon_task(
"set nonblocking on a listening socket failed\n",
0, 0, 0 );
slapd_shutdown = 2;
ldap_pvt_thread_cond_signal( &listener_init_cond );
return (void*)-1;
}

slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l], -1 );
}

ldap_pvt_thread_cond_signal( &listener_init_cond );

#ifdef HAVE_NT_SERVICE_MANAGER
if ( started_event != NULL ) {
ldap_pvt_thread_cond_signal( &started_event );
Expand Down Expand Up @@ -2892,6 +2904,9 @@ slapd_daemon( void )
SLAP_SOCK_INIT(i);
}

ldap_pvt_thread_mutex_init( &listener_init_mutex );
ldap_pvt_thread_cond_init( &listener_init_cond );

for ( i=0; i<slapd_daemon_threads; i++ )
{
/* listener as a separate THREAD */
Expand All @@ -2905,6 +2920,20 @@ slapd_daemon( void )
}
}

ldap_pvt_thread_cond_wait( &listener_init_cond, &listener_init_mutex );
if ( slapd_shutdown ) {
Debug( LDAP_DEBUG_ANY,
"listener initialization failed\n", 0, 0, 0 );
return 1;
}

#ifdef HAVE_SYSTEMD
rc = sd_notify( 1, "READY=1" );
if ( rc < 0 )
Debug( LDAP_DEBUG_ANY,
"systemd sd_notify failed (%d)\n", rc, 0, 0 );
#endif /* HAVE_SYSTEMD */

/* wait for the listener threads to complete */
for ( i=0; i<slapd_daemon_threads; i++ )
ldap_pvt_thread_join( listener_tid[i], (void *)NULL );
Expand Down
11 changes: 11 additions & 0 deletions servers/slapd/slapd.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=OpenLDAP Server
After=syslog.target network-online.target
Documentation=man:slapd

[Service]
Type=notify
ExecStart=%LIBEXECDIR%/slapd -d 0 -h "ldapi:/// ldap:///"

[Install]
WantedBy=multi-user.target