-
Notifications
You must be signed in to change notification settings - Fork 0
/
reboot.c
executable file
·134 lines (104 loc) · 2.69 KB
/
reboot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <stdint.h>
#include <time.h>
#include <sys/time.h>
#include <linux/watchdog.h>
#include "reboot.h"
#ifdef __cplusplus
extern "C" {
#endif //__cplusplus
static unsigned long _get_file_size(const char *filename)
{
struct stat buf;
if(stat(filename, &buf)<0)
{
return 0;
}
return (unsigned long)buf.st_size;
}
void FLASH_AddOneResetLog(char *line)
{
FILE *fp=NULL;
unsigned long fSize;
fSize=_get_file_size(IPC_RESET_LOG);
if ((fSize > IPC_RESET_LOG_SIZE) ||(fSize==0)) //too large or no logfile exist, create it
fp = fopen(IPC_RESET_LOG, "w+"); //reset to zero
else
fp = fopen(IPC_RESET_LOG, "r+"); //add log to reset.log
if (fp)
{
fseek(fp,0,2); //move to the end
fputs(line,fp); //write a line to file
fflush(fp); //only block device has buffer(cache) requiring flush operation.
fclose(fp);
}
return ;
}
int WdtReboot(int ResetCode)
{
int ret=0;
char line[128]={0};
time_t timep=0;
struct tm *pLocalTime;
int fd=-1;
unsigned int ulTemp=0;
// int Temp=0;
time(&timep); //get current time, how many senconds
pLocalTime=localtime(&timep); //convert to date/time format
sprintf(line,"%s(%d) is called at %4d-%02d-%02d %02d:%02d:%02d. \n",(char*)__FUNCTION__,ResetCode
,pLocalTime->tm_year+1900,pLocalTime->tm_mon+1,pLocalTime->tm_mday,pLocalTime->tm_hour,pLocalTime->tm_min,pLocalTime->tm_sec);
FLASH_AddOneResetLog(line);
usleep(200000); //waiting file operation done.
fd = open("/dev/watchdog", O_RDWR); //try one
if (fd<0)
fd=open("/dev/wdt", O_RDWR); //try another one if fail
if (fd<0)
{
//printf("watchdog device not found! Trying busybox reboot... \n");
//system("rm /sbin/reboot;ln -s /bin/busybox /sbin/reboot;/sbin/reboot");
system("ln -s /bin/busybox /tmp/reboot;/tmp/reboot");
sleep(1);
system("reboot");
}
else
{
ulTemp=1; //let wdt timeout at once
ret=ioctl(fd, WDIOC_SETTIMEOUT, &ulTemp);
close(fd);
sleep(1);
system("reboot");
for (;;); //dead loop to waiting wdt reset
}
return ret;
}
#ifdef __cplusplus
}
#endif //__cplusplus
#ifdef SMAIN
//usage: reboot 51
int main(int argc,char *argv[])
{
int ret=0;
if (argc==1)
{
printf("usage: reboot reset_code[1:999] NOTE:0 is reserved!This is wdt reset!\n");
usleep(200000); //wait 200ms
ret=WdtReboot(0);
}
else
{
printf("reboot %s\n",argv[1]);
ret=WdtReboot(atoi(argv[1]));
}
return ret;
}
#endif //#ifdef SMAIN