blob: 881a4dfb57da1ccf33cfd0777a8831cbf6f92243 [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
Elliott Hughesda40c002015-03-27 23:20:44 -070030int watchdogd_main(int argc, char **argv) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070031 open_devnull_stdio();
32 klog_init();
Elliott Hughesda40c002015-03-27 23:20:44 -070033 klog_set_level(KLOG_NOTICE_LEVEL);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070034
Elliott Hughesda40c002015-03-27 23:20:44 -070035 int interval = 10;
36 if (argc >= 2) interval = atoi(argv[1]);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070037
Elliott Hughesda40c002015-03-27 23:20:44 -070038 int margin = 10;
39 if (argc >= 3) margin = atoi(argv[2]);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070040
Elliott Hughesda40c002015-03-27 23:20:44 -070041 NOTICE("watchdogd started (interval %d, margin %d)!\n", interval, margin);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070042
Elliott Hughesda40c002015-03-27 23:20:44 -070043 int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
44 if (fd == -1) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070045 ERROR("watchdogd: Failed to open %s: %s\n", DEV_NAME, strerror(errno));
46 return 1;
47 }
48
Elliott Hughesda40c002015-03-27 23:20:44 -070049 int timeout = interval + margin;
50 int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070051 if (ret) {
52 ERROR("watchdogd: Failed to set timeout to %d: %s\n", timeout, strerror(errno));
53 ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
54 if (ret) {
55 ERROR("watchdogd: Failed to get timeout: %s\n", strerror(errno));
56 } else {
Elliott Hughesda40c002015-03-27 23:20:44 -070057 if (timeout > margin) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070058 interval = timeout - margin;
Elliott Hughesda40c002015-03-27 23:20:44 -070059 } else {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070060 interval = 1;
Elliott Hughesda40c002015-03-27 23:20:44 -070061 }
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070062 ERROR("watchdogd: Adjusted interval to timeout returned by driver: timeout %d, interval %d, margin %d\n",
63 timeout, interval, margin);
64 }
65 }
66
Elliott Hughesda40c002015-03-27 23:20:44 -070067 while (true) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070068 write(fd, "", 1);
69 sleep(interval);
70 }
71}