| 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) { | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 31 |     open_devnull_stdio(); | 
 | 32 |     klog_init(); | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 33 |     klog_set_level(KLOG_NOTICE_LEVEL); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 34 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 35 |     int interval = 10; | 
 | 36 |     if (argc >= 2) interval = atoi(argv[1]); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 37 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 38 |     int margin = 10; | 
 | 39 |     if (argc >= 3) margin = atoi(argv[2]); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 40 |  | 
| Mark Salyzyn | 636b1eb | 2015-06-26 09:10:13 -0700 | [diff] [blame] | 41 |     NOTICE("started (interval %d, margin %d)!\n", interval, margin); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 42 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 43 |     int fd = open(DEV_NAME, O_RDWR|O_CLOEXEC); | 
 | 44 |     if (fd == -1) { | 
| Mark Salyzyn | 636b1eb | 2015-06-26 09:10:13 -0700 | [diff] [blame] | 45 |         ERROR("Failed to open %s: %s\n", DEV_NAME, strerror(errno)); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 46 |         return 1; | 
 | 47 |     } | 
 | 48 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 49 |     int timeout = interval + margin; | 
 | 50 |     int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 51 |     if (ret) { | 
| Mark Salyzyn | 636b1eb | 2015-06-26 09:10:13 -0700 | [diff] [blame] | 52 |         ERROR("Failed to set timeout to %d: %s\n", timeout, strerror(errno)); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 53 |         ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout); | 
 | 54 |         if (ret) { | 
| Mark Salyzyn | 636b1eb | 2015-06-26 09:10:13 -0700 | [diff] [blame] | 55 |             ERROR("Failed to get timeout: %s\n", strerror(errno)); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 56 |         } else { | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 57 |             if (timeout > margin) { | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 58 |                 interval = timeout - margin; | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 59 |             } else { | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 60 |                 interval = 1; | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 61 |             } | 
| Mark Salyzyn | 636b1eb | 2015-06-26 09:10:13 -0700 | [diff] [blame] | 62 |             WARNING("Adjusted interval to timeout returned by driver:" | 
 | 63 |                     " timeout %d, interval %d, margin %d\n", | 
 | 64 |                     timeout, interval, margin); | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 65 |         } | 
 | 66 |     } | 
 | 67 |  | 
| Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 68 |     while (true) { | 
| Arve Hjønnevåg | d97d907 | 2012-06-13 21:51:56 -0700 | [diff] [blame] | 69 |         write(fd, "", 1); | 
 | 70 |         sleep(interval); | 
 | 71 |     } | 
 | 72 | } |