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
)
53 pub use worker_task
::*;
64 pub use verify_job
::*;
72 mod email_notifications
;
73 pub use email_notifications
::*;
84 pub(crate) async
fn reload_proxy_certificate() -> Result
<(), Error
> {
85 let proxy_pid
= crate::server
::read_pid(pbs_buildcfg
::PROXMOX_BACKUP_PROXY_PID_FN
)?
;
86 let sock
= crate::server
::ctrl_sock_from_pid(proxy_pid
);
87 let _
: Value
= proxmox_rest_server
::send_raw_command(sock
, "{\"command\":\"reload-certificate\"}\n")
92 pub(crate) async
fn notify_datastore_removed() -> Result
<(), Error
> {
93 let proxy_pid
= crate::server
::read_pid(pbs_buildcfg
::PROXMOX_BACKUP_PROXY_PID_FN
)?
;
94 let sock
= crate::server
::ctrl_sock_from_pid(proxy_pid
);
95 let _
: Value
= proxmox_rest_server
::send_raw_command(sock
, "{\"command\":\"datastore-removed\"}\n")
100 /// Create the base run-directory.
102 /// This exists to fixate the permissions for the run *base* directory while allowing intermediate
103 /// directories after it to have different permissions.
104 pub fn create_run_dir() -> Result
<(), Error
> {
105 let backup_user
= pbs_config
::backup_user()?
;
106 let opts
= CreateOptions
::new()
107 .owner(backup_user
.uid
)
108 .group(backup_user
.gid
);
109 let _
: bool
= create_path(pbs_buildcfg
::PROXMOX_BACKUP_RUN_DIR_M
!(), None
, Some(opts
))?
;