Linux Watchdog Sysfs Interface-Free Linux Device Drivers Training Online

PREV_LEC | NEXT_LEC
Linux Watchdog Sysfs Interface
Free Linux Device Drivers Course — Watchdog Device Drivers
7 sysfs attributes
Pretimeout governors
Zero lines of C required

Not every watchdog task needs a compiled program. The kernel’s watchdog framework exposes a complete sysfs interface that lets you inspect and, in some cases, tune a watchdog device using nothing but cat and echo. This lecture, part of our free linux device drivers course, walks through every attribute under /sys/class/watchdog/watchdogX/ and finishes the watchdog chapter by covering pretimeout governor selection — the policy layer that decides what happens in the moments before a watchdog actually resets your board.

CONFIG_WATCHDOG_SYSFS pretimeout_governor /sys/class/watchdog free linux kernel development course free embedded systems course

What You Will Learn

  • Every attribute exposed under /sys/class/watchdog/watchdogX/ and what each one means
  • How to enable the sysfs interface with CONFIG_WATCHDOG_SYSFS
  • What a pretimeout governor is, and how noop and panic differ
  • How to read and switch governors, and read the pretimeout value, from a shell script
  • How this closes out the watchdog subsystem chapter of the course

Prerequisites

This lecture builds directly on our earlier coverage of struct watchdog_device, the WDIOF_* flags, and the boot/status ioctls. If pretimeout as a concept is new to you, it was introduced when we covered the watchdog governor framework earlier in this chapter — a quick refresher there will help this lecture land faster.

Enabling the Sysfs Interface

The sysfs view of a watchdog device is gated behind the CONFIG_WATCHDOG_SYSFS kernel config option. When it’s enabled, every registered watchdog gets its own directory at /sys/class/watchdog/watchdogX/, where X is the index the kernel assigned that device — the same index used in /dev/watchdogX. This is a genuinely useful design: it means the exact same device can be driven by a C program using ioctls, or scripted entirely from the shell, without either approach knowing about the other.

The Watchdog Attribute Directory

Each watchdog’s sysfs directory exposes the following read-only or read-mostly attributes:

AttributeMeaning
nowayout1 if the device cannot be disarmed once started, 0 if magic-close is possible
statusSysfs equivalent of WDIOC_GETSTATUS — current internal status bits
timeleftSysfs equivalent of WDIOC_GETTIMELEFT — seconds until reset if not pinged
timeoutCurrently programmed timeout, in seconds
identityIdentity string for the watchdog device
bootstatusSysfs equivalent of WDIOC_GETBOOTSTATUS — why the last reset happened
stateWhether the device is currently active or inactive

Notice the pattern: nearly every ioctl we’ve covered in this chapter has a direct sysfs equivalent. That is intentional — the watchdog core implements both interfaces on top of the same internal watchdog_device state, so they can never disagree with each other on a given kernel.

Two Interfaces, One Source of Truth
struct watchdog_device (in kernel)
/ \
/ \
/dev/watchdogX (ioctl) /sys/class/watchdog/watchdogX/ (sysfs)
used by C programs used by shell scripts / udev / monitoring tools

Reading Attributes From the Shell

Every read-only attribute is a plain text file, so inspecting a watchdog needs nothing more than cat:

$ cat /sys/class/watchdog/watchdog0/identity
ep_swwdt
$ cat /sys/class/watchdog/watchdog0/timeout
30
$ cat /sys/class/watchdog/watchdog0/state
active
$ cat /sys/class/watchdog/watchdog0/timeleft
17

This is genuinely handy for a health-check cron job or a monitoring agent that shouldn’t have to open a device node and issue ioctls just to confirm a watchdog is still counting down.

Pretimeout Governors: The Policy Layer

A pretimeout is a warning that fires shortly before the watchdog’s real timeout expires — enough advance notice to, say, dump a stack trace or flush a log before the reset actually happens. What the kernel *does* with that warning is decided by a pretimeout governor, and governors are a familiar pattern if you’ve touched other kernel subsystems: CPUFreq has governors for scaling policy, thermal has governors for cooling policy, and watchdog has governors for pretimeout policy. Each one lives in its own small driver and is selected independently per device.

Two governors ship in mainline:

  • noop — does nothing beyond notifying the core; useful when user space itself wants to handle the warning (for example, by watching /dev/watchdogX for the pretimeout event).
  • panic — immediately panics the kernel when the pretimeout fires, which is useful when you specifically want a full kernel panic dump captured before the hardware watchdog forces a hard reset that would otherwise erase all diagnostic context.

Checking and Selecting a Governor

You can list which governors are available for a given watchdog, read which one is active, and switch it — all without writing a single line of C:

# List available governors
$ cat /sys/class/watchdog/watchdog0/pretimeout_available_governors
noop panic

# Check the currently active governor
$ cat /sys/class/watchdog/watchdog0/pretimeout_governor
panic

# Switch to noop
$ echo -n noop > /sys/class/watchdog/watchdog0/pretimeout_governor

# Confirm the switch took effect
$ cat /sys/class/watchdog/watchdog0/pretimeout_governor
noop

# Check how many seconds before timeout the pretimeout fires
$ cat /sys/class/watchdog/watchdog0/pretimeout
10

Note the -n flag on echo — it suppresses the trailing newline. Some sysfs write handlers are strict about trailing whitespace, and it’s a good habit to carry into any sysfs scripting you do elsewhere in the kernel.

Original Demo: ep_wdt_sysfs_report.sh

Here is a small original shell script — written for this course, not lifted from any reference — that dumps a complete, readable snapshot of a watchdog device using only the sysfs interface:

#!/bin/sh
# ep_wdt_sysfs_report.sh
# Usage: ./ep_wdt_sysfs_report.sh 0
WD=/sys/class/watchdog/watchdog${1:-0}

if [ ! -d "$WD" ]; then
    echo "No such watchdog: $WD" >&2
    exit 1
fi

echo "== $WD report =="
echo "identity   : $(cat $WD/identity 2>/dev/null)"
echo "state      : $(cat $WD/state 2>/dev/null)"
echo "timeout    : $(cat $WD/timeout 2>/dev/null)s"
echo "timeleft   : $(cat $WD/timeleft 2>/dev/null)s"
echo "nowayout   : $(cat $WD/nowayout 2>/dev/null)"
echo "bootstatus : $(cat $WD/bootstatus 2>/dev/null)"

if [ -f "$WD/pretimeout_governor" ]; then
    echo "pretimeout : $(cat $WD/pretimeout 2>/dev/null)s, governor=$(cat $WD/pretimeout_governor)"
fi
Example Output
$ chmod +x ep_wdt_sysfs_report.sh
$ ./ep_wdt_sysfs_report.sh 0
== /sys/class/watchdog/watchdog0 report ==
identity : ep_swwdt
state : active
timeout : 30s
timeleft : 22s
nowayout : 0
bootstatus : 0
pretimeout : 10s, governor=noop

Common Mistakes and Troubleshooting

  • Assuming pretimeout attributes always exist — pretimeout_governor and friends only appear if the driver supports pretimeout and CONFIG_WATCHDOG_PRETIMEOUT_GOV is enabled; always check the file exists before reading it.
  • Writing an unsupported governor name — the write fails with -EINVAL if the name isn’t in pretimeout_available_governors; always check that file first.
  • Forgetting sysfs needs root — writes to pretimeout_governor typically require root privileges even though reads don’t.
  • Racing ioctl and sysfs writers — because both interfaces share the same underlying watchdog_device, having one process ping via ioctl while another reconfigures via sysfs is safe from a data-corruption standpoint, but can be confusing operationally; pick one interface per device where you can.

Best Practices

  • Use sysfs for monitoring and occasional configuration from shell scripts or udev rules; use the ioctl/device-node interface for the actual keep-alive daemon that must run continuously.
  • Choose the panic pretimeout governor on boards where capturing a kernel panic dump before reset is more valuable than a graceful user-space warning.
  • Always verify a written governor took effect by reading it back — sysfs write success does not always guarantee the value matches what you expect if the driver applies additional validation.

Performance and Security Considerations

Sysfs reads and writes here are cheap — single register reads or simple field assignments — so there’s no meaningful performance concern for periodic polling. The security angle is more relevant: because sysfs writes to pretimeout_governor (and to timeout, on devices that expose it) usually require root, make sure any daemon or udev rule that touches these files runs with the minimum privilege actually needed, rather than blanket root access to the whole device tree.

Wrapping Up the Watchdog Chapter

Across this chapter of the course we went from the watchdog subsystem’s core data structures, through capability flags and the full watchdog_ops callback table, registration and restart-priority handling, pretimeout and GPIO-based watchdogs, the /dev/watchdog user-space lifecycle with magic-close semantics, ioctl-based status and boot-status queries, and now the complete sysfs interface with pretimeout governors. Together these give you everything needed to both write a robust watchdog driver and correctly consume one from user space — whether you’re servicing it with a compiled daemon or a shell script running from cron.

Summary / Key Takeaways

  • CONFIG_WATCHDOG_SYSFS exposes every watchdog device at /sys/class/watchdog/watchdogX/.
  • Sysfs attributes mirror the ioctl interface one-for-one, backed by the same in-kernel watchdog_device state.
  • Pretimeout governors decide what happens when a pretimeout warning fires — noop defers to user space, panic forces a kernel panic for diagnostics.
  • Governors and the pretimeout value itself can be inspected and changed with nothing more than cat and echo.

Conclusion

The sysfs interface is often the fastest way to sanity-check a watchdog during board bring-up, long before a full user-space daemon exists to manage it. Combined with the ioctl interface covered earlier in this free embedded systems course, you now have a complete toolkit for both driving and observing Linux watchdog devices. The next chapter moves into kernel development and debugging techniques, starting with how to read a kernel panic and use kernel tracing effectively.

Frequently Asked Questions

What kernel config option enables the watchdog sysfs interface?

CONFIG_WATCHDOG_SYSFS. Once enabled, every registered watchdog device gets a directory under /sys/class/watchdog/.

Do sysfs and ioctl watchdog interfaces ever disagree?

No, both are implemented on top of the same in-kernel watchdog_device state, so a value read through sysfs will match the equivalent ioctl on the same device.

What is a pretimeout governor?

A policy driver that decides what action to take when a watchdog pretimeout warning fires, before the actual timeout resets the system — mainline ships noop and panic governors.

When should I use the panic pretimeout governor?

When capturing a full kernel panic dump before a hard reset is more valuable than letting user space handle the warning gracefully, which is common on boards used for kernel debugging.

Can I write to every sysfs watchdog attribute?

No, several attributes like identity, state, and bootstatus are read-only; only a small set such as pretimeout_governor (and timeout, on supporting drivers) accept writes.

Why did my write to pretimeout_governor fail?

Most commonly either the process lacked root privileges, or the governor name written wasn’t present in pretimeout_available_governors.

Is the sysfs interface a replacement for the ioctl interface?

Not entirely — sysfs is ideal for inspection and occasional configuration from shell scripts, while a long-running keep-alive daemon still typically uses the /dev/watchdog device node directly.

Chapter Complete: Watchdog Device Drivers

Ready for what’s next? We move into kernel development and debugging — panic analysis and kernel tracing.

Next Chapter Browse the Full Course
PREV_LEC | NEXT_LEC

Leave a Reply

Your email address will not be published. Required fields are marked *