从用户空间,我如何判断Linux的软件看门狗是否配置成没有出路? [英] From userspace, how can I tell if Linux's soft watchdog is configured with no way out?

查看:290
本文介绍了从用户空间,我如何判断Linux的软件看门狗是否配置成没有出路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Linux编写系统监视器,并希望包含一些看门狗功能.在内核中,即使关闭/dev/watchdog,也可以将watchdog配置为继续运行.换句话说,如果守护程序正常退出并关闭/dev/watchdog,则系统仍会在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的方式.如果启用此设置,则我的守护程序将需要(最好)在退出时启动有序关闭,或者(至少)警告用户系统即将重新启动.

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/watchdog,一旦打开设备,内核将开始倒数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/watchdog已正确关闭, 除非你的内核是用 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/watchdog.因此,如果将其设置得很高,我的SIGINT处理程序需要执行其他操作(即,至少警告用户).

... 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).

我找不到从用户空间获取此设置的方法:(感谢您的帮助.

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

推荐答案

AHA!深入研究内核的linux/watchdog.hdrivers/watchdog/softdog.c之后,我能够确定softdog ioctl()接口的功能.查看它在struct 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).因此,当正常终止时,我必须在/dev/watchdog 然后中将其写入一个字符'V',然后将其关闭,计时器将停止计数.

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.

在文件描述符上向/dev/watchdog询问WDIOC_GETSUPPORT的简单ioctl()允许确定该标志是否被设置.伪代码:

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_MAGICCLOSE,则应假定软看门狗配置了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天全站免登陆