From ec6366b703f7f94a0d7c57167b33ffd6bc93ebed Mon Sep 17 00:00:00 2001 From: Jonathan Ribas Date: Wed, 5 Mar 2025 16:07:50 +0100 Subject: [PATCH] Apply review suggestion --- pkg/security/ptracer/ptracer.go | 20 +++++++++++--------- pkg/security/ptracer/ptracer_test.go | 12 ++---------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/pkg/security/ptracer/ptracer.go b/pkg/security/ptracer/ptracer.go index eef227667a6edd..5f828d9a943bad 100644 --- a/pkg/security/ptracer/ptracer.go +++ b/pkg/security/ptracer/ptracer.go @@ -541,17 +541,19 @@ func (ctx *CWSPtracerCtx) AttachTracer() error { return nil } +var forwardedSignals = []os.Signal{ + // Signal, number, and possible cause of container runtime sending them + syscall.SIGHUP, // 1 - Reload configuration (useful for reloading services inside a container) + syscall.SIGINT, // 2 - Graceful shutdown (sent when stopping container interactively) + syscall.SIGQUIT, // 3 - Graceful shutdown + core dump (used for debugging containerized apps) + syscall.SIGUSR1, // 10 - Application-specific user-defined signal (can trigger app reloads) + syscall.SIGUSR2, // 12 - Another user-defined signal, often used for hot reloads inside a container + syscall.SIGTERM, // 15 - Default stop signal (`docker stop`, `kubectl delete pod`) +} + func startSignalForwarder(pid int) { sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, - // Signal, number, and possible cause of container runtime sending them - syscall.SIGHUP, // 1 - Reload configuration (useful for reloading services inside a container) - syscall.SIGINT, // 2 - Graceful shutdown (sent when stopping container interactively) - syscall.SIGQUIT, // 3 - Graceful shutdown + core dump (used for debugging containerized apps) - syscall.SIGUSR1, // 10 - Application-specific user-defined signal (can trigger app reloads) - syscall.SIGUSR2, // 12 - Another user-defined signal, often used for hot reloads inside a container - syscall.SIGTERM, // 15 - Default stop signal (`docker stop`, `kubectl delete pod`) - ) + signal.Notify(sigChan, forwardedSignals...) go func() { for sig := range sigChan { unixSig, _ := sig.(syscall.Signal) diff --git a/pkg/security/ptracer/ptracer_test.go b/pkg/security/ptracer/ptracer_test.go index d470fb6a62d76a..bae00a85f10bee 100644 --- a/pkg/security/ptracer/ptracer_test.go +++ b/pkg/security/ptracer/ptracer_test.go @@ -66,15 +66,6 @@ func child() { } func TestSignalForwarding(t *testing.T) { - forwardedSignals := []syscall.Signal{ - syscall.SIGHUP, - syscall.SIGINT, - syscall.SIGQUIT, - syscall.SIGUSR1, - syscall.SIGUSR2, - syscall.SIGTERM, - } - // fork to have a child to receive signals err := fork.Fork("child") if err != nil { @@ -100,7 +91,8 @@ func TestSignalForwarding(t *testing.T) { for _, sig := range forwardedSignals { t.Run(fmt.Sprintf("%v", sig), func(t *testing.T) { // send signal to ourselves - syscall.Kill(os.Getpid(), sig) + unixSig, _ := sig.(syscall.Signal) + syscall.Kill(os.Getpid(), unixSig) // wait for child response n, err := fifo.Read(buffer)