From 099369f8b7f39afe08b6a518195948b05a937af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20L=C3=A9ger?= Date: Tue, 26 Feb 2019 09:05:49 +0100 Subject: [PATCH] Improve systemd integration When fluidsynth is run as a service using systemd, make sure the service is considered started only when it is ready to process events. In order to do so: - Add an optional runtime dependency to libsystemd to the fluidsynth executable - Change the systemd service type to "notify" - Have fluidsynth notify systemd that the service is started after the server is started - Have fluidsynth notify systemd that the service is stopping after joining the server thread --- CMakeLists.txt | 12 ++++++++++++ cmake_admin/report.cmake | 6 ++++++ fluidsynth.service.in | 2 ++ src/CMakeLists.txt | 5 +++++ src/config.cmake | 3 +++ src/fluidsynth.c | 14 +++++++++++++- 6 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b7592597..42d9bf5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,6 +80,10 @@ if ( CMAKE_SYSTEM MATCHES "Linux|FreeBSD|DragonFly" ) option ( enable-alsa "compile ALSA support (if it is available)" on ) endif ( CMAKE_SYSTEM MATCHES "Linux|FreeBSD|DragonFly" ) +if ( CMAKE_SYSTEM MATCHES "Linux" ) + option ( enable-systemd "compile systemd support (if it is available)" on ) +endif ( CMAKE_SYSTEM MATCHES "Linux" ) + if ( CMAKE_SYSTEM MATCHES "Darwin" ) option ( enable-coreaudio "compile CoreAudio support (if it is available)" on ) option ( enable-coremidi "compile CoreMIDI support (if it is available)" on ) @@ -521,6 +525,14 @@ else(NOT enable-pkgconfig) remove_definitions( -DHAVE_LASH ) endif ( enable-lash ) + unset ( SYSTEMD_SUPPORT CACHE ) + if ( enable-systemd ) + pkg_check_modules ( SYSTEMD libsystemd ) + set ( SYSTEMD_SUPPORT ${SYSTEMD_FOUND} ) + else ( enable-systemd ) + unset_pkg_config ( SYSTEMD ) + endif ( enable-systemd ) + unset ( DBUS_SUPPORT CACHE ) if ( enable-dbus ) pkg_check_modules ( DBUS dbus-1>=1.0.0 ) diff --git a/cmake_admin/report.cmake b/cmake_admin/report.cmake index e25a989d..c72a46e4 100644 --- a/cmake_admin/report.cmake +++ b/cmake_admin/report.cmake @@ -98,6 +98,12 @@ else ( LASH_SUPPORT ) message ( "LASH support: no" ) endif ( LASH_SUPPORT ) +if ( SYSTEMD_SUPPORT ) + message ( "systemd support: yes" ) +else ( SYSTEMD_SUPPORT ) + message ( "systemd support: no" ) +endif ( SYSTEMD_SUPPORT ) + if ( DART_SUPPORT ) message ( "OS/2 DART support: yes" ) else ( DART_SUPPORT ) diff --git a/fluidsynth.service.in b/fluidsynth.service.in index 20b5696a..7a035e96 100644 --- a/fluidsynth.service.in +++ b/fluidsynth.service.in @@ -4,6 +4,8 @@ Documentation=man:fluidsynth(1) After=sound.target [Service] +Type=notify +NotifyAccess=main EnvironmentFile=@FLUID_DAEMON_ENV_FILE@ EnvironmentFile=-%h/.config/fluidsynth ExecStart=@CMAKE_INSTALL_PREFIX@/@BIN_INSTALL_DIR@/fluidsynth -is $OTHER_OPTS $SOUND_FONT diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a0ba53d1..f7316326 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -95,6 +95,10 @@ if ( LASH_SUPPORT ) include_directories ( ${LASH_INCLUDE_DIRS}) endif ( LASH_SUPPORT ) +if ( SYSTEMD_SUPPORT ) + include_directories ( ${SYSTEMD_INCLUDE_DIRS}) +endif ( SYSTEMD_SUPPORT ) + if ( DART_SUPPORT ) set ( fluid_dart_SOURCES drivers/fluid_dart.c ) include_directories ( ${DART_INCLUDE_DIRS} ) @@ -341,6 +345,7 @@ endif ( FLUID_CPPFLAGS ) target_link_libraries ( fluidsynth libfluidsynth + ${SYSTEMD_LIBRARIES} ${FLUID_LIBS} ) diff --git a/src/config.cmake b/src/config.cmake index 2d1d786b..71019e73 100644 --- a/src/config.cmake +++ b/src/config.cmake @@ -49,6 +49,9 @@ /* whether or not we are supporting lash */ #cmakedefine HAVE_LASH @HAVE_LASH@ +/* Define if systemd support is enabled */ +#cmakedefine SYSTEMD_SUPPORT @SYSTEMD_SUPPORT@ + /* Define to 1 if you have the `MidiShare' library (-lMidiShare). */ #cmakedefine HAVE_LIBMIDISHARE @HAVE_LIBMIDISHARE@ diff --git a/src/fluidsynth.c b/src/fluidsynth.c index 7bd6cdf6..704b33ee 100644 --- a/src/fluidsynth.c +++ b/src/fluidsynth.c @@ -31,6 +31,9 @@ #include "fluid_lash.h" +#ifdef SYSTEMD_SUPPORT +#include +#endif void print_usage(void); void print_help(fluid_settings_t *settings); @@ -892,6 +895,12 @@ int main(int argc, char **argv) fprintf(stderr, "Failed to create the server.\n" "Continuing without it.\n"); } +#ifdef SYSTEMD_SUPPORT + else + { + sd_notify(0, "READY=1"); + } +#endif } #endif @@ -954,10 +963,13 @@ cleanup: fluid_server_join(server); } +#ifdef SYSTEMD_SUPPORT + sd_notify(0, "STOPPING=1"); +#endif delete_fluid_server(server); } -#endif +#endif /* NETWORK_SUPPORT */ if(cmd_handler != NULL) {