1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <linux/watchdog.h>
24
25 #include "log.h"
26 #include "util.h"
27
28 #define DEV_NAME "/dev/watchdog"
29
watchdogd_main(int argc,char ** argv)30 int watchdogd_main(int argc, char **argv) {
31 InitKernelLogging(argv);
32
33 int interval = 10;
34 if (argc >= 2) interval = atoi(argv[1]);
35
36 int margin = 10;
37 if (argc >= 3) margin = atoi(argv[2]);
38
39 LOG(INFO) << "watchdogd started (interval " << interval << ", margin " << margin << ")!";
40
41 int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
42 if (fd == -1) {
43 PLOG(ERROR) << "Failed to open " << DEV_NAME;
44 return 1;
45 }
46
47 int timeout = interval + margin;
48 int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
49 if (ret) {
50 PLOG(ERROR) << "Failed to set timeout to " << timeout;
51 ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
52 if (ret) {
53 PLOG(ERROR) << "Failed to get timeout";
54 } else {
55 if (timeout > margin) {
56 interval = timeout - margin;
57 } else {
58 interval = 1;
59 }
60 LOG(WARNING) << "Adjusted interval to timeout returned by driver: "
61 << "timeout " << timeout
62 << ", interval " << interval
63 << ", margin " << margin;
64 }
65 }
66
67 while (true) {
68 write(fd, "", 1);
69 sleep(interval);
70 }
71 }
72