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 | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 22 | |
| 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 Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 31 | #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 Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 38 | |
| 39 | using android::base::Basename; |
| 40 | using android::base::StringPrintf; |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 41 | using std::literals::chrono_literals::operator""s; |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 42 | |
| 43 | int main(int argc, char** argv) { |
| 44 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 45 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 46 | 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 Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 50 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 51 | LOG(INFO) << "gs_watchdogd started (interval " << interval.count() |
| 52 | << ", margin " << margin.count() << ")!"; |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 53 | |
| 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 Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 61 | std::vector<android::base::unique_fd> wdt_dev_fds; |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 62 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 63 | 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 Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 67 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 68 | 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 Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 73 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 74 | wdt_dev_fds.emplace_back(fd); |
| 75 | ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout_secs); |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 76 | if (ret) { |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 77 | 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 Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 81 | } else { |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 82 | 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 Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 87 | } |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 91 | globfree(&globbuf); |
| 92 | |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 93 | while (true) { |
Woody Lin | de06172 | 2024-01-15 16:01:32 +0800 | [diff] [blame^] | 94 | for (const auto& fd : wdt_dev_fds) { |
| 95 | TEMP_FAILURE_RETRY(write(fd, "", 1)); |
| 96 | } |
| 97 | sleep(interval.count()); |
Woody Lin | 70a0dbc | 2022-07-11 18:39:11 +0800 | [diff] [blame] | 98 | } |
| 99 | } |