/* watchdog-test framework * Copyright (C) 2014 Red Hat Inc. * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include #include #include #include #include #include #include #define WATCHDOG_DEVICE "/dev/watchdog" #define WATCHDOG_TIMEOUT 30 int main () { int fd, i; int timeout; char input[256]; time_t start_t, t; setvbuf (stdout, NULL, _IONBF, 0); printf ("\n"); printf ("Welcome to the watchdog test framework.\n"); printf ("You should read the README file and run this in the guest.\n"); printf ("DO NOT RUN IT IN THE HOST!\n"); printf ("\n"); printf ("The test is as follows:\n"); printf ("(1) I will set up the watchdog with a %d second timeout.\n", WATCHDOG_TIMEOUT); printf ("(2) I will ping the watchdog for %d seconds. During this time\n" " the guest should run normally.\n", WATCHDOG_TIMEOUT * 2); printf ("(3) I will stop pinging the watchdog and just count up. If the\n" " virtual watchdog device is set correctly, then the watchdog\n" " action (eg. reboot) should happen around the %d second mark.\n", WATCHDOG_TIMEOUT); printf ("\n"); printf ("Do you want to start the test? Type \"yes\" without quotes:\n"); if (fgets (input, sizeof input, stdin) == NULL || strcmp (input, "yes") != 0) { printf ("Exiting the program.\n"); exit (EXIT_SUCCESS); } printf ("\n"); printf ("Setting up the watchdog (%s) with a %d second timeout.\n", WATCHDOG_DEVICE, WATCHDOG_TIMEOUT); sync (); fd = open (WATCHDOG_DEVICE, O_WRONLY); if (fd == -1) { perror (WATCHDOG_DEVICE); exit (EXIT_FAILURE); } timeout = WATCHDOG_TIMEOUT; if (ioctl (fd, WDIOC_SETTIMEOUT, &timeout) == -1) { perror ("ioctl: WDIOC_SETTIMEOUT: error setting timeout"); exit (EXIT_FAILURE); } if (ioctl (fd, WDIOC_GETTIMEOUT, &timeout) == -1) perror ("ioctl: WDIOC_GETTIMEOUT"); else { printf ("Timeout is set to %d seconds.\n"); if (timeout != WATCHDOG_TIMEOUT) printf ("Note: some watchdog devices don't support setting exact timeout values.\n"); } printf ("\n"); printf ("Pinging the watchdog for %d seconds ...\n", WATCHDOG_TIMEOUT*2); printf ("\n"); time (&start_t); for (;;) { time (&t); if (t - start_t > WATCHDOG_TIMEOUT * 2) break; printf ("%d... ", (int) (t - start_t)); sync (); sleep (3); if (ioctl (fd, WDIOC_KEEPALIVE, 0) == -1) perror ("ioctl: WDIOC_KEEPALIVE"); printf ("* "); } printf ("\n"); printf ("Stopping pings.\n"); printf ("The watchdog action should happen at around %d mark.\n"); printf ("\n"); time (&start_t); for (;;) { time (&t); printf ("%d... ", (int) (t - start_t)); sync (); sleep (3); } /*NOTREACHED*/ exit (EXIT_SUCCESS); }