forked from acassen/keepalived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemd.c
124 lines (106 loc) · 3.26 KB
/
systemd.c
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
/*
* Soft: Keepalived is a failover program for the LVS project
* <www.linuxvirtualserver.org>. It monitor & manipulate
* a loadbalanced server pool using multi-layer checks.
*
* Part: systemd integration.
*
* Author: Alexandre Cassen, <[email protected]>
*
* 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.
*
* 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.
*
* Copyright (C) 2020-2020 Alexandre Cassen, <[email protected]>
*/
#include "config.h"
#include <stdbool.h>
#include <systemd/sd-daemon.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include "systemd.h"
#include "logger.h"
static bool parent_is_systemd;
bool
check_parent_systemd(void)
{
char stat_buf[13]; /* "1 (systemd) " */
int fd;
int len;
/* If our parent is not the init process, it can't be systemd */
if (getppid() != 1)
return false;
if ((fd = open("/proc/1/stat", O_RDONLY)) == -1)
return false;
len = read(fd, stat_buf, sizeof(stat_buf) - 1);
close(fd);
if (len < 0)
return false;
stat_buf[len] = '\0';
if (strcmp(stat_buf, "1 (systemd) "))
return false;
/* systemd sets $NOTIFY_SOCKET to allow returning of notify information,
* but it is only set if the service file has "Type=notify" */
if (!getenv("NOTIFY_SOCKET"))
return false;
parent_is_systemd = true;
return true;
}
void
systemd_notify_running(void)
{
if (parent_is_systemd)
sd_notifyf(0, "READY=1\nMAINPID=%lu", (unsigned long)getpid());
}
void
systemd_notify_reloading(void)
{
if (parent_is_systemd)
sd_notify(0, "RELOADING=1");
}
void
systemd_notify_error(int error_code)
{
if (parent_is_systemd)
sd_notifyf(0, "ERRNO=%d", error_code);
}
void
systemd_notify_stopping(void)
{
if (parent_is_systemd)
sd_notify(0, "STOPPING=1");
}
/* Clear the environment variable NOTIFY_SOCKET to stop any
* child processes we create sending notify messages to systemd */
void
systemd_unset_notify(void)
{
/* This is only called in a child process of the main
* process. If the systemd service type is notify (e.g. Debian),
* then systemd will log:
* keepalived.service: Got notification message from PID nnnn, but reception only permitted for main PID nnnm
* We therefore need to appear as though our parent process (the main process) is sending the message.
*
* In fact, it seems a bit daft to send a message just to clear a local environment variable, but this seems to
* be the only option offered by systemd.
*/
if (parent_is_systemd) {
#ifdef HAVE_SD_PID_NOTIFY
sd_pid_notify(getppid(), 1, "");
#else
/* The above systemd message will be logged, but we don't have the functionality to stop it other
* than setting NotifyAccess=all, but that is dangerous. */
sd_notify(1, "");
#endif
}
}