]> git.proxmox.com Git - mirror_frr.git/blame - lib/systemd.c
lib: Add library code to interact with systemd
[mirror_frr.git] / lib / systemd.c
CommitLineData
ddd82ff6
DS
1/* lib/systemd Code
2 Copyright (C) 2016 Cumulus Networks, Inc.
3 Donald Sharp
4
5This file is part of Quagga.
6
7Quagga is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 2, or (at your option) any
10later version.
11
12Quagga is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with Quagga; see the file COPYING. If not, write to the Free
19Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
21
22#include <zebra.h>
23
24#include "thread.h"
25#include "systemd.h"
26
27#if defined HAVE_SYSTEMD
28#include <systemd/sd-daemon.h>
29#endif
30
31/*
32 * Wrapper this silliness if we
33 * don't have systemd
34 */
35void
36systemd_send_information (const char *info)
37{
38#if defined HAVE_SYSTEMD
39 sd_notify (0, info);
40#else
41 return;
42#endif
43}
44
45/*
46 * A return of 0 means that we are not watchdoged
47 */
48static int
49systemd_get_watchdog_time (void)
50{
51#if defined HAVE_SYSTEMD
52 uint64_t usec;
53 int ret;
54
55 ret = sd_watchdog_enabled (0, &usec);
56
57 /*
58 * If return is 0 -> we don't want watchdog
59 * if return is < 0, some sort of failure occurred
60 */
61 if (ret <= 0)
62 return 0;
63
64 return (usec / 1000000)/ 3;
65#else
66 return 0;
67#endif
68}
69
70void
71systemd_send_stopping (void)
72{
73 systemd_send_information ("STOPPING=1");
74}
75
76/*
77 * How many seconds should we wait between watchdog sends
78 */
79int wsecs = 0;
80struct thread_master *systemd_master = NULL;
81
82static int
83systemd_send_watchdog (struct thread *t)
84{
85 systemd_send_information ("WATCHDOG=1");
86
87 thread_add_timer (systemd_master, systemd_send_watchdog, NULL, wsecs);
88
89 return 1;
90}
91
92void
93systemd_send_started (struct thread_master *m)
94{
95 assert (m != NULL);
96
97 wsecs = systemd_get_watchdog_time();
98 systemd_master = m;
99
100 systemd_send_information ("READY=1");
101 if (wsecs != 0)
102 thread_add_timer (m, systemd_send_watchdog, m, wsecs);
103}