Mercurial > vordog
comparison vd_watchdog/vd_watchdog.c @ 1:0b9854adb86c
Remove tedious messages and daemonize vd_watchdog.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 08 Jul 2008 11:07:31 +0800 |
parents | 71475f5afa92 |
children | 89e9d591748d |
comparison
equal
deleted
inserted
replaced
0:71475f5afa92 | 1:0b9854adb86c |
---|---|
1 #include <stdio.h> | 1 #include <stdio.h> |
2 #include <stdlib.h> | 2 #include <stdlib.h> |
3 #include <unistd.h> | 3 #include <unistd.h> |
4 #include <fcntl.h> | 4 #include <fcntl.h> |
5 #include <sys/ioctl.h> | 5 #include <sys/ioctl.h> |
6 #include <sys/types.h> | |
6 #include "vordog.h" | 7 #include "vordog.h" |
7 | 8 |
8 #define DEVNAME "/dev/vordog0" | 9 #define DEVNAME "/dev/vordog0" |
9 #define PROG "vd_watchdog" | 10 #define PROG "vd_watchdog" |
10 | 11 |
12 static int unit_factors[] = {0, 1, 60, 3600}; | |
13 | |
11 static void | 14 static void |
12 watchdog(int min) { | 15 watchdog(int timeout, int unit) { |
13 struct vordog_cfg cfg; | 16 struct vordog_cfg cfg; |
14 int intval; | 17 int intval; |
15 int r; | 18 int r; |
16 int fd; | 19 int fd; |
17 | 20 |
18 fd = open(DEVNAME, O_RDWR); | 21 fd = open(DEVNAME, O_RDWR); |
19 if(fd == -1) { | 22 if(fd == -1) { |
20 perror(PROG); | 23 perror(PROG); |
21 exit(1); | 24 exit(1); |
22 } | 25 } |
23 cfg.init_val = min; | 26 |
24 cfg.unit = VDT_1M; | 27 cfg.init_val = timeout; |
28 cfg.unit = unit; | |
25 r = ioctl(fd, VDCTL_ENABLE, &cfg); | 29 r = ioctl(fd, VDCTL_ENABLE, &cfg); |
26 if(r == -1) { | 30 if(r == -1) { |
27 perror(PROG); | 31 perror(PROG); |
28 exit(1); | 32 exit(1); |
29 } | 33 } |
30 | 34 |
31 intval = min * 60 * 3 / 4; | 35 /* reset timer periodically */ |
36 intval = timeout * unit_factors[unit]; | |
32 while(1) { | 37 while(1) { |
33 sleep(intval); | 38 sleep(intval); |
34 r = ioctl(fd, VDCTL_RESET); | 39 r = ioctl(fd, VDCTL_RESET); |
35 if(r == -1) { | 40 if(r == -1) { |
36 perror(PROG); | 41 perror(PROG); |
37 exit(1); | 42 exit(1); |
38 } | 43 } |
39 printf("reset\n"); | |
40 } | 44 } |
41 } | 45 } |
42 | 46 |
47 static void | |
48 daemonize(void) { | |
49 pid_t pid; | |
50 | |
51 pid = fork(); | |
52 if(pid == -1) { | |
53 perror("fork"); | |
54 exit(1); | |
55 } | |
56 | |
57 if(pid != 0) | |
58 exit(0); /* parent */ | |
59 | |
60 /* child process */ | |
61 pid = setsid(); | |
62 if(pid == -1) { | |
63 perror("setsid"); | |
64 exit(1); | |
65 } | |
66 | |
67 close(STDOUT_FILENO); | |
68 close(STDOUT_FILENO); | |
69 /* close(STDERR_FILENO); */ | |
70 } | |
71 | |
72 static void | |
73 usage(const char *prog) { | |
74 fprintf(stderr, "Usage: %s [-msMH] <timeout>\n"); | |
75 fprintf(stderr, "\t-s\tcount in seconds.\n"); | |
76 fprintf(stderr, "\t-M\tcount in minutes.\n"); | |
77 fprintf(stderr, "\t-m\tcount in hours.\n"); | |
78 fprintf(stderr, | |
79 "\t<timeout> is interval between periodically reset of\n" | |
80 "\t\twatchdog timer, or the system would be reseted without\n" | |
81 "\t\treseting timer for 4 times. (default: 30s)\n" | |
82 "\t\t(note: 1~255)\n"); | |
83 } | |
84 | |
85 | |
86 static char *unit_names[] = { | |
87 "* 4ms", | |
88 "seconds", | |
89 "minutes", | |
90 "hours" | |
91 }; | |
43 | 92 |
44 int | 93 int |
45 main(int argc, const char *argv[]) { | 94 main(int argc, char * const argv[]) { |
46 watchdog(1); | 95 int timeout, unit; |
96 int ch; | |
97 const char *prog = argv[0]; | |
98 | |
99 timeout = 30; /* default: 30s */ | |
100 unit = VDT_1S; | |
101 | |
102 while((ch = getopt(argc, argv, "")) != -1) { | |
103 switch(ch) { | |
104 case 's': | |
105 unit = VDT_1S; | |
106 break; | |
107 case 'M': | |
108 unit = VDT_1M; | |
109 break; | |
110 case 'H': | |
111 unit = VDT_1H; | |
112 break; | |
113 case '?': | |
114 default: | |
115 usage(prog); | |
116 exit(1); | |
117 } | |
118 } | |
119 argc -= optind; | |
120 argv += optind; | |
121 | |
122 if(argc != 1 && argc != 0) { | |
123 usage(prog); | |
124 return 1; | |
125 } | |
126 | |
127 if(argc == 1) | |
128 timeout = atoi(argv[0]); | |
129 if(timeout < 1 || timeout > 255) { | |
130 usage(prog); | |
131 return 1; | |
132 } | |
133 | |
134 printf("Reset watchdog timer every %d %s\n", | |
135 timeout, unit_names[unit]); | |
136 | |
137 daemonize(); | |
138 watchdog(timeout, unit); | |
47 return 0; | 139 return 0; |
48 } | 140 } |