From ddd82ff66cd6092b1d99dcf6013b97a1620f6d53 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Wed, 10 Feb 2016 07:24:11 -0500 Subject: [PATCH] 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 --- configure.ac | 7 ++++ lib/Makefile.am | 4 +- lib/systemd.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/systemd.h | 34 ++++++++++++++++ 4 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 lib/systemd.c create mode 100644 lib/systemd.h diff --git a/configure.ac b/configure.ac index 578f5a2c0a..72e62c4667 100755 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/lib/Makefile.am b/lib/Makefile.am index 43fa906128..42d94d7694 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 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 + +#include "thread.h" +#include "systemd.h" + +#if defined HAVE_SYSTEMD +#include +#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 index 0000000000..aef9c8ec82 --- /dev/null +++ b/lib/systemd.h @@ -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 *); + -- 2.39.5