Description: Add support to systemd integration via status notifications
Author: Dhionel Díaz <ddiaz@cenditel.gob.ve>
Last-Update: 2015-09-10

---
Index: xorp-1.8.6~wip.20160217/SConstruct
===================================================================
--- xorp-1.8.6~wip.20160217.orig/SConstruct	2016-07-03 16:11:21.986774364 +0100
+++ xorp-1.8.6~wip.20160217/SConstruct	2016-07-03 16:11:21.626765928 +0100
@@ -88,6 +88,7 @@ vars.AddVariables(
     BoolVariable('enable_builddirrun',  'Enable Xorp to run from BUILD_DIR', False),
     BoolVariable('enable_boost',  'Use BOOST', False),
     BoolVariable('enable_ustl',  'Use uSTL', False),
+    BoolVariable('enable_systemd',  'Use systemd', False),
     BoolVariable('enable_bgp',  'Build BGP', True),
     BoolVariable('enable_buildinfo',  'Build Info, see libxorp/create_buildinfo.sh', True),
     BoolVariable('enable_olsr',  'Build OLSR', True),
@@ -286,6 +287,7 @@ print 'Enable BuildInfo: ', env['enable_
 print 'Xorp can run from BUILD_DIR: ', env['enable_builddirrun']
 print 'Try Enable BOOST: ', env['enable_boost']
 print 'Try Enable uSTL : ', env['enable_ustl']
+print 'Try Enable systemd: ', env['enable_systemd']
 print 'Disable IPv6:     ', env['disable_ipv6']
 print 'Disable libtecla: ', env['disable_libtecla']
 print 'Disable Firewall: ', env['disable_fw']
@@ -426,6 +428,12 @@ if tst and not ((tst == "no") or (tst ==
 else:
     env['enable_ustl'] = False
 
+tst = ARGUMENTS.get('enable_systemd', False)
+if tst and not (tst == "no"):
+    env['enable_systemd'] = True
+else:
+    env['enable_systemd'] = False
+
 tst = ARGUMENTS.get('enable_tests', False)
 if tst and not ((tst == "no") or (tst == "false")):
     env['enable_tests'] = True
@@ -728,6 +736,9 @@ if not env.GetOption('clean') and \
         #  so just over-ride it here and assume it exists.
         conf.Define('HAS_BOOST_NONCOPYABLE_INC')
 
+    if env['enable_systemd']:
+        conf.Define('XORP_USE_SYSTEMD')
+
     # Big ball of mud.
     from config.allconfig import DoAllConfig
     DoAllConfig(env, conf, host_os)
Index: xorp-1.8.6~wip.20160217/rtrmgr/SConscript
===================================================================
--- xorp-1.8.6~wip.20160217.orig/rtrmgr/SConscript	2016-07-03 16:11:21.986774364 +0100
+++ xorp-1.8.6~wip.20160217/rtrmgr/SConscript	2016-07-03 16:11:21.734768459 +0100
@@ -223,6 +223,9 @@ if (rtrmgr_env.has_key('mingw') and rtrm
 
     rtrmgr_env.Append(LIBS = ['xorp_core', 'crypto'])
 
+if rtrmgr_env['enable_systemd']:
+    rtrmgr_env.ParseConfig("pkg-config --cflags --libs libsystemd")
+
 
 rtrmgr_srcs = [
 	'main_rtrmgr.cc',
Index: xorp-1.8.6~wip.20160217/rtrmgr/main_rtrmgr.cc
===================================================================
--- xorp-1.8.6~wip.20160217.orig/rtrmgr/main_rtrmgr.cc	2016-07-03 16:11:04.302359944 +0100
+++ xorp-1.8.6~wip.20160217/rtrmgr/main_rtrmgr.cc	2016-07-03 16:12:05.235787870 +0100
@@ -65,6 +65,9 @@
 #include <getopt.h>
 #endif
 
+#ifdef XORP_USE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#endif
 
 //
 // Default values
@@ -358,6 +361,11 @@ Rtrmgr::run()
 	}
 
 	_ready = true;
+#ifdef XORP_USE_SYSTEMD
+      // Notify systemd that service startup has been successfully completed
+      sd_notify(0, "READY=1\n"
+              "STATUS=Startup successfully completed.");
+#endif
 	//
 	// Loop while handling configuration events and signals
 	//
@@ -369,6 +377,11 @@ Rtrmgr::run()
 	}
 	fflush(stdout);
 	_ready = false;
+#ifdef XORP_USE_SYSTEMD
+      // Notify systemd that the service is beginning its shutdown
+      sd_notify(0, "STOPPING=1\n"
+              "STATUS=Shutdown process started.");
+#endif
 
 	//
 	// Shutdown everything
@@ -782,5 +795,9 @@ cleanup_and_exit(int errcode)
     xlog_stop();
     xlog_exit();
 
+#ifdef XORP_USE_SYSTEMD
+      // Send final status message to systemd
+      sd_notify(0, "STATUS=Exiting.");
+#endif
     exit(errcode);
 }
