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> 21 22#include <linux/watchdog.h> 23 24#include "log.h" 25#include "util.h" 26 27#define DEV_NAME "/dev/watchdog" 28 29int watchdogd_main(int argc, char **argv) 30{ 31 int fd; 32 int ret; 33 int interval = 10; 34 int margin = 10; 35 int timeout; 36 37 open_devnull_stdio(); 38 klog_init(); 39 40 INFO("Starting watchdogd\n"); 41 42 if (argc >= 2) 43 interval = atoi(argv[1]); 44 45 if (argc >= 3) 46 margin = atoi(argv[2]); 47 48 timeout = interval + margin; 49 50 fd = open(DEV_NAME, O_RDWR); 51 if (fd < 0) { 52 ERROR("watchdogd: Failed to open %s: %s\n", DEV_NAME, strerror(errno)); 53 return 1; 54 } 55 56 ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout); 57 if (ret) { 58 ERROR("watchdogd: Failed to set timeout to %d: %s\n", timeout, strerror(errno)); 59 ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout); 60 if (ret) { 61 ERROR("watchdogd: Failed to get timeout: %s\n", strerror(errno)); 62 } else { 63 if (timeout > margin) 64 interval = timeout - margin; 65 else 66 interval = 1; 67 ERROR("watchdogd: Adjusted interval to timeout returned by driver: timeout %d, interval %d, margin %d\n", 68 timeout, interval, margin); 69 } 70 } 71 72 while(1) { 73 write(fd, "", 1); 74 sleep(interval); 75 } 76} 77 78