blob: e03a2c31d775c78238f290b6f934f310d96379e4 [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>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070019#include <linux/watchdog.h>
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070020#include <stdlib.h>
21#include <string.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080022#include <unistd.h>
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070023
Tom Cherry3f5eaae52017-04-06 16:30:22 -070024#include <android-base/logging.h>
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070025
Tom Cherrye7656b72017-05-01 17:10:09 -070026#ifdef _INIT_INIT_H
27#error "Do not include init.h in files used by ueventd or watchdogd; it will expose init's globals"
28#endif
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070029
30#define DEV_NAME "/dev/watchdog"
31
Tom Cherry81f5d3e2017-06-22 12:53:17 -070032namespace android {
33namespace init {
34
Elliott Hughesda40c002015-03-27 23:20:44 -070035int watchdogd_main(int argc, char **argv) {
Tom Cherry74069d12018-07-20 15:26:25 -070036 android::base::InitLogging(argv, &android::base::KernelLogger);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070037
Elliott Hughesda40c002015-03-27 23:20:44 -070038 int interval = 10;
39 if (argc >= 2) interval = atoi(argv[1]);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070040
Elliott Hughesda40c002015-03-27 23:20:44 -070041 int margin = 10;
42 if (argc >= 3) margin = atoi(argv[2]);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070043
Elliott Hughesf86b5a62016-06-24 15:12:21 -070044 LOG(INFO) << "watchdogd started (interval " << interval << ", margin " << margin << ")!";
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070045
Elliott Hughesda40c002015-03-27 23:20:44 -070046 int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
47 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070048 PLOG(ERROR) << "Failed to open " << DEV_NAME;
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070049 return 1;
50 }
51
Elliott Hughesda40c002015-03-27 23:20:44 -070052 int timeout = interval + margin;
53 int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070054 if (ret) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070055 PLOG(ERROR) << "Failed to set timeout to " << timeout;
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070056 ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
57 if (ret) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070058 PLOG(ERROR) << "Failed to get timeout";
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070059 } else {
Elliott Hughesda40c002015-03-27 23:20:44 -070060 if (timeout > margin) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070061 interval = timeout - margin;
Elliott Hughesda40c002015-03-27 23:20:44 -070062 } else {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070063 interval = 1;
Elliott Hughesda40c002015-03-27 23:20:44 -070064 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -070065 LOG(WARNING) << "Adjusted interval to timeout returned by driver: "
66 << "timeout " << timeout
67 << ", interval " << interval
68 << ", margin " << margin;
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070069 }
70 }
71
Elliott Hughesda40c002015-03-27 23:20:44 -070072 while (true) {
Arve Hjønnevågd97d9072012-06-13 21:51:56 -070073 write(fd, "", 1);
74 sleep(interval);
75 }
76}
Tom Cherry81f5d3e2017-06-22 12:53:17 -070077
78} // namespace init
79} // namespace android