]>
Commit | Line | Data |
---|---|---|
51b499db DM |
1 | //! Tools and utilities |
2 | //! | |
3 | //! This is a collection of small and useful tools. | |
6100071f WB |
4 | use std::any::Any; |
5 | use std::collections::HashMap; | |
62ee2eb4 | 6 | use std::hash::BuildHasher; |
365bb90f | 7 | use std::fs::{File, OpenOptions}; |
386990ba | 8 | use std::io::{self, BufRead, ErrorKind, Read}; |
00ec8d16 | 9 | use std::os::unix::io::{AsRawFd, RawFd}; |
6100071f WB |
10 | use std::path::Path; |
11 | use std::time::Duration; | |
e693818a | 12 | use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH}; |
365bb90f | 13 | |
f7d4e4b5 | 14 | use anyhow::{bail, format_err, Error}; |
af926291 | 15 | use serde_json::Value; |
c5946faf | 16 | use openssl::hash::{hash, DigestBytes, MessageDigest}; |
8a1028e0 | 17 | use percent_encoding::AsciiSet; |
0fe5d605 | 18 | |
f35197f4 | 19 | use proxmox::tools::vec; |
a4acb6ef | 20 | use proxmox::sys::error::SysResult; |
f35197f4 | 21 | |
00ec8d16 WB |
22 | pub use proxmox::tools::fd::Fd; |
23 | ||
6100071f | 24 | pub mod acl; |
556eb70e | 25 | pub mod async_io; |
6ed25cbe | 26 | pub mod borrow; |
ec01eead | 27 | pub mod cert; |
6100071f | 28 | pub mod daemon; |
10effc98 | 29 | pub mod disks; |
b4d5787d | 30 | pub mod fs; |
4939255f | 31 | pub mod format; |
1685c2e3 | 32 | pub mod lru_cache; |
daef93f4 | 33 | pub mod runtime; |
6100071f WB |
34 | pub mod ticket; |
35 | pub mod timer; | |
ba974798 | 36 | pub mod statistics; |
f486e9e5 | 37 | pub mod systemd; |
177a2de9 | 38 | pub mod nom; |
f1d99e3f DM |
39 | |
40 | mod wrapped_reader_stream; | |
41 | pub use wrapped_reader_stream::*; | |
dcd033a5 DM |
42 | |
43 | mod std_channel_writer; | |
44 | pub use std_channel_writer::*; | |
45 | ||
2dcdd3b4 | 46 | pub mod xattr; |
8cf6e764 | 47 | |
a650f503 DM |
48 | mod process_locker; |
49 | pub use process_locker::*; | |
50 | ||
3b151414 DM |
51 | mod file_logger; |
52 | pub use file_logger::*; | |
53 | ||
490be29e DM |
54 | mod broadcast_future; |
55 | pub use broadcast_future::*; | |
56 | ||
fded74d0 | 57 | /// The `BufferedRead` trait provides a single function |
0a72e267 DM |
58 | /// `buffered_read`. It returns a reference to an internal buffer. The |
59 | /// purpose of this traid is to avoid unnecessary data copies. | |
fded74d0 | 60 | pub trait BufferedRead { |
318564ac DM |
61 | /// This functions tries to fill the internal buffers, then |
62 | /// returns a reference to the available data. It returns an empty | |
63 | /// buffer if `offset` points to the end of the file. | |
0a72e267 DM |
64 | fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error>; |
65 | } | |
66 | ||
51b499db DM |
67 | /// Directly map a type into a binary buffer. This is mostly useful |
68 | /// for reading structured data from a byte stream (file). You need to | |
69 | /// make sure that the buffer location does not change, so please | |
70 | /// avoid vec resize while you use such map. | |
71 | /// | |
72 | /// This function panics if the buffer is not large enough. | |
50ea4396 | 73 | pub fn map_struct<T>(buffer: &[u8]) -> Result<&T, Error> { |
dc3de618 DM |
74 | if buffer.len() < ::std::mem::size_of::<T>() { |
75 | bail!("unable to map struct - buffer too small"); | |
76 | } | |
6100071f | 77 | Ok(unsafe { &*(buffer.as_ptr() as *const T) }) |
dc3de618 DM |
78 | } |
79 | ||
51b499db DM |
80 | /// Directly map a type into a mutable binary buffer. This is mostly |
81 | /// useful for writing structured data into a byte stream (file). You | |
82 | /// need to make sure that the buffer location does not change, so | |
83 | /// please avoid vec resize while you use such map. | |
84 | /// | |
85 | /// This function panics if the buffer is not large enough. | |
50ea4396 | 86 | pub fn map_struct_mut<T>(buffer: &mut [u8]) -> Result<&mut T, Error> { |
dc3de618 DM |
87 | if buffer.len() < ::std::mem::size_of::<T>() { |
88 | bail!("unable to map struct - buffer too small"); | |
89 | } | |
6100071f | 90 | Ok(unsafe { &mut *(buffer.as_ptr() as *mut T) }) |
dc3de618 DM |
91 | } |
92 | ||
51b499db DM |
93 | /// Create a file lock using fntl. This function allows you to specify |
94 | /// a timeout if you want to avoid infinite blocking. | |
8cd29fb2 SR |
95 | /// |
96 | /// With timeout set to 0, non-blocking mode is used and the function | |
97 | /// will fail immediately if the lock can't be acquired. | |
1628a4c7 WB |
98 | pub fn lock_file<F: AsRawFd>( |
99 | file: &mut F, | |
100 | exclusive: bool, | |
101 | timeout: Option<Duration>, | |
a4acb6ef | 102 | ) -> Result<(), io::Error> { |
6100071f WB |
103 | let lockarg = if exclusive { |
104 | nix::fcntl::FlockArg::LockExclusive | |
105 | } else { | |
106 | nix::fcntl::FlockArg::LockShared | |
107 | }; | |
1628a4c7 WB |
108 | |
109 | let timeout = match timeout { | |
110 | None => { | |
a4acb6ef | 111 | nix::fcntl::flock(file.as_raw_fd(), lockarg).into_io_result()?; |
1628a4c7 WB |
112 | return Ok(()); |
113 | } | |
114 | Some(t) => t, | |
115 | }; | |
116 | ||
8cd29fb2 SR |
117 | if timeout.as_nanos() == 0 { |
118 | let lockarg = if exclusive { | |
119 | nix::fcntl::FlockArg::LockExclusiveNonblock | |
120 | } else { | |
121 | nix::fcntl::FlockArg::LockSharedNonblock | |
122 | }; | |
a4acb6ef | 123 | nix::fcntl::flock(file.as_raw_fd(), lockarg).into_io_result()?; |
8cd29fb2 SR |
124 | return Ok(()); |
125 | } | |
126 | ||
1628a4c7 WB |
127 | // unblock the timeout signal temporarily |
128 | let _sigblock_guard = timer::unblock_timeout_signal(); | |
129 | ||
130 | // setup a timeout timer | |
131 | let mut timer = timer::Timer::create( | |
132 | timer::Clock::Realtime, | |
6100071f WB |
133 | timer::TimerEvent::ThisThreadSignal(timer::SIGTIMEOUT), |
134 | )?; | |
1628a4c7 | 135 | |
6100071f WB |
136 | timer.arm( |
137 | timer::TimerSpec::new() | |
138 | .value(Some(timeout)) | |
139 | .interval(Some(Duration::from_millis(10))), | |
140 | )?; | |
1628a4c7 | 141 | |
a4acb6ef | 142 | nix::fcntl::flock(file.as_raw_fd(), lockarg).into_io_result()?; |
1628a4c7 WB |
143 | Ok(()) |
144 | } | |
365bb90f | 145 | |
51b499db | 146 | /// Open or create a lock file (append mode). Then try to |
add5861e | 147 | /// acquire a lock using `lock_file()`. |
6100071f | 148 | pub fn open_file_locked<P: AsRef<Path>>(path: P, timeout: Duration) -> Result<File, Error> { |
1628a4c7 | 149 | let path = path.as_ref(); |
6100071f WB |
150 | let mut file = match OpenOptions::new().create(true).append(true).open(path) { |
151 | Ok(file) => file, | |
152 | Err(err) => bail!("Unable to open lock {:?} - {}", path, err), | |
153 | }; | |
28b96b56 DM |
154 | match lock_file(&mut file, true, Some(timeout)) { |
155 | Ok(_) => Ok(file), | |
add5861e | 156 | Err(err) => bail!("Unable to acquire lock {:?} - {}", path, err), |
28b96b56 | 157 | } |
365bb90f DM |
158 | } |
159 | ||
51b499db DM |
160 | /// Split a file into equal sized chunks. The last chunk may be |
161 | /// smaller. Note: We cannot implement an `Iterator`, because iterators | |
162 | /// cannot return a borrowed buffer ref (we want zero-copy) | |
6100071f WB |
163 | pub fn file_chunker<C, R>(mut file: R, chunk_size: usize, mut chunk_cb: C) -> Result<(), Error> |
164 | where | |
165 | C: FnMut(usize, &[u8]) -> Result<bool, Error>, | |
166 | R: Read, | |
43eeef28 | 167 | { |
6100071f | 168 | const READ_BUFFER_SIZE: usize = 4 * 1024 * 1024; // 4M |
43eeef28 | 169 | |
6100071f WB |
170 | if chunk_size > READ_BUFFER_SIZE { |
171 | bail!("chunk size too large!"); | |
172 | } | |
43eeef28 | 173 | |
8ea3b1d1 | 174 | let mut buf = vec::undefined(READ_BUFFER_SIZE); |
43eeef28 DM |
175 | |
176 | let mut pos = 0; | |
177 | let mut file_pos = 0; | |
178 | loop { | |
179 | let mut eof = false; | |
180 | let mut tmp = &mut buf[..]; | |
6100071f | 181 | // try to read large portions, at least chunk_size |
43eeef28 DM |
182 | while pos < chunk_size { |
183 | match file.read(tmp) { | |
6100071f WB |
184 | Ok(0) => { |
185 | eof = true; | |
186 | break; | |
187 | } | |
43eeef28 DM |
188 | Ok(n) => { |
189 | pos += n; | |
6100071f WB |
190 | if pos > chunk_size { |
191 | break; | |
192 | } | |
43eeef28 DM |
193 | tmp = &mut tmp[n..]; |
194 | } | |
195 | Err(ref e) if e.kind() == ErrorKind::Interrupted => { /* try again */ } | |
5f0c2d56 | 196 | Err(e) => bail!("read chunk failed - {}", e.to_string()), |
43eeef28 DM |
197 | } |
198 | } | |
43eeef28 DM |
199 | let mut start = 0; |
200 | while start + chunk_size <= pos { | |
6100071f WB |
201 | if !(chunk_cb)(file_pos, &buf[start..start + chunk_size])? { |
202 | break; | |
203 | } | |
43eeef28 DM |
204 | file_pos += chunk_size; |
205 | start += chunk_size; | |
206 | } | |
207 | if eof { | |
208 | if start < pos { | |
209 | (chunk_cb)(file_pos, &buf[start..pos])?; | |
210 | //file_pos += pos - start; | |
211 | } | |
212 | break; | |
213 | } else { | |
214 | let rest = pos - start; | |
215 | if rest > 0 { | |
216 | let ptr = buf.as_mut_ptr(); | |
6100071f WB |
217 | unsafe { |
218 | std::ptr::copy_nonoverlapping(ptr.add(start), ptr, rest); | |
219 | } | |
43eeef28 DM |
220 | pos = rest; |
221 | } else { | |
222 | pos = 0; | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
227 | Ok(()) | |
43eeef28 | 228 | } |
0fe5d605 | 229 | |
f5f13ebc | 230 | pub fn json_object_to_query(data: Value) -> Result<String, Error> { |
f5f13ebc DM |
231 | let mut query = url::form_urlencoded::Serializer::new(String::new()); |
232 | ||
233 | let object = data.as_object().ok_or_else(|| { | |
234 | format_err!("json_object_to_query: got wrong data type (expected object).") | |
235 | })?; | |
236 | ||
237 | for (key, value) in object { | |
238 | match value { | |
6100071f WB |
239 | Value::Bool(b) => { |
240 | query.append_pair(key, &b.to_string()); | |
241 | } | |
242 | Value::Number(n) => { | |
243 | query.append_pair(key, &n.to_string()); | |
244 | } | |
245 | Value::String(s) => { | |
246 | query.append_pair(key, &s); | |
247 | } | |
f5f13ebc DM |
248 | Value::Array(arr) => { |
249 | for element in arr { | |
250 | match element { | |
6100071f WB |
251 | Value::Bool(b) => { |
252 | query.append_pair(key, &b.to_string()); | |
253 | } | |
254 | Value::Number(n) => { | |
255 | query.append_pair(key, &n.to_string()); | |
256 | } | |
257 | Value::String(s) => { | |
258 | query.append_pair(key, &s); | |
259 | } | |
260 | _ => bail!( | |
261 | "json_object_to_query: unable to handle complex array data types." | |
262 | ), | |
f5f13ebc DM |
263 | } |
264 | } | |
265 | } | |
266 | _ => bail!("json_object_to_query: unable to handle complex data types."), | |
267 | } | |
268 | } | |
269 | ||
270 | Ok(query.finish()) | |
271 | } | |
272 | ||
0fe5d605 | 273 | pub fn required_string_param<'a>(param: &'a Value, name: &str) -> Result<&'a str, Error> { |
6100071f | 274 | match param[name].as_str() { |
0fe5d605 DM |
275 | Some(s) => Ok(s), |
276 | None => bail!("missing parameter '{}'", name), | |
277 | } | |
278 | } | |
0d38dcb4 | 279 | |
e17d5d86 DM |
280 | pub fn required_string_property<'a>(param: &'a Value, name: &str) -> Result<&'a str, Error> { |
281 | match param[name].as_str() { | |
282 | Some(s) => Ok(s), | |
283 | None => bail!("missing property '{}'", name), | |
284 | } | |
285 | } | |
286 | ||
0d38dcb4 | 287 | pub fn required_integer_param<'a>(param: &'a Value, name: &str) -> Result<i64, Error> { |
6100071f | 288 | match param[name].as_i64() { |
0d38dcb4 DM |
289 | Some(s) => Ok(s), |
290 | None => bail!("missing parameter '{}'", name), | |
f8dfbb45 DM |
291 | } |
292 | } | |
293 | ||
e17d5d86 DM |
294 | pub fn required_integer_property<'a>(param: &'a Value, name: &str) -> Result<i64, Error> { |
295 | match param[name].as_i64() { | |
296 | Some(s) => Ok(s), | |
297 | None => bail!("missing property '{}'", name), | |
298 | } | |
299 | } | |
300 | ||
f8dfbb45 | 301 | pub fn required_array_param<'a>(param: &'a Value, name: &str) -> Result<Vec<Value>, Error> { |
6100071f | 302 | match param[name].as_array() { |
f8dfbb45 DM |
303 | Some(s) => Ok(s.to_vec()), |
304 | None => bail!("missing parameter '{}'", name), | |
0d38dcb4 DM |
305 | } |
306 | } | |
383e8577 | 307 | |
e17d5d86 DM |
308 | pub fn required_array_property<'a>(param: &'a Value, name: &str) -> Result<Vec<Value>, Error> { |
309 | match param[name].as_array() { | |
310 | Some(s) => Ok(s.to_vec()), | |
311 | None => bail!("missing property '{}'", name), | |
312 | } | |
313 | } | |
314 | ||
62ee2eb4 | 315 | pub fn complete_file_name<S: BuildHasher>(arg: &str, _param: &HashMap<String, String, S>) -> Vec<String> { |
383e8577 DM |
316 | let mut result = vec![]; |
317 | ||
6100071f | 318 | use nix::fcntl::AtFlags; |
383e8577 DM |
319 | use nix::fcntl::OFlag; |
320 | use nix::sys::stat::Mode; | |
383e8577 | 321 | |
62ee2eb4 | 322 | let mut dirname = std::path::PathBuf::from(if arg.is_empty() { "./" } else { arg }); |
383e8577 DM |
323 | |
324 | let is_dir = match nix::sys::stat::fstatat(libc::AT_FDCWD, &dirname, AtFlags::empty()) { | |
325 | Ok(stat) => (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR, | |
326 | Err(_) => false, | |
327 | }; | |
328 | ||
329 | if !is_dir { | |
330 | if let Some(parent) = dirname.parent() { | |
331 | dirname = parent.to_owned(); | |
332 | } | |
333 | } | |
334 | ||
6100071f WB |
335 | let mut dir = |
336 | match nix::dir::Dir::openat(libc::AT_FDCWD, &dirname, OFlag::O_DIRECTORY, Mode::empty()) { | |
337 | Ok(d) => d, | |
338 | Err(_) => return result, | |
339 | }; | |
383e8577 DM |
340 | |
341 | for item in dir.iter() { | |
342 | if let Ok(entry) = item { | |
343 | if let Ok(name) = entry.file_name().to_str() { | |
6100071f WB |
344 | if name == "." || name == ".." { |
345 | continue; | |
346 | } | |
383e8577 DM |
347 | let mut newpath = dirname.clone(); |
348 | newpath.push(name); | |
349 | ||
6100071f WB |
350 | if let Ok(stat) = |
351 | nix::sys::stat::fstatat(libc::AT_FDCWD, &newpath, AtFlags::empty()) | |
352 | { | |
383e8577 DM |
353 | if (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR { |
354 | newpath.push(""); | |
355 | if let Some(newpath) = newpath.to_str() { | |
356 | result.push(newpath.to_owned()); | |
357 | } | |
358 | continue; | |
6100071f | 359 | } |
383e8577 DM |
360 | } |
361 | if let Some(newpath) = newpath.to_str() { | |
362 | result.push(newpath.to_owned()); | |
363 | } | |
6100071f | 364 | } |
383e8577 DM |
365 | } |
366 | } | |
367 | ||
368 | result | |
369 | } | |
443f3743 DM |
370 | |
371 | /// Scan directory for matching file names. | |
372 | /// | |
373 | /// Scan through all directory entries and call `callback()` function | |
374 | /// if the entry name matches the regular expression. This function | |
375 | /// used unix `openat()`, so you can pass absolute or relative file | |
376 | /// names. This function simply skips non-UTF8 encoded names. | |
377 | pub fn scandir<P, F>( | |
378 | dirfd: RawFd, | |
121f18ef | 379 | path: &P, |
443f3743 | 380 | regex: ®ex::Regex, |
6100071f | 381 | mut callback: F, |
443f3743 | 382 | ) -> Result<(), Error> |
6100071f WB |
383 | where |
384 | F: FnMut(RawFd, &str, nix::dir::Type) -> Result<(), Error>, | |
385 | P: ?Sized + nix::NixPath, | |
443f3743 | 386 | { |
121f18ef | 387 | for entry in self::fs::scan_subdir(dirfd, path, regex)? { |
443f3743 DM |
388 | let entry = entry?; |
389 | let file_type = match entry.file_type() { | |
390 | Some(file_type) => file_type, | |
391 | None => bail!("unable to detect file type"), | |
392 | }; | |
443f3743 | 393 | |
6100071f WB |
394 | callback( |
395 | entry.parent_fd(), | |
396 | unsafe { entry.file_name_utf8_unchecked() }, | |
397 | file_type, | |
398 | )?; | |
443f3743 DM |
399 | } |
400 | Ok(()) | |
401 | } | |
7e13b2d6 | 402 | |
c5946faf WB |
403 | /// Shortcut for md5 sums. |
404 | pub fn md5sum(data: &[u8]) -> Result<DigestBytes, Error> { | |
405 | hash(MessageDigest::md5(), data).map_err(Error::from) | |
406 | } | |
407 | ||
7e13b2d6 | 408 | pub fn get_hardware_address() -> Result<String, Error> { |
1631c54f | 409 | static FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub"; |
7e13b2d6 | 410 | |
e18a6c9e | 411 | let contents = proxmox::tools::fs::file_get_contents(FILENAME)?; |
c5946faf | 412 | let digest = md5sum(&contents)?; |
7e13b2d6 | 413 | |
c5946faf | 414 | Ok(proxmox::tools::bin_to_hex(&digest)) |
7e13b2d6 | 415 | } |
22968600 | 416 | |
af2fddea DM |
417 | pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> { |
418 | if digest1 != digest2 { | |
6100071f | 419 | bail!("detected modified configuration - file changed by other user? Try again."); |
af2fddea DM |
420 | } |
421 | Ok(()) | |
422 | } | |
b9903d63 DM |
423 | |
424 | /// Extract authentication cookie from cookie header. | |
425 | /// We assume cookie_name is already url encoded. | |
426 | pub fn extract_auth_cookie(cookie: &str, cookie_name: &str) -> Option<String> { | |
b9903d63 | 427 | for pair in cookie.split(';') { |
b9903d63 DM |
428 | let (name, value) = match pair.find('=') { |
429 | Some(i) => (pair[..i].trim(), pair[(i + 1)..].trim()), | |
430 | None => return None, // Cookie format error | |
431 | }; | |
432 | ||
433 | if name == cookie_name { | |
8a1028e0 | 434 | use percent_encoding::percent_decode; |
b9903d63 DM |
435 | if let Ok(value) = percent_decode(value.as_bytes()).decode_utf8() { |
436 | return Some(value.into()); | |
437 | } else { | |
438 | return None; // Cookie format error | |
439 | } | |
440 | } | |
441 | } | |
442 | ||
443 | None | |
444 | } | |
af53186e DM |
445 | |
446 | pub fn join(data: &Vec<String>, sep: char) -> String { | |
af53186e DM |
447 | let mut list = String::new(); |
448 | ||
449 | for item in data { | |
62ee2eb4 | 450 | if !list.is_empty() { |
6100071f WB |
451 | list.push(sep); |
452 | } | |
af53186e DM |
453 | list.push_str(item); |
454 | } | |
455 | ||
456 | list | |
457 | } | |
ff7049d4 | 458 | |
002a191a DM |
459 | /// Detect modified configuration files |
460 | /// | |
add5861e | 461 | /// This function fails with a reasonable error message if checksums do not match. |
002a191a DM |
462 | pub fn detect_modified_configuration_file(digest1: &[u8;32], digest2: &[u8;32]) -> Result<(), Error> { |
463 | if digest1 != digest2 { | |
464 | bail!("detected modified configuration - file changed by other user? Try again."); | |
465 | } | |
466 | Ok(()) | |
467 | } | |
468 | ||
3578d99f DM |
469 | /// normalize uri path |
470 | /// | |
471 | /// Do not allow ".", "..", or hidden files ".XXXX" | |
472 | /// Also remove empty path components | |
473 | pub fn normalize_uri_path(path: &str) -> Result<(String, Vec<&str>), Error> { | |
3578d99f DM |
474 | let items = path.split('/'); |
475 | ||
476 | let mut path = String::new(); | |
477 | let mut components = vec![]; | |
478 | ||
479 | for name in items { | |
6100071f WB |
480 | if name.is_empty() { |
481 | continue; | |
482 | } | |
62ee2eb4 | 483 | if name.starts_with('.') { |
3578d99f DM |
484 | bail!("Path contains illegal components."); |
485 | } | |
486 | path.push('/'); | |
487 | path.push_str(name); | |
488 | components.push(name); | |
489 | } | |
490 | ||
491 | Ok((path, components)) | |
492 | } | |
493 | ||
97fab7aa | 494 | /// Helper to check result from std::process::Command output |
143b6545 DM |
495 | /// |
496 | /// The exit_code_check() function should return true if the exit code | |
497 | /// is considered successful. | |
498 | pub fn command_output( | |
499 | output: std::process::Output, | |
144006fa | 500 | exit_code_check: Option<fn(i32) -> bool>, |
143b6545 | 501 | ) -> Result<String, Error> { |
97fab7aa DM |
502 | |
503 | if !output.status.success() { | |
504 | match output.status.code() { | |
505 | Some(code) => { | |
143b6545 DM |
506 | let is_ok = match exit_code_check { |
507 | Some(check_fn) => check_fn(code), | |
508 | None => code == 0, | |
509 | }; | |
510 | if !is_ok { | |
97fab7aa DM |
511 | let msg = String::from_utf8(output.stderr) |
512 | .map(|m| if m.is_empty() { String::from("no error message") } else { m }) | |
513 | .unwrap_or_else(|_| String::from("non utf8 error message (suppressed)")); | |
514 | ||
515 | bail!("status code: {} - {}", code, msg); | |
516 | } | |
517 | } | |
518 | None => bail!("terminated by signal"), | |
519 | } | |
520 | } | |
521 | ||
522 | let output = String::from_utf8(output.stdout)?; | |
523 | ||
524 | Ok(output) | |
525 | } | |
526 | ||
144006fa DM |
527 | pub fn run_command( |
528 | mut command: std::process::Command, | |
529 | exit_code_check: Option<fn(i32) -> bool>, | |
530 | ) -> Result<String, Error> { | |
531 | ||
532 | let output = command.output() | |
533 | .map_err(|err| format_err!("failed to execute {:?} - {}", command, err))?; | |
534 | ||
535 | let output = crate::tools::command_output(output, exit_code_check) | |
536 | .map_err(|err| format_err!("command {:?} failed - {}", command, err))?; | |
537 | ||
538 | Ok(output) | |
539 | } | |
97fab7aa | 540 | |
ff7049d4 | 541 | pub fn fd_change_cloexec(fd: RawFd, on: bool) -> Result<(), Error> { |
6100071f | 542 | use nix::fcntl::{fcntl, FdFlag, F_GETFD, F_SETFD}; |
ff7049d4 WB |
543 | let mut flags = FdFlag::from_bits(fcntl(fd, F_GETFD)?) |
544 | .ok_or_else(|| format_err!("unhandled file flags"))?; // nix crate is stupid this way... | |
545 | flags.set(FdFlag::FD_CLOEXEC, on); | |
546 | fcntl(fd, F_SETFD(flags))?; | |
547 | Ok(()) | |
548 | } | |
9136f857 | 549 | |
9136f857 DM |
550 | static mut SHUTDOWN_REQUESTED: bool = false; |
551 | ||
552 | pub fn request_shutdown() { | |
6100071f WB |
553 | unsafe { |
554 | SHUTDOWN_REQUESTED = true; | |
555 | } | |
7a630df7 | 556 | crate::server::server_shutdown(); |
9136f857 DM |
557 | } |
558 | ||
559 | #[inline(always)] | |
560 | pub fn shutdown_requested() -> bool { | |
561 | unsafe { SHUTDOWN_REQUESTED } | |
562 | } | |
92da93b2 DM |
563 | |
564 | pub fn fail_on_shutdown() -> Result<(), Error> { | |
565 | if shutdown_requested() { | |
566 | bail!("Server shutdown requested - aborting task"); | |
567 | } | |
568 | Ok(()) | |
569 | } | |
d96bb7f1 | 570 | |
c4044009 WB |
571 | /// safe wrapper for `nix::unistd::pipe2` defaulting to `O_CLOEXEC` and guarding the file |
572 | /// descriptors. | |
efd1536e WB |
573 | pub fn pipe() -> Result<(Fd, Fd), Error> { |
574 | let (pin, pout) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?; | |
575 | Ok((Fd(pin), Fd(pout))) | |
576 | } | |
2edc341b | 577 | |
c4044009 WB |
578 | /// safe wrapper for `nix::sys::socket::socketpair` defaulting to `O_CLOEXEC` and guarding the file |
579 | /// descriptors. | |
580 | pub fn socketpair() -> Result<(Fd, Fd), Error> { | |
581 | use nix::sys::socket; | |
582 | let (pa, pb) = socket::socketpair( | |
583 | socket::AddressFamily::Unix, | |
584 | socket::SockType::Stream, | |
585 | None, | |
586 | socket::SockFlag::SOCK_CLOEXEC, | |
587 | )?; | |
588 | Ok((Fd(pa), Fd(pb))) | |
589 | } | |
590 | ||
591 | ||
2edc341b DM |
592 | /// An easy way to convert types to Any |
593 | /// | |
594 | /// Mostly useful to downcast trait objects (see RpcEnvironment). | |
595 | pub trait AsAny { | |
dd5495d6 | 596 | fn as_any(&self) -> &dyn Any; |
2edc341b DM |
597 | } |
598 | ||
599 | impl<T: Any> AsAny for T { | |
6100071f WB |
600 | fn as_any(&self) -> &dyn Any { |
601 | self | |
602 | } | |
2edc341b | 603 | } |
8a1028e0 WB |
604 | |
605 | /// This used to be: `SIMPLE_ENCODE_SET` plus space, `"`, `#`, `<`, `>`, backtick, `?`, `{`, `}` | |
606 | pub const DEFAULT_ENCODE_SET: &AsciiSet = &percent_encoding::CONTROLS // 0..1f and 7e | |
607 | // The SIMPLE_ENCODE_SET adds space and anything >= 0x7e (7e itself is already included above) | |
608 | .add(0x20) | |
609 | .add(0x7f) | |
610 | // the DEFAULT_ENCODE_SET added: | |
611 | .add(b' ') | |
612 | .add(b'"') | |
613 | .add(b'#') | |
614 | .add(b'<') | |
615 | .add(b'>') | |
616 | .add(b'`') | |
617 | .add(b'?') | |
618 | .add(b'{') | |
619 | .add(b'}'); | |
386990ba WB |
620 | |
621 | /// Get an iterator over lines of a file, skipping empty lines and comments (lines starting with a | |
622 | /// `#`). | |
623 | pub fn file_get_non_comment_lines<P: AsRef<Path>>( | |
624 | path: P, | |
625 | ) -> Result<impl Iterator<Item = io::Result<String>>, Error> { | |
626 | let path = path.as_ref(); | |
627 | ||
628 | Ok(io::BufReader::new( | |
629 | File::open(path).map_err(|err| format_err!("error opening {:?}: {}", path, err))?, | |
630 | ) | |
631 | .lines() | |
632 | .filter_map(|line| match line { | |
633 | Ok(line) => { | |
634 | let line = line.trim(); | |
635 | if line.is_empty() || line.starts_with('#') { | |
636 | None | |
637 | } else { | |
638 | Some(Ok(line.to_string())) | |
639 | } | |
640 | } | |
641 | Err(err) => Some(Err(err)), | |
642 | })) | |
643 | } | |
e693818a DC |
644 | |
645 | pub fn epoch_now() -> Result<Duration, SystemTimeError> { | |
646 | SystemTime::now().duration_since(UNIX_EPOCH) | |
647 | } | |
648 | ||
649 | pub fn epoch_now_f64() -> Result<f64, SystemTimeError> { | |
650 | Ok(epoch_now()?.as_secs_f64()) | |
651 | } | |
652 | ||
653 | pub fn epoch_now_u64() -> Result<u64, SystemTimeError> { | |
654 | Ok(epoch_now()?.as_secs()) | |
655 | } | |
ac7513e3 DM |
656 | |
657 | pub fn setup_safe_path_env() { | |
658 | std::env::set_var("PATH", "/sbin:/bin:/usr/sbin:/usr/bin"); | |
659 | // Make %ENV safer - as suggested by https://perldoc.perl.org/perlsec.html | |
660 | for name in &["IFS", "CDPATH", "ENV", "BASH_ENV"] { | |
661 | std::env::remove_var(name); | |
662 | } | |
663 | } | |
cdf1da28 WB |
664 | |
665 | pub fn strip_ascii_whitespace(line: &[u8]) -> &[u8] { | |
666 | let line = match line.iter().position(|&b| !b.is_ascii_whitespace()) { | |
667 | Some(n) => &line[n..], | |
668 | None => return &[], | |
669 | }; | |
670 | match line.iter().rev().position(|&b| !b.is_ascii_whitespace()) { | |
671 | Some(n) => &line[..(line.len() - n)], | |
672 | None => &[], | |
673 | } | |
674 | } |