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