]>
Commit | Line | Data |
---|---|---|
dce94d0e WB |
1 | //! Helpers for daemons/services. |
2 | ||
3 | use std::ffi::CString; | |
9c351a36 | 4 | use std::os::raw::{c_char, c_int}; |
620dccf1 | 5 | use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; |
dce94d0e WB |
6 | use std::os::unix::ffi::OsStrExt; |
7 | use std::panic::UnwindSafe; | |
8 | ||
9 | use failure::*; | |
4422ba2c WB |
10 | use tokio::prelude::*; |
11 | ||
e3f41f21 | 12 | use crate::server; |
620dccf1 | 13 | use crate::tools::{fd_change_cloexec, self}; |
dce94d0e WB |
14 | |
15 | // Unfortunately FnBox is nightly-only and Box<FnOnce> is unusable, so just use Box<Fn>... | |
620dccf1 | 16 | pub type BoxedStoreFunc = Box<dyn FnMut() -> Result<String, Error> + UnwindSafe + Send>; |
dce94d0e WB |
17 | |
18 | /// Helper trait to "store" something in the environment to be re-used after re-executing the | |
19 | /// service on a reload. | |
e4311382 | 20 | pub trait Reloadable: Sized { |
dce94d0e | 21 | fn restore(var: &str) -> Result<Self, Error>; |
620dccf1 | 22 | fn get_store_func(&self) -> Result<BoxedStoreFunc, Error>; |
dce94d0e WB |
23 | } |
24 | ||
25 | /// Manages things to be stored and reloaded upon reexec. | |
26 | /// Anything which should be restorable should be instantiated via this struct's `restore` method, | |
e4311382 | 27 | pub struct Reloader { |
dce94d0e WB |
28 | pre_exec: Vec<PreExecEntry>, |
29 | } | |
30 | ||
31 | // Currently we only need environment variables for storage, but in theory we could also add | |
32 | // variants which need temporary files or pipes... | |
33 | struct PreExecEntry { | |
34 | name: &'static str, // Feel free to change to String if necessary... | |
35 | store_fn: BoxedStoreFunc, | |
36 | } | |
37 | ||
e4311382 | 38 | impl Reloader { |
dce94d0e WB |
39 | pub fn new() -> Self { |
40 | Self { | |
41 | pre_exec: Vec::new(), | |
42 | } | |
43 | } | |
44 | ||
45 | /// Restore an object from an environment variable of the given name, or, if none exists, uses | |
46 | /// the function provided in the `or_create` parameter to instantiate the new "first" instance. | |
47 | /// | |
48 | /// Values created via this method will be remembered for later re-execution. | |
49 | pub fn restore<T, F>(&mut self, name: &'static str, or_create: F) -> Result<T, Error> | |
50 | where | |
e4311382 | 51 | T: Reloadable, |
dce94d0e WB |
52 | F: FnOnce() -> Result<T, Error>, |
53 | { | |
54 | let res = match std::env::var(name) { | |
55 | Ok(varstr) => T::restore(&varstr)?, | |
56 | Err(std::env::VarError::NotPresent) => or_create()?, | |
57 | Err(_) => bail!("variable {} has invalid value", name), | |
58 | }; | |
59 | ||
60 | self.pre_exec.push(PreExecEntry { | |
61 | name, | |
620dccf1 | 62 | store_fn: res.get_store_func()?, |
dce94d0e WB |
63 | }); |
64 | Ok(res) | |
65 | } | |
66 | ||
67 | fn pre_exec(self) -> Result<(), Error> { | |
620dccf1 | 68 | for mut item in self.pre_exec { |
dce94d0e WB |
69 | std::env::set_var(item.name, (item.store_fn)()?); |
70 | } | |
71 | Ok(()) | |
72 | } | |
73 | ||
74 | pub fn fork_restart(self) -> Result<(), Error> { | |
75 | // Get the path to our executable as CString | |
76 | let exe = CString::new( | |
77 | std::fs::read_link("/proc/self/exe")? | |
78 | .into_os_string() | |
79 | .as_bytes() | |
80 | )?; | |
81 | ||
82 | // Get our parameters as Vec<CString> | |
83 | let args = std::env::args_os(); | |
84 | let mut new_args = Vec::with_capacity(args.len()); | |
85 | for arg in args { | |
86 | new_args.push(CString::new(arg.as_bytes())?); | |
87 | } | |
88 | ||
89 | // Start ourselves in the background: | |
90 | use nix::unistd::{fork, ForkResult}; | |
91 | match fork() { | |
92 | Ok(ForkResult::Child) => { | |
93 | // At this point we call pre-exec helpers. We must be certain that if they fail for | |
94 | // whatever reason we can still call `_exit()`, so use catch_unwind. | |
95 | match std::panic::catch_unwind(move || self.do_exec(exe, new_args)) { | |
96 | Ok(_) => eprintln!("do_exec returned unexpectedly!"), | |
97 | Err(_) => eprintln!("panic in re-exec"), | |
98 | } | |
99 | // No matter how we managed to get here, this is the time where we bail out quickly: | |
100 | unsafe { | |
101 | libc::_exit(-1) | |
102 | } | |
103 | } | |
104 | Ok(ForkResult::Parent { child }) => { | |
105 | eprintln!("forked off a new server (pid: {})", child); | |
106 | Ok(()) | |
107 | } | |
108 | Err(e) => { | |
109 | eprintln!("fork() failed, restart delayed: {}", e); | |
110 | Ok(()) | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
115 | fn do_exec(self, exe: CString, args: Vec<CString>) -> Result<(), Error> { | |
116 | self.pre_exec()?; | |
117 | nix::unistd::setsid()?; | |
118 | nix::unistd::execvp(&exe, &args)?; | |
119 | Ok(()) | |
120 | } | |
121 | } | |
4422ba2c | 122 | |
af70c181 | 123 | // For now all we need to do is store and reuse a tcp listening socket: |
e4311382 | 124 | impl Reloadable for tokio::net::TcpListener { |
af70c181 WB |
125 | // NOTE: The socket must not be closed when the store-function is called: |
126 | // FIXME: We could become "independent" of the TcpListener and its reference to the file | |
127 | // descriptor by `dup()`ing it (and check if the listener still exists via kcmp()?) | |
620dccf1 WB |
128 | fn get_store_func(&self) -> Result<BoxedStoreFunc, Error> { |
129 | let mut fd_opt = Some(tools::Fd( | |
130 | nix::fcntl::fcntl(self.as_raw_fd(), nix::fcntl::FcntlArg::F_DUPFD_CLOEXEC(0))? | |
131 | )); | |
132 | Ok(Box::new(move || { | |
133 | let fd = fd_opt.take().unwrap(); | |
134 | fd_change_cloexec(fd.as_raw_fd(), false)?; | |
135 | Ok(fd.into_raw_fd().to_string()) | |
136 | })) | |
af70c181 WB |
137 | } |
138 | ||
139 | fn restore(var: &str) -> Result<Self, Error> { | |
140 | let fd = var.parse::<u32>() | |
141 | .map_err(|e| format_err!("invalid file descriptor: {}", e))? | |
142 | as RawFd; | |
143 | fd_change_cloexec(fd, true)?; | |
144 | Ok(Self::from_std( | |
145 | unsafe { std::net::TcpListener::from_raw_fd(fd) }, | |
146 | &tokio::reactor::Handle::default(), | |
147 | )?) | |
148 | } | |
149 | } | |
a690ecac WB |
150 | |
151 | /// This creates a future representing a daemon which reloads itself when receiving a SIGHUP. | |
152 | /// If this is started regularly, a listening socket is created. In this case, the file descriptor | |
153 | /// number will be remembered in `PROXMOX_BACKUP_LISTEN_FD`. | |
154 | /// If the variable already exists, its contents will instead be used to restore the listening | |
155 | /// socket. The finished listening socket is then passed to the `create_service` function which | |
156 | /// can be used to setup the TLS and the HTTP daemon. | |
157 | pub fn create_daemon<F, S>( | |
158 | address: std::net::SocketAddr, | |
159 | create_service: F, | |
160 | ) -> Result<impl Future<Item = (), Error = ()>, Error> | |
161 | where | |
162 | F: FnOnce(tokio::net::TcpListener) -> Result<S, Error>, | |
163 | S: Future<Item = (), Error = ()>, | |
164 | { | |
165 | let mut reloader = Reloader::new(); | |
166 | ||
167 | let listener: tokio::net::TcpListener = reloader.restore( | |
168 | "PROXMOX_BACKUP_LISTEN_FD", | |
169 | move || Ok(tokio::net::TcpListener::bind(&address)?), | |
170 | )?; | |
171 | ||
172 | let service = create_service(listener)?; | |
173 | ||
a690ecac WB |
174 | let mut reloader = Some(reloader); |
175 | ||
e3f41f21 | 176 | Ok(service |
e3f41f21 DM |
177 | .map(move |_| { |
178 | crate::tools::request_shutdown(); // make sure we are in shutdown mode | |
179 | if server::is_reload_request() { | |
180 | log::info!("daemon reload..."); | |
181 | if let Err(e) = reloader.take().unwrap().fork_restart() { | |
182 | log::error!("error during reload: {}", e); | |
183 | } | |
184 | } else { | |
185 | log::info!("daemon shutting down..."); | |
186 | } | |
9136f857 DM |
187 | }) |
188 | .map_err(|_| ()) | |
a690ecac WB |
189 | ) |
190 | } | |
9c351a36 WB |
191 | |
192 | #[link(name = "systemd")] | |
193 | extern "C" { | |
194 | fn sd_notify(unset_environment: c_int, state: *const c_char) -> c_int; | |
195 | } | |
196 | ||
197 | pub enum SystemdNotify { | |
198 | Ready, | |
199 | Reloading, | |
200 | Stopping, | |
201 | Status(String), | |
202 | MainPid(nix::unistd::Pid), | |
203 | } | |
204 | ||
205 | pub fn systemd_notify(state: SystemdNotify) -> Result<(), Error> { | |
206 | let message = match state { | |
207 | SystemdNotify::Ready => CString::new("READY=1"), | |
208 | SystemdNotify::Reloading => CString::new("RELOADING=1"), | |
209 | SystemdNotify::Stopping => CString::new("STOPPING=1"), | |
210 | SystemdNotify::Status(msg) => CString::new(format!("STATUS={}", msg)), | |
211 | SystemdNotify::MainPid(pid) => CString::new(format!("MAINPID={}", pid)), | |
212 | }?; | |
213 | let rc = unsafe { sd_notify(0, message.as_ptr()) }; | |
214 | if rc < 0 { | |
215 | bail!( | |
216 | "systemd_notify failed: {}", | |
217 | std::io::Error::from_raw_os_error(-rc), | |
218 | ); | |
219 | } | |
220 | Ok(()) | |
221 | } |