blob: b196147d465a71db49ecc5bb8274212e637dd3d7 [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) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070031 InitKernelLogging(argv);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070032
Elliott Hughesda40c002015-03-27 23:20:44 -070033 int interval = 10;
34 if (argc >= 2) interval = atoi(argv[1]);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070035
Elliott Hughesda40c002015-03-27 23:20:44 -070036 int margin = 10;
37 if (argc >= 3) margin = atoi(argv[2]);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070038
Elliott Hughesf86b5a62016-06-24 15:12:21 -070039 LOG(INFO) << "watchdogd started (interval " << interval << ", margin " << margin << ")!";
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070040
Elliott Hughesda40c002015-03-27 23:20:44 -070041 int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
42 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070043 PLOG(ERROR) << "Failed to open " << DEV_NAME;
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070044 return 1;
45 }
46
Elliott Hughesda40c002015-03-27 23:20:44 -070047 int timeout = interval + margin;
48 int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070049 if (ret) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070050 PLOG(ERROR) << "Failed to set timeout to " << timeout;
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070051 ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
52 if (ret) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070053 PLOG(ERROR) << "Failed to get timeout";
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070054 } else {
Elliott Hughesda40c002015-03-27 23:20:44 -070055 if (timeout > margin) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070056 interval = timeout - margin;
Elliott Hughesda40c002015-03-27 23:20:44 -070057 } else {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070058 interval = 1;
Elliott Hughesda40c002015-03-27 23:20:44 -070059 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -070060 LOG(WARNING) << "Adjusted interval to timeout returned by driver: "
61 << "timeout " << timeout
62 << ", interval " << interval
63 << ", margin " << margin;
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070064 }
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}