Linux软件看门狗 [英] Linux software watchdog

查看:291
本文介绍了Linux软件看门狗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的Linux系统监控器,并希望包括一些看门狗功能。在内核中,可以配置看门狗保持,即使为/ dev /看门狗被关闭下去。换句话说,如果我的守护进程正常退出和关闭的/ dev /看门狗,系统仍然会重新开机59秒更高版本。可能会或可能不会为用户所期望的行为。

I am writing a system monitor for Linux and want to include some watchdog functionality. In the kernel, you can configure the watchdog to keep going even if /dev/watchdog is closed. In other words, if my daemon exits normally and closes /dev/watchdog, the system would still re-boot 59 seconds later. That may or may not be desirable behavior for the user.

我需要让我的守护意识到此设置,因为它会影响我如何处理SIGINT。如果设置为开,我的守护进程需要(preferably)开始退出有序地关闭或(至少)警告说,系统会在短期内重新启动用户。

I need to make my daemon aware of this setting because it will influence how I handle SIGINT. If the setting is on, my daemon would need to (preferably) start an orderly shutdown on exit or (at least) warn the user that the system is going to reboot shortly.

有谁知道,以获得从用户空间此设置的方法?我没有看到的sysconf()什么获得的价值。同样,我需要能够告诉如果软件看门狗被启用,开始使用。

Does anyone know of a method to obtain this setting from user space? I don't see anything in sysconf() to get the value. Likewise, I need to be able to tell if the software watchdog is enabled to begin with.

编辑:

Linux提供了一个非常简单的看门狗接口。一个进程可以打开/ dev /看门狗,一旦设备被打开时,内核将开始60秒倒计时重启,除非一些数据写入到该文件,在这种情况下,时钟重新设置。

Linux provides a very simple watchdog interface. A process can open /dev/watchdog , once the device is opened, the kernel will begin a 60 second count down to reboot unless some data is written to that file, in which case the clock re-sets.

根据内核的配置方式,关闭该文件可以或可以不停止倒计时。从文档:

Depending on how the kernel is configured, closing that file may or may not stop the countdown. From the documentation:

看门狗可以不停止
  使当设备重新启动
  为/ dev /看门狗正确关闭,
  除非你的内核编译
  该CONFIG_WATCHDOG_NOWAYOUT选项
  启用。

The watchdog can be stopped without causing a reboot if the device /dev/watchdog is closed correctly, unless your kernel is compiled with the CONFIG_WATCHDOG_NOWAYOUT option enabled.

我需要能够告诉如果CONFIG_WATCHDOG_NOWAYOUT是从用户空间中设置,这样我可以处理的关机说守护进程不同。换句话说,如果设置较高时,一个简单的:

I need to be able to tell if CONFIG_WATCHDOG_NOWAYOUT was set from within a user space daemon, so that I can handle the shutdown of said daemon differently. In other words, if that setting is high, a simple:

# /etc/init.d/mydaemon stop

...会重新启动59秒的系统,因为没有什么是不再写入到/ dev /看门狗。因此,如果将其设置高,我的信号情报处理程序需要做更多的事情(即警告在最少的用户)。

... would reboot the system in 59 seconds, because nothing is writing to /dev/watchdog any longer. So, if its set high, my handler for SIGINT needs to do additional things (i.e. warn the user at the least).

我找不到获得从用户空间此设置的一种方式:(任何帮助AP preciated。

I can not find a way of obtaining this setting from user space :( Any help is appreciated.

推荐答案

AHA!通过内核的的Linux / watchdog.h挖后司机/看门狗/ softdog.c ,我能够确定在加密狗的ioctl()接口的功能。看着它在宣布的能力结构watchdog_info

AHA! After digging through the kernel's linux/watchdog.h and drivers/watchdog/softdog.c, I was able to determine the capabilities of the softdog ioctl() interface. Looking at the capabilities that it announces in struct watchdog_info:

static struct watchdog_info ident = {
                .options =              WDIOF_SETTIMEOUT |
                                        WDIOF_KEEPALIVEPING |
                                        WDIOF_MAGICCLOSE,
                .firmware_version =     0,
                .identity =             "Software Watchdog",
        };

它的支持魔关闭(似乎)覆盖 CONFIG_WATCHDOG_NOWAYOUT 。所以,正常终止的时候,我有一个单一的字符'V'写入的/ dev /看门狗 ,然后关闭它,定时器将停止计数。

It does support a magic close that (seems to) override CONFIG_WATCHDOG_NOWAYOUT. So, when terminating normally, I have to write a single char 'V' to /dev/watchdog then close it, and the timer will stop counting.

一个简单的的ioctl()上的文件描述符的/ dev /看门狗要求 WDIOC_GETSUPPORT 允许一,以确定是否设置了这个标志。伪code:

A simple ioctl() on a file descriptor to /dev/watchdog asking WDIOC_GETSUPPORT allows one to determine if this flag is set. Pseudo code:

int fd;
struct watchdog_info info;

fd = open("/dev/watchdog", O_WRONLY);
if (fd == -1) {
   perror("open");
   // abort, timer did not start - no additional concerns
}

if (ioctl(fd, WDIOC_GETSUPPORT, &info)) {
    perror("ioctl");
    // abort, but you probably started the timer! See below.
}

if (WDIOF_MAGICCLOSE & info.options) {
   printf("Watchdog supports magic close char\n");
   // You have started the timer here! Handle that appropriately.
}

在与硬件看门狗工作时,您可能希望与 O_NONBLOCK 来开那么的ioctl()不是的open()块(因此检测忙卡)。

When working with hardware watchdogs, you might want to open with O_NONBLOCK so ioctl() not open() blocks (hence detecting a busy card).

如果 WDIOF_MAGICCLO​​SE 不支持,应该只是假设软看门狗配置了NOWAYOUT。请记住,只是的设备成功启动倒计时。如果你正在做的是探测,看看它是否支持魔术密切,确实如此,那么的魔法关闭的。否则,一定要处理的事实,你现在有一个正在运行的监督。

If WDIOF_MAGICCLOSE is not supported, one should just assume that the soft watchdog is configured with NOWAYOUT. Remember, just opening the device successfully starts the countdown. If all you're doing is probing to see if it supports magic close and it does, then magic close it. Otherwise, be sure to deal with the fact that you now have a running watchdog.

遗憾的是,知道肯定没有真正的方法没有实际启动它,至少不是我能找到。

Unfortunately, there's no real way to know for sure without actually starting it, at least not that I could find.

这篇关于Linux软件看门狗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆