summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2023-01-27 14:44:36 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2023-01-27 14:52:01 +0000
commit39b32521291dde5254d6148b3601b2c17bb4cdad (patch)
tree50bffa5ad490444e580bb99f2326183f3fcb9a9b
parent8c30492c7a186414df01dade43024d1c73f20b4c (diff)
downloadpam_usercg_rust-39b32521291dde5254d6148b3601b2c17bb4cdad.tar.gz
pam_usercg_rust-39b32521291dde5254d6148b3601b2c17bb4cdad.tar.xz
pam_usercg_rust-39b32521291dde5254d6148b3601b2c17bb4cdad.zip
clean up error handling
By adding an implicit conversion from std::io::Error to SessionError, a lot of the .or(SessionError) bits can be dropped.
-rw-r--r--src/lib.rs19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 1b55c8f..73cd9b0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -27,20 +27,25 @@ fn create_and_open_dir<P: AsRef<Path> + Copy>(
struct SessionError;
+impl From<std::io::Error> for SessionError {
+ fn from(_: std::io::Error) -> Self {
+ SessionError
+ }
+}
+
fn open_session(h: &mut PamHandle) -> Result<(), SessionError> {
let user = h.get_user(None).or(Err(SessionError))?;
let user = users::get_user_by_name(&user).ok_or(SessionError)?;
let aa = cap_std::ambient_authority();
- let d = Dir::open_ambient_dir(CG_MOUNT, aa).or(Err(SessionError))?;
- let d = create_and_open_dir(&d, "user").or(Err(SessionError))?;
- let d = create_and_open_dir(&d, &user.uid().to_string())
- .or(Err(SessionError))?;
- let d = create_and_open_dir(&d, "leaf").or(Err(SessionError))?;
+ let d = Dir::open_ambient_dir(CG_MOUNT, aa)?;
+ let d = create_and_open_dir(&d, "user")?;
+ let d = create_and_open_dir(&d, &user.uid().to_string())?;
+ let d = create_and_open_dir(&d, "leaf")?;
let pid = process::id().to_string();
let mut options = OpenOptions::new();
options.write(true);
- let mut procs = d.open_with("cgroup.procs", &options).or(Err(SessionError))?;
- procs.write_all(pid.as_bytes()).or(Err(SessionError))?;
+ let mut procs = d.open_with("cgroup.procs", &options)?;
+ procs.write_all(pid.as_bytes())?;
Ok(())
}