]> git.proxmox.com Git - mirror_frr.git/blame - lib/systemd.c
Merge branch 'stable/3.0'
[mirror_frr.git] / lib / systemd.c
CommitLineData
ddd82ff6 1/* lib/systemd Code
896014f4
DL
2 * Copyright (C) 2016 Cumulus Networks, Inc.
3 * Donald Sharp
4 *
5 * This file is part of Quagga.
6 *
7 * Quagga is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * Quagga is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
ddd82ff6
DS
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
651415bd 49systemd_get_watchdog_time (int the_process)
ddd82ff6
DS
50{
51#if defined HAVE_SYSTEMD
52 uint64_t usec;
651415bd 53 char *watchdog = NULL;
ddd82ff6
DS
54 int ret;
55
56 ret = sd_watchdog_enabled (0, &usec);
57
58 /*
59 * If return is 0 -> we don't want watchdog
60 * if return is < 0, some sort of failure occurred
61 */
651415bd 62 if (ret < 0)
ddd82ff6
DS
63 return 0;
64
651415bd
DS
65 /*
66 * systemd can return that this process
67 * is not the expected sender of the watchdog timer
68 * If we set the_process = 0 then we expect to
69 * be able to send the watchdog to systemd
70 * irrelevant of the pid of this process.
71 */
72 if (ret == 0 && the_process)
73 return 0;
74
75 if (ret == 0 && !the_process)
76 {
77 watchdog = getenv ("WATCHDOG_USEC");
78 if (!watchdog)
79 return 0;
80
81 usec = atol (watchdog);
82 }
83
ddd82ff6
DS
84 return (usec / 1000000)/ 3;
85#else
86 return 0;
87#endif
88}
89
90void
91systemd_send_stopping (void)
92{
93 systemd_send_information ("STOPPING=1");
94}
95
96/*
97 * How many seconds should we wait between watchdog sends
98 */
99int wsecs = 0;
100struct thread_master *systemd_master = NULL;
101
102static int
103systemd_send_watchdog (struct thread *t)
104{
105 systemd_send_information ("WATCHDOG=1");
106
ffa2c898 107 thread_add_timer(systemd_master, systemd_send_watchdog, NULL, wsecs, NULL);
ddd82ff6
DS
108
109 return 1;
110}
111
112void
651415bd 113systemd_send_started (struct thread_master *m, int the_process)
ddd82ff6
DS
114{
115 assert (m != NULL);
116
651415bd 117 wsecs = systemd_get_watchdog_time(the_process);
ddd82ff6
DS
118 systemd_master = m;
119
120 systemd_send_information ("READY=1");
121 if (wsecs != 0)
ffa2c898 122 thread_add_timer(m, systemd_send_watchdog, m, wsecs, NULL);
ddd82ff6 123}