sd_notify support for clvmd
Hello,
The following patch adds sd_notify support to clvmd. I needed this because systemd doesn't handle re-execing forking daemons well ('clvmd -S' would cause systemd to kill the clvmd.service) and just using the '-f' option to clvmd to run in foreground would mean loosing any startup notification.
Even if not this re-exec problem, running clvmd in the foreground and reporting via sd_notify still is probably the preferable way to run it under systemd.
The sd_notify support will be enabled whenever libsystemd-daemon library is available, unless explicitly disabled via '--disable-sd-notify' configure option.
To use this feature, use the following in the clvmd.service file:
[Service]
Type=notify
NotifyAccess=main
ExecStart=/usr/sbin/clvmd -f $CLVMDOPTS
There is one limitation: the -T<secs> (startup timeout) function is not effective when clvmd runs in foreground. I was thinking about re-implementing it with a thread calling sd_notify() after sleep(), but this would complicate the patch a lot.
Greets
Jacek
PATCH 1/1 sd_notify(3) support in clvmd
Use sd_notify() function from the libsystemd-daemon library to report startup progress of clvmd.
This fearture can be disabled via the configure script.
--- configure.in | 28 ++++++++++++++++++++++++++++ daemons/clvmd/Makefile.in | 7 +++++++ daemons/clvmd/clvmd.c | 32 ++++++++++++++++++++++++++++++++ lib/misc/configure.h.in | 3 +++ 4 files changed, 70 insertions(+), 0 deletions(-)
configure.in +28 -0
@@ -757,6 +757,34 @@ if test "x$CLVMD" != xnone; then fi ################################################################################ +dnl -- Disable sd_notify +AC_MSG_CHECKING(whether to enable sd_notify) +AC_ARG_ENABLE([sd_notify], + AC_HELP_STRING([--disable-sd-notify], + [disable sd_notify support in clvmd]), + [SD_NOTIFY=$enableval], [SD_NOTIFY=auto]) +AC_MSG_RESULT($SD_NOTIFY) + +if test x$SD_NOTIFY != xno ; then + if test x$PKGCONFIG_INIT != x1; then + pkg_config_init + fi + PKG_CHECK_MODULES([LIBSYSTEMD_DAEMON], [libsystemd-daemon], + [SD_NOTIFY=yes], [ + if test x$SD_NOTIFY = yes ; then + AC_MSG_ERROR([bailing out]) + else + SD_NOTIFY=no + fi + ]) +fi +AC_SUBST([SD_NOTIFY]) +if test x$SD_NOTIFY = xyes; then + AC_DEFINE([ENABLE_SD_NOTIFY], [1], + [Define to 1 to include sd_notify() support in clvmd]) +fi + +################################################################################ dnl -- Build cluster mirror log daemon AC_MSG_CHECKING(whether to build cluster mirror log daemon) AC_ARG_ENABLE(cmirrord,
daemons/clvmd/Makefile.in +7 -0
@@ -29,6 +29,8 @@ QUORUM_LIBS = @QUORUM_LIBS@ QUORUM_CFLAGS = @QUORUM_CFLAGS@ SALCK_LIBS = @SALCK_LIBS@ SALCK_CFLAGS = @SALCK_CFLAGS@ +LIBSYSTEMD_DAEMON_LIBS = @LIBSYSTEMD_DAEMON_LIBS@ +LIBSYSTEMD_DAEMON_CFLAGS = @LIBSYSTEMD_DAEMON_CFLAGS@ SOURCES = \ clvmd-command.c \
@@ -81,6 +83,11 @@ LVMLIBS = $(LVMINTERNAL_LIBS) ifeq ("@DMEVENTD@", "yes") LVMLIBS += -ldevmapper-event endif + +ifeq ("@SD_NOTIFY@", "yes") + LMLIBS += $(LIBSYSTEMD_DAEMON_LIBS) + CFLAGS += $(LIBSYSTEMD_DAEMON_CFLAGS) +endif include $(top_builddir)/make.tmpl
daemons/clvmd/clvmd.c +32 -0
@@ -41,6 +41,10 @@#include <sys/un.h> #include <sys/utsname.h> +#ifdef ENABLE_SD_NOTIFY +#include <systemd/sd-daemon.h> +#endif +#ifndef TRUE #define TRUE 1#endif
@@ -483,6 +487,9 @@ int main(int argc, char *argv[])but the cluster is not ready yet */ local_sock =open_local_sock ();if (local_sock <0 ) { +#ifdef ENABLE_SD_NOTIFY +sd_notify (0 ,"STATUS=clvmd could not create local socket" ); +#endif child_init_signal_and_exit (DFAIL_LOCAL_SOCK );/* NOTREACHED */ }
@@ -514,6 +521,10 @@ int main(int argc, char *argv[])pthread_barrier_init (&lvm_start_barrier,NULL ,2 );init_lvhash (); +#ifdef ENABLE_SD_NOTIFY +sd_notify (0 ,"STATUS=Starting the cluster interface" ); +#endif +/* Start the cluster interface */ if (cluster_iface ==IF_AUTO ) cluster_iface =get_cluster_type ();
@@ -557,10 +568,16 @@ int main(int argc, char *argv[])if (!clops) {DEBUGLOG ("Can't initialise cluster interface\n" );log_error ("Can't initialise cluster interface\n" ); +#ifdef ENABLE_SD_NOTIFY +sd_notify (0 ,"STATUS=Can't initialise cluster interface" ); +#endif child_init_signal_and_exit (DFAIL_CLUSTER_IF );/* NOTREACHED */ }DEBUGLOG ("Cluster ready, doing some more initialisation\n" ); +#ifdef ENABLE_SD_NOTIFY +sd_notify (0 ,"STATUS=Cluster ready, doing some more initialisation" ); +#endif /* Save our CSID */ clops->get_our_csid (our_csid);
@@ -573,6 +590,9 @@ int main(int argc, char *argv[])/* Add the local socket to the list */ newfd =malloc (sizeof (struct local_client ));if (!newfd) { +#ifdef ENABLE_SD_NOTIFY +sd_notify (0 ,"STATUS=clvmd failed, not enough memory" ); +#endif child_init_signal_and_exit (DFAIL_MALLOC );/* NOTREACHED */ }
@@ -599,6 +619,9 @@ int main(int argc, char *argv[]) clops->cluster_init_completed ();DEBUGLOG ("clvmd ready for work\n" ); +#ifdef ENABLE_SD_NOTIFY +sd_notify (0 ,"READY=1\nSTATUS=clvmd ready for work" ); +#endif child_init_signal (SUCCESS );/* Try to shutdown neatly */
@@ -608,6 +631,10 @@ int main(int argc, char *argv[])/* Do some work */ main_loop (local_sock, cmd_timeout); +#ifdef ENABLE_SD_NOTIFY +sd_notify (0 ,"STATUS=Shutting down" ); +#endif +pthread_mutex_lock (&lvm_thread_mutex);pthread_cond_signal (&lvm_thread_cond);pthread_mutex_unlock (&lvm_thread_mutex);
@@ -632,6 +659,11 @@ int main(int argc, char *argv[]) } ret =0 ; + +#ifdef ENABLE_SD_NOTIFY +sd_notify (0 ,"STATUS=Finished" ); +#endif + out:dm_hash_destroy (lvm_params.excl_uuid);
lib/misc/configure.h.in +3 -0
@@ -77,6 +77,9 @@ /* Library version */ #undef DM_LIB_VERSION +/* Define to 1 to include sd_notify() support in clvmd */ +#undef ENABLE_SD_NOTIFY + /* Define to 1 if you have `alloca', as a function or macro. */ #undef HAVE_ALLOCA
Signature · 1 line
1.7.7.4