From 75f6908f71b0cab268e5a2e09a0582f967806bfc Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 19 Sep 2025 07:20:49 +0200 Subject: [PATCH] Revert "common/daemon: add a method to reexec itself" This reverts commit 216786d40e4c9469da008aef429fb6e4d8fe97de. We don't really need that if we move logic to cmd package. --- cmd/components.go | 1 - common/daemon/root.go | 37 ++++--------------------------------- common/daemon/root_test.go | 23 ----------------------- common/daemon/tests.go | 10 ---------- 4 files changed, 4 insertions(+), 67 deletions(-) diff --git a/cmd/components.go b/cmd/components.go index 92b0556c..9358f885 100644 --- a/cmd/components.go +++ b/cmd/components.go @@ -39,7 +39,6 @@ func StartStopComponents(r *reporter.Reporter, daemonComponent daemon.Component, <-daemonComponent.Terminated() r.Info().Msg("stopping all components") - daemonComponent.FinishReexec() return nil } diff --git a/common/daemon/root.go b/common/daemon/root.go index 6a310fb5..4caec74e 100644 --- a/common/daemon/root.go +++ b/common/daemon/root.go @@ -9,7 +9,6 @@ package daemon import ( "os" "os/signal" - "sync/atomic" "syscall" "gopkg.in/tomb.v2" @@ -21,8 +20,6 @@ import ( type Component interface { Start() error Stop() error - Reexec() - FinishReexec() Track(t *tomb.Tomb, who string) // Lifecycle @@ -33,9 +30,8 @@ type Component interface { // realComponent is a non-mock implementation of the Component // interface. type realComponent struct { - r *reporter.Reporter - tombs []tombWithOrigin - shouldReexec atomic.Bool + r *reporter.Reporter + tombs []tombWithOrigin lifecycleComponent } @@ -74,12 +70,11 @@ func (c *realComponent) Start() error { c.Terminate() }(t) } - // On signal, terminate or reexec + // On signal, terminate go func() { signals := make(chan os.Signal, 1) signal.Notify(signals, - syscall.SIGINT, - syscall.SIGTERM) + syscall.SIGINT, syscall.SIGTERM) select { case s := <-signals: c.r.Debug().Stringer("signal", s).Msg("signal received") @@ -102,30 +97,6 @@ func (c *realComponent) Stop() error { return nil } -// Reexec will reexecute the current process with the same arguments. -func (c *realComponent) Reexec() { - c.shouldReexec.Store(true) - c.Terminate() -} - -// FinishReexec should be called just before exiting to trigger the real reexec. -func (c *realComponent) FinishReexec() { - if c.shouldReexec.Load() { - executable, err := os.Executable() - if err != nil { - c.r.Err(err).Msg("cannot get executable name") - return - } - - env := os.Environ() - args := append([]string{executable}, os.Args[1:]...) - c.r.Info().Strs("args", args).Msg("reexec in progress") - if err := syscall.Exec(executable, args, env); err != nil { - c.r.Err(err).Msg("cannot reexec") - } - } -} - // Add a new tomb to be tracked. This is only used before Start(). func (c *realComponent) Track(t *tomb.Tomb, who string) { c.tombs = append(c.tombs, tombWithOrigin{ diff --git a/common/daemon/root_test.go b/common/daemon/root_test.go index 2ea5e3a7..fe835638 100644 --- a/common/daemon/root_test.go +++ b/common/daemon/root_test.go @@ -5,7 +5,6 @@ package daemon import ( "errors" - "os" "syscall" "testing" "testing/synctest" @@ -63,28 +62,6 @@ func TestTerminateWithSignal(t *testing.T) { } } -func TestReexecWithSignal(t *testing.T) { - - r := reporter.NewMock(t) - c, err := New(r) - if err != nil { - t.Fatalf("New() error:\n%+v", err) - } - helpers.StartStop(t, c) - - c.Reexec() - if os.Getenv("TEST_DAEMON_REEXEC") == "1" { - // This is a way to increase a bit coverage - executable, _ := os.Executable() - os.Remove(executable) - c.FinishReexec() - return - } - os.Setenv("TEST_DAEMON_REEXEC", "1") - c.FinishReexec() - t.Fatalf("No reexec done!") -} - func TestStop(t *testing.T) { r := reporter.NewMock(t) c, err := New(r) diff --git a/common/daemon/tests.go b/common/daemon/tests.go index af9041bc..1e22e4ad 100644 --- a/common/daemon/tests.go +++ b/common/daemon/tests.go @@ -17,8 +17,6 @@ type MockComponent struct { lifecycleComponent } -var _ Component = &MockComponent{} - // NewMock will create a daemon component that does nothing. func NewMock(t testing.TB) Component { t.Helper() @@ -40,14 +38,6 @@ func (c *MockComponent) Stop() error { return nil } -// Reexec does nothing for the mock implementation -func (c *MockComponent) Reexec() { -} - -// FinishReexec does nothing for the mock implementation -func (c *MockComponent) FinishReexec() { -} - // Track does nothing func (c *MockComponent) Track(_ *tomb.Tomb, _ string) { }