github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/install/stop_nix.go (about) 1 // Copyright 2019 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 //go:build (!windows && !darwin) || netbsd || openbsd || freebsd 5 // +build !windows,!darwin netbsd openbsd freebsd 6 7 package install 8 9 import ( 10 "context" 11 "fmt" 12 "os/exec" 13 "time" 14 15 "github.com/keybase/client/go/libkb" 16 keybase1 "github.com/keybase/client/go/protocol/keybase1" 17 ) 18 19 func StopAllButService(mctx libkb.MetaContext, exitCode keybase1.ExitCode) { 20 clients := libkb.GetClientStatus(mctx) 21 mctx.Debug("Shutting down Keybase clients...: %+v", clients) 22 for _, client := range clients { 23 if client.Details.ClientType == keybase1.ClientType_CLI { 24 continue 25 } 26 mctx.Debug("Shutting down Keybase client %+v", client) 27 28 // NOTE KBFS catches the SIGTERM and attempts to unmount mountdir before terminating, 29 // so we don't have to do it ourselves. 30 // NOTE We kill the GUI by its main electron process ID, which should 31 // shut down its child processes. 32 err := stopPID(mctx, client.Details.Pid) 33 if err != nil { 34 mctx.Debug("Error killing client %+v: %s", client, err) 35 } 36 } 37 38 if mctx.G().Env.GetRunMode() == libkb.ProductionRunMode { 39 // NOTE killall only inspects the first 15 characters; we need to use pkill -f 40 err := stopProcessInexactMatch(mctx, "keybase-redirector") 41 if err != nil { 42 mctx.Debug("Error killing keybase-redirector: %s", err) 43 } 44 } 45 } 46 47 func stopProcessInexactMatch(mctx libkb.MetaContext, process string) error { 48 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 49 defer cancel() 50 output, err := exec.CommandContext(ctx, "pkill", "-SIGTERM", "-f", process).CombinedOutput() 51 mctx.Debug("Output (pkill -SIGTERM -f %s): %s; err=%v", process, string(output), err) 52 return err 53 } 54 55 func stopPID(mctx libkb.MetaContext, pid int) error { 56 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 57 defer cancel() 58 output, err := exec.CommandContext(ctx, "kill", "-SIGTERM", fmt.Sprintf("%d", pid)).CombinedOutput() 59 mctx.Debug("Output (kill -SIGTERM %d): %s; err=%v", pid, string(output), err) 60 return err 61 }