File: pluto_sd.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (94 lines) | stat: -rw-r--r-- 2,840 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* pluto_sd.c
 * Status notifications for systemd
 * Copyright (C) 2013 Matt Rogers <mrogers@redhat.com>
 * Copyright (C) 2016 Paul Wouters <pwouters@redhat.com>
 * Copyright (C) 2019 Andrew Cagney <cagney@gnu.org>
 * Copyright (C) 2019 D. Hugh Redelmeier <hugh@mimosa.com>
 *
 * This program 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 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program 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.
 */

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "constants.h"
#include "lswsd.h"

#include "defs.h"		/* for so_serial_t */
#include "log.h"
#include "timer.h"
#include "pluto_sd.h"

static global_timer_cb sd_watchdog_event;

void pluto_sd_init(struct logger *logger)
{
	uint64_t sd_usecs;
	int ret = lswsd_watchdog_enabled(&sd_usecs);

	if (ret == 0) {
		llog(RC_LOG, logger, "systemd watchdog not enabled - not sending watchdog keepalives");
		return;
	}
	if (ret < 0) {
		llog(RC_LOG, logger, "systemd watchdog returned error %d - not sending watchdog keepalives", ret);
		return;
	}

	llog(RC_LOG, logger, "systemd watchdog for ipsec service configured with timeout of %"PRIu64" usecs", sd_usecs);
	uintmax_t sd_secs = sd_usecs / 2 / 1000000; /* suggestion from sd_watchdog_enabled(3) */
	llog(RC_LOG, logger, "watchdog: sending probes every %ju secs", sd_secs);
	/* tell systemd that we have finished starting up */
	pluto_sd(PLUTO_SD_START, SD_REPORT_NO_STATUS);
	/* start the keepalive events */
	enable_periodic_timer(EVENT_SD_WATCHDOG, sd_watchdog_event,
			      deltatime(sd_secs));
}

/*
 * Interface for lswsd_notify(3) calls.
 */
void pluto_sd(int action, int status)
{
	enum_buf ab;
	dbg("pluto_sd: executing action %s(%d), status %d",
	    str_enum(&sd_action_names, action, &ab), action, status);

	switch (action) {
	case PLUTO_SD_WATCHDOG:
		lswsd_notify("WATCHDOG=1");
		break;
	case PLUTO_SD_RELOADING:
		lswsd_notify("RELOADING=1");
		break;
	case PLUTO_SD_READY:
		lswsd_notify("READY=1");
		break;
	case PLUTO_SD_STOPPING:
		lswsd_notifyf("STOPPING=1\nSTATUS=PLUTO_EXIT=%d", status);
		break;
	case PLUTO_SD_START:
		lswsd_notifyf("READY=1\nSTATUS=Startup completed.\nMAINPID=%lu",
			(unsigned long) getpid());
		break;
	case PLUTO_SD_EXIT:
		lswsd_notifyf("STATUS=Exited.\nERRNO=%i", status);
		break;
	default:
		bad_case(action);
		break;
	}
}

void sd_watchdog_event(struct logger *unused_logger UNUSED)
{
	pluto_sd(PLUTO_SD_WATCHDOG, SD_REPORT_NO_STATUS);
}