Skip to main content

rlmctl_common/
error.rs

1use std::path::PathBuf;
2
3/// Instructions to enable cgroup delegation for the calling user, shared by
4/// every error and hint that needs to explain the fix.
5pub const DELEGATION_HINT: &str = "enable cgroup delegation (then log out and back in):\n  sudo mkdir -p /etc/systemd/system/user@.service.d\n  printf '[Service]\\nDelegate=cpu memory io\\n' | sudo tee /etc/systemd/system/user@.service.d/rlm-delegate.conf\n  sudo systemctl daemon-reload";
6
7pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    #[error("process with pid {0} not found (process may have exited)")]
12    ProcessNotFound(u32),
13
14    #[error("no process found matching '{0}'\n  hint: check the name with: pgrep -a {0}")]
15    ProcessNameNotFound(String),
16
17    #[error("cgroup operation failed: {0}")]
18    Cgroup(String),
19
20    #[error("invalid memory value: {0}\n  hint: use format like '512M', '2G', or '1024' (bytes)")]
21    InvalidMemory(String),
22
23    #[error("invalid cpu value: {0}\n  hint: use percentage like '50%' or '150%' (for 1.5 cores)")]
24    InvalidCpu(String),
25
26    #[error("invalid arguments: {0}")]
27    InvalidArgs(String),
28
29    #[error("permission denied: {path}\n  hint: {hint}", hint = DELEGATION_HINT)]
30    PermissionDenied { path: PathBuf },
31
32    #[error("cgroups v2 not available at {0}\n  hint: ensure your kernel supports cgroups v2 (Linux 4.5+) and it's mounted")]
33    CgroupsV2NotAvailable(PathBuf),
34
35    #[error("config error: {0}")]
36    Config(String),
37
38    #[error(transparent)]
39    Io(#[from] std::io::Error),
40}