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