summaryrefslogtreecommitdiffstats
path: root/lib/systemd.c
blob: 2238dc9f3da6d759a97c1b89f3737ffc9800ba5f (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* 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 this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <zebra.h>
#include <sys/un.h>

#include "thread.h"
#include "systemd.h"
#include "lib_errors.h"

/* these are cleared from env so they don't "leak" into things we fork(),
 * particularly for watchfrr starting individual daemons
 *
 * watchdog_pid is currently not used since watchfrr starts forking.
 * (TODO: handle that better, somehow?)
 */
static pid_t watchdog_pid = -1;
static intmax_t watchdog_msec;

/* not used yet, but can trigger auto-switch to journald logging */
bool sd_stdout_is_journal;
bool sd_stderr_is_journal;

static char *notify_socket;

/* talk to whatever entity claims to be systemd ;)
 *
 * refer to sd_notify docs for messages systemd accepts over this socket.
 * This function should be functionally equivalent to sd_notify().
 */
static void systemd_send_information(const char *info)
{
	int sock;
	struct sockaddr_un sun;

	if (!notify_socket)
		return;

	sock = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (sock < 0)
		return;

	sun.sun_family = AF_UNIX;
	strlcpy(sun.sun_path, notify_socket, sizeof(sun.sun_path));

	/* linux abstract unix socket namespace */
	if (sun.sun_path[0] == '@')
		sun.sun_path[0] = '\0';

	/* nothing we can do if this errors out... */
	(void)sendto(sock, info, strlen(info), 0, (struct sockaddr *)&sun,
		     sizeof(sun));

	close(sock);
}

void systemd_send_stopping(void)
{
	systemd_send_information("STATUS=");
	systemd_send_information("STOPPING=1");
}

static struct thread_master *systemd_master = NULL;

static int systemd_send_watchdog(struct thread *t)
{
	systemd_send_information("WATCHDOG=1");

	assert(watchdog_msec > 0);
	thread_add_timer_msec(systemd_master, systemd_send_watchdog, NULL,
			      watchdog_msec, NULL);
	return 1;
}

void systemd_send_started(struct thread_master *m)
{
	assert(m != NULL);

	systemd_master = m;

	systemd_send_information("READY=1");
	if (watchdog_msec > 0)
		systemd_send_watchdog(NULL);
}

void systemd_send_status(const char *status)
{
	char buffer[1024];

	snprintf(buffer, sizeof(buffer), "STATUS=%s", status);
	systemd_send_information(buffer);
}

static intmax_t getenv_int(const char *varname, intmax_t dflt)
{
	char *val, *err;
	intmax_t intval;

	val = getenv(varname);
	if (!val)
		return dflt;

	intval = strtoimax(val, &err, 0);
	if (*err || !*val)
		return dflt;
	return intval;
}

void systemd_init_env(void)
{
	char *tmp;
	uintmax_t dev, ino;
	int len;
	struct stat st;

	notify_socket = getenv("NOTIFY_SOCKET");

	/* no point in setting up watchdog w/o notify socket */
	if (notify_socket) {
		intmax_t watchdog_usec;

		watchdog_pid = getenv_int("WATCHDOG_PID", -1);
		if (watchdog_pid <= 0)
			watchdog_pid = -1;

		/* note this is the deadline, hence the divide by 3 */
		watchdog_usec = getenv_int("WATCHDOG_USEC", 0);
		if (watchdog_usec >= 3000)
			watchdog_msec = watchdog_usec / 3000;
		else {
			if (watchdog_usec != 0)
				flog_err(
					EC_LIB_UNAVAILABLE,
					"systemd expects a %jd microsecond watchdog timer, but FRR only supports millisecond resolution!",
					watchdog_usec);
			watchdog_msec = 0;
		}
	}

	tmp = getenv("JOURNAL_STREAM");
	if (tmp && sscanf(tmp, "%ju:%ju%n", &dev, &ino, &len) == 2
	    && (size_t)len == strlen(tmp)) {
		if (fstat(1, &st) == 0 && st.st_dev == (dev_t)dev
		    && st.st_ino == (ino_t)ino)
			sd_stdout_is_journal = true;
		if (fstat(2, &st) == 0 && st.st_dev == (dev_t)dev
		    && st.st_ino == (ino_t)ino)
			sd_stderr_is_journal = true;
	}

	/* these should *not* be passed to any other process we start */
	unsetenv("WATCHDOG_PID");
	unsetenv("WATCHDOG_USEC");
}