blob: 0790811299ce6a38aba7eeb220cd93ea49617224 [file] [log] [blame]
Arve Hjønnevågd97d9072012-06-13 21:51:56 -07001/*
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>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080021#include <unistd.h>
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070022
23#include <linux/watchdog.h>
24
25#include "log.h"
26#include "util.h"
27
28#define DEV_NAME "/dev/watchdog"
29
30int watchdogd_main(int argc, char **argv)
31{
32 int fd;
33 int ret;
34 int interval = 10;
35 int margin = 10;
36 int timeout;
37
38 open_devnull_stdio();
39 klog_init();
40
41 INFO("Starting watchdogd\n");
42
43 if (argc >= 2)
44 interval = atoi(argv[1]);
45
46 if (argc >= 3)
47 margin = atoi(argv[2]);
48
49 timeout = interval + margin;
50
Nick Kralevich45a884f2015-02-02 14:37:22 -080051 fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070052 if (fd < 0) {
53 ERROR("watchdogd: Failed to open %s: %s\n", DEV_NAME, strerror(errno));
54 return 1;
55 }
56
57 ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
58 if (ret) {
59 ERROR("watchdogd: Failed to set timeout to %d: %s\n", timeout, strerror(errno));
60 ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
61 if (ret) {
62 ERROR("watchdogd: Failed to get timeout: %s\n", strerror(errno));
63 } else {
64 if (timeout > margin)
65 interval = timeout - margin;
66 else
67 interval = 1;
68 ERROR("watchdogd: Adjusted interval to timeout returned by driver: timeout %d, interval %d, margin %d\n",
69 timeout, interval, margin);
70 }
71 }
72
73 while(1) {
74 write(fd, "", 1);
75 sleep(interval);
76 }
77}
78