Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

看门狗用户空间程序(可用来检测服务器死机)

$
0
0

提供一下源码供大家参考和学习,同时能够提问和交流:


#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#define WDT_DEVICE "/dev/watchdog"

#define WATCHDOG_IOCTL_BASE 'W'

#define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)
#define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)
#define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)

#define WDIOS_ENABLECARD 0x0002 /* Turn on the watchdog timer */
#define WDIOS_DISABLECARD 0x0001        /* Turn off the watchdog timer */

int Init()
{
          int wdt_fd;
    
          //open device file
     wdt_fd = open(WDT_DEVICE, O_RDWR);

     if(wdt_fd < 0){
         
              printf("Open WDT_DEVICE failed!\n");
         return -1;
     }
    
          return wdt_fd;
}

int main()
{
         int wdt_fd;
    int timeout;
    
    timeout = 300;//300s

    wdt_fd = Init();

    ioctl(wdt_fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);
    ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);//timeout为超时值

     while(1){
         
         //write(wdt, &arg, sizeof(arg));//若sizeof(arg)为0则不会喂狗
         ioctl(wdt_fd, WDIOC_KEEPALIVE,NULL);//第3个参数驱动不用

             sleep(300);

     }
    

     ioctl(wdt_fd, WDIOC_SETOPTIONS, WDIOS_DISABLECARD);

     close(wdt_fd);
    
     return 1;
        
}


作者:hqyhqyhq 发表于2013-5-17 11:10:53 原文链接
阅读:0 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>