]>
Commit | Line | Data |
---|---|---|
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 | 7 | use anyhow::{format_err, Error}; |
a68768cf TL |
8 | use lazy_static::lazy_static; |
9 | use nix::unistd::Pid; | |
fca1cef2 | 10 | use serde_json::Value; |
a68768cf TL |
11 | |
12 | use proxmox::sys::linux::procfs::PidStat; | |
6c76aa43 | 13 | use proxmox::tools::fs::{create_path, CreateOptions}; |
a68768cf | 14 | |
af06decd | 15 | use pbs_buildcfg; |
a68768cf TL |
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 | ||
04b053d8 TL |
30 | pub 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 | ||
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 | ||
a68768cf | 41 | pub 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 | ||
45 | pub fn our_ctrl_sock() -> String { | |
46 | ctrl_sock_from_pid(*PID) | |
47 | } | |
48 | ||
882594c5 DM |
49 | mod environment; |
50 | pub use environment::*; | |
51 | ||
634132fe DM |
52 | mod upid; |
53 | pub use upid::*; | |
54 | ||
882594c5 DM |
55 | mod worker_task; |
56 | pub use worker_task::*; | |
7a630df7 | 57 | |
42a87f7b DM |
58 | mod h2service; |
59 | pub use h2service::*; | |
60 | ||
882594c5 | 61 | pub mod formatter; |
7a630df7 | 62 | |
882594c5 DM |
63 | #[macro_use] |
64 | pub mod rest; | |
65 | ||
1298618a DM |
66 | pub mod jobstate; |
67 | ||
68 | mod verify_job; | |
69 | pub use verify_job::*; | |
b9e7bcc2 | 70 | |
b8d90798 HL |
71 | mod prune_job; |
72 | pub use prune_job::*; | |
73 | ||
3b707fbb DM |
74 | mod gc_job; |
75 | pub use gc_job::*; | |
76 | ||
b9e7bcc2 DM |
77 | mod email_notifications; |
78 | pub use email_notifications::*; | |
b0ef9631 HL |
79 | |
80 | mod report; | |
81 | pub use report::*; | |
027ef213 WB |
82 | |
83 | pub mod ticket; | |
ba39ab20 SR |
84 | |
85 | pub mod auth; | |
fca1cef2 | 86 | |
aa2838c2 WB |
87 | pub mod pull; |
88 | ||
fca1cef2 | 89 | pub(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 | |
97 | pub(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. | |
109 | pub 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 | } |