-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemd.cpp
More file actions
76 lines (57 loc) · 1.47 KB
/
systemd.cpp
File metadata and controls
76 lines (57 loc) · 1.47 KB
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
// Copyright - 2021 - Jan Christoph Uhde <[email protected]>
#include <ext/systemd.hpp>
#ifdef EXT_SYSTEMD_AVAILABLE
extern "C" {
#include <systemd/sd-daemon.h>
#include <string.h>
}
#endif
namespace ext { namespace systemd {
#ifdef EXT_SYSTEMD_AVAILABLE
bool active() {
return true;
}
bool notify_ready(){
return sd_notify(0 /*unset_environment*/, "READY=1") > 0;
}
bool notify_alive(){
return sd_notify(0 /*unset_environment*/, "WATCHDOG=1") > 0;
}
bool notify_stopping(){
return sd_notify(0 /*unset_environment*/, "STOPPING=1") > 0;
}
bool notify_error(int errno){
return sd_notifyf(0 /*unset_environment*/
,"STATUS=Failed: %s\n"
"ERRNO=%i"
, strerror(errno), errno
) > 0;
}
bool notify_error(std::string const& reason, int errno){
// use std::format in the future
using namespace std::literals::string_literals;
std::string block = ( "STATUS=Failed: "s + reason );
block += ( "\nERRNO="s + std::to_string(errno) );
return sd_notify(0 /*unset_environment*/, block.data()) > 0;
}
#else // not EXT_SYSTEMD_AVAILABLE
bool active() {
return false;
}
bool notify_ready(){
return true;
}
bool notify_alive(){
return true;
}
bool notify_stopping(){
return true;
}
bool notify_error(int errno){
return true;
}
bool notify_error(std::string const& reason, int errno){
return true;
}
#endif // EXT_SYSTEMD_AVAILABLE
}} // namespace ext::systemd