Skip to content

Commit

Permalink
Improve systemd integration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fleger authored and derselbst committed Mar 2, 2019
1 parent 9671e41 commit 099369f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 )
Expand Down
6 changes: 6 additions & 0 deletions cmake_admin/report.cmake
Expand Up @@ -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 )
Expand Down
2 changes: 2 additions & 0 deletions fluidsynth.service.in
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -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} )
Expand Down Expand Up @@ -341,6 +345,7 @@ endif ( FLUID_CPPFLAGS )

target_link_libraries ( fluidsynth
libfluidsynth
${SYSTEMD_LIBRARIES}
${FLUID_LIBS}
)

Expand Down
3 changes: 3 additions & 0 deletions src/config.cmake
Expand Up @@ -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@

Expand Down
14 changes: 13 additions & 1 deletion src/fluidsynth.c
Expand Up @@ -31,6 +31,9 @@

#include "fluid_lash.h"

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

void print_usage(void);
void print_help(fluid_settings_t *settings);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -954,10 +963,13 @@ int main(int argc, char **argv)
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)
{
Expand Down

0 comments on commit 099369f

Please sign in to comment.