set pgid and send kill signal on exit

This commit is contained in:
Charlie Moog 2021-12-26 09:41:43 -06:00
parent 7f68d01181
commit 892fc090b6
No known key found for this signature in database
GPG key ID: 54C2F30EA784F821
2 changed files with 18 additions and 1 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/go-errors/errors"
"github.com/integrii/flaggy"
"github.com/jesseduffield/lazydocker/pkg/app"
"github.com/jesseduffield/lazydocker/pkg/commands"
"github.com/jesseduffield/lazydocker/pkg/config"
"github.com/jesseduffield/yaml"
)
@ -73,6 +74,7 @@ func main() {
if err == nil {
err = app.Run()
}
commands.CloseDockerSocketConnection()
if err != nil {
if errMessage, known := app.KnownError(err); known {

View file

@ -15,6 +15,7 @@ import (
"sort"
"strings"
"sync"
"syscall"
"time"
"github.com/acarl005/stripansi"
@ -151,16 +152,30 @@ func tryDial(ctx context.Context, socketPath string) error {
return nil
}
// CloseDockerSocketConnection kills the docker socket SSH forwarding process, if it exists.
//
// If will exist when DOCKER_HOST has the protocol scheme `ssh://`.
func CloseDockerSocketConnection() {
if dockerSSHConnection != nil {
syscall.Kill(-dockerSSHConnection.Process.Pid, syscall.SIGKILL)
}
}
// dockerSSHConnection holds package-level state for the last-opened SSH tunnel to a remote docker socket.
var dockerSSHConnection *exec.Cmd
func tunnelSSH(ctx context.Context, host, localSocket string) error {
cmd := exec.CommandContext(ctx, "ssh", "-L", localSocket+":/var/run/docker.sock", host, "-N")
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err := cmd.Start()
if err != nil {
return err
}
dockerSSHConnection = cmd
return nil
}
// Build a new docker client from the enviornment.
// Build a new docker client from the environment.
//
// Handle special cases including `ssh://` host schemes.
func clientBuilder(c *client.Client) error {