添加看门狗程序

在嵌入式系统中,为了防止主应用程序因为不明的原因无故死掉或者程序跑飞,需要加入一个看门狗程序保证系统能够重启(reboot)。

 

  1 //Linux watch dog test
  2 
  3 
  4 //http://www.cnblogs.com/zengjfgit/p/5396041.html
  5 
  6 /***********************************************************************
  7  *                  Linux Watchdog Test Program
  8  * 说明:
  9  *     由于之前的reset一直没有得到解决,所以这个Watchdog功能一直没有处理,
 10  * 现在问题解决了,于是需要加入这个测试程序。
 11  *
 12  **********************************************************************/
 13 
 14 #include <stdio.h>
 15 #include <stdlib.h>
 16 #include <string.h>
 17 #include <unistd.h>
 18 #include <fcntl.h>
 19 #include <sys/ioctl.h>
 20 #include <linux/types.h>
 21 #include <linux/watchdog.h>
 22 
 23 #define  ZERO 0
 24 #define  SUCCESS 0
 25 #define  FAIL   1
 26 
 27 #define     DDBUG                                 1
 28 #undef      DDBUG
 29 #if DDBUG
 30     #define DEBUG_PRINT(fmt, args...) fprintf(stdout, fmt, ##args)
 31 #else
 32     #define DEBUG_PRINT(fmt, args...)
 33 #endif
 34 
 35 
 36 // watchdog 只要一直打开设备节点不喂,然后等待设定的时间结束引发reset。
 37 int main(void)
 38 {
 39 
 40     int fd_watchdog;
 41     fd_watchdog = open("/dev/watchdog", O_WRONLY); //open device
 42     if (fd_watchdog == -1) {
 43         fprintf(stderr, "Watchdog device not enabled.\n");
 44         fflush(stderr);
 45         exit(-1);
 46     }
 47 
 48     int timeout = 60;  //60seconds
 49     const int wdt_enable = WDIOS_ENABLECARD;
 50     ioctl(fd_watchdog, WDIOC_SETOPTIONS, (void*)&wdt_enable);  //open watchdog   
 51     ioctl(fd_watchdog, WDIOC_SETTIMEOUT, &timeout);
 52     DEBUG_PRINT("The timeout was set to %d seconds\n", timeout);
 53     
 54 
 55 
 56     while(1){
 57         
 58         {
 59             ioctl(fd_watchdog, WDIOC_GETTIMEOUT, &timeout);
 60             printf("The timeout was is %d seconds.\n", timeout);
 61         }
 62         
 63         int timeleft = timeout - 10;
 64         //while((timeleft--) >= 0) {
 65         //    //DEBUG_PRINT("The timeout left %d seconds\n", timeleft);
 66         //    sleep(1);
 67         //}
 68         
 69         mysleep(timeleft);
 70         
 71         if( SUCCESS == checkProcessExist("miniTerminal")) // Only when miniTerminal is running, will we feed the watchdog
 72         {     
 73             ////feed the watchdog
 74             if(fd_watchdog >= 0) {
 75                 feeddog(fd_watchdog);
 76             }
 77         }
 78         
 79         sleep(10);
 80     }
 81 }
 82 
 83 void mysleep(int timelength){
 84     while((timelength--) >= 0) {
 85         //DEBUG_PRINT("The timeout left %d seconds\n", timeleft);
 86         sleep(1);
 87     }
 88     
 89     return;
 90 }
 91 
 92 
 93 void feeddog(int fd_watchdog)
 94 {
 95     //ioctl(fd_watchdog, WDIOC_KEEPALIVE, 0);
 96     static unsigned char food = 0;
 97     ssize_t eaten = write(fd_watchdog, &food, 1);
 98     if(eaten != 1) {
 99         DEBUG_PRINT("\n!!! FAILED feeding watchdog\n");
100         //syslog(LOG_WARNING, "FAILED feeding watchdog");
101     }  
102 
103      fsync(fd_watchdog);  // if you don't fsync fd_watchdog, then the write won't function normally.
104      return;
105 }
106 
107 int checkProcessExist(char *name)
108 {
109     //char *Name = "miniTerminal";
110     FILE *pstr;
111     char cmd[100];
112     char number[10];
113     
114     int ret = SUCCESS;
115     
116     memset(cmd, 0 , sizeof(cmd));
117     sprintf(cmd,"ps | grep %s | grep -v grep | wc -l",name);
118     pstr=popen(cmd,"r");
119 
120     fgets(number,9, pstr);
121     DEBUG_PRINT("number is %d\n",atoi(number));
122     
123     if( ZERO == atoi(number))
124     {
125         DEBUG_PRINT("process - %s not exist!\n",name);
126         ret = FAIL;
127     }
128     else
129     {
130         DEBUG_PRINT("process - %s exist!\n",name);
131         ret = SUCCESS;
132     }
133     
134     pclose(pstr);
135     return ret;
136     
137 }

 

设置看门狗的超时时间为80秒,如果在80秒以内检测到程序存在,则喂狗;否则,不喂狗,等待系统reboot。测试,OK。

posted @ 2016-07-21 09:25  xiulug  阅读(2545)  评论(0编辑  收藏  举报