agent: replace check! with map_err for readability

It's ambiguous and not easy to read to call method use macro.

Signed-off-by: Tim Zhang <tim@hyper.sh>
This commit is contained in:
Tim Zhang 2020-10-13 15:49:09 +08:00
parent 09aca49ed7
commit 7f9e5913e0

View File

@ -67,17 +67,6 @@ const CLOG_FD: &str = "CLOG_FD";
const FIFO_FD: &str = "FIFO_FD"; const FIFO_FD: &str = "FIFO_FD";
const HOME_ENV_KEY: &str = "HOME"; const HOME_ENV_KEY: &str = "HOME";
#[macro_export]
macro_rules! check {
($what:expr, $where:expr) => ({
if let Err(e) = $what {
let subsystem = $where;
let logger = slog_scope::logger().new(o!("subsystem" => subsystem));
warn!(logger, "{:?}", e);
}
})
}
#[derive(PartialEq, Clone, Copy)] #[derive(PartialEq, Clone, Copy)]
pub enum Status { pub enum Status {
CREATED, CREATED,
@ -778,10 +767,9 @@ impl BaseContainer for LinuxContainer {
let st = self.oci_state()?; let st = self.oci_state()?;
let (pfd_log, cfd_log) = unistd::pipe().context("failed to create pipe")?; let (pfd_log, cfd_log) = unistd::pipe().context("failed to create pipe")?;
check!(
fcntl::fcntl(pfd_log, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC)), let _ = fcntl::fcntl(pfd_log, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC))
"fcntl pfd log FD_CLOEXEC" .map_err(|e| warn!(logger, "fcntl pfd log FD_CLOEXEC {:?}", e));
);
let child_logger = logger.new(o!("action" => "child process log")); let child_logger = logger.new(o!("action" => "child process log"));
let log_handler = thread::spawn(move || { let log_handler = thread::spawn(move || {
@ -810,18 +798,16 @@ impl BaseContainer for LinuxContainer {
info!(logger, "exec fifo opened!"); info!(logger, "exec fifo opened!");
let (prfd, cwfd) = unistd::pipe().context("failed to create pipe")?; let (prfd, cwfd) = unistd::pipe().context("failed to create pipe")?;
let (crfd, pwfd) = unistd::pipe().context("failed to create pipe")?; let (crfd, pwfd) = unistd::pipe().context("failed to create pipe")?;
check!(
fcntl::fcntl(prfd, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC)), let _ = fcntl::fcntl(prfd, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC))
"fcntl prfd FD_CLOEXEC" .map_err(|e| warn!(logger, "fcntl prfd FD_CLOEXEC {:?}", e));
);
check!( let _ = fcntl::fcntl(pwfd, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC))
fcntl::fcntl(pwfd, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC)), .map_err(|e| warn!(logger, "fcntl pwfd FD_COLEXEC {:?}", e));
"fcntl pwfd FD_COLEXEC"
);
defer!({ defer!({
check!(unistd::close(prfd), "close prfd"); let _ = unistd::close(prfd).map_err(|e| warn!(logger, "close prfd {:?}", e));
check!(unistd::close(pwfd), "close pwfd"); let _ = unistd::close(pwfd).map_err(|e| warn!(logger, "close pwfd {:?}", e));
}); });
let child_stdin: std::process::Stdio; let child_stdin: std::process::Stdio;
@ -831,14 +817,10 @@ impl BaseContainer for LinuxContainer {
if tty { if tty {
let pseudo = pty::openpty(None, None)?; let pseudo = pty::openpty(None, None)?;
p.term_master = Some(pseudo.master); p.term_master = Some(pseudo.master);
check!( let _ = fcntl::fcntl(pseudo.master, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC))
fcntl::fcntl(pseudo.master, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC)), .map_err(|e| warn!(logger, "fnctl pseudo.master {:?}", e));
"fnctl pseudo.master" let _ = fcntl::fcntl(pseudo.slave, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC))
); .map_err(|e| warn!(logger, "fcntl pseudo.slave {:?}", e));
check!(
fcntl::fcntl(pseudo.slave, FcntlArg::F_SETFD(FdFlag::FD_CLOEXEC)),
"fcntl pseudo.slave"
);
child_stdin = unsafe { std::process::Stdio::from_raw_fd(pseudo.slave) }; child_stdin = unsafe { std::process::Stdio::from_raw_fd(pseudo.slave) };
child_stdout = unsafe { std::process::Stdio::from_raw_fd(pseudo.slave) }; child_stdout = unsafe { std::process::Stdio::from_raw_fd(pseudo.slave) };
@ -865,11 +847,10 @@ impl BaseContainer for LinuxContainer {
//restore the parent's process's pid namespace. //restore the parent's process's pid namespace.
defer!({ defer!({
check!( let _ = sched::setns(old_pid_ns, CloneFlags::CLONE_NEWPID)
sched::setns(old_pid_ns, CloneFlags::CLONE_NEWPID), .map_err(|e| warn!(logger, "settns CLONE_NEWPID {:?}", e));
"settns CLONE_NEWPID" let _ = unistd::close(old_pid_ns)
); .map_err(|e| warn!(logger, "close old pid namespace {:?}", e));
check!(unistd::close(old_pid_ns), "close old pid namespace");
}); });
let pidns = get_pid_namespace(&self.logger, linux)?; let pidns = get_pid_namespace(&self.logger, linux)?;
@ -911,7 +892,7 @@ impl BaseContainer for LinuxContainer {
} }
if p.init { if p.init {
check!(unistd::close(fifofd), "close fifofd"); let _ = unistd::close(fifofd).map_err(|e| warn!(logger, "close fifofd {:?}", e));
} }
info!(logger, "child pid: {}", p.pid); info!(logger, "child pid: {}", p.pid);
@ -929,10 +910,8 @@ impl BaseContainer for LinuxContainer {
Err(e) => { Err(e) => {
error!(logger, "create container process error {:?}", e); error!(logger, "create container process error {:?}", e);
// kill the child process. // kill the child process.
check!( let _ = signal::kill(Pid::from_raw(p.pid), Some(Signal::SIGKILL))
signal::kill(Pid::from_raw(p.pid), Some(Signal::SIGKILL)), .map_err(|e| warn!(logger, "signal::kill joining namespaces {:?}", e));
"signal::kill joining namespaces"
);
return Err(e); return Err(e);
} }
}; };
@ -945,10 +924,9 @@ impl BaseContainer for LinuxContainer {
let (exit_pipe_r, exit_pipe_w) = unistd::pipe2(OFlag::O_CLOEXEC) let (exit_pipe_r, exit_pipe_w) = unistd::pipe2(OFlag::O_CLOEXEC)
.context("failed to create pipe") .context("failed to create pipe")
.map_err(|e| { .map_err(|e| {
check!( let _ = signal::kill(Pid::from_raw(child.id() as i32), Some(Signal::SIGKILL))
signal::kill(Pid::from_raw(child.id() as i32), Some(Signal::SIGKILL)), .map_err(|e| warn!(logger, "signal::kill creating pipe {:?}", e));
"signal::kill creating pipe"
);
e e
})?; })?;
@ -962,7 +940,9 @@ impl BaseContainer for LinuxContainer {
self.processes.insert(p.pid, p); self.processes.insert(p.pid, p);
info!(logger, "wait on child log handler"); info!(logger, "wait on child log handler");
check!(log_handler.join(), "joining log handler"); let _ = log_handler
.join()
.map_err(|e| warn!(logger, "joining log handler {:?}", e));
info!(logger, "create process completed"); info!(logger, "create process completed");
return Ok(()); return Ok(());
} }