Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(daemon): add ExternalMode reboot #529

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions internals/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
ErrRestartSocket = fmt.Errorf("daemon stop requested to wait for socket activation")
ErrRestartServiceFailure = fmt.Errorf("daemon stop requested due to service failure")
ErrRestartCheckFailure = fmt.Errorf("daemon stop requested due to check failure")
ErrRestartExternal = fmt.Errorf("daemon stop requested due to externally-handled reboot")

systemdSdNotify = systemd.SdNotify
sysGetuid = sys.Getuid
Expand Down Expand Up @@ -677,6 +678,10 @@ func (d *Daemon) rebootDelay() (time.Duration, error) {
}

func (d *Daemon) doReboot(sigCh chan<- os.Signal, waitTimeout time.Duration) error {
if rebootMode == ExternalMode {
return ErrRestartExternal
}

rebootDelay, err := d.rebootDelay()
if err != nil {
return err
Expand All @@ -702,25 +707,31 @@ func (d *Daemon) doReboot(sigCh chan<- os.Signal, waitTimeout time.Duration) err

const rebootMsg = "reboot scheduled to update the system"

var rebootHandler = systemdModeReboot

type RebootMode int

const (
// Reboot uses systemd
SystemdMode RebootMode = iota + 1
// Reboot uses direct kernel syscalls
SyscallMode
// Reboot is handled externally after the daemon stops
ExternalMode
)

var (
rebootHandler = systemdModeReboot
rebootMode = SystemdMode
)

// SetRebootMode configures how the system issues a reboot. The default
// reboot handler mode is SystemdMode, which relies on systemd
// (or similar) provided functionality to reboot.
func SetRebootMode(mode RebootMode) {
rebootMode = mode
switch mode {
case SystemdMode:
rebootHandler = systemdModeReboot
case SyscallMode:
case SyscallMode, ExternalMode:
rebootHandler = syscallModeReboot
default:
panic(fmt.Sprintf("unsupported reboot mode %v", mode))
Expand Down
54 changes: 54 additions & 0 deletions internals/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package daemon
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -793,6 +794,7 @@ func (s *daemonSuite) TestRestartSystemWiring(c *C) {
oldRebootWaitTimeout := rebootWaitTimeout
defer func() {
rebootHandler = systemdModeReboot
rebootMode = SystemdMode
rebootNoticeWait = oldRebootNoticeWait
rebootWaitTimeout = oldRebootWaitTimeout
}()
Expand Down Expand Up @@ -1172,6 +1174,58 @@ services:
c.Assert(err, Equals, ErrRestartServiceFailure)
}

func (s *daemonSuite) TestRebootExternal(c *C) {
oldRebootWaitTimeout := rebootWaitTimeout
defer func() {
rebootWaitTimeout = oldRebootWaitTimeout
}()
rebootWaitTimeout = 0

didFallbackReboot := false
defer FakeSyscallSync(func() {})()
defer FakeSyscallReboot(func(cmd int) error {
if cmd == syscall.LINUX_REBOOT_CMD_RESTART {
didFallbackReboot = true
}
return nil
})()
SetRebootMode(ExternalMode)
defer SetRebootMode(SystemdMode)

d := s.newDaemon(c)
makeDaemonListeners(c, d)
c.Assert(d.Start(), IsNil)

st := d.overlord.State()
st.Lock()
restart.Request(st, restart.RestartSystem)
st.Unlock()

select {
case <-d.Dying():
case <-time.After(2 * time.Second):
c.Fatal("RequestRestart -> overlord -> Kill chain didn't work")
}

d.mu.Lock()
restartType := d.requestedRestart
d.mu.Unlock()

c.Assert(restartType, Equals, restart.RestartSystem)

err := d.Stop(nil)
c.Assert(errors.Is(err, ErrRestartExternal), Equals, true)

d.mu.Lock()
d.requestedRestart = restart.RestartUnset
d.mu.Unlock()

c.Assert(didFallbackReboot, Equals, true)
st.Lock()
restart.ClearReboot(st)
st.Unlock()
}

func (s *daemonSuite) TestConnTrackerCanShutdown(c *C) {
ct := &connTracker{conns: make(map[net.Conn]struct{})}
c.Check(ct.CanStandby(), Equals, true)
Expand Down
Loading