| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2012 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 <errno.h> | 
 | 18 | #include <fcntl.h> | 
 | 19 | #include <stdlib.h> | 
 | 20 | #include <string.h> | 
| Elliott Hughes | 3d74d7a | 2015-01-29 21:31:23 -0800 | [diff] [blame] | 21 | #include <unistd.h> | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 22 |  | 
 | 23 | #include <linux/watchdog.h> | 
 | 24 |  | 
 | 25 | #include "log.h" | 
 | 26 | #include "util.h" | 
 | 27 |  | 
 | 28 | #define DEV_NAME "/dev/watchdog" | 
 | 29 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 30 | int watchdogd_main(int argc, char **argv) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 31 |     InitKernelLogging(argv); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 32 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 33 |     int interval = 10; | 
 | 34 |     if (argc >= 2) interval = atoi(argv[1]); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 35 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 36 |     int margin = 10; | 
 | 37 |     if (argc >= 3) margin = atoi(argv[2]); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 38 |  | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 39 |     LOG(INFO) << "watchdogd started (interval " << interval << ", margin " << margin << ")!"; | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 40 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 41 |     int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC); | 
 | 42 |     if (fd == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 43 |         PLOG(ERROR) << "Failed to open " << DEV_NAME; | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 44 |         return 1; | 
 | 45 |     } | 
 | 46 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 47 |     int timeout = interval + margin; | 
 | 48 |     int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 49 |     if (ret) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 50 |         PLOG(ERROR) << "Failed to set timeout to " << timeout; | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 51 |         ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout); | 
 | 52 |         if (ret) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 53 |             PLOG(ERROR) << "Failed to get timeout"; | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 54 |         } else { | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 55 |             if (timeout > margin) { | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 56 |                 interval = timeout - margin; | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 57 |             } else { | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 58 |                 interval = 1; | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 59 |             } | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 60 |             LOG(WARNING) << "Adjusted interval to timeout returned by driver: " | 
 | 61 |                          << "timeout " << timeout | 
 | 62 |                          << ", interval " << interval | 
 | 63 |                          << ", margin " << margin; | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 64 |         } | 
 | 65 |     } | 
 | 66 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 67 |     while (true) { | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 68 |         write(fd, "", 1); | 
 | 69 |         sleep(interval); | 
 | 70 |     } | 
 | 71 | } |