2009-04-25 16:56:19 +04:00
|
|
|
/*
|
|
|
|
* Virtual hardware watchdog.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 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
|
2009-07-17 00:47:01 +04:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2009-04-25 16:56:19 +04:00
|
|
|
*
|
|
|
|
* By Richard W.M. Jones (rjones@redhat.com).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/option.h"
|
|
|
|
#include "qemu/config-file.h"
|
|
|
|
#include "qemu/queue.h"
|
2012-12-17 21:19:43 +04:00
|
|
|
#include "qapi/qmp/types.h"
|
2012-12-17 21:19:49 +04:00
|
|
|
#include "monitor/monitor.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/sysemu.h"
|
2009-04-25 16:56:19 +04:00
|
|
|
#include "hw/watchdog.h"
|
|
|
|
|
2009-08-21 12:31:32 +04:00
|
|
|
/* Possible values for action parameter. */
|
|
|
|
#define WDT_RESET 1 /* Hard reset. */
|
|
|
|
#define WDT_SHUTDOWN 2 /* Shutdown. */
|
|
|
|
#define WDT_POWEROFF 3 /* Quit. */
|
|
|
|
#define WDT_PAUSE 4 /* Pause. */
|
|
|
|
#define WDT_DEBUG 5 /* Prints a message and continues running. */
|
|
|
|
#define WDT_NONE 6 /* Do nothing. */
|
|
|
|
|
|
|
|
static int watchdog_action = WDT_RESET;
|
2009-09-12 11:36:22 +04:00
|
|
|
static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
|
2009-04-25 16:56:19 +04:00
|
|
|
|
|
|
|
void watchdog_add_model(WatchdogTimerModel *model)
|
|
|
|
{
|
2009-09-12 11:36:22 +04:00
|
|
|
QLIST_INSERT_HEAD(&watchdog_list, model, entry);
|
2009-04-25 16:56:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns:
|
|
|
|
* 0 = continue
|
|
|
|
* 1 = exit program with error
|
|
|
|
* 2 = exit program without error
|
|
|
|
*/
|
|
|
|
int select_watchdog(const char *p)
|
|
|
|
{
|
|
|
|
WatchdogTimerModel *model;
|
2009-08-21 12:31:34 +04:00
|
|
|
QemuOpts *opts;
|
2009-04-25 16:56:19 +04:00
|
|
|
|
|
|
|
/* -watchdog ? lists available devices and exits cleanly. */
|
2012-08-02 16:45:54 +04:00
|
|
|
if (is_help_option(p)) {
|
2009-09-12 11:36:22 +04:00
|
|
|
QLIST_FOREACH(model, &watchdog_list, entry) {
|
2009-04-25 16:56:19 +04:00
|
|
|
fprintf(stderr, "\t%s\t%s\n",
|
|
|
|
model->wdt_name, model->wdt_description);
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2009-09-12 11:36:22 +04:00
|
|
|
QLIST_FOREACH(model, &watchdog_list, entry) {
|
2009-04-25 16:56:19 +04:00
|
|
|
if (strcasecmp(model->wdt_name, p) == 0) {
|
2009-08-21 12:31:34 +04:00
|
|
|
/* add the device */
|
2012-12-06 10:47:22 +04:00
|
|
|
opts = qemu_opts_create_nofail(qemu_find_opts("device"));
|
2009-08-21 12:31:34 +04:00
|
|
|
qemu_opt_set(opts, "driver", p);
|
2009-04-25 16:56:19 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
|
2009-09-12 11:36:22 +04:00
|
|
|
QLIST_FOREACH(model, &watchdog_list, entry) {
|
2009-04-25 16:56:19 +04:00
|
|
|
fprintf(stderr, "\t%s\t%s\n",
|
|
|
|
model->wdt_name, model->wdt_description);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int select_watchdog_action(const char *p)
|
|
|
|
{
|
|
|
|
if (strcasecmp(p, "reset") == 0)
|
|
|
|
watchdog_action = WDT_RESET;
|
|
|
|
else if (strcasecmp(p, "shutdown") == 0)
|
|
|
|
watchdog_action = WDT_SHUTDOWN;
|
|
|
|
else if (strcasecmp(p, "poweroff") == 0)
|
|
|
|
watchdog_action = WDT_POWEROFF;
|
|
|
|
else if (strcasecmp(p, "pause") == 0)
|
|
|
|
watchdog_action = WDT_PAUSE;
|
|
|
|
else if (strcasecmp(p, "debug") == 0)
|
|
|
|
watchdog_action = WDT_DEBUG;
|
|
|
|
else if (strcasecmp(p, "none") == 0)
|
|
|
|
watchdog_action = WDT_NONE;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-25 18:13:04 +03:00
|
|
|
static void watchdog_mon_event(const char *action)
|
|
|
|
{
|
|
|
|
QObject *data;
|
|
|
|
|
|
|
|
data = qobject_from_jsonf("{ 'action': %s }", action);
|
|
|
|
monitor_protocol_event(QEVENT_WATCHDOG, data);
|
|
|
|
qobject_decref(data);
|
|
|
|
}
|
|
|
|
|
2009-04-25 16:56:19 +04:00
|
|
|
/* This actually performs the "action" once a watchdog has expired,
|
|
|
|
* ie. reboot, shutdown, exit, etc.
|
|
|
|
*/
|
|
|
|
void watchdog_perform_action(void)
|
|
|
|
{
|
|
|
|
switch(watchdog_action) {
|
|
|
|
case WDT_RESET: /* same as 'system_reset' in monitor */
|
2010-02-25 18:13:04 +03:00
|
|
|
watchdog_mon_event("reset");
|
2009-04-25 16:56:19 +04:00
|
|
|
qemu_system_reset_request();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WDT_SHUTDOWN: /* same as 'system_powerdown' in monitor */
|
2010-02-25 18:13:04 +03:00
|
|
|
watchdog_mon_event("shutdown");
|
2009-04-25 16:56:19 +04:00
|
|
|
qemu_system_powerdown_request();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WDT_POWEROFF: /* same as 'quit' command in monitor */
|
2010-02-25 18:13:04 +03:00
|
|
|
watchdog_mon_event("poweroff");
|
2009-04-25 16:56:19 +04:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WDT_PAUSE: /* same as 'stop' command in monitor */
|
2010-02-25 18:13:04 +03:00
|
|
|
watchdog_mon_event("pause");
|
2011-09-30 21:45:27 +04:00
|
|
|
vm_stop(RUN_STATE_WATCHDOG);
|
2009-04-25 16:56:19 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WDT_DEBUG:
|
2010-02-25 18:13:04 +03:00
|
|
|
watchdog_mon_event("debug");
|
2009-04-25 16:56:19 +04:00
|
|
|
fprintf(stderr, "watchdog: timer fired\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WDT_NONE:
|
2010-02-25 18:13:04 +03:00
|
|
|
watchdog_mon_event("none");
|
2009-04-25 16:56:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|