Skip to content

Commit

Permalink
Ignore no such process when stopping tunnel (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ostenbom authored Jun 14, 2023
1 parent 39274f0 commit 0e569ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
3 changes: 3 additions & 0 deletions linkup-cli/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub enum PidError {
BadPidFile(String),
#[error("signal error: {0}")]
SignalErr(String),
#[error("no such process: {0}")]
NoSuchProcess(String),
}

pub fn send_sigint(pid_str: &str) -> Result<(), PidError> {
Expand All @@ -22,6 +24,7 @@ pub fn send_sigint(pid_str: &str) -> Result<(), PidError> {

match kill(pid, Some(Signal::SIGINT)) {
Ok(_) => Ok(()),
Err(nix::Error::ESRCH) => Err(PidError::NoSuchProcess(pid_str.to_string())),
Err(e) => Err(PidError::SignalErr(e.to_string())),
}
}
17 changes: 11 additions & 6 deletions linkup-cli/src/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ pub fn stop() -> Result<(), CliError> {

pub fn stop_tunnel() -> Result<(), CliError> {
match get_pid(LINKUP_CLOUDFLARED_PID) {
Ok(pid) => send_sigint(&pid).map_err(|e| {
CliError::StopErr(format!(
"Could not send SIGINT to cloudflared pid {}: {}",
pid, e
))
}),
Ok(pid) => {
match send_sigint(&pid) {
Ok(_) => Ok(()),
// If we're trying to stop it but it's already died, that's fine
Err(PidError::NoSuchProcess(_)) => Ok(()),
Err(e) => Err(CliError::StopErr(format!(
"Could not send SIGINT to cloudflared pid {}: {}",
pid, e
))),
}
}
Err(PidError::NoPidFile(_)) => Ok(()),
Err(e) => Err(CliError::StopErr(format!(
"Could not get cloudflared pid: {}",
Expand Down

0 comments on commit 0e569ae

Please sign in to comment.