]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: Add library code to interact with systemd
authorDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 10 Feb 2016 12:24:11 +0000 (07:24 -0500)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 10 Feb 2016 20:06:59 +0000 (15:06 -0500)
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>
configure.ac
lib/Makefile.am
lib/systemd.c [new file with mode: 0644]
lib/systemd.h [new file with mode: 0644]

index 578f5a2c0ae10c35bfc5f2bbe02ae4bf0f98d64f..72e62c4667104db27b9f0a38757371b91e1be7ce 100755 (executable)
@@ -300,6 +300,8 @@ AC_ARG_ENABLE(pcreposix,
 [  --enable-pcreposix          enable using PCRE Posix libs for regex functions])
 AC_ARG_ENABLE(fpm,
 [  --enable-fpm            enable Forwarding Plane Manager support])
+AC_ARG_ENABLE(systemd,
+[  --enable-systemd    enable Systemd support])
 AC_ARG_ENABLE(werror,
   AS_HELP_STRING([--enable-werror], [enable -Werror (recommended for developers only)]))
 
@@ -324,6 +326,11 @@ if test x"${enable_time_check}" != x"no" ; then
   fi
 fi
 
+if test x"${enable_systemd}" != x"no" ; then
+  AC_DEFINE(HAVE_SYSTEMD,,Compile systemd support in)
+  LIBS="$LIBS -lsystemd "
+fi
+
 if test "${enable_shell_access}" = "yes"; then
    AC_DEFINE(HAVE_SHELL_ACCESS,,Allow user to use ssh/telnet/bash)
 fi
index 43fa9061282916d1422197e802c2ab8899051813..42d94d76940db9282ad142f57985bf289f1188e4 100644 (file)
@@ -14,7 +14,7 @@ libzebra_la_SOURCES = \
        filter.c routemap.c distribute.c stream.c str.c log.c plist.c \
        zclient.c sockopt.c smux.c agentx.c snmp.c md5.c if_rmap.c keychain.c privs.c \
        sigevent.c pqueue.c jhash.c memtypes.c workqueue.c nexthop.c json.c \
-       ptm_lib.c csv.c bfd.c vrf.c
+       ptm_lib.c csv.c bfd.c vrf.c systemd.c
 
 BUILT_SOURCES = memtypes.h route_types.h gitversion.h
 
@@ -30,7 +30,7 @@ pkginclude_HEADERS = \
        plist.h zclient.h sockopt.h smux.h md5.h if_rmap.h keychain.h \
        privs.h sigevent.h pqueue.h jhash.h zassert.h memtypes.h \
        workqueue.h route_types.h libospf.h nexthop.h json.h \
-       ptm_lib.h csv.h bfd.h vrf.h
+       ptm_lib.h csv.h bfd.h vrf.h systemd.h
 
 noinst_HEADERS = \
        plist_int.h
diff --git a/lib/systemd.c b/lib/systemd.c
new file mode 100644 (file)
index 0000000..5f5f5c3
--- /dev/null
@@ -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);
+}
diff --git a/lib/systemd.h b/lib/systemd.h
new file mode 100644 (file)
index 0000000..aef9c8e
--- /dev/null
@@ -0,0 +1,34 @@
+/* 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.  */
+
+/*
+ * Wrapper functions to systemd calls.
+ *
+ * Design point is that if systemd is not being used on this system
+ * then these functions becomes a no-op.
+ *
+ * To turn on systemd compilation, use --enable-systemd on
+ * configure run.
+ */
+void systemd_send_information (const char *info);
+void systemd_send_stopping (void);
+void systemd_send_started (struct thread_master *);
+