]> git.proxmox.com Git - proxmox-backup.git/blobdiff - proxmox-rest-server/src/lib.rs
proxmox-rest-server: improve logging
[proxmox-backup.git] / proxmox-rest-server / src / lib.rs
index 2f29f9cd72135b4662e919bc5be8740fb243d017..5f1cefbb601a8d81bb4299008694bb5445364b53 100644 (file)
@@ -1,14 +1,19 @@
 use std::os::unix::io::RawFd;
+use std::sync::atomic::{Ordering, AtomicBool};
 
 use anyhow::{bail, format_err, Error};
+use nix::unistd::Pid;
 
 use proxmox::tools::fd::Fd;
+use proxmox::sys::linux::procfs::PidStat;
 use proxmox::api::UserInformation;
+use proxmox::tools::fs::CreateOptions;
 
 mod compression;
 pub use compression::*;
 
 pub mod daemon;
+
 pub mod formatter;
 
 mod environment;
@@ -27,7 +32,13 @@ mod api_config;
 pub use api_config::ApiConfig;
 
 mod rest;
-pub use rest::{RestServer, handle_api_request};
+pub use rest::RestServer;
+
+mod worker_task;
+pub use worker_task::*;
+
+mod h2service;
+pub use h2service::*;
 
 pub enum AuthError {
     Generic(Error),
@@ -48,18 +59,50 @@ pub trait ApiAuth {
     ) -> Result<(String, Box<dyn UserInformation + Sync + Send>), AuthError>;
 }
 
-static mut SHUTDOWN_REQUESTED: bool = false;
+lazy_static::lazy_static!{
+    static ref PID: i32 = unsafe { libc::getpid() };
+    static ref PSTART: u64 = PidStat::read_from_pid(Pid::from_raw(*PID)).unwrap().starttime;
+}
+
+pub fn pid() -> i32 {
+    *PID
+}
+
+pub fn pstart() -> u64 {
+    *PSTART
+}
+
+pub fn write_pid(pid_fn: &str) -> Result<(), Error> {
+    let pid_str = format!("{}\n", *PID);
+    proxmox::tools::fs::replace_file(pid_fn, pid_str.as_bytes(), CreateOptions::new())
+}
+
+pub fn read_pid(pid_fn: &str) -> Result<i32, Error> {
+    let pid = proxmox::tools::fs::file_get_contents(pid_fn)?;
+    let pid = std::str::from_utf8(&pid)?.trim();
+    pid.parse().map_err(|err| format_err!("could not parse pid - {}", err))
+}
+
+pub fn ctrl_sock_from_pid(pid: i32) -> String {
+    // Note: The control socket always uses @/run/proxmox-backup/ as prefix
+    // for historc reason.
+    format!("\0{}/control-{}.sock", "/run/proxmox-backup", pid)
+}
+
+pub fn our_ctrl_sock() -> String {
+    ctrl_sock_from_pid(*PID)
+}
+
+static SHUTDOWN_REQUESTED: AtomicBool = AtomicBool::new(false);
 
 pub fn request_shutdown() {
-    unsafe {
-        SHUTDOWN_REQUESTED = true;
-    }
+    SHUTDOWN_REQUESTED.store(true, Ordering::SeqCst);
     crate::server_shutdown();
 }
 
 #[inline(always)]
 pub fn shutdown_requested() -> bool {
-    unsafe { SHUTDOWN_REQUESTED }
+    SHUTDOWN_REQUESTED.load(Ordering::SeqCst)
 }
 
 pub fn fail_on_shutdown() -> Result<(), Error> {