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
173
174
175
use std::env;
use std::os::unix::net::UnixDatagram;
use std::path::Path;
#[cfg(feature = "async_await")]
pub mod async_await;
#[cfg(feature = "async_io")]
pub mod async_io;
#[derive(Debug)]
pub struct Message(InnerMessage);
impl Message {
pub fn ready() -> Self {
Message(InnerMessage::Ready)
}
pub fn status(status: String) -> Result<Self, std::io::Error> {
if status.as_bytes().iter().any(|x| *x == b'\n') {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"newline not allowed",
));
}
Ok(Message(InnerMessage::Status(status)))
}
pub fn watchdog() -> Self {
Message(InnerMessage::Watchdog)
}
}
#[derive(Debug)]
enum InnerMessage {
Ready,
Status(String),
Watchdog,
}
#[derive(Debug)]
pub enum Error {
NoSocket,
Io(std::io::Error),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::NoSocket => write!(f, "NOTIFY_SOCKET variable not set"),
Error::Io(err) => write!(f, "{}", err),
}
}
}
impl From<env::VarError> for Error {
fn from(_: env::VarError) -> Error {
Error::NoSocket
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Error {
Error::Io(err)
}
}
impl std::error::Error for Error {}
pub struct SdNotify(UnixDatagram);
impl SdNotify {
pub fn from_env() -> Result<Self, Error> {
let sockname = env::var("NOTIFY_SOCKET")?;
Self::from_path(sockname)
}
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
let socket = UnixDatagram::unbound()?;
socket.connect(path)?;
Ok(SdNotify(socket))
}
pub fn notify_ready(&self) -> Result<(), std::io::Error> {
self.state(Message::ready())
}
pub fn set_status(&self, status: String) -> Result<(), std::io::Error> {
self.state(Message::status(status)?)
}
pub fn ping_watchdog(&self) -> Result<(), std::io::Error> {
self.state(Message::watchdog())
}
fn state(&self, state: Message) -> Result<(), std::io::Error> {
match state.0 {
InnerMessage::Ready => self.0.send(b"READY=1")?,
InnerMessage::Status(status) => self.0.send(format!("STATUS={}", status).as_bytes())?,
InnerMessage::Watchdog => self.0.send(b"WATCHDOG=1")?,
};
Ok(())
}
}
#[cfg(test)]
mod tests {
#[test]
fn ok() {
use super::*;
let path = "/tmp/kek-async.sock";
let _ = std::fs::remove_file(path);
let listener = UnixDatagram::bind(path).unwrap();
let notifier = SdNotify::from_path(path).unwrap();
notifier.state(Message::ready()).unwrap();
let mut buf = [0; 100];
listener.recv(&mut buf).unwrap();
assert_eq!(&buf[..7], b"READY=1");
}
}