blob: 82e01d02a9c0914842c74985d5ebbb1176fbba2d [file] [log] [blame]
Woody Lin70a0dbc2022-07-11 18:39:11 +08001/*
2 * Copyright (C) 2022 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
Woody Linde061722024-01-15 16:01:32 +080017#include <android-base/chrono_utils.h>
Woody Lin70a0dbc2022-07-11 18:39:11 +080018#include <android-base/file.h>
19#include <android-base/logging.h>
20#include <android-base/stringprintf.h>
Woody Linde061722024-01-15 16:01:32 +080021#include <android-base/unique_fd.h>
Woody Lin70a0dbc2022-07-11 18:39:11 +080022
23#include <errno.h>
24#include <fcntl.h>
25#include <glob.h>
26#include <linux/watchdog.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
Woody Linde061722024-01-15 16:01:32 +080031#include <chrono>
32#include <vector>
33
34#define DEV_GLOB "/sys/devices/platform/*.watchdog_cl*/watchdog/watchdog*"
35
36#define DEFAULT_INTERVAL 10s
37#define DEFAULT_MARGIN 10s
Woody Lin70a0dbc2022-07-11 18:39:11 +080038
39using android::base::Basename;
40using android::base::StringPrintf;
Woody Linde061722024-01-15 16:01:32 +080041using std::literals::chrono_literals::operator""s;
Woody Lin70a0dbc2022-07-11 18:39:11 +080042
43int main(int argc, char** argv) {
44 android::base::InitLogging(argv, &android::base::KernelLogger);
45
Woody Linde061722024-01-15 16:01:32 +080046 std::chrono::seconds interval = argc >= 2
47 ? std::chrono::seconds(atoi(argv[1])) : DEFAULT_INTERVAL;
48 std::chrono::seconds margin = argc >= 3
49 ? std::chrono::seconds(atoi(argv[2])) : DEFAULT_MARGIN;
Woody Lin70a0dbc2022-07-11 18:39:11 +080050
Woody Linde061722024-01-15 16:01:32 +080051 LOG(INFO) << "gs_watchdogd started (interval " << interval.count()
52 << ", margin " << margin.count() << ")!";
Woody Lin70a0dbc2022-07-11 18:39:11 +080053
54 glob_t globbuf;
55 int ret = glob(DEV_GLOB, GLOB_MARK, nullptr, &globbuf);
56 if (ret) {
57 PLOG(ERROR) << "Failed to lookup glob " << DEV_GLOB << ": " << ret;
58 return 1;
59 }
60
Woody Linde061722024-01-15 16:01:32 +080061 std::vector<android::base::unique_fd> wdt_dev_fds;
Woody Lin70a0dbc2022-07-11 18:39:11 +080062
Woody Linde061722024-01-15 16:01:32 +080063 for (size_t i = 0; i < globbuf.gl_pathc; i++) {
64 std::chrono::seconds timeout = interval + margin;
65 int timeout_secs = timeout.count();
66 std::string dev_path = StringPrintf("/dev/%s", Basename(globbuf.gl_pathv[i]).c_str());
Woody Lin70a0dbc2022-07-11 18:39:11 +080067
Woody Linde061722024-01-15 16:01:32 +080068 int fd = TEMP_FAILURE_RETRY(open(dev_path.c_str(), O_RDWR | O_CLOEXEC));
69 if (fd == -1) {
70 PLOG(ERROR) << "Failed to open " << dev_path;
71 return 1;
72 }
Woody Lin70a0dbc2022-07-11 18:39:11 +080073
Woody Linde061722024-01-15 16:01:32 +080074 wdt_dev_fds.emplace_back(fd);
75 ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout_secs);
Woody Lin70a0dbc2022-07-11 18:39:11 +080076 if (ret) {
Woody Linde061722024-01-15 16:01:32 +080077 PLOG(ERROR) << "Failed to set timeout to " << timeout_secs;
78 ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout_secs);
79 if (ret) {
80 PLOG(ERROR) << "Failed to get timeout";
Woody Lin70a0dbc2022-07-11 18:39:11 +080081 } else {
Woody Linde061722024-01-15 16:01:32 +080082 interval = timeout > margin ? timeout - margin : 1s;
83 LOG(WARNING) << "Adjusted interval to timeout returned by driver: "
84 << "timeout " << timeout_secs
85 << ", interval " << interval.count()
86 << ", margin " << margin.count();
Woody Lin70a0dbc2022-07-11 18:39:11 +080087 }
Woody Lin70a0dbc2022-07-11 18:39:11 +080088 }
89 }
90
Woody Linde061722024-01-15 16:01:32 +080091 globfree(&globbuf);
92
Woody Lin70a0dbc2022-07-11 18:39:11 +080093 while (true) {
Woody Linde061722024-01-15 16:01:32 +080094 for (const auto& fd : wdt_dev_fds) {
95 TEMP_FAILURE_RETRY(write(fd, "", 1));
96 }
97 sleep(interval.count());
Woody Lin70a0dbc2022-07-11 18:39:11 +080098 }
99}