]> git.proxmox.com Git - proxmox-backup.git/commitdiff
server: write main daemon PID to run directory
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 2 Nov 2020 18:18:36 +0000 (19:18 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 2 Nov 2020 18:50:24 +0000 (19:50 +0100)
so that we can easily get the main PID of the last recently launched
daemon. Will be used to get the control socket of that one for access
lgo rotate in a future patch

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
etc/proxmox-backup-proxy.service.in
etc/proxmox-backup.service.in
src/bin/proxmox-backup-api.rs
src/bin/proxmox-backup-proxy.rs
src/buildcfg.rs
src/server.rs

index e389a5650b9a1a3ab959d932c55ccd6594aff629..7ca806aa4a1879161570027aba70a981e6d71c77 100644 (file)
@@ -9,6 +9,7 @@ After=proxmox-backup.service
 Type=notify
 ExecStart=%LIBEXECDIR%/proxmox-backup/proxmox-backup-proxy
 ExecReload=/bin/kill -HUP $MAINPID
+PIDFile=/run/proxmox-backup/proxy.pid
 Restart=on-failure
 User=%PROXY_USER%
 Group=%PROXY_USER%
index a843abd504cf8f0107e8bfcf6114ff193e347aba..76705a0e40f9e7c593f70bb3d1bc4cd9826d8121 100644 (file)
@@ -7,6 +7,7 @@ After=network.target
 Type=notify
 ExecStart=%LIBEXECDIR%/proxmox-backup/proxmox-backup-api
 ExecReload=/bin/kill -HUP $MAINPID
+PIDFile=/run/proxmox-backup/api.pid
 Restart=on-failure
 
 [Install]
index 934562574e4926757486e225774d1947220a9369..7ca2667145d50db266ba034d54ec35dd21535793 100644 (file)
@@ -78,6 +78,7 @@ async fn run() -> Result<(), Error> {
         },
     );
 
+    server::write_pid(buildcfg::PROXMOX_BACKUP_API_PID_FN)?;
     daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
 
     let init_result: Result<(), Error> = try_block!({
index ce67faeea8354e8bf822ae8b87a9495fa1f8fb8d..2ae796f8be0a76f5b76c66de200099bd9583a259 100644 (file)
@@ -145,6 +145,7 @@ async fn run() -> Result<(), Error> {
         },
     );
 
+    server::write_pid(buildcfg::PROXMOX_BACKUP_PROXY_PID_FN)?;
     daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
 
     let init_result: Result<(), Error> = try_block!({
index 7b61ff26df0e26486c27696a47d2b87ab78e766b..402f2c9d33abbcfb6dbb8a15f1db5d46d304d7b1 100644 (file)
@@ -12,12 +12,19 @@ macro_rules! PROXMOX_BACKUP_LOG_DIR_M { () => ("/var/log/proxmox-backup") }
 
 /// namespaced directory for in-memory (tmpfs) run state
 pub const PROXMOX_BACKUP_RUN_DIR: &str = PROXMOX_BACKUP_RUN_DIR_M!();
+
 /// namespaced directory for persistent logging
 pub const PROXMOX_BACKUP_LOG_DIR: &str = PROXMOX_BACKUP_LOG_DIR_M!();
 
 /// logfile for all API reuests handled by the proxy and privileged API daemons
 pub const API_ACCESS_LOG_FN: &str = concat!(PROXMOX_BACKUP_LOG_DIR_M!(), "/api/access.log");
 
+/// the PID filename for the unprivileged proxy daemon
+pub const PROXMOX_BACKUP_PROXY_PID_FN: &str = concat!(PROXMOX_BACKUP_RUN_DIR_M!(), "/proxy.pid");
+
+/// the PID filename for the privileged api daemon
+pub const PROXMOX_BACKUP_API_PID_FN: &str = concat!(PROXMOX_BACKUP_RUN_DIR_M!(), "/api.pid");
+
 /// Prepend configuration directory to a file name
 ///
 /// This is a simply way to get the full path for configuration files.
index aa4b57ec6417fadbeef82732723075b9d96986e8..05fd25d9e7bc146039a02f0be7f9e8eef57669ed 100644 (file)
@@ -4,6 +4,7 @@
 //! services. We want async IO, so this is built on top of
 //! tokio/hyper.
 
+use anyhow::{format_err, Error};
 use lazy_static::lazy_static;
 use nix::unistd::Pid;
 
@@ -24,6 +25,18 @@ pub fn pstart() -> u64 {
     *PSTART
 }
 
+pub fn write_pid(pid_fn: &str) -> Result<(), Error> {
+    let pid_str = format!("{}\n", *PID);
+    let opts = proxmox::tools::fs::CreateOptions::new();
+    proxmox::tools::fs::replace_file(pid_fn, pid_str.as_bytes(), opts)
+}
+
+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 {
     format!("\0{}/control-{}.sock", buildcfg::PROXMOX_BACKUP_RUN_DIR, pid)
 }