From d6de1d9af7e97ad72198dc665fb6ef9a2634d0c4 Mon Sep 17 00:00:00 2001 From: VCC Live/Core <56296664+vcc-core@users.noreply.github.com> Date: Thu, 30 Apr 2020 16:55:28 +0200 Subject: [PATCH] [Core] Add systemd sd_notify support --- Makefile.am | 2 +- build/freeswitch.service | 4 ++++ configure.ac | 11 +++++++++++ src/switch_core.c | 12 ++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Makefile.am b/Makefile.am index ac15c3d7a3..09d500a4d7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -248,7 +248,7 @@ endif lib_LTLIBRARIES = libfreeswitch.la libfreeswitch_la_CFLAGS = $(CORE_CFLAGS) $(SQLITE_CFLAGS) $(GUMBO_CFLAGS) $(FVAD_CFLAGS) $(FREETYPE_CFLAGS) $(CURL_CFLAGS) $(PCRE_CFLAGS) $(SPEEX_CFLAGS) $(LIBEDIT_CFLAGS) $(openssl_CFLAGS) $(SOFIA_SIP_CFLAGS) $(AM_CFLAGS) $(TPL_CFLAGS) libfreeswitch_la_LDFLAGS = -version-info 1:0:0 $(AM_LDFLAGS) $(PLATFORM_CORE_LDFLAGS) -no-undefined -libfreeswitch_la_LIBADD = $(CORE_LIBS) $(APR_LIBS) $(SQLITE_LIBS) $(GUMBO_LIBS) $(FVAD_LIBS) $(FREETYPE_LIBS) $(CURL_LIBS) $(PCRE_LIBS) $(SPEEX_LIBS) $(LIBEDIT_LIBS) $(openssl_LIBS) $(PLATFORM_CORE_LIBS) $(TPL_LIBS) $(SPANDSP_LIBS) $(SOFIA_SIP_LIBS) +libfreeswitch_la_LIBADD = $(CORE_LIBS) $(APR_LIBS) $(SQLITE_LIBS) $(GUMBO_LIBS) $(FVAD_LIBS) $(FREETYPE_LIBS) $(CURL_LIBS) $(PCRE_LIBS) $(SPEEX_LIBS) $(LIBEDIT_LIBS) $(SYSTEMD_LIBS) $(openssl_LIBS) $(PLATFORM_CORE_LIBS) $(TPL_LIBS) $(SPANDSP_LIBS) $(SOFIA_SIP_LIBS) libfreeswitch_la_DEPENDENCIES = $(BUILT_SOURCES) if HAVE_PNG diff --git a/build/freeswitch.service b/build/freeswitch.service index ae6921b4df..2b0a035723 100644 --- a/build/freeswitch.service +++ b/build/freeswitch.service @@ -4,6 +4,10 @@ After=syslog.target network.target After=postgresql.service postgresql-9.3.service postgresql-9.4.service mysqld.service httpd.service [Service] +# You can use Type=notify only if you compile FreeSWITCH with --enable-systemd configure option +# In this case you have to run FreeSWITCH in foreground mode (-nf option)! +#Type=notify +#NotifyAccess=main User=freeswitch EnvironmentFile=-/etc/sysconfig/freeswitch # RuntimeDirectory is not yet supported in CentOS 7. A workaround is to use /etc/tmpfiles.d/freeswitch.conf diff --git a/configure.ac b/configure.ac index b5dfe74909..eb65a4765d 100644 --- a/configure.ac +++ b/configure.ac @@ -1578,6 +1578,17 @@ AS_IF([test "x$enable_core_libedit_support" != "xno"],[ AC_MSG_ERROR([You need to either install libedit-dev (>= 2.11) or configure with --disable-core-libedit-support]) ])])]) +AC_ARG_ENABLE(systemd, + [AS_HELP_STRING([--enable-systemd], [Compile with systemd notify support])]) + +AS_IF([test "x$enable_systemd" = "xyes"],[ + PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 219], [ + AC_DEFINE([HAVE_SYSTEMD], [1], [Define to 1 if systemd is available])]) +]) + +AC_SUBST(SYSTEMD_LIBS) + + dnl --------------------------------------------------------------------------- dnl - OpenLDAP SDK dnl --------------------------------------------------------------------------- diff --git a/src/switch_core.c b/src/switch_core.c index cacff3cdeb..ac6001c612 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -73,6 +73,10 @@ #define pclose _pclose #endif +#ifdef HAVE_SYSTEMD +#include +#endif + SWITCH_DECLARE_DATA switch_directories SWITCH_GLOBAL_dirs = { 0 }; SWITCH_DECLARE_DATA switch_filenames SWITCH_GLOBAL_filenames = { 0 }; @@ -2558,6 +2562,11 @@ SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(switch_core_flag_t free(cmd); } +#ifdef HAVE_SYSTEMD + sd_notifyf(0, "READY=1\n" + "MAINPID=%lu\n", (unsigned long) getpid()); +#endif + return SWITCH_STATUS_SUCCESS; } @@ -2880,6 +2889,9 @@ SWITCH_DECLARE(int32_t) switch_core_session_ctl(switch_session_ctl_t cmd, void * switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Restarting\n"); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Shutting down\n"); +#ifdef HAVE_SYSTEMD + sd_notifyf(0, "STOPPING=1\n"); +#endif #ifdef _MSC_VER fclose(stdin); #endif