go-systemd icon indicating copy to clipboard operation
go-systemd copied to clipboard

daemon: add `SdNotifyMonotonicUsec` helper function

Open corhere opened this issue 1 year ago • 1 comments

The synchronized service reload protocol added in systemd version 253 requires that the service provides a MONOTONIC_USEC field alongside the RELOADING=1 notification message for synchronization purposes. The value carried in this field must be the system CLOCK_MONOTONIC timestamp at the time the notification message was generated as systemd compares it to other CLOCK_MONOTONIC timestamps taken by pid1.

While the Go standard library does utilize CLOCK_MONOTONIC in the implementation of package "time", the absolute monotonic timestamps in time.Time values are not made available to programmers. Users familiar with idiomatic usage of monotonic timestamps in Go might (incorrectly) try to implement MONOTONIC_USEC using process-relative monotonic timestamps, like so:

var processStart = time.Now()

func NotifyReloadingINCORRECT() {
	ts := time.Since(processStart)/time.Microsecond  // WRONG
	msg := fmt.Sprintf(
		daemon.SdNotifyReload+"\nMONOTONIC_USEC=%d", ts,
	)
	_, _ = daemon.SdNotify(false, msg)
}

Help users fall into the pit of success by providing a helper function SdNotifyMonotonicUsec() which returns a MONOTONIC_USEC variable-assignment string which encodes the system CLOCK_MONOTONIC timestamp in decimal microseconds, as systemd expects.

corhere avatar Feb 13 '24 18:02 corhere

@cgwalters ptal

thaJeztah avatar Feb 20 '24 13:02 thaJeztah