blob: 333e023d67587bcd4bf4272f94219d5989447778 [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 Lin0dd653e2024-06-17 14:13:12 +080022#include <log/log.h>
Woody Lin70a0dbc2022-07-11 18:39:11 +080023
Woody Lin70a0dbc2022-07-11 18:39:11 +080024#include <fcntl.h>
25#include <glob.h>
26#include <linux/watchdog.h>
27#include <stdlib.h>
28#include <string.h>
Woody Lin0dd653e2024-06-17 14:13:12 +080029#include <sys/cdefs.h>
Woody Lin70a0dbc2022-07-11 18:39:11 +080030#include <unistd.h>
31
Woody Lin0dd653e2024-06-17 14:13:12 +080032#include <cstdlib>
Woody Linde061722024-01-15 16:01:32 +080033#include <vector>
34
Woody Lin0dd653e2024-06-17 14:13:12 +080035#define NSEC_PER_SEC (1000LL * 1000LL * 1000LL)
36
Woody Linde061722024-01-15 16:01:32 +080037#define DEV_GLOB "/sys/devices/platform/*.watchdog_cl*/watchdog/watchdog*"
38
Woody Lin70a0dbc2022-07-11 18:39:11 +080039using android::base::Basename;
40using android::base::StringPrintf;
41
Woody Lin0dd653e2024-06-17 14:13:12 +080042int main(int __unused argc, char** argv) {
43 auto min_timeout_nsecs = std::numeric_limits<typeof(NSEC_PER_SEC)>::max();
44
Woody Lin70a0dbc2022-07-11 18:39:11 +080045 android::base::InitLogging(argv, &android::base::KernelLogger);
46
Woody Lin70a0dbc2022-07-11 18:39:11 +080047 glob_t globbuf;
48 int ret = glob(DEV_GLOB, GLOB_MARK, nullptr, &globbuf);
49 if (ret) {
50 PLOG(ERROR) << "Failed to lookup glob " << DEV_GLOB << ": " << ret;
51 return 1;
52 }
53
Woody Linde061722024-01-15 16:01:32 +080054 std::vector<android::base::unique_fd> wdt_dev_fds;
Woody Lin70a0dbc2022-07-11 18:39:11 +080055
Woody Linde061722024-01-15 16:01:32 +080056 for (size_t i = 0; i < globbuf.gl_pathc; i++) {
Woody Lin0dd653e2024-06-17 14:13:12 +080057 int timeout_secs;
Woody Linde061722024-01-15 16:01:32 +080058 std::string dev_path = StringPrintf("/dev/%s", Basename(globbuf.gl_pathv[i]).c_str());
Woody Lin70a0dbc2022-07-11 18:39:11 +080059
Woody Linde061722024-01-15 16:01:32 +080060 int fd = TEMP_FAILURE_RETRY(open(dev_path.c_str(), O_RDWR | O_CLOEXEC));
61 if (fd == -1) {
62 PLOG(ERROR) << "Failed to open " << dev_path;
63 return 1;
64 }
Woody Lin70a0dbc2022-07-11 18:39:11 +080065
Woody Lin0dd653e2024-06-17 14:13:12 +080066 ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout_secs);
Woody Lin70a0dbc2022-07-11 18:39:11 +080067 if (ret) {
Woody Lin0dd653e2024-06-17 14:13:12 +080068 PLOG(ERROR) << "Failed to get timeout on " << dev_path;
69 continue;
70 } else {
71 min_timeout_nsecs = std::min(min_timeout_nsecs, NSEC_PER_SEC * timeout_secs);
Woody Lin70a0dbc2022-07-11 18:39:11 +080072 }
Woody Lin0dd653e2024-06-17 14:13:12 +080073
74 wdt_dev_fds.emplace_back(fd);
Woody Lin70a0dbc2022-07-11 18:39:11 +080075 }
76
Woody Linde061722024-01-15 16:01:32 +080077 globfree(&globbuf);
78
Woody Lin0dd653e2024-06-17 14:13:12 +080079 if (wdt_dev_fds.empty()) {
80 LOG(ERROR) << "no valid wdt dev found";
81 return 1;
82 }
83
84 timespec ts;
85 auto result = div(min_timeout_nsecs / 2, NSEC_PER_SEC);
86 ts.tv_sec = result.quot;
87 ts.tv_nsec = result.rem;
88
Woody Lin70a0dbc2022-07-11 18:39:11 +080089 while (true) {
Woody Lin0dd653e2024-06-17 14:13:12 +080090 timespec rem = ts;
91
Woody Linde061722024-01-15 16:01:32 +080092 for (const auto& fd : wdt_dev_fds) {
93 TEMP_FAILURE_RETRY(write(fd, "", 1));
94 }
Woody Lin0dd653e2024-06-17 14:13:12 +080095
96 if (TEMP_FAILURE_RETRY(nanosleep(&rem, &rem))) {
97 PLOG(ERROR) << "nanosleep failed";
98 return 1;
99 }
Woody Lin70a0dbc2022-07-11 18:39:11 +0800100 }
101}