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/chrono_utils.h>
18 #include <android-base/file.h>
19 #include <android-base/logging.h>
20 #include <android-base/stringprintf.h>
21 #include <android-base/unique_fd.h>
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
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
38
39 using android::base::Basename;
40 using android::base::StringPrintf;
41 using std::literals::chrono_literals::operator""s;
42
main(int argc,char ** argv)43 int main(int argc, char** argv) {
44 android::base::InitLogging(argv, &android::base::KernelLogger);
45
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;
50
51 LOG(INFO) << "gs_watchdogd started (interval " << interval.count()
52 << ", margin " << margin.count() << ")!";
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
61 std::vector<android::base::unique_fd> wdt_dev_fds;
62
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());
67
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 }
73
74 wdt_dev_fds.emplace_back(fd);
75 ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout_secs);
76 if (ret) {
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";
81 } else {
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();
87 }
88 }
89 }
90
91 globfree(&globbuf);
92
93 while (true) {
94 for (const auto& fd : wdt_dev_fds) {
95 TEMP_FAILURE_RETRY(write(fd, "", 1));
96 }
97 sleep(interval.count());
98 }
99 }
100