1 //! Proxmox Server/Service framework
3 //! This code provides basic primitives to build our REST API
4 //! services. We want async IO, so this is built on top of
7 use anyhow
::{format_err, Error}
;
8 use lazy_static
::lazy_static
;
10 use serde_json
::Value
;
12 use proxmox
::sys
::linux
::procfs
::PidStat
;
13 use proxmox
::tools
::fs
::{create_path, CreateOptions}
;
18 static ref PID
: i32 = unsafe { libc::getpid() }
;
19 static ref PSTART
: u64 = PidStat
::read_from_pid(Pid
::from_raw(*PID
)).unwrap().starttime
;
26 pub fn pstart() -> u64 {
30 pub fn write_pid(pid_fn
: &str) -> Result
<(), Error
> {
31 let pid_str
= format
!("{}\n", *PID
);
32 proxmox
::tools
::fs
::replace_file(pid_fn
, pid_str
.as_bytes(), CreateOptions
::new())
35 pub 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
))
41 pub fn ctrl_sock_from_pid(pid
: i32) -> String
{
42 format
!("\0{}/control-{}.sock", pbs_buildcfg
::PROXMOX_BACKUP_RUN_DIR
, pid
)
45 pub fn our_ctrl_sock() -> String
{
46 ctrl_sock_from_pid(*PID
)
50 pub use worker_task
::*;
58 pub use verify_job
::*;
66 mod email_notifications
;
67 pub use email_notifications
::*;
78 pub(crate) async
fn reload_proxy_certificate() -> Result
<(), Error
> {
79 let proxy_pid
= crate::server
::read_pid(pbs_buildcfg
::PROXMOX_BACKUP_PROXY_PID_FN
)?
;
80 let sock
= crate::server
::ctrl_sock_from_pid(proxy_pid
);
81 let _
: Value
= proxmox_rest_server
::send_raw_command(sock
, "{\"command\":\"reload-certificate\"}\n")
86 pub(crate) async
fn notify_datastore_removed() -> Result
<(), Error
> {
87 let proxy_pid
= crate::server
::read_pid(pbs_buildcfg
::PROXMOX_BACKUP_PROXY_PID_FN
)?
;
88 let sock
= crate::server
::ctrl_sock_from_pid(proxy_pid
);
89 let _
: Value
= proxmox_rest_server
::send_raw_command(sock
, "{\"command\":\"datastore-removed\"}\n")
94 /// Create the base run-directory.
96 /// This exists to fixate the permissions for the run *base* directory while allowing intermediate
97 /// directories after it to have different permissions.
98 pub fn create_run_dir() -> Result
<(), Error
> {
99 let backup_user
= pbs_config
::backup_user()?
;
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
))?
;