]>
Commit | Line | Data |
---|---|---|
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 | ||
7 | use anyhow::{format_err, Error}; | |
8 | use lazy_static::lazy_static; | |
9 | use nix::unistd::Pid; | |
10 | use serde_json::Value; | |
11 | ||
12 | use proxmox::sys::linux::procfs::PidStat; | |
13 | use proxmox::tools::fs::{create_path, CreateOptions}; | |
14 | ||
15 | use pbs_buildcfg; | |
16 | ||
17 | lazy_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 | ||
22 | pub fn pid() -> i32 { | |
23 | *PID | |
24 | } | |
25 | ||
26 | pub fn pstart() -> u64 { | |
27 | *PSTART | |
28 | } | |
29 | ||
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()) | |
33 | } | |
34 | ||
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)) | |
39 | } | |
40 | ||
41 | pub fn ctrl_sock_from_pid(pid: i32) -> String { | |
42 | format!("\0{}/control-{}.sock", pbs_buildcfg::PROXMOX_BACKUP_RUN_DIR, pid) | |
43 | } | |
44 | ||
45 | pub fn our_ctrl_sock() -> String { | |
46 | ctrl_sock_from_pid(*PID) | |
47 | } | |
48 | ||
49 | mod worker_task; | |
50 | pub use worker_task::*; | |
51 | ||
52 | mod h2service; | |
53 | pub use h2service::*; | |
54 | ||
55 | pub mod jobstate; | |
56 | ||
57 | mod verify_job; | |
58 | pub use verify_job::*; | |
59 | ||
60 | mod prune_job; | |
61 | pub use prune_job::*; | |
62 | ||
63 | mod gc_job; | |
64 | pub use gc_job::*; | |
65 | ||
66 | mod email_notifications; | |
67 | pub use email_notifications::*; | |
68 | ||
69 | mod report; | |
70 | pub use report::*; | |
71 | ||
72 | pub mod ticket; | |
73 | ||
74 | pub mod auth; | |
75 | ||
76 | pub mod pull; | |
77 | ||
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") | |
82 | .await?; | |
83 | Ok(()) | |
84 | } | |
85 | ||
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") | |
90 | .await?; | |
91 | Ok(()) | |
92 | } | |
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. | |
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))?; | |
104 | Ok(()) | |
105 | } |