]> git.proxmox.com Git - proxmox-backup.git/blame - src/server/mod.rs
move src/server/rest.rs to proxmox-rest-server crate
[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
634132fe
DM
49mod upid;
50pub use upid::*;
51
882594c5
DM
52mod worker_task;
53pub use worker_task::*;
7a630df7 54
42a87f7b
DM
55mod h2service;
56pub use h2service::*;
57
1298618a
DM
58pub mod jobstate;
59
60mod verify_job;
61pub use verify_job::*;
b9e7bcc2 62
b8d90798
HL
63mod prune_job;
64pub use prune_job::*;
65
3b707fbb
DM
66mod gc_job;
67pub use gc_job::*;
68
b9e7bcc2
DM
69mod email_notifications;
70pub use email_notifications::*;
b0ef9631
HL
71
72mod report;
73pub use report::*;
027ef213
WB
74
75pub mod ticket;
ba39ab20
SR
76
77pub mod auth;
fca1cef2 78
aa2838c2
WB
79pub mod pull;
80
fca1cef2 81pub(crate) async fn reload_proxy_certificate() -> Result<(), Error> {
af06decd 82 let proxy_pid = crate::server::read_pid(pbs_buildcfg::PROXMOX_BACKUP_PROXY_PID_FN)?;
fca1cef2 83 let sock = crate::server::ctrl_sock_from_pid(proxy_pid);
fd6d2438 84 let _: Value = proxmox_rest_server::send_raw_command(sock, "{\"command\":\"reload-certificate\"}\n")
fca1cef2
WB
85 .await?;
86 Ok(())
87}
062cf75c
DC
88
89pub(crate) async fn notify_datastore_removed() -> Result<(), Error> {
af06decd 90 let proxy_pid = crate::server::read_pid(pbs_buildcfg::PROXMOX_BACKUP_PROXY_PID_FN)?;
062cf75c 91 let sock = crate::server::ctrl_sock_from_pid(proxy_pid);
fd6d2438 92 let _: Value = proxmox_rest_server::send_raw_command(sock, "{\"command\":\"datastore-removed\"}\n")
062cf75c
DC
93 .await?;
94 Ok(())
95}
6c76aa43
WB
96
97/// Create the base run-directory.
98///
99/// This exists to fixate the permissions for the run *base* directory while allowing intermediate
100/// directories after it to have different permissions.
101pub fn create_run_dir() -> Result<(), Error> {
21211748 102 let backup_user = pbs_config::backup_user()?;
6c76aa43
WB
103 let opts = CreateOptions::new()
104 .owner(backup_user.uid)
105 .group(backup_user.gid);
106 let _: bool = create_path(pbs_buildcfg::PROXMOX_BACKUP_RUN_DIR_M!(), None, Some(opts))?;
107 Ok(())
108}