blob: 0d16db9077a8541fc77462adef2dc8ce0131279b [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
Mark Salyzyn636b1eb2015-06-26 09:10:13 -070041 NOTICE("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) {
Mark Salyzyn636b1eb2015-06-26 09:10:13 -070045 ERROR("Failed to open %s: %s\n", DEV_NAME, strerror(errno));
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070046 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) {
Mark Salyzyn636b1eb2015-06-26 09:10:13 -070052 ERROR("Failed to set timeout to %d: %s\n", timeout, strerror(errno));
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070053 ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
54 if (ret) {
Mark Salyzyn636b1eb2015-06-26 09:10:13 -070055 ERROR("Failed to get timeout: %s\n", strerror(errno));
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070056 } 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 }
Mark Salyzyn636b1eb2015-06-26 09:10:13 -070062 WARNING("Adjusted interval to timeout returned by driver:"
63 " timeout %d, interval %d, margin %d\n",
64 timeout, interval, margin);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070065 }
66 }
67
Elliott Hughesda40c002015-03-27 23:20:44 -070068 while (true) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070069 write(fd, "", 1);
70 sleep(interval);
71 }
72}