Documentation
¶
Index ¶
Constants ¶
const ( // WDIOC_KEEPALIVE is the ioctl command to reset/pet the watchdog timer // This is equivalent to _IO('W', 5) in C WDIOC_KEEPALIVE = 0x40045705 // WDIOC_SETTIMEOUT is the ioctl command to set the watchdog timeout // This is equivalent to _IOWR('W', 6, int) in C WDIOC_SETTIMEOUT = 0x40045706 // WDIOC_GETTIMEOUT is the ioctl command to get the watchdog timeout // This is equivalent to _IOR('W', 7, int) in C WDIOC_GETTIMEOUT = 0x40045707 )
Linux watchdog ioctl constants Reference: include/uapi/linux/watchdog.h
const ( // MaxWatchdogRetries is the maximum number of retry attempts for watchdog operations MaxWatchdogRetries = 2 // InitialWatchdogRetryDelay is the initial delay between watchdog retry attempts InitialWatchdogRetryDelay = 50 * time.Millisecond // MaxWatchdogRetryDelay is the maximum delay between watchdog retry attempts MaxWatchdogRetryDelay = 500 * time.Millisecond // WatchdogRetryBackoffFactor is the exponential backoff factor for watchdog retry delays WatchdogRetryBackoffFactor = 2.0 )
Retry configuration constants for watchdog operations
const ( // SoftdogModule is the name of the Linux software watchdog kernel module SoftdogModule = "softdog" // DefaultSoftdogTimeout is the default timeout in seconds for the softdog module DefaultSoftdogTimeout = 60 // SoftdogModprobe is the command to load the softdog module SoftdogModprobe = "modprobe" // NsenterCommand is the command to enter host namespaces NsenterCommand = "nsenter" // HostPID is the PID of the host init process HostPID = "1" )
Softdog configuration constants
Variables ¶
var ( // ErrIoctlNotSupported indicates that the watchdog driver doesn't support ioctl operations ErrIoctlNotSupported = errors.New("ioctl not supported by watchdog driver") )
Errors for watchdog operations
Functions ¶
This section is empty.
Types ¶
type Watchdog ¶
type Watchdog struct {
// contains filtered or unexported fields
}
Watchdog represents a Linux kernel watchdog device interface. It provides methods to interact with hardware watchdog devices through the Linux watchdog subsystem.
func New ¶
New creates a new Watchdog instance by opening the watchdog device at the specified path. Common paths include '/dev/watchdog' or '/dev/watchdog0'.
Parameters:
- path: The filesystem path to the watchdog device (e.g., "/dev/watchdog")
Returns:
- *Watchdog: A new Watchdog instance if successful
- error: An error if the device cannot be opened
The device is opened with O_WRONLY flag as required by most watchdog devices. Once opened, the watchdog timer is typically activated and must be periodically reset using the Pet() method to prevent system reset.
func NewWithLogger ¶
NewWithLogger creates a new Watchdog instance with a logger for retry operations
func NewWithSoftdogFallback ¶
NewWithSoftdogFallback creates a new Watchdog instance, attempting to use the specified path first, and falling back to loading and using the softdog module if no hardware watchdog is available.
This function provides automatic fallback behavior: 1. Try to open the specified watchdog device path 2. If that fails and no other watchdog devices exist, try to load softdog module 3. If softdog loads successfully, use /dev/watchdog as the device path
Parameters:
- path: The preferred filesystem path to the watchdog device (e.g., "/dev/watchdog")
- logger: Logger for debugging and error reporting
Returns:
- *Watchdog: A new Watchdog instance if successful
- error: An error if neither hardware nor software watchdog can be initialized
This is the recommended function for production use as it provides the best reliability.
func NewWithSoftdogFallbackAndTestMode ¶
func NewWithSoftdogFallbackAndTestMode(path string, testMode bool, logger logr.Logger) (*Watchdog, error)
NewWithSoftdogFallbackAndTestMode creates a new Watchdog instance with optional test mode support. This is similar to NewWithSoftdogFallback but allows enabling test mode for the softdog module.
Parameters:
- path: The preferred filesystem path to the watchdog device (e.g., "/dev/watchdog")
- testMode: If true, enables soft_noboot=1 for softdog (prevents actual reboots during testing)
- logger: Logger for debugging and error reporting
Returns:
- *Watchdog: A new Watchdog instance if successful
- error: An error if neither hardware nor software watchdog can be initialized
Test mode is useful for development and testing environments where you want to test watchdog functionality without triggering actual system reboots.
func (*Watchdog) Close ¶
Close closes the watchdog device file descriptor and releases associated resources.
IMPORTANT: Closing the watchdog device may have different behaviors depending on the specific watchdog driver: - Some drivers stop the watchdog timer when the device is closed - Others continue running and will reset the system if not reopened and pet - Some require writing 'V' to the device before closing to stop the timer
Returns:
- error: An error if the device cannot be closed properly
This method marks the watchdog as closed and prevents further operations. It's safe to call Close() multiple times.
func (*Watchdog) IsOpen ¶
IsOpen returns true if the watchdog device is currently open and available for operations.
func (*Watchdog) IsSoftdog ¶
IsSoftdog returns true if this watchdog is using the software watchdog (softdog) module
func (*Watchdog) Pet ¶
Pet resets the watchdog timer, preventing the system from being reset. This method must be called periodically (before the timeout expires) to keep the system running. The frequency depends on the watchdog's configured timeout value.
This method includes retry logic for transient errors, as watchdog petting is critical for system stability. It uses a two-tier approach: 1. Primary: WDIOC_KEEPALIVE ioctl command (preferred method) 2. Fallback: Write-based keep-alive when ioctl is not supported (ENOTTY)
Returns:
- error: An error if the watchdog cannot be pet after retries, or if the device is not open
This method automatically falls back to write-based keep-alive when the WDIOC_KEEPALIVE ioctl is not supported by the watchdog driver (such as some softdog implementations). This ensures compatibility across different kernel configurations and architectures.