Skip to content

Commit

Permalink
feat(daemon): introduce "external" reboot mode
Browse files Browse the repository at this point in the history
This reboot mode ensures that the machine is not restarted during
daemon.Stop() when a restart has been requested. Instead, it signals the
daemon.Stop() caller that further actions to ensure a restart are
required through a new exported error daemon.ErrRestartExternal.

This is required in scenarios where further actions are required before
the machine can be safely rebooted, such as syncing and unmounting
filesystems.
  • Loading branch information
anpep committed Sep 3, 2024
1 parent 183eadf commit 96dde03
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 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 reboot handled externally")

systemdSdNotify = systemd.SdNotify
sysGetuid = sys.Getuid
Expand Down Expand Up @@ -711,6 +712,8 @@ const (
SystemdMode RebootMode = iota + 1
// Reboot uses direct kernel syscalls
SyscallMode
// Reboot is handled externally after the daemon stops
ExternalMode
)

// SetRebootMode configures how the system issues a reboot. The default
Expand All @@ -722,6 +725,8 @@ func SetRebootMode(mode RebootMode) {
rebootHandler = systemdModeReboot
case SyscallMode:
rebootHandler = syscallModeReboot
case ExternalMode:
rebootHandler = externalModeReboot
default:
panic(fmt.Sprintf("unsupported reboot mode %v", mode))
}
Expand Down Expand Up @@ -773,6 +778,13 @@ func syscallModeReboot(rebootDelay time.Duration) error {
return nil
}

func externalModeReboot(rebootDelay time.Duration) error {
if rebootDelay > 0 {
time.Sleep(rebootDelay)
}
return ErrRestartExternal
}

func (d *Daemon) Dying() <-chan struct{} {
return d.tomb.Dying()
}
Expand Down

0 comments on commit 96dde03

Please sign in to comment.