watchdogd
Advanced system & process supervisor for Linux
ex2.c
/* Another wdog client example, using compat API, press Ctrl-C to exit ...
*
* Copyright (c) 2016-2024 Joachim Wiberg <troglobit@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "wdog.h"
extern char *__progname;
extern int __wdog_testmode;
#define DEBUG(fmt, args...) if (dbg) printf("%s: " fmt "\n", __progname, ##args);
#define PERROR(fmt, args...) if (dbg) fprintf(stderr, "%s: " fmt ": %s\n", __progname, ##args, strerror(errno));
int main(int argc, char *argv[])
{
int id, i, dbg = 0;
unsigned int ack = -1;
wdog_reason_t reason;
if (argc >= 2 && !strncmp(argv[1], "-V", 2))
dbg = 1;
DEBUG("Checking connectivity with watchdogd ...");
if (wdog_ping()) {
PERROR("Failed connectivity check");
return 1;
}
DEBUG("OK (debug:%d)!", id);
printf("Reset counter: %u\n", reason.counter);
printf("Reset reason: %d - %s\n", reason.code, wdog_reset_reason_str(&reason));
switch (reason.code) {
printf("Process wid %d, label %s\n", reason.wid, reason.label);
break;
default:
break;
}
id = wdog_subscribe(NULL, 2000, &ack);
if (id < 0) {
perror("Failed connecting to wdog");
return 1;
}
printf("Press Ctrl-C to exit.\n");
for (i = 0; i < 200; i++) {
DEBUG("Kicking ...");
if (wdog_kick(id, 2000, ack, &ack))
PERROR("Failed kicking");
sleep(1);
if (i == 30)
wdog_forced_reset("Terminal failure");
}
DEBUG("Exiting ...");
if (wdog_unsubscribe(id, ack)) {
PERROR("Failed unsubscribe");
return 1;
}
return 0;
}