1 //! Helpers for daemons/services.
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
;
11 use std
::task
::{Context, Poll}
;
12 use std
::path
::PathBuf
;
14 use anyhow
::{bail, format_err, Error}
;
16 use proxmox
::tools
::io
::{ReadExt, WriteExt}
;
19 use crate::tools
::{fd_change_cloexec, self}
;
21 #[link(name = "systemd")]
23 fn sd_journal_stream_fd(identifier
: *const c_uchar
, priority
: c_int
, level_prefix
: c_int
) -> c_int
;
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
>;
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
>;
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,
40 pre_exec
: Vec
<PreExecEntry
>,
44 // Currently we only need environment variables for storage, but in theory we could also add
45 // variants which need temporary files or pipes...
47 name
: &'
static str, // Feel free to change to String if necessary...
48 store_fn
: BoxedStoreFunc
,
52 pub fn new() -> Result
<Self, Error
> {
56 // Get the path to our executable as PathBuf
57 self_exe
: std
::fs
::read_link("/proc/self/exe")?
,
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.
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
>
69 U
: Future
<Output
= Result
<T
, Error
>>,
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
),
77 self.pre_exec
.push(PreExecEntry
{
79 store_fn
: res
.get_store_func()?
,
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
)()?
);
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());
96 new_args
.push(CString
::new(arg
.as_bytes())?
);
99 // Synchronisation pipe:
100 let (pold
, pnew
) = super::socketpair()?
;
102 // Start ourselves in the background:
103 use nix
::unistd
::{fork, ForkResult}
;
105 Ok(ForkResult
::Child
) => {
106 // Double fork so systemd can supervise us without nagging...
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())
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
);
125 if let Err(e
) = pnew
.read_exact(&mut ok
) {
126 log
::error
!("parent vanished before notifying systemd: {}", e
);
131 assert_eq
!(ok
[0], 1, "reload handshake should have sent a 1 byte");
133 std
::mem
::drop(pnew
);
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)?
;
143 log
::error
!("failed to update STDOUT journal redirection ({})", fd
);
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)?
;
150 log
::error
!("failed to update STDERR journal redirection ({})", fd
);
153 self.do_reexec(new_args
)
156 Ok(Ok(())) => eprintln
!("do_reexec returned!"),
157 Ok(Err(err
)) => eprintln
!("do_reexec failed: {}", err
),
158 Err(_
) => eprintln
!("panic in re-exec"),
161 Ok(ForkResult
::Parent { child }
) => {
162 std
::mem
::drop((pold
, pnew
));
163 log
::debug
!("forked off a new server (second pid: {})", child
);
165 Err(e
) => log
::error
!("fork() failed, restart delayed: {}", e
),
167 // No matter how we managed to get here, this is the time where we bail out quickly:
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())
178 let child
= nix
::unistd
::Pid
::from_raw(match unsafe { pold.read_le_value() }
{
181 log
::error
!("failed to receive pid of double-forked child process: {}", e
);
182 // systemd will complain but won't kill the service...
187 if let Err(e
) = systemd_notify(SystemdNotify
::MainPid(child
)) {
188 log
::error
!("failed to notify systemd about the new main pid: {}", e
);
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
);
199 log
::error
!("fork() failed, restart delayed: {}", e
);
205 fn do_reexec(self, args
: Vec
<CString
>) -> Result
<(), Error
> {
206 let exe
= CString
::new(self.self_exe
.as_os_str().as_bytes())?
;
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");
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))?
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())
231 fn restore(var
: &str) -> Result
<Self, Error
> {
232 let fd
= var
.parse
::<u32>()
233 .map_err(|e
| format_err
!("invalid file descriptor: {}", e
))?
235 fd_change_cloexec(fd
, true)?
;
237 unsafe { std::net::TcpListener::from_raw_fd(fd) }
,
242 pub struct NotifyReady
;
244 impl Future
for NotifyReady
{
245 type Output
= Result
<(), Error
>;
247 fn poll(self: Pin
<&mut Self>, _cx
: &mut Context
) -> Poll
<Result
<(), Error
>> {
248 systemd_notify(SystemdNotify
::Ready
)?
;
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
,
262 ) -> Result
<(), Error
>
264 F
: FnOnce(tokio
::net
::TcpListener
, NotifyReady
) -> Result
<S
, Error
>,
265 S
: Future
<Output
= ()>,
267 let mut reloader
= Reloader
::new()?
;
269 let listener
: tokio
::net
::TcpListener
= reloader
.restore(
270 "PROXMOX_BACKUP_LISTEN_FD",
271 move || async
move { Ok(tokio::net::TcpListener::bind(&address).await?) }
,
274 create_service(listener
, NotifyReady
)?
.await
;
276 let mut reloader
= Some(reloader
);
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
);
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()));
289 log
::info
!("daemon shutting down...");
294 #[link(name = "systemd")]
296 fn sd_notify(unset_environment
: c_int
, state
: *const c_char
) -> c_int
;
299 pub enum SystemdNotify
{
304 MainPid(nix
::unistd
::Pid
),
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
)),
315 let rc
= unsafe { sd_notify(0, message.as_ptr()) }
;
318 "systemd_notify failed: {}",
319 std
::io
::Error
::from_raw_os_error(-rc
),