]> git.proxmox.com Git - proxmox-backup.git/blame - src/server/mod.rs
worker task: allow to configure path and owner/group
[proxmox-backup.git] / src / server / mod.rs
CommitLineData
882594c5
DM
1//! Proxmox Server/Service framework
2//!
3//! This code provides basic primitives to build our REST API
4//! services. We want async IO, so this is built on top of
5//! tokio/hyper.
6
04b053d8 7use anyhow::{format_err, Error};
a68768cf
TL
8use lazy_static::lazy_static;
9use nix::unistd::Pid;
fca1cef2 10use serde_json::Value;
a68768cf
TL
11
12use proxmox::sys::linux::procfs::PidStat;
6c76aa43 13use proxmox::tools::fs::{create_path, CreateOptions};
a68768cf 14
af06decd 15use pbs_buildcfg;
a68768cf
TL
16
17lazy_static! {
18 static ref PID: i32 = unsafe { libc::getpid() };
19 static ref PSTART: u64 = PidStat::read_from_pid(Pid::from_raw(*PID)).unwrap().starttime;
20}
21
22pub fn pid() -> i32 {
23 *PID
24}
25
26pub fn pstart() -> u64 {
27 *PSTART
28}
29
04b053d8
TL
30pub fn write_pid(pid_fn: &str) -> Result<(), Error> {
31 let pid_str = format!("{}\n", *PID);
6c76aa43 32 proxmox::tools::fs::replace_file(pid_fn, pid_str.as_bytes(), CreateOptions::new())
04b053d8
TL
33}
34
35pub fn read_pid(pid_fn: &str) -> Result<i32, Error> {
36 let pid = proxmox::tools::fs::file_get_contents(pid_fn)?;
37 let pid = std::str::from_utf8(&pid)?.trim();
38 pid.parse().map_err(|err| format_err!("could not parse pid - {}", err))
39}
40
a68768cf 41pub fn ctrl_sock_from_pid(pid: i32) -> String {
af06decd 42 format!("\0{}/control-{}.sock", pbs_buildcfg::PROXMOX_BACKUP_RUN_DIR, pid)
a68768cf
TL
43}
44
45pub fn our_ctrl_sock() -> String {
46 ctrl_sock_from_pid(*PID)
47}
48
882594c5
DM
49mod worker_task;
50pub use worker_task::*;
7a630df7 51
42a87f7b
DM
52mod h2service;
53pub use h2service::*;
54
1298618a
DM
55pub mod jobstate;
56
57mod verify_job;
58pub use verify_job::*;
b9e7bcc2 59
b8d90798
HL
60mod prune_job;
61pub use prune_job::*;
62
3b707fbb
DM
63mod gc_job;
64pub use gc_job::*;
65
b9e7bcc2
DM
66mod email_notifications;
67pub use email_notifications::*;
b0ef9631
HL
68
69mod report;
70pub use report::*;
027ef213
WB
71
72pub mod ticket;
ba39ab20
SR
73
74pub mod auth;
fca1cef2 75
aa2838c2
WB
76pub mod pull;
77
fca1cef2 78pub(crate) async fn reload_proxy_certificate() -> Result<(), Error> {
af06decd 79 let proxy_pid = crate::server::read_pid(pbs_buildcfg::PROXMOX_BACKUP_PROXY_PID_FN)?;
fca1cef2 80 let sock = crate::server::ctrl_sock_from_pid(proxy_pid);
fd6d2438 81 let _: Value = proxmox_rest_server::send_raw_command(sock, "{\"command\":\"reload-certificate\"}\n")
fca1cef2
WB
82 .await?;
83 Ok(())
84}
062cf75c
DC
85
86pub(crate) async fn notify_datastore_removed() -> Result<(), Error> {
af06decd 87 let proxy_pid = crate::server::read_pid(pbs_buildcfg::PROXMOX_BACKUP_PROXY_PID_FN)?;
062cf75c 88 let sock = crate::server::ctrl_sock_from_pid(proxy_pid);
fd6d2438 89 let _: Value = proxmox_rest_server::send_raw_command(sock, "{\"command\":\"datastore-removed\"}\n")
062cf75c
DC
90 .await?;
91 Ok(())
92}
6c76aa43
WB
93
94/// Create the base run-directory.
95///
96/// This exists to fixate the permissions for the run *base* directory while allowing intermediate
97/// directories after it to have different permissions.
98pub fn create_run_dir() -> Result<(), Error> {
21211748 99 let backup_user = pbs_config::backup_user()?;
6c76aa43
WB
100 let opts = CreateOptions::new()
101 .owner(backup_user.uid)
102 .group(backup_user.gid);
103 let _: bool = create_path(pbs_buildcfg::PROXMOX_BACKUP_RUN_DIR_M!(), None, Some(opts))?;
104 Ok(())
105}