Skip to content

Commit

Permalink
Improve process handling during command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahrj committed Aug 8, 2021
1 parent 1b8a489 commit 584301e
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions ssh_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,41 @@ func makeSSHSessionHandler(shell string) ssh.Handler {
if stdIn, err := cmd.StdinPipe(); err != nil {
log.Println("Could not initialize StdInPipe", err)
s.Exit(1)
return
} else {
go io.Copy(stdIn, s)
go func() {
if _, err := io.Copy(stdIn, s); err != nil {
log.Printf("Error while copying input from %s to stdIn: %s", s.RemoteAddr().String(), err)
}
if err := stdIn.Close(); err != nil {
log.Println("Error while closing stdInPipe:", err)
}
}()
}
cmd.Stdout = s
cmd.Stderr = s

if err := cmd.Run(); err != nil {
log.Println("Command execution failed:", err)
io.WriteString(s, err.Error())
done := make(chan error, 1)
go func() { done <- cmd.Run() }()

select {
case err := <-done:
if err != nil {
log.Println("Command execution failed:", err)
io.WriteString(s, "Command execution failed: "+err.Error())
} else {
log.Println("Command execution successful")
}
s.Exit(cmd.ProcessState.ExitCode())

case <-s.Context().Done():
log.Println("Session closed by remote, killing dangling process")
if cmd.Process != nil && cmd.ProcessState == nil {
if err := cmd.Process.Kill(); err != nil {
log.Println("Failed to kill process:", err)
}
}
}
s.Exit(cmd.ProcessState.ExitCode())

default:
log.Println("No PTY requested, no command supplied")
Expand Down

0 comments on commit 584301e

Please sign in to comment.