From ae2628e57a25e86568fe00281f07cecfa103cde4 Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Mon, 18 Nov 2024 13:33:46 +0900 Subject: [PATCH] cmd/fshim/ipc: install signal handler on shim start Getting killed at this point will result in inconsistent state. Signed-off-by: Ophestra Umiker --- cmd/fshim/ipc/shim/shim.go | 11 +++++++++++ internal/app/start.go | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/fshim/ipc/shim/shim.go b/cmd/fshim/ipc/shim/shim.go index a03b600..db4a9b0 100644 --- a/cmd/fshim/ipc/shim/shim.go +++ b/cmd/fshim/ipc/shim/shim.go @@ -5,6 +5,7 @@ import ( "net" "os" "os/exec" + "os/signal" "strings" "sync" "sync/atomic" @@ -128,6 +129,16 @@ func (s *Shim) Start() (*time.Time, error) { } defer func() { killShim() }() + // take alternative exit path on signal + sig := make(chan os.Signal, 2) + signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) + go func() { + v := <-sig + fmsg.Printf("got %s after program start", v) + s.killFallback <- nil + signal.Ignore(syscall.SIGINT, syscall.SIGTERM) + }() + accept() var conn *net.UnixConn select { diff --git a/internal/app/start.go b/internal/app/start.go index 0b90583..c33d26f 100644 --- a/internal/app/start.go +++ b/internal/app/start.go @@ -179,7 +179,11 @@ func (a *app) Wait() (int, error) { // alternative exit path when kill was unsuccessful case err := <-a.shim.WaitFallback(): r = 255 - fmsg.Printf("cannot terminate shim on faulted setup: %v", err) + if err != nil { + fmsg.Printf("cannot terminate shim on faulted setup: %v", err) + } else { + fmsg.VPrintln("alternative exit path selected") + } } }