在嵌入式系統中,為了防止主應用程序因為不明的原因無故死掉或者程序跑飛,需要加入一個看門狗程序保證系統能夠重啟(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。