[Asterisk-code-review] sd notify (systemd status notifications) support (asterisk[master])

Tzafrir Cohen asteriskteam at digium.com
Mon Jun 27 15:00:51 CDT 2016


Tzafrir Cohen has uploaded a new change for review.

  https://gerrit.asterisk.org/3094

Change subject: sd_notify (systemd status notifications) support
......................................................................

sd_notify (systemd status notifications) support

sd_notify() is used to notify systemd of changes to the status of the
process. This allows the systemd daemon to know when the process
finished loading (and thus only start another program after Asterisk has
finished loading).

To use this, use a systemd unit with 'Type=notify' for Asterisk.

This commit also adds the function ast_sd_notify(), a wrapper around
sd_notify that does nothing if not built with systemd support.

Change-Id: Ied6a59dafd5ef331c5c7ae8f3ccd2dfc94be7811
---
M CHANGES
M include/asterisk/io.h
M main/Makefile
M main/asterisk.c
M main/loader.c
5 files changed, 28 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/94/3094/1

diff --git a/CHANGES b/CHANGES
index 6e6bec6..40207f8 100644
--- a/CHANGES
+++ b/CHANGES
@@ -243,6 +243,10 @@
    context. If enabled then a hint will be automatically created with the name of
    the device.
 
+* If Asterisk is built with systemd support, and run under systemd, it will
+  notify systemd of its state using sd_notify. Use 'Type=notify' in
+  asterisk.service.
+
 
 Functions
 ------------------
diff --git a/include/asterisk/io.h b/include/asterisk/io.h
index 2bddd37..8e41965 100644
--- a/include/asterisk/io.h
+++ b/include/asterisk/io.h
@@ -139,6 +139,21 @@
 
 int ast_get_termcols(int fd);
 
+/*!
+ * \brief a wrapper for sd_notify(): notify systemd of any state changes.
+ * \param state a string that states the changes. See sd_notify(3).
+ * The wrapper does nothing if systemd ('s development headers) was not
+ * detected on the system.
+ * \returns >=0 on success, negative value on error.
+ */
+#ifdef HAVE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#define ast_sd_notify(state)   sd_notify(0, state)
+#else
+#define ast_sd_notify(state)
+#endif
+
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
diff --git a/main/Makefile b/main/Makefile
index 729ae9c..d32d8dd 100644
--- a/main/Makefile
+++ b/main/Makefile
@@ -45,6 +45,9 @@
 AST_LIBS+=$(CRYPT_LIB)
 AST_LIBS+=$(AST_CLANG_BLOCKS_LIBS)
 AST_LIBS+=$(RT_LIB)
+ifeq ($(HAVE_SYSTEMD),1)
+AST_LIBS+=$(SYSTEMD_LIB)
+endif
 
 ifneq ($(findstring $(OSARCH), linux-gnu uclinux linux-uclibc linux-musl kfreebsd-gnu),)
   ifneq ($(findstring LOADABLE_MODULES,$(MENUSELECT_CFLAGS)),)
diff --git a/main/asterisk.c b/main/asterisk.c
index 95f1b3e..73fbcc6 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -1990,6 +1990,9 @@
 		ast_module_shutdown();
 	}
 
+	if (!restart) {
+		ast_sd_notify("STOPPING=1");
+	}
 	if (ast_opt_console || (ast_opt_remote && !ast_opt_exec)) {
 		ast_el_write_default_histfile();
 		if (consolethread == AST_PTHREADT_NULL || consolethread == pthread_self()) {
@@ -4727,6 +4730,7 @@
 	ast_register_cleanup(main_atexit);
 
 	run_startup_commands();
+	ast_sd_notify("READY=1");
 
 	ast_verb(0, COLORIZE_FMT "\n", COLORIZE(COLOR_BRGREEN, 0, "Asterisk Ready."));
 
diff --git a/main/loader.c b/main/loader.c
index f959221..c9b400a 100644
--- a/main/loader.c
+++ b/main/loader.c
@@ -891,6 +891,7 @@
 		res = AST_MODULE_RELOAD_IN_PROGRESS;
 		goto module_reload_exit;
 	}
+	ast_sd_notify("RELOAD=1");
 	ast_lastreloadtime = ast_tvnow();
 
 	if (ast_opt_lock_confdir) {
@@ -966,6 +967,7 @@
 	if (ast_opt_lock_confdir) {
 		ast_unlock_path(ast_config_AST_CONFIG_DIR);
 	}
+	ast_sd_notify("READY=1");
 	ast_mutex_unlock(&reloadlock);
 
 module_reload_exit:

-- 
To view, visit https://gerrit.asterisk.org/3094
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ied6a59dafd5ef331c5c7ae8f3ccd2dfc94be7811
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Tzafrir Cohen <tzafrir.cohen at xorcom.com>



More information about the asterisk-code-review mailing list