linux watchdog demo hacking

Linux Watchdog 操作详解
本文解析了如何在 Linux 环境下操作 Watchdog 设备,包括如何启动和关闭 Watchdog 功能,以及发送 IO 控制命令来保持 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);
    }
}

 

转载于:https://www.cnblogs.com/zengjfgit/p/5328356.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值