1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
pub mod tar;
use std::{
io::{Error, ErrorKind, Result},
process::ExitStatus,
result,
};
use ipc_channel::ipc;
use nix::{sys::wait, unistd};
pub trait ToIOResult<T> {
fn to_io_result(self) -> Result<T>;
}
impl<T> ToIOResult<T> for nix::Result<T> {
fn to_io_result(self) -> Result<T> {
self.map_err(|error| match error {
nix::Error::Sys(code) => Error::from_raw_os_error(code as i32),
_ => Error::new(ErrorKind::Other, error),
})
}
}
pub trait Checkable {
fn check(&self) -> Result<()>;
}
impl Checkable for ExitStatus {
fn check(&self) -> Result<()> {
if self.success() {
Ok(())
} else {
Err(Error::new(
ErrorKind::Other,
format!("process exited with status {}", self),
))
}
}
}
impl Checkable for wait::WaitStatus {
fn check(&self) -> Result<()> {
match self {
wait::WaitStatus::Exited(_, 0) => Ok(()),
_ => Err(Error::new(
ErrorKind::Other,
format!("process exited with status {:?}", self),
)),
}
}
}
pub trait CheckDisconnect {
type Output;
fn check_disconnect(self) -> Result<Self::Output>;
}
impl<T> CheckDisconnect for result::Result<T, ipc::IpcError> {
type Output = result::Result<(), T>;
fn check_disconnect(self) -> Result<Self::Output> {
match self {
Ok(v) => Ok(Err(v)),
Err(ipc::IpcError::Disconnected) => Ok(Ok(())),
Err(error) => Err(Error::new(ErrorKind::Other, error)),
}
}
}
pub struct SetEUID(unistd::Uid);
impl Drop for SetEUID {
fn drop(&mut self) {
unistd::seteuid(self.0).expect("failed to reset effective UID");
}
}
pub fn seteuid(uid: unistd::Uid) -> Result<SetEUID> {
let old_uid = unistd::geteuid();
unistd::seteuid(uid).to_io_result()?;
Ok(SetEUID(old_uid))
}
pub struct SetEGID(unistd::Gid);
impl Drop for SetEGID {
fn drop(&mut self) {
unistd::setegid(self.0).expect("failed to reset effective GID");
}
}
pub fn setegid(gid: unistd::Gid) -> Result<SetEGID> {
let old_gid = unistd::getegid();
unistd::setegid(gid).to_io_result()?;
Ok(SetEGID(old_gid))
}
|