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

View file

@ -15,6 +15,7 @@ import (
"sort" "sort"
"strings" "strings"
"sync" "sync"
"syscall"
"time" "time"
"github.com/acarl005/stripansi" "github.com/acarl005/stripansi"
@ -151,16 +152,30 @@ func tryDial(ctx context.Context, socketPath string) error {
return nil 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 { func tunnelSSH(ctx context.Context, host, localSocket string) error {
cmd := exec.CommandContext(ctx, "ssh", "-L", localSocket+":/var/run/docker.sock", host, "-N") cmd := exec.CommandContext(ctx, "ssh", "-L", localSocket+":/var/run/docker.sock", host, "-N")
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err := cmd.Start() err := cmd.Start()
if err != nil { if err != nil {
return err return err
} }
dockerSSHConnection = cmd
return nil 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. // Handle special cases including `ssh://` host schemes.
func clientBuilder(c *client.Client) error { func clientBuilder(c *client.Client) error {