Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 1 | /* |
| 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 Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 17 | #include <android-base/chrono_utils.h> |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 18 | #include <android-base/file.h> |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/stringprintf.h> |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 21 | #include <android-base/unique_fd.h> |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 22 | #include <log/log.h> |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 23 | |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 24 | #include <fcntl.h> |
| 25 | #include <glob.h> |
| 26 | #include <linux/watchdog.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 29 | #include <sys/cdefs.h> |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 30 | #include <unistd.h> |
| 31 | |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 32 | #include <cstdlib> |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 35 | #define NSEC_PER_SEC (1000LL * 1000LL * 1000LL) |
| 36 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 37 | #define DEV_GLOB "/sys/devices/platform/*.watchdog_cl*/watchdog/watchdog*" |
| 38 | |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 39 | using android::base::Basename; |
| 40 | using android::base::StringPrintf; |
| 41 | |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 42 | int main(int __unused argc, char** argv) { |
| 43 | auto min_timeout_nsecs = std::numeric_limits<typeof(NSEC_PER_SEC)>::max(); |
| 44 | |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 45 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 46 | |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 47 | 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 Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 54 | std::vector<android::base::unique_fd> wdt_dev_fds; |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 55 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 56 | for (size_t i = 0; i < globbuf.gl_pathc; i++) { |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 57 | int timeout_secs; |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 58 | std::string dev_path = StringPrintf("/dev/%s", Basename(globbuf.gl_pathv[i]).c_str()); |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 59 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 60 | 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 Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 65 | |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 66 | ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout_secs); |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 67 | if (ret) { |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 68 | 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 Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 72 | } |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 73 | |
| 74 | wdt_dev_fds.emplace_back(fd); |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 75 | } |
| 76 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 77 | globfree(&globbuf); |
| 78 | |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 79 | 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 Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 89 | while (true) { |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 90 | timespec rem = ts; |
| 91 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame] | 92 | for (const auto& fd : wdt_dev_fds) { |
| 93 | TEMP_FAILURE_RETRY(write(fd, "", 1)); |
| 94 | } |
Woody Lin | 0dd653e | 2024-06-17 14:13:12 +0800 | [diff] [blame] | 95 | |
| 96 | if (TEMP_FAILURE_RETRY(nanosleep(&rem, &rem))) { |
| 97 | PLOG(ERROR) << "nanosleep failed"; |
| 98 | return 1; |
| 99 | } |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 100 | } |
| 101 | } |