10

I'm trying to feed the watchdog device - /dev/watchdog in Raspbian - from C.

The problem is no matter how i try to access the device always throws busy or permission denied errors (as the process is already running and being fed by the system..).

In the watchdog API says that '..the driver will not disable the watchdog unless a specific magic character 'V' has been sent to /dev/watchdog just before closing the file.' but then again i cannot write /dev/watchdog.. I tried:

echo V > /dev/watchdog //bash, /dev/watchdog: Permission denied

open("/dev/watchdog", O_WRONLY); //C, Device or resource busy

Is there any way to free the device so i can control the heartbeat from C?

TMichel
  • 4,336
  • 9
  • 44
  • 67

2 Answers2

6

I had this problem when i worked with Raspberry Pi. My application used extensive CPU time. After 1 or 2 days work it caused the Raspberry pi hangs. So i decided to use the wtchdog. When i wanted to write watchdog device from C++ program i got same error.

The solution that i found:

open a new rule file sudo nano /etc/udev/rules.d/60-watchdog.rules

and add this line to the file KERNEL=="watchdog", MODE="0666"

After this, i was able to access watchdog form terminal screen or c++ program.

mozer
  • 319
  • 5
  • 13
  • I cannot try it right now but if it works, this is gold. Thank you very much and I'll keep you updated. – TMichel Mar 03 '16 at 08:20
  • Didn't work for me: still "Device or resource busy". @TMichel: do you have any progress? – beemaster Mar 17 '17 at 08:58
  • 1
    This is essentially opening up read and write permissions to everybody on the system (which is a bad idea if you care about security). If you're going to do this, do something like `KERNEL=="watchdog", MODE="0660", GROUP="watchdog"` and add your user to the `watchdog` group. Ideally, this should work with root though... – Chris Watts Mar 17 '17 at 12:45
  • 1
    @CJxD: I understand it changes the file permissions. And it does. But it doesn't help. I've tried the very simple code in 'c' that just opens the file: `int fd = open("/dev/watchdog", O_WRONLY);` Run it from root. Got the same error. It looks like the error comes from within the 'open' function. – beemaster Mar 17 '17 at 14:06
2

Why the 'device or resource busy' error

The issue I found was that systemd and wd_keepalive seemed to be using the watchdog resource per fuser output:

>sudo fuser -v /dev/watchdog
                     USER        PID ACCESS COMMAND
/dev/watchdog:       root      15087 F.... wd_keepalive

and

>sudo fuser -v /dev/watchdog
                     USER        PID ACCESS COMMAND
/dev/watchdog:       root          1 F.... systemd

Fix the Busy Error

I removed watchdog timer references from /etc/systemd/system.conf to get rid of the busy issue on the systemd busy system.

If you're feeling bold, you can kill -9 your wd_keepalive PID, then echo your character toward the /dev/watchdog device, if you want to manually control the watchdog. I prefer just letting the daemon do its thing.

Watchdog setup that works on Pi running Stretch 12/17/18

Unlike some other suggestions on SO and the net, I did not need to install watchdog as a device on the Pi e.g. in /boot/config.txt. Nor did I need to call any services other than via systemctl. I just ran

sudo apt-get install watchdog
sudo update-rc.d watchdog defaults

Then to configure watchdog I put these lines in /etc/watchdog.conf

watchdog-device = /dev/watchdog

# Set default Timeout
watchdog-timeout         = 14

Running Watchdog

Then the only thing I needed to do to use watchdog was call this from my application, which I run after bootup:

sudo systemctl enable watchdog
sudo systemctl start watchdog
sudo systemctl -l status watchdog
Kelton Temby
  • 825
  • 10
  • 20
  • Removing the references to watchdog in `/etc/systemd/system.conf` then rebooting, fixed the problem for me in Ubuntu 16.04. Thank you. – xinthose Jun 04 '20 at 19:19