ldap/servers/slapd/daemon.c | 12 ++++++++++++
ldap/servers/slapd/main.c | 21 +++++++++++++++++++++
rpm/389-ds-base.spec.in | 3 ++-
wrappers/systemd.template.service.in | 2 +-
4 files changed, 36 insertions(+), 2 deletions(-)
New commits:
commit ab81e175cedbc4db52ac8a8c3089bdd5165ddb27
Author: William Brown <firstyear(a)redhat.com>
Date: Tue Jan 19 12:57:09 2016 +1000
Ticket 47944 - Implement sd_notify for systemd
Bug Description: Systemd assumes processes are ready as soon as the PID file
is ready.
Fix Description: By calling sd_notify, we are able to tell systemd when we are
actually ready to process requests (or as close to it as possible), and when
we are shutting down.
https://fedorahosted.org/389/ticket/47977
Author: wibrown
Review by: mreynolds (Thanks!)
diff --git a/ldap/servers/slapd/daemon.c b/ldap/servers/slapd/daemon.c
index ba7a39a..4fb85d5 100644
--- a/ldap/servers/slapd/daemon.c
+++ b/ldap/servers/slapd/daemon.c
@@ -1247,6 +1247,14 @@ void slapd_daemon( daemon_ports_t *ports )
/* The server is ready and listening for connections. Logging "slapd started"
message. */
unfurl_banners(the_connection_table,ports,n_tcps,s_tcps,i_unix);
+#ifdef WITH_SYSTEMD
+ sd_notifyf(0, "READY=1\n"
+ "STATUS=slapd started: Ready to process requests\n"
+ "MAINPID=%lu",
+ (unsigned long) getpid()
+ );
+#endif
+
#ifdef ENABLE_NUNC_STANS
if (enable_nunc_stans && ns_thrpool_wait(tp)) {
LDAPDebug( LDAP_DEBUG_ANY,
@@ -1284,6 +1292,10 @@ void slapd_daemon( daemon_ports_t *ports )
/* We get here when the server is shutting down */
/* Do what we have to do before death */
+#ifdef WITH_SYSTEMD
+ sd_notify(0, "STOPPING=1");
+#endif
+
connection_table_abandon_all_operations(the_connection_table); /* abandon all operations
in progress */
if ( ! in_referral_mode ) {
diff --git a/ldap/servers/slapd/main.c b/ldap/servers/slapd/main.c
index 1181628..c04408c 100644
--- a/ldap/servers/slapd/main.c
+++ b/ldap/servers/slapd/main.c
@@ -619,6 +619,27 @@ main( int argc, char **argv)
}
}
+#ifdef WITH_SYSTEMD
+ /*
+ * HUGE WARNING: Systemd has some undocumented magic. with Type=notify, this
+ * acts as type=simple, but waits for ns-slapd to tell systemd it's good to go.
+ * If ns-slapd daemonises, systemd will KILL IT because simple==no forking.
+ *
+ * So instead, we need to work out if we have the NOTIFY_SOCKET env variable
+ * and if we do, we need to prevent forking so systemd doesn't nail us to
+ * the wall.
+ *
+ * Of course, systemd makes NO GUARANTEE that it will be called notify_socket
+ * in the next version, nor that it won't give the variable to a service type
+ * which isn't of the type notify ..... This could all go very wrong :)
+ */
+ char *notify = getenv( "NOTIFY_SOCKET");
+ if (notify) {
+ should_detach = 0;
+ }
+#endif
+
+
/* used to set configfile to the default config file name here */
if ( (myname = strrchr( argv[0], '/' )) == NULL ) {
myname = slapi_ch_strdup( argv[0] );
diff --git a/rpm/389-ds-base.spec.in b/rpm/389-ds-base.spec.in
index 7dfa0b5..163bc33 100644
--- a/rpm/389-ds-base.spec.in
+++ b/rpm/389-ds-base.spec.in
@@ -247,7 +247,8 @@ NUNC_STANS_FLAGS="--enable-nunc-stans
--with-nunc-stans=../nunc-stans-%{nunc_sta
%configure --enable-autobind --with-selinux $OPENLDAP_FLAG $TMPFILES_FLAG \
--with-systemdsystemunitdir=%{_unitdir} \
--with-systemdsystemconfdir=%{_sysconfdir}/systemd/system \
- --with-systemdgroupname=%{groupname} $NSSARGS $NUNC_STANS_FLAGS
+ --with-systemdgroupname=%{groupname} $NSSARGS $NUNC_STANS_FLAGS \
+ --with-systemd
# Generate symbolic info for debuggers
export XCFLAGS=$RPM_OPT_FLAGS
diff --git a/wrappers/systemd.template.service.in b/wrappers/systemd.template.service.in
index f0aa287..4e4cf76 100644
--- a/wrappers/systemd.template.service.in
+++ b/wrappers/systemd.template.service.in
@@ -18,7 +18,7 @@ PartOf=@systemdgroupname@
After=chronyd.service
[Service]
-Type=forking
+Type=notify
EnvironmentFile=@initconfigdir@/@package_name@
EnvironmentFile=@initconfigdir@/@package_name@-%i
ExecStart=@sbindir@/ns-slapd -D @instconfigdir@/slapd-%i -i
@localstatedir@/run/@package_name(a)/slapd-%i.pid -w
@localstatedir@/run/@package_name(a)/slapd-%i.startpid