all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox 1/3] add systemd escape_unit and unescape_unit
@ 2021-09-23 10:09 Dietmar Maurer
  2021-09-23 10:09 ` [pbs-devel] [PATCH proxmox 2/3] add UPID api type Dietmar Maurer
  2021-09-23 10:09 ` [pbs-devel] [PATCH proxmox 3/3] bump proxmox version to 0.13.4-1 Dietmar Maurer
  0 siblings, 2 replies; 3+ messages in thread
From: Dietmar Maurer @ 2021-09-23 10:09 UTC (permalink / raw)
  To: pbs-devel

---
 proxmox/src/tools/mod.rs     |  1 +
 proxmox/src/tools/systemd.rs | 83 ++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 proxmox/src/tools/systemd.rs

diff --git a/proxmox/src/tools/mod.rs b/proxmox/src/tools/mod.rs
index b172b09..b3a3cb0 100644
--- a/proxmox/src/tools/mod.rs
+++ b/proxmox/src/tools/mod.rs
@@ -20,6 +20,7 @@ pub mod serde;
 pub mod time;
 pub mod uuid;
 pub mod vec;
+pub mod systemd;
 
 #[cfg(feature = "tfa")]
 pub mod tfa;
diff --git a/proxmox/src/tools/systemd.rs b/proxmox/src/tools/systemd.rs
new file mode 100644
index 0000000..68f8631
--- /dev/null
+++ b/proxmox/src/tools/systemd.rs
@@ -0,0 +1,83 @@
+use anyhow::{bail, Error};
+
+fn parse_hex_digit(d: u8) -> Result<u8, Error> {
+    if d >= b'0' && d <= b'9' {
+        return Ok(d - b'0');
+    }
+    if d >= b'A' && d <= b'F' {
+        return Ok(d - b'A' + 10);
+    }
+    if d >= b'a' && d <= b'f' {
+        return Ok(d - b'a' + 10);
+    }
+    bail!("got invalid hex digit");
+}
+
+/// Escape strings for usage in systemd unit names
+pub fn escape_unit(mut unit: &str, is_path: bool) -> String {
+    if is_path {
+        unit = unit.trim_matches('/');
+        if unit.is_empty() {
+            return String::from("-");
+        }
+    }
+
+    let unit = unit.as_bytes();
+
+    let mut escaped = String::new();
+
+    for (i, c) in unit.iter().enumerate() {
+        if *c == b'/' {
+            escaped.push('-');
+            continue;
+        }
+        if (i == 0 && *c == b'.')
+            || !(*c == b'_'
+                || *c == b'.'
+                || (*c >= b'0' && *c <= b'9')
+                || (*c >= b'A' && *c <= b'Z')
+                || (*c >= b'a' && *c <= b'z'))
+        {
+            escaped.push_str(&format!("\\x{:0x}", c));
+        } else {
+            escaped.push(*c as char);
+        }
+    }
+    escaped
+}
+
+/// Unescape strings used in systemd unit names
+pub fn unescape_unit(text: &str) -> Result<String, Error> {
+    let mut i = text.as_bytes();
+
+    let mut data: Vec<u8> = Vec::new();
+
+    loop {
+        if i.is_empty() {
+            break;
+        }
+        let next = i[0];
+        if next == b'\\' {
+            if i.len() < 4 {
+                bail!("short input");
+            }
+            if i[1] != b'x' {
+                bail!("unkwnown escape sequence");
+            }
+            let h1 = parse_hex_digit(i[2])?;
+            let h0 = parse_hex_digit(i[3])?;
+            data.push(h1 << 4 | h0);
+            i = &i[4..]
+        } else if next == b'-' {
+            data.push(b'/');
+            i = &i[1..]
+        } else {
+            data.push(next);
+            i = &i[1..]
+        }
+    }
+
+    let text = String::from_utf8(data)?;
+
+    Ok(text)
+}
-- 
2.30.2





^ permalink raw reply	[flat|nested] 3+ messages in thread

* [pbs-devel] [PATCH proxmox 2/3] add UPID api type
  2021-09-23 10:09 [pbs-devel] [PATCH proxmox 1/3] add systemd escape_unit and unescape_unit Dietmar Maurer
@ 2021-09-23 10:09 ` Dietmar Maurer
  2021-09-23 10:09 ` [pbs-devel] [PATCH proxmox 3/3] bump proxmox version to 0.13.4-1 Dietmar Maurer
  1 sibling, 0 replies; 3+ messages in thread
From: Dietmar Maurer @ 2021-09-23 10:09 UTC (permalink / raw)
  To: pbs-devel

---
 proxmox/src/api/mod.rs  |   2 +
 proxmox/src/api/upid.rs | 149 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+)
 create mode 100644 proxmox/src/api/upid.rs

diff --git a/proxmox/src/api/mod.rs b/proxmox/src/api/mod.rs
index aaec7df..18b5cd2 100644
--- a/proxmox/src/api/mod.rs
+++ b/proxmox/src/api/mod.rs
@@ -53,3 +53,5 @@ pub use router::{
 pub mod cli;
 
 pub mod de;
+
+pub mod upid;
diff --git a/proxmox/src/api/upid.rs b/proxmox/src/api/upid.rs
new file mode 100644
index 0000000..fd3e9c9
--- /dev/null
+++ b/proxmox/src/api/upid.rs
@@ -0,0 +1,149 @@
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+use anyhow::{bail, Error};
+
+use crate::api::schema::{ApiStringFormat, ApiType, Schema, StringSchema};
+use crate::const_regex;
+use crate::sys::linux::procfs;
+
+/// 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
+/// UPID:{node}:{pid}:{pstart}:{task_id}:{starttime}:{worker_type}:{worker_id}:{userid}:
+/// UPID:elsa:00004F37:0039E469:00000000:5CA78B83:garbage_collection::root@pam:
+/// ```
+/// Please note that we use tokio, so a single thread can run multiple
+/// tasks.
+// #[api] - manually implemented API type
+#[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 authenticated entity who started the task
+    pub auth_id: String,
+    /// The node name.
+    pub node: String,
+}
+
+crate::forward_serialize_to_display!(UPID);
+crate::forward_deserialize_to_from_str!(UPID);
+
+const_regex! {
+    pub PROXMOX_UPID_REGEX = 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}):",
+        r"(?P<wtype>[^:\s]+):(?P<wid>[^:\s]*):(?P<authid>[^:\s]+):$"
+    );
+}
+
+pub const PROXMOX_UPID_FORMAT: ApiStringFormat =
+    ApiStringFormat::Pattern(&PROXMOX_UPID_REGEX);
+
+pub const UPID_SCHEMA: Schema = StringSchema::new("Unique Process/Task Identifier")
+    .min_length("UPID:N:12345678:12345678:12345678:::".len())
+    .max_length(128) // arbitrary
+    .format(&PROXMOX_UPID_FORMAT)
+    .schema();
+
+impl ApiType for UPID {
+    const API_SCHEMA: Schema = UPID_SCHEMA;
+}
+
+impl UPID {
+    /// Create a new UPID
+    pub fn new(
+        worker_type: &str,
+        worker_id: Option<String>,
+        auth_id: String,
+    ) -> Result<Self, Error> {
+
+        let pid = unsafe { libc::getpid() };
+
+        let bad: &[_] = &['/', ':', ' '];
+
+        if worker_type.contains(bad) {
+            bail!("illegal characters in worker type '{}'", worker_type);
+        }
+
+        if auth_id.contains(bad) {
+            bail!("illegal characters in auth_id '{}'", auth_id);
+        }
+
+        static WORKER_TASK_NEXT_ID: AtomicUsize = AtomicUsize::new(0);
+
+        let task_id = WORKER_TASK_NEXT_ID.fetch_add(1, Ordering::SeqCst);
+
+        Ok(UPID {
+            pid,
+            pstart: procfs::PidStat::read_from_pid(nix::unistd::Pid::from_raw(pid))?.starttime,
+            starttime: crate::tools::time::epoch_i64(),
+            task_id,
+            worker_type: worker_type.to_owned(),
+            worker_id,
+            auth_id,
+            node: crate::tools::nodename().to_owned(),
+        })
+    }
+}
+
+
+impl std::str::FromStr for UPID {
+    type Err = Error;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        if let Some(cap) = PROXMOX_UPID_REGEX.captures(s) {
+
+            let worker_id = if cap["wid"].is_empty() {
+                None
+            } else {
+                let wid = crate::tools::systemd::unescape_unit(&cap["wid"])?;
+                Some(wid)
+            };
+
+            Ok(UPID {
+                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,
+                auth_id: cap["authid"].to_string(),
+                node: cap["node"].to_string(),
+            })
+        } 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 {
+            crate::tools::systemd::escape_unit(id, false)
+        } else {
+            String::new()
+        };
+
+        // 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}:{}:{}:{}:",
+               self.node, self.pid, self.pstart, self.task_id, self.starttime, self.worker_type, wid, self.auth_id)
+    }
+}
-- 
2.30.2





^ permalink raw reply	[flat|nested] 3+ messages in thread

* [pbs-devel] [PATCH proxmox 3/3] bump proxmox version to 0.13.4-1
  2021-09-23 10:09 [pbs-devel] [PATCH proxmox 1/3] add systemd escape_unit and unescape_unit Dietmar Maurer
  2021-09-23 10:09 ` [pbs-devel] [PATCH proxmox 2/3] add UPID api type Dietmar Maurer
@ 2021-09-23 10:09 ` Dietmar Maurer
  1 sibling, 0 replies; 3+ messages in thread
From: Dietmar Maurer @ 2021-09-23 10:09 UTC (permalink / raw)
  To: pbs-devel

---
 proxmox/Cargo.toml       |  2 +-
 proxmox/debian/changelog | 12 ++++++++++++
 proxmox/debian/control   | 36 ++++++++++++++++++------------------
 3 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/proxmox/Cargo.toml b/proxmox/Cargo.toml
index 76b6450..b901969 100644
--- a/proxmox/Cargo.toml
+++ b/proxmox/Cargo.toml
@@ -1,7 +1,7 @@
 [package]
 name = "proxmox"
 edition = "2018"
-version = "0.13.3"
+version = "0.13.4"
 authors = [
     "Dietmar Maurer <dietmar@proxmox.com>",
     "Wolfgang Bumiller <w.bumiller@proxmox.com>",
diff --git a/proxmox/debian/changelog b/proxmox/debian/changelog
index 6edeaa7..cc1db02 100644
--- a/proxmox/debian/changelog
+++ b/proxmox/debian/changelog
@@ -1,3 +1,15 @@
+rust-proxmox (0.13.4-1) stable; urgency=medium
+
+  * add UPID api type
+
+  * add systemd escape_unit and unescape_unit
+
+  * schema: add extra info to array parameters
+
+  * schema: print item type-text instead of <array>
+
+ -- Proxmox Support Team <support@proxmox.com>  Thu, 23 Sep 2021 12:06:08 +0200
+
 rust-proxmox (0.13.3-1) unstable; urgency=medium
 
   * atomic_open_or_create_file: add support for OFlag::O_EXCL
diff --git a/proxmox/debian/control b/proxmox/debian/control
index 4d738c2..aed31c7 100644
--- a/proxmox/debian/control
+++ b/proxmox/debian/control
@@ -82,8 +82,8 @@ Provides:
  librust-proxmox-0+test-harness-dev (= ${binary:Version}),
  librust-proxmox-0.13-dev (= ${binary:Version}),
  librust-proxmox-0.13+test-harness-dev (= ${binary:Version}),
- librust-proxmox-0.13.3-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+test-harness-dev (= ${binary:Version})
+ librust-proxmox-0.13.4-dev (= ${binary:Version}),
+ librust-proxmox-0.13.4+test-harness-dev (= ${binary:Version})
 Description: Proxmox library - Rust source code
  This package contains the source for the Rust proxmox crate, packaged by
  debcargo for use with cargo and dh-cargo.
@@ -101,8 +101,8 @@ Provides:
  librust-proxmox-0+proxmox-api-macro-dev (= ${binary:Version}),
  librust-proxmox-0.13+api-macro-dev (= ${binary:Version}),
  librust-proxmox-0.13+proxmox-api-macro-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+api-macro-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+proxmox-api-macro-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+api-macro-dev (= ${binary:Version}),
+ librust-proxmox-0.13.4+proxmox-api-macro-dev (= ${binary:Version})
 Description: Proxmox library - feature "api-macro" and 1 more
  This metapackage enables feature "api-macro" for the Rust proxmox crate, by
  pulling in any additional dependencies needed by that feature.
@@ -122,8 +122,8 @@ Provides:
  librust-proxmox-0+u2f-dev (= ${binary:Version}),
  librust-proxmox-0.13+base32-dev (= ${binary:Version}),
  librust-proxmox-0.13+u2f-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+base32-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+u2f-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+base32-dev (= ${binary:Version}),
+ librust-proxmox-0.13.4+u2f-dev (= ${binary:Version})
 Description: Proxmox library - feature "base32" and 1 more
  This metapackage enables feature "base32" for the Rust proxmox crate, by
  pulling in any additional dependencies needed by that feature.
@@ -143,7 +143,7 @@ Depends:
 Provides:
  librust-proxmox-0+cli-dev (= ${binary:Version}),
  librust-proxmox-0.13+cli-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+cli-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+cli-dev (= ${binary:Version})
 Description: Proxmox library - feature "cli"
  This metapackage enables feature "cli" for the Rust proxmox crate, by pulling
  in any additional dependencies needed by that feature.
@@ -161,7 +161,7 @@ Depends:
 Provides:
  librust-proxmox-0+default-dev (= ${binary:Version}),
  librust-proxmox-0.13+default-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+default-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+default-dev (= ${binary:Version})
 Description: Proxmox library - feature "default"
  This metapackage enables feature "default" for the Rust proxmox crate, by
  pulling in any additional dependencies needed by that feature.
@@ -177,7 +177,7 @@ Depends:
 Provides:
  librust-proxmox-0+examples-dev (= ${binary:Version}),
  librust-proxmox-0.13+examples-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+examples-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+examples-dev (= ${binary:Version})
 Description: Proxmox library - feature "examples"
  This metapackage enables feature "examples" for the Rust proxmox crate, by
  pulling in any additional dependencies needed by that feature.
@@ -192,7 +192,7 @@ Depends:
 Provides:
  librust-proxmox-0+futures-dev (= ${binary:Version}),
  librust-proxmox-0.13+futures-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+futures-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+futures-dev (= ${binary:Version})
 Description: Proxmox library - feature "futures"
  This metapackage enables feature "futures" for the Rust proxmox crate, by
  pulling in any additional dependencies needed by that feature.
@@ -208,7 +208,7 @@ Depends:
 Provides:
  librust-proxmox-0+hyper-dev (= ${binary:Version}),
  librust-proxmox-0.13+hyper-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+hyper-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+hyper-dev (= ${binary:Version})
 Description: Proxmox library - feature "hyper"
  This metapackage enables feature "hyper" for the Rust proxmox crate, by pulling
  in any additional dependencies needed by that feature.
@@ -226,8 +226,8 @@ Provides:
  librust-proxmox-0+tfa-dev (= ${binary:Version}),
  librust-proxmox-0.13+openssl-dev (= ${binary:Version}),
  librust-proxmox-0.13+tfa-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+openssl-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+tfa-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+openssl-dev (= ${binary:Version}),
+ librust-proxmox-0.13.4+tfa-dev (= ${binary:Version})
 Description: Proxmox library - feature "openssl" and 1 more
  This metapackage enables feature "openssl" for the Rust proxmox crate, by
  pulling in any additional dependencies needed by that feature.
@@ -247,8 +247,8 @@ Provides:
  librust-proxmox-0+sortable-macro-dev (= ${binary:Version}),
  librust-proxmox-0.13+proxmox-sortable-macro-dev (= ${binary:Version}),
  librust-proxmox-0.13+sortable-macro-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+proxmox-sortable-macro-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+sortable-macro-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+proxmox-sortable-macro-dev (= ${binary:Version}),
+ librust-proxmox-0.13.4+sortable-macro-dev (= ${binary:Version})
 Description: Proxmox library - feature "proxmox-sortable-macro" and 1 more
  This metapackage enables feature "proxmox-sortable-macro" for the Rust proxmox
  crate, by pulling in any additional dependencies needed by that feature.
@@ -268,7 +268,7 @@ Depends:
 Provides:
  librust-proxmox-0+router-dev (= ${binary:Version}),
  librust-proxmox-0.13+router-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+router-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+router-dev (= ${binary:Version})
 Description: Proxmox library - feature "router"
  This metapackage enables feature "router" for the Rust proxmox crate, by
  pulling in any additional dependencies needed by that feature.
@@ -283,7 +283,7 @@ Depends:
 Provides:
  librust-proxmox-0+tokio-dev (= ${binary:Version}),
  librust-proxmox-0.13+tokio-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+tokio-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+tokio-dev (= ${binary:Version})
 Description: Proxmox library - feature "tokio"
  This metapackage enables feature "tokio" for the Rust proxmox crate, by pulling
  in any additional dependencies needed by that feature.
@@ -298,7 +298,7 @@ Depends:
 Provides:
  librust-proxmox-0+tokio-stream-dev (= ${binary:Version}),
  librust-proxmox-0.13+tokio-stream-dev (= ${binary:Version}),
- librust-proxmox-0.13.3+tokio-stream-dev (= ${binary:Version})
+ librust-proxmox-0.13.4+tokio-stream-dev (= ${binary:Version})
 Description: Proxmox library - feature "tokio-stream"
  This metapackage enables feature "tokio-stream" for the Rust proxmox crate, by
  pulling in any additional dependencies needed by that feature.
-- 
2.30.2





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-23 10:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-23 10:09 [pbs-devel] [PATCH proxmox 1/3] add systemd escape_unit and unescape_unit Dietmar Maurer
2021-09-23 10:09 ` [pbs-devel] [PATCH proxmox 2/3] add UPID api type Dietmar Maurer
2021-09-23 10:09 ` [pbs-devel] [PATCH proxmox 3/3] bump proxmox version to 0.13.4-1 Dietmar Maurer

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal