summaryrefslogtreecommitdiff
path: root/lib/systemd.c
diff options
context:
space:
mode:
authorDonald Sharp <sharpd@cumulusnetworks.com>2016-02-10 07:24:11 -0500
committerDonald Sharp <sharpd@cumulusnetworks.com>2016-02-10 15:06:59 -0500
commitddd82ff66cd6092b1d99dcf6013b97a1620f6d53 (patch)
tree8186a685236ce2c827c0679531ab6f10b592ff39 /lib/systemd.c
parente6a59b35055cbff52915e292bd7da156aee03787 (diff)
lib: Add library code to interact with systemd
Modify the lib/ code to include the ability to interact with systemd. Provide the ability to interact with systemd's watchdog timer as well. There is no code here that actually uses this change. Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
Diffstat (limited to 'lib/systemd.c')
-rw-r--r--lib/systemd.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/systemd.c b/lib/systemd.c
new file mode 100644
index 0000000000..5f5f5c3fff
--- /dev/null
+++ b/lib/systemd.c
@@ -0,0 +1,103 @@
+/* lib/systemd Code
+ Copyright (C) 2016 Cumulus Networks, Inc.
+ Donald Sharp
+
+This file is part of Quagga.
+
+Quagga is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 2, or (at your option) any
+later version.
+
+Quagga is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Quagga; see the file COPYING. If not, write to the Free
+Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA. */
+
+#include <zebra.h>
+
+#include "thread.h"
+#include "systemd.h"
+
+#if defined HAVE_SYSTEMD
+#include <systemd/sd-daemon.h>
+#endif
+
+/*
+ * Wrapper this silliness if we
+ * don't have systemd
+ */
+void
+systemd_send_information (const char *info)
+{
+#if defined HAVE_SYSTEMD
+ sd_notify (0, info);
+#else
+ return;
+#endif
+}
+
+/*
+ * A return of 0 means that we are not watchdoged
+ */
+static int
+systemd_get_watchdog_time (void)
+{
+#if defined HAVE_SYSTEMD
+ uint64_t usec;
+ int ret;
+
+ ret = sd_watchdog_enabled (0, &usec);
+
+ /*
+ * If return is 0 -> we don't want watchdog
+ * if return is < 0, some sort of failure occurred
+ */
+ if (ret <= 0)
+ return 0;
+
+ return (usec / 1000000)/ 3;
+#else
+ return 0;
+#endif
+}
+
+void
+systemd_send_stopping (void)
+{
+ systemd_send_information ("STOPPING=1");
+}
+
+/*
+ * How many seconds should we wait between watchdog sends
+ */
+int wsecs = 0;
+struct thread_master *systemd_master = NULL;
+
+static int
+systemd_send_watchdog (struct thread *t)
+{
+ systemd_send_information ("WATCHDOG=1");
+
+ thread_add_timer (systemd_master, systemd_send_watchdog, NULL, wsecs);
+
+ return 1;
+}
+
+void
+systemd_send_started (struct thread_master *m)
+{
+ assert (m != NULL);
+
+ wsecs = systemd_get_watchdog_time();
+ systemd_master = m;
+
+ systemd_send_information ("READY=1");
+ if (wsecs != 0)
+ thread_add_timer (m, systemd_send_watchdog, m, wsecs);
+}