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 | |
| 17 | #include <android-base/file.h> |
| 18 | #include <android-base/logging.h> |
| 19 | #include <android-base/stringprintf.h> |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <glob.h> |
| 24 | #include <linux/watchdog.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #define DEV_GLOB "/sys/devices/platform/*.watchdog_cl0/watchdog/watchdog*" |
| 30 | |
| 31 | using android::base::Basename; |
| 32 | using android::base::StringPrintf; |
| 33 | |
| 34 | int main(int argc, char** argv) { |
| 35 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 36 | |
| 37 | int interval = 10; |
| 38 | if (argc >= 2) interval = atoi(argv[1]); |
| 39 | |
| 40 | int margin = 10; |
| 41 | if (argc >= 3) margin = atoi(argv[2]); |
| 42 | |
| 43 | LOG(INFO) << "gs_watchdogd started (interval " << interval << ", margin " << margin << ")!"; |
| 44 | |
| 45 | glob_t globbuf; |
| 46 | int ret = glob(DEV_GLOB, GLOB_MARK, nullptr, &globbuf); |
| 47 | if (ret) { |
| 48 | PLOG(ERROR) << "Failed to lookup glob " << DEV_GLOB << ": " << ret; |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | if (globbuf.gl_pathc > 1) { |
| 53 | PLOG(WARNING) << "Multiple watchdog dev path found by " << DEV_GLOB; |
| 54 | } |
| 55 | |
| 56 | std::string dev_path = StringPrintf("/dev/%s", Basename(globbuf.gl_pathv[0]).c_str()); |
| 57 | globfree(&globbuf); |
| 58 | |
| 59 | int fd = open(dev_path.c_str(), O_RDWR | O_CLOEXEC); |
| 60 | if (fd == -1) { |
| 61 | PLOG(ERROR) << "Failed to open " << dev_path; |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | int timeout = interval + margin; |
| 66 | ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout); |
| 67 | if (ret) { |
| 68 | PLOG(ERROR) << "Failed to set timeout to " << timeout; |
| 69 | ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout); |
| 70 | if (ret) { |
| 71 | PLOG(ERROR) << "Failed to get timeout"; |
| 72 | } else { |
| 73 | if (timeout > margin) { |
| 74 | interval = timeout - margin; |
| 75 | } else { |
| 76 | interval = 1; |
| 77 | } |
| 78 | LOG(WARNING) << "Adjusted interval to timeout returned by driver: " |
| 79 | << "timeout " << timeout << ", interval " << interval << ", margin " |
| 80 | << margin; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | while (true) { |
| 85 | write(fd, "", 1); |
| 86 | sleep(interval); |
| 87 | } |
| 88 | } |