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