First off, I want to mention that I’ve compiled my kernel with the following options, and the watchdog is compiled in and not a module.
CONFIG_WATCHDOG=y
CONFIG_WATCHDOG_NOWAYOUT=y
I found that if I did not use the second option, when my program exited abnormally, the watchdog file closed, and disabled itself. Maybe I was doing something wrong, but my solution was simply to increase the watchdog timer to 24 hours when I properly exit my program. I only properly exit my program when I’m running on my development platform. In normal operations the program gets started and runs forever.
Building on those sample programs you mention, My code looks like the following:
int WatchDog = open("/dev/watchdog", O_WRONLY);
if (WatchDog >= 0)
{
int WatchDogFlags = WDIOS_ENABLECARD;
int WatchDogTimeout = 45; // value that is not used, but simply different so that I can confirm the reading of the value below gets a value from the watchdog itself.
ioctl(WatchDog, WDIOC_SETOPTIONS, &WatchDogFlags);
cout << “Watchdog is enabled.” << endl;
ioctl(WatchDog, WDIOC_GETTIMEOUT, &WatchDogTimeout);
cout << “Watchdog timeout is " << WatchDogTimeout << " seconds.” << endl;
WatchDogTimeout = 120;
ioctl(WatchDog, WDIOC_SETTIMEOUT, &WatchDogTimeout);
cout << “Watchdog timeout was set to " << WatchDogTimeout << " seconds.” << endl;
}
// Here’s where I poll the watchdog inside my main processing loop.
int dummy;
if (WatchDog >= 0)
ioctl(WatchDog, WDIOC_KEEPALIVE, &dummy); // trigger the watchdog
if (WatchDog >= 0)
{
// set watchdog timeout to some very large number, in case the linux kernel does not allow the watchdog to be disabled once it’s been activated
// The LINUX kernel configration option is CONFIG_WATCHDOG_NOWAYOUT=y
// Ive decided that it’s a good thing to use, since otherwise if the program exits unexpectedly, the watchdog file gets closed and the watchdog will not reset the system.
int WatchDogTimeout = 86400; // 24 hours
ioctl(WatchDog, WDIOC_SETTIMEOUT, &WatchDogTimeout);
cout << “Watchdog timeout was set to " << WatchDogTimeout << " seconds.” << endl;
int WatchDogFlags = WDIOS_DISABLECARD;
ioctl(WatchDog, WDIOC_SETOPTIONS, &WatchDogFlags);
cout << “Watchdog is disabled.” << endl;
close(WatchDog);
}