]> git.proxmox.com Git - proxmox-backup.git/blob - src/tools/daemon.rs
d/control: add ',' after qrencode dependency
[proxmox-backup.git] / src / tools / daemon.rs
1 //! Helpers for daemons/services.
2
3 use std::ffi::CString;
4 use std::future::Future;
5 use std::io::{Read, Write};
6 use std::os::raw::{c_char, c_uchar, c_int};
7 use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
8 use std::os::unix::ffi::OsStrExt;
9 use std::panic::UnwindSafe;
10 use std::pin::Pin;
11 use std::task::{Context, Poll};
12 use std::path::PathBuf;
13
14 use anyhow::{bail, format_err, Error};
15
16 use proxmox::tools::io::{ReadExt, WriteExt};
17
18 use crate::server;
19 use crate::tools::{fd_change_cloexec, self};
20
21 #[link(name = "systemd")]
22 extern "C" {
23 fn sd_journal_stream_fd(identifier: *const c_uchar, priority: c_int, level_prefix: c_int) -> c_int;
24 }
25
26 // Unfortunately FnBox is nightly-only and Box<FnOnce> is unusable, so just use Box<Fn>...
27 pub type BoxedStoreFunc = Box<dyn FnMut() -> Result<String, Error> + UnwindSafe + Send>;
28
29 /// Helper trait to "store" something in the environment to be re-used after re-executing the
30 /// service on a reload.
31 pub trait Reloadable: Sized {
32 fn restore(var: &str) -> Result<Self, Error>;
33 fn get_store_func(&self) -> Result<BoxedStoreFunc, Error>;
34 }
35
36 /// Manages things to be stored and reloaded upon reexec.
37 /// Anything which should be restorable should be instantiated via this struct's `restore` method,
38 #[derive(Default)]
39 pub struct Reloader {
40 pre_exec: Vec<PreExecEntry>,
41 self_exe: PathBuf,
42 }
43
44 // Currently we only need environment variables for storage, but in theory we could also add
45 // variants which need temporary files or pipes...
46 struct PreExecEntry {
47 name: &'static str, // Feel free to change to String if necessary...
48 store_fn: BoxedStoreFunc,
49 }
50
51 impl Reloader {
52 pub fn new() -> Result<Self, Error> {
53 Ok(Self {
54 pre_exec: Vec::new(),
55
56 // Get the path to our executable as PathBuf
57 self_exe: std::fs::read_link("/proc/self/exe")?,
58 })
59 }
60
61 /// Restore an object from an environment variable of the given name, or, if none exists, uses
62 /// the function provided in the `or_create` parameter to instantiate the new "first" instance.
63 ///
64 /// Values created via this method will be remembered for later re-execution.
65 pub async fn restore<T, F, U>(&mut self, name: &'static str, or_create: F) -> Result<T, Error>
66 where
67 T: Reloadable,
68 F: FnOnce() -> U,
69 U: Future<Output = Result<T, Error>>,
70 {
71 let res = match std::env::var(name) {
72 Ok(varstr) => T::restore(&varstr)?,
73 Err(std::env::VarError::NotPresent) => or_create().await?,
74 Err(_) => bail!("variable {} has invalid value", name),
75 };
76
77 self.pre_exec.push(PreExecEntry {
78 name,
79 store_fn: res.get_store_func()?,
80 });
81 Ok(res)
82 }
83
84 fn pre_exec(self) -> Result<(), Error> {
85 for mut item in self.pre_exec {
86 std::env::set_var(item.name, (item.store_fn)()?);
87 }
88 Ok(())
89 }
90
91 pub fn fork_restart(self) -> Result<(), Error> {
92 // Get our parameters as Vec<CString>
93 let args = std::env::args_os();
94 let mut new_args = Vec::with_capacity(args.len());
95 for arg in args {
96 new_args.push(CString::new(arg.as_bytes())?);
97 }
98
99 // Synchronisation pipe:
100 let (pold, pnew) = super::socketpair()?;
101
102 // Start ourselves in the background:
103 use nix::unistd::{fork, ForkResult};
104 match fork() {
105 Ok(ForkResult::Child) => {
106 // Double fork so systemd can supervise us without nagging...
107 match fork() {
108 Ok(ForkResult::Child) => {
109 std::mem::drop(pold);
110 // At this point we call pre-exec helpers. We must be certain that if they fail for
111 // whatever reason we can still call `_exit()`, so use catch_unwind.
112 match std::panic::catch_unwind(move || {
113 let mut pnew = unsafe {
114 std::fs::File::from_raw_fd(pnew.into_raw_fd())
115 };
116 let pid = nix::unistd::Pid::this();
117 if let Err(e) = unsafe { pnew.write_host_value(pid.as_raw()) } {
118 log::error!("failed to send new server PID to parent: {}", e);
119 unsafe {
120 libc::_exit(-1);
121 }
122 }
123
124 let mut ok = [0u8];
125 if let Err(e) = pnew.read_exact(&mut ok) {
126 log::error!("parent vanished before notifying systemd: {}", e);
127 unsafe {
128 libc::_exit(-1);
129 }
130 }
131 assert_eq!(ok[0], 1, "reload handshake should have sent a 1 byte");
132
133 std::mem::drop(pnew);
134
135 // Try to reopen STDOUT/STDERR journald streams to get correct PID in logs
136 let ident = CString::new(self.self_exe.file_name().unwrap().as_bytes()).unwrap();
137 let ident = ident.as_bytes();
138 let fd = unsafe { sd_journal_stream_fd(ident.as_ptr(), libc::LOG_INFO, 1) };
139 if fd >= 0 && fd != 1 {
140 let fd = proxmox::tools::fd::Fd(fd); // add drop handler
141 nix::unistd::dup2(fd.as_raw_fd(), 1)?;
142 } else {
143 log::error!("failed to update STDOUT journal redirection ({})", fd);
144 }
145 let fd = unsafe { sd_journal_stream_fd(ident.as_ptr(), libc::LOG_ERR, 1) };
146 if fd >= 0 && fd != 2 {
147 let fd = proxmox::tools::fd::Fd(fd); // add drop handler
148 nix::unistd::dup2(fd.as_raw_fd(), 2)?;
149 } else {
150 log::error!("failed to update STDERR journal redirection ({})", fd);
151 }
152
153 self.do_reexec(new_args)
154 })
155 {
156 Ok(Ok(())) => eprintln!("do_reexec returned!"),
157 Ok(Err(err)) => eprintln!("do_reexec failed: {}", err),
158 Err(_) => eprintln!("panic in re-exec"),
159 }
160 }
161 Ok(ForkResult::Parent { child }) => {
162 std::mem::drop((pold, pnew));
163 log::debug!("forked off a new server (second pid: {})", child);
164 }
165 Err(e) => log::error!("fork() failed, restart delayed: {}", e),
166 }
167 // No matter how we managed to get here, this is the time where we bail out quickly:
168 unsafe {
169 libc::_exit(-1)
170 }
171 }
172 Ok(ForkResult::Parent { child }) => {
173 log::debug!("forked off a new server (first pid: {}), waiting for 2nd pid", child);
174 std::mem::drop(pnew);
175 let mut pold = unsafe {
176 std::fs::File::from_raw_fd(pold.into_raw_fd())
177 };
178 let child = nix::unistd::Pid::from_raw(match unsafe { pold.read_le_value() } {
179 Ok(v) => v,
180 Err(e) => {
181 log::error!("failed to receive pid of double-forked child process: {}", e);
182 // systemd will complain but won't kill the service...
183 return Ok(());
184 }
185 });
186
187 if let Err(e) = systemd_notify(SystemdNotify::MainPid(child)) {
188 log::error!("failed to notify systemd about the new main pid: {}", e);
189 }
190
191 // notify child that it is now the new main process:
192 if let Err(e) = pold.write_all(&[1u8]) {
193 log::error!("child vanished during reload: {}", e);
194 }
195
196 Ok(())
197 }
198 Err(e) => {
199 log::error!("fork() failed, restart delayed: {}", e);
200 Ok(())
201 }
202 }
203 }
204
205 fn do_reexec(self, args: Vec<CString>) -> Result<(), Error> {
206 let exe = CString::new(self.self_exe.as_os_str().as_bytes())?;
207 self.pre_exec()?;
208 nix::unistd::setsid()?;
209 let args: Vec<&std::ffi::CStr> = args.iter().map(|s| s.as_ref()).collect();
210 nix::unistd::execvp(&exe, &args)?;
211 panic!("exec misbehaved");
212 }
213 }
214
215 // For now all we need to do is store and reuse a tcp listening socket:
216 impl Reloadable for tokio::net::TcpListener {
217 // NOTE: The socket must not be closed when the store-function is called:
218 // FIXME: We could become "independent" of the TcpListener and its reference to the file
219 // descriptor by `dup()`ing it (and check if the listener still exists via kcmp()?)
220 fn get_store_func(&self) -> Result<BoxedStoreFunc, Error> {
221 let mut fd_opt = Some(tools::Fd(
222 nix::fcntl::fcntl(self.as_raw_fd(), nix::fcntl::FcntlArg::F_DUPFD_CLOEXEC(0))?
223 ));
224 Ok(Box::new(move || {
225 let fd = fd_opt.take().unwrap();
226 fd_change_cloexec(fd.as_raw_fd(), false)?;
227 Ok(fd.into_raw_fd().to_string())
228 }))
229 }
230
231 fn restore(var: &str) -> Result<Self, Error> {
232 let fd = var.parse::<u32>()
233 .map_err(|e| format_err!("invalid file descriptor: {}", e))?
234 as RawFd;
235 fd_change_cloexec(fd, true)?;
236 Ok(Self::from_std(
237 unsafe { std::net::TcpListener::from_raw_fd(fd) },
238 )?)
239 }
240 }
241
242 pub struct NotifyReady;
243
244 impl Future for NotifyReady {
245 type Output = Result<(), Error>;
246
247 fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Result<(), Error>> {
248 systemd_notify(SystemdNotify::Ready)?;
249 Poll::Ready(Ok(()))
250 }
251 }
252
253 /// This creates a future representing a daemon which reloads itself when receiving a SIGHUP.
254 /// If this is started regularly, a listening socket is created. In this case, the file descriptor
255 /// number will be remembered in `PROXMOX_BACKUP_LISTEN_FD`.
256 /// If the variable already exists, its contents will instead be used to restore the listening
257 /// socket. The finished listening socket is then passed to the `create_service` function which
258 /// can be used to setup the TLS and the HTTP daemon.
259 pub async fn create_daemon<F, S>(
260 address: std::net::SocketAddr,
261 create_service: F,
262 ) -> Result<(), Error>
263 where
264 F: FnOnce(tokio::net::TcpListener, NotifyReady) -> Result<S, Error>,
265 S: Future<Output = ()>,
266 {
267 let mut reloader = Reloader::new()?;
268
269 let listener: tokio::net::TcpListener = reloader.restore(
270 "PROXMOX_BACKUP_LISTEN_FD",
271 move || async move { Ok(tokio::net::TcpListener::bind(&address).await?) },
272 ).await?;
273
274 create_service(listener, NotifyReady)?.await;
275
276 let mut reloader = Some(reloader);
277
278 crate::tools::request_shutdown(); // make sure we are in shutdown mode
279 if server::is_reload_request() {
280 log::info!("daemon reload...");
281 if let Err(e) = systemd_notify(SystemdNotify::Reloading) {
282 log::error!("failed to notify systemd about the state change: {}", e);
283 }
284 if let Err(e) = reloader.take().unwrap().fork_restart() {
285 log::error!("error during reload: {}", e);
286 let _ = systemd_notify(SystemdNotify::Status("error during reload".to_string()));
287 }
288 } else {
289 log::info!("daemon shutting down...");
290 }
291 Ok(())
292 }
293
294 #[link(name = "systemd")]
295 extern "C" {
296 fn sd_notify(unset_environment: c_int, state: *const c_char) -> c_int;
297 }
298
299 pub enum SystemdNotify {
300 Ready,
301 Reloading,
302 Stopping,
303 Status(String),
304 MainPid(nix::unistd::Pid),
305 }
306
307 pub fn systemd_notify(state: SystemdNotify) -> Result<(), Error> {
308 let message = match state {
309 SystemdNotify::Ready => CString::new("READY=1"),
310 SystemdNotify::Reloading => CString::new("RELOADING=1"),
311 SystemdNotify::Stopping => CString::new("STOPPING=1"),
312 SystemdNotify::Status(msg) => CString::new(format!("STATUS={}", msg)),
313 SystemdNotify::MainPid(pid) => CString::new(format!("MAINPID={}", pid)),
314 }?;
315 let rc = unsafe { sd_notify(0, message.as_ptr()) };
316 if rc < 0 {
317 bail!(
318 "systemd_notify failed: {}",
319 std::io::Error::from_raw_os_error(-rc),
320 );
321 }
322 Ok(())
323 }