linux watchdog demo hacking

简介: /********************************************************************** * linux watchdog demo hacking * 说明: * 本文主要解析linux watchdog大概应该如何操作。
/**********************************************************************
 *                    linux watchdog demo hacking
 * 说明:
 *     本文主要解析linux watchdog大概应该如何操作。
 *
 *                                    2016-3-28 深圳 南山平山村 曾剑锋
 *********************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/watchdog.h>

int fd;

/*
 * This function simply sends an IOCTL to the driver, which in turn ticks
 * the PC Watchdog card to reset its internal timer so it doesn't trigger
 * a computer reset.
 * 这个函数仅仅是发送一个IOCTL命令给驱动,重新启动Watchdog的内部时钟计数器,
 * 这样就不会导致系统重启。
 */
static void keep_alive(void)
{
    int dummy;

    ioctl(fd, WDIOC_KEEPALIVE, &dummy);
}

/*
 * The main program.  Run the program with "-d" to disable the card,
 * or "-e" to enable the card.
 * 主程序,通过传递参数-d来关闭watchdog,-e来打开watchdog。
 */
int main(int argc, char *argv[])
{
    int flags;

    // 打开设备节点
    fd = open("/dev/watchdog", O_WRONLY);
    if (fd == -1) {
        fprintf(stderr, "Watchdog device not enabled.\n");
        fflush(stderr);
        exit(-1);
    }

    if (argc > 1) {
        // 关闭watchdog
        if (!strncasecmp(argv[1], "-d", 2)) {
            flags = WDIOS_DISABLECARD;
            ioctl(fd, WDIOC_SETOPTIONS, &flags);
            fprintf(stderr, "Watchdog card disabled.\n");
            fflush(stderr);
            exit(0);
        // 使能watchdog
        } else if (!strncasecmp(argv[1], "-e", 2)) {
            flags = WDIOS_ENABLECARD;
            ioctl(fd, WDIOC_SETOPTIONS, &flags);
            fprintf(stderr, "Watchdog card enabled.\n");
            fflush(stderr);
            exit(0);
        } else {
            fprintf(stderr, "-d to disable, -e to enable.\n");
            fprintf(stderr, "run by itself to tick the card.\n");
            fflush(stderr);
            exit(0);
        }
    } else {
        fprintf(stderr, "Watchdog Ticking Away!\n");
        fflush(stderr);
    }

    while(1) {
        // 每一秒喂狗一次
        keep_alive();
        sleep(1);
    }
}

 

目录
相关文章
|
2月前
|
存储 Linux 应用服务中间件
VMware安装无GUI版本的Linux(CentOS7)——安装Nginx示例demo
VMware安装无GUI版本的Linux(CentOS7)——安装Nginx示例demo
120 1
|
4月前
|
监控 Linux
linux实现守护进程demo
linux实现守护进程demo
19 0
|
消息中间件 Java 大数据
【大数据】Linux下Storm(0.9版本以上)的环境配置和小Demo
在storm发布到0.9.x以后,配置storm将会变得简单很多,也就是只需要配置zookeeper和storm即可,而不再需要配置zeromq和jzmq,由于网上面的storm配置绝大部分都是0.9以前的storm版本,所以有很多工作是不需要进行的,下面就storm的0.9.5版本在linux环境下进行配置进行详细解析。   由于配置storm只需要两个步骤,大大简化了配置,也是storm团队做了很大的努力,让程序员们专注于程序,让storm配置进行异常简单,好了,废话说了不少,下面正式开始讲解。
217 0
【大数据】Linux下Storm(0.9版本以上)的环境配置和小Demo
|
Linux
Linux Watchdog Test Program
/*********************************************************************** * Linux Watchdog Test Program * 说明: * 由于之前的reset一直没有得到解决,所以这个Watchdog功能一直没有处理, * 现在问题解决了,于是需要加入这个测试程序。
886 0
|
Linux
Linux SocketCan client server demo hacking
/*********************************************************************** * Linux SocketCan client server demo hacking * 说明: * 本文主要是解读Linux上的SocketCan的基本使用方法,内容和Linux上的 * 网络编程差不多。
1324 0
|
Linux 索引
linux SPI bus demo hacking
/********************************************************************** * linux SPI bus demo hacking * 说明: * 本文主要解析linux应用程序如何使用SPI总线和设备通信。
1133 0
|
Linux
linux tmp75 /dev/i2c-* 获取数据 demo
/********************************************************************** * linux tmp75 /dev/i2c-* 获取数据 demo * 说明: * 之前尝试过一次用这种方式来读EEPROM,结果以失败告终,也没找到原因, * 今天定位到问题是由于I2C_SLAVE、I2C_SLAVE_FORCE导致的,之前一直尝试 * I2C_SLAVE,今天定位到问题是I2C总线忙,改成用I2C_SLAVE_FORCE就解决。
1378 0
|
Linux
linux 读取input输入设备demo
/******************************************************************* * linux 读取input输入设备demo * 说明: * 本文主要是解读以前同事写的input设备的一个demo程序。
898 0
|
Linux Android开发
Linux power supply class hacking
/*************************************************************************** * Linux power supply class hacking * 声明: * 本文主要是记录linux电源管理的工作机制是什么,那些供Android jni使用 * 的属性文件是如何生成的,调用机制是什么。
785 0
|
Linux Android开发 移动开发
I.MX6 Android Linux UART send receive with multi-thread and multi-mode demo
/******************************************************************************************* * I.
670 0