2020-08-06 13:46:01 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2020-08-06 13:46:01 +00:00
|
|
|
use chrono::Local;
|
2019-04-08 16:43:26 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use regex::Regex;
|
|
|
|
|
2020-01-16 07:58:54 +00:00
|
|
|
use proxmox::sys::linux::procfs;
|
|
|
|
|
2020-08-06 13:46:01 +00:00
|
|
|
use crate::api2::types::Userid;
|
|
|
|
|
2019-04-08 16:43:26 +00:00
|
|
|
/// Unique Process/Task Identifier
|
|
|
|
///
|
|
|
|
/// We use this to uniquely identify worker task. UPIDs have a short
|
|
|
|
/// string repesentaion, which gives additional information about the
|
|
|
|
/// type of the task. for example:
|
|
|
|
/// ```text
|
2020-08-06 13:46:01 +00:00
|
|
|
/// UPID:{node}:{pid}:{pstart}:{task_id}:{starttime}:{worker_type}:{worker_id}:{userid}:
|
2019-04-08 16:43:26 +00:00
|
|
|
/// UPID:elsa:00004F37:0039E469:00000000:5CA78B83:garbage_collection::root@pam:
|
|
|
|
/// ```
|
|
|
|
/// Please note that we use tokio, so a single thread can run multiple
|
|
|
|
/// tasks.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct UPID {
|
|
|
|
/// The Unix PID
|
|
|
|
pub pid: libc::pid_t,
|
|
|
|
/// The Unix process start time from `/proc/pid/stat`
|
|
|
|
pub pstart: u64,
|
|
|
|
/// The task start time (Epoch)
|
|
|
|
pub starttime: i64,
|
|
|
|
/// The task ID (inside the process/thread)
|
|
|
|
pub task_id: usize,
|
|
|
|
/// Worker type (arbitrary ASCII string)
|
|
|
|
pub worker_type: String,
|
|
|
|
/// Worker ID (arbitrary ASCII string)
|
|
|
|
pub worker_id: Option<String>,
|
|
|
|
/// The user who started the task
|
2020-08-06 13:46:01 +00:00
|
|
|
pub userid: Userid,
|
2019-04-08 16:43:26 +00:00
|
|
|
/// The node name.
|
|
|
|
pub node: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UPID {
|
|
|
|
|
|
|
|
/// Create a new UPID
|
2020-08-06 13:46:01 +00:00
|
|
|
pub fn new(
|
|
|
|
worker_type: &str,
|
|
|
|
worker_id: Option<String>,
|
|
|
|
userid: Userid,
|
|
|
|
) -> Result<Self, Error> {
|
2019-04-08 16:43:26 +00:00
|
|
|
|
|
|
|
let pid = unsafe { libc::getpid() };
|
|
|
|
|
2019-04-15 05:47:51 +00:00
|
|
|
let bad: &[_] = &['/', ':', ' '];
|
|
|
|
|
|
|
|
if worker_type.contains(bad) {
|
|
|
|
bail!("illegal characters in worker type '{}'", worker_type);
|
|
|
|
}
|
|
|
|
if let Some(ref worker_id) = worker_id {
|
|
|
|
if worker_id.contains(bad) {
|
|
|
|
bail!("illegal characters in worker id '{}'", worker_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static WORKER_TASK_NEXT_ID: AtomicUsize = AtomicUsize::new(0);
|
2019-04-08 16:43:26 +00:00
|
|
|
|
|
|
|
let task_id = WORKER_TASK_NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
|
|
|
Ok(UPID {
|
|
|
|
pid,
|
2020-01-16 10:39:44 +00:00
|
|
|
pstart: procfs::PidStat::read_from_pid(nix::unistd::Pid::from_raw(pid))?.starttime,
|
2019-04-08 16:43:26 +00:00
|
|
|
starttime: Local::now().timestamp(),
|
|
|
|
task_id,
|
|
|
|
worker_type: worker_type.to_owned(),
|
|
|
|
worker_id,
|
2020-08-06 13:46:01 +00:00
|
|
|
userid,
|
2019-08-03 15:06:23 +00:00
|
|
|
node: proxmox::tools::nodename().to_owned(),
|
2019-04-08 16:43:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the absolute path to the task log file
|
|
|
|
pub fn log_path(&self) -> std::path::PathBuf {
|
|
|
|
let mut path = std::path::PathBuf::from(super::PROXMOX_BACKUP_TASK_DIR);
|
|
|
|
path.push(format!("{:02X}", self.pstart % 256));
|
|
|
|
path.push(self.to_string());
|
|
|
|
path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl std::str::FromStr for UPID {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref REGEX: Regex = Regex::new(concat!(
|
|
|
|
r"^UPID:(?P<node>[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?):(?P<pid>[0-9A-Fa-f]{8}):",
|
|
|
|
r"(?P<pstart>[0-9A-Fa-f]{8,9}):(?P<task_id>[0-9A-Fa-f]{8,16}):(?P<starttime>[0-9A-Fa-f]{8}):",
|
2020-08-06 13:46:01 +00:00
|
|
|
r"(?P<wtype>[^:\s]+):(?P<wid>[^:\s]*):(?P<userid>[^:\s]+):$"
|
2019-04-08 16:43:26 +00:00
|
|
|
)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(cap) = REGEX.captures(s) {
|
|
|
|
|
2019-10-25 16:44:51 +00:00
|
|
|
Ok(UPID {
|
2019-04-08 16:43:26 +00:00
|
|
|
pid: i32::from_str_radix(&cap["pid"], 16).unwrap(),
|
|
|
|
pstart: u64::from_str_radix(&cap["pstart"], 16).unwrap(),
|
|
|
|
starttime: i64::from_str_radix(&cap["starttime"], 16).unwrap(),
|
|
|
|
task_id: usize::from_str_radix(&cap["task_id"], 16).unwrap(),
|
|
|
|
worker_type: cap["wtype"].to_string(),
|
|
|
|
worker_id: if cap["wid"].is_empty() { None } else { Some(cap["wid"].to_string()) },
|
2020-08-06 13:46:01 +00:00
|
|
|
userid: cap["userid"].parse()?,
|
2019-04-08 16:43:26 +00:00
|
|
|
node: cap["node"].to_string(),
|
2019-10-25 16:44:51 +00:00
|
|
|
})
|
2019-04-08 16:43:26 +00:00
|
|
|
} else {
|
|
|
|
bail!("unable to parse UPID '{}'", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for UPID {
|
|
|
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
|
|
|
|
let wid = if let Some(ref id) = self.worker_id { id } else { "" };
|
|
|
|
|
|
|
|
// Note: pstart can be > 32bit if uptime > 497 days, so this can result in
|
|
|
|
// more that 8 characters for pstart
|
|
|
|
|
|
|
|
write!(f, "UPID:{}:{:08X}:{:08X}:{:08X}:{:08X}:{}:{}:{}:",
|
2020-08-06 13:46:01 +00:00
|
|
|
self.node, self.pid, self.pstart, self.task_id, self.starttime, self.worker_type, wid, self.userid)
|
2019-04-08 16:43:26 +00:00
|
|
|
}
|
|
|
|
}
|