/* * Copyright (C) 2018 AVM GmbH * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */ #include #include #include #include #include #include #include #include #include "wdt.h" #include "wdt-private.h" static void warn(const char *argv0, const char *fmt, ...) { va_list ap; fprintf(stderr, "%s: ", argv0); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); } static int detect_device(void) { const char *devname; devname = watchdog_device_name(); if (devname) printf("Found AVM watchdog device: %s\n", devname); else puts("No AVM watchdog device found"); return !devname; } static int write_args_to_device(const char *argv0, char **argv) { const char *action; int fd, i, ret; FILE *fp; ret = -1; action = "opening"; fd = watchdog_open_device(); if (fd < 0) goto bail; fp = fdopen(fd, "w"); if (!fp) goto bail; action = "writing to"; for (i = 0; argv[i]; ++i) { if (i > 0) fputc(' ', fp); ret = fputs(argv[i], fp); if (ret == EOF) goto bail; } ret = fclose(fp); bail: if (ret) warn(argv0, "Error %s watchdog device: %s\n", action, strerror(errno)); return ret; } int main(int argc, char *argv[]) { static struct option lopt[] = {{"detect", no_argument, 0, 0}}; bool detectonly = false; int c, ret; c = getopt_long(argc, argv, "", lopt, NULL); switch (c) { case -1: break; case 0: detectonly = true; break; default: warn(argv[0], "Unknown options.\n"); return EXIT_FAILURE; } if (detectonly) ret = detect_device(); else ret = write_args_to_device(argv[0], &argv[optind]); return !!ret; }