Linux 软件看门狗 watchdog

2830阅读 0评论2014-08-12 习_霜
分类:C/C++

文章内容摘自:
http://blog.csdn.net/rstevens/article/details/1853779
http://blog.csdn.net/liigo/article/details/9227205
Linux 自带了一个 watchdog 的实现,用于监视系统的运行,包括一个内核 watchdog module 和一个用户空间的 watchdog 程序。内核 watchdog 模块通过 /dev/watchdog 这个字符设备与用户空间通信。用户空间程序一旦打开 /dev/watchdog 设备(俗称“开门放狗”),就会导致在内核中启动一个1分钟的定时器(系统默认时间),此后,用户空间程序需要保证在1分钟之内向这个设备写入数据(俗称“定期喂狗”),每次写操作会导致重新设定定时器。如果用户空间程序在1分钟之内没有写操作,定时器到期会导致一次系统 reboot 操作(“狗咬人了”呵呵)。通过这种机制,我们可以保证系统核心进程大部分时间都处于运行状态,即使特定情形下进程崩溃,因无法正常定时“喂狗”,Linux系统在看门狗作用下重新启动(reboot),核心进程又运行起来了。多用于嵌入式系统。

用户空间程序可通过关闭 /dev/watchdog 来停止内核中的定时器。

用户空间的 watchdog 守护进程:

在用户空间,还有一个叫做 watchdog 的守护进程,它可以定期对系统进行检测,包括:

如果某项检测失败,则可能导致一次 soft reboot (模拟一次 shutdown 命令的执行)

它还可以通过 /dev/watchdog 来触发内核 watchdog 的运行。

附一份watchdog控制代码:

点击(此处)折叠或打开

  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. #include <linux/watchdog.h>

  6. #include "log.h"
  7. #include "util.h"

  8. #define DEV_NAME "/dev/watchdog"

  9. int watchdogd_main(int argc, char **argv)
  10. {
  11.     int fd;
  12.     int ret;
  13.     int interval = 10;
  14.     int margin = 10;
  15.     int timeout;

  16.     open_devnull_stdio();
  17.     klog_init();

  18.     INFO("Starting watchdogd\n");

  19.     if (argc >= 2)
  20.         interval = atoi(argv[1]);

  21.     if (argc >= 3)
  22.         margin = atoi(argv[2]);

  23.     timeout = interval + margin;

  24.     fd = open(DEV_NAME, O_RDWR);
  25.     if (fd < 0) {
  26.         ERROR("watchdogd: Failed to open %s: %s\n", DEV_NAME, strerror(errno));
  27.         return 1;
  28.     }

  29.     ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
  30.     if (ret) {
  31.         ERROR("watchdogd: Failed to set timeout to %d: %s\n", timeout, strerror(errno));
  32.         ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
  33.  if (ret) {
  34.         ERROR("watchdogd: Failed to set timeout to %d: %s\n", timeout, strerror(errno));
  35.         ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
  36.         if (ret) {
  37.             ERROR("watchdogd: Failed to get timeout: %s\n", strerror(errno));
  38.         } else {
  39.             if (timeout > margin)
  40.                 interval = timeout - margin;
  41.             else
  42.                 interval = 1;
  43.             ERROR("watchdogd: Adjusted interval to timeout returned by driver: timeout %d, interval %d, margin %d\n",
  44.                   timeout, interval, margin);
  45.         }
  46.     }

  47.     while(1) {
  48.         write(fd, "", 1);
  49.         sleep(interval);
  50.     }
  51. }




上一篇:printf打印不显示或者出错解决
下一篇:Android recovery更新简单流程及注意点