1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "qemu-common.h"
23#include "qemu-option.h"
24#include "qemu-config.h"
25#include "qemu-queue.h"
26#include "qemu-objects.h"
27#include "monitor.h"
28#include "sysemu.h"
29#include "hw/watchdog.h"
30
31
32#define WDT_RESET 1
33#define WDT_SHUTDOWN 2
34#define WDT_POWEROFF 3
35#define WDT_PAUSE 4
36#define WDT_DEBUG 5
37#define WDT_NONE 6
38
39static int watchdog_action = WDT_RESET;
40static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
41
42void watchdog_add_model(WatchdogTimerModel *model)
43{
44 QLIST_INSERT_HEAD(&watchdog_list, model, entry);
45}
46
47
48
49
50
51
52int select_watchdog(const char *p)
53{
54 WatchdogTimerModel *model;
55 QemuOpts *opts;
56
57
58 if (strcmp(p, "?") == 0) {
59 QLIST_FOREACH(model, &watchdog_list, entry) {
60 fprintf(stderr, "\t%s\t%s\n",
61 model->wdt_name, model->wdt_description);
62 }
63 return 2;
64 }
65
66 QLIST_FOREACH(model, &watchdog_list, entry) {
67 if (strcasecmp(model->wdt_name, p) == 0) {
68
69 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
70 qemu_opt_set(opts, "driver", p);
71 return 0;
72 }
73 }
74
75 fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
76 QLIST_FOREACH(model, &watchdog_list, entry) {
77 fprintf(stderr, "\t%s\t%s\n",
78 model->wdt_name, model->wdt_description);
79 }
80 return 1;
81}
82
83int select_watchdog_action(const char *p)
84{
85 if (strcasecmp(p, "reset") == 0)
86 watchdog_action = WDT_RESET;
87 else if (strcasecmp(p, "shutdown") == 0)
88 watchdog_action = WDT_SHUTDOWN;
89 else if (strcasecmp(p, "poweroff") == 0)
90 watchdog_action = WDT_POWEROFF;
91 else if (strcasecmp(p, "pause") == 0)
92 watchdog_action = WDT_PAUSE;
93 else if (strcasecmp(p, "debug") == 0)
94 watchdog_action = WDT_DEBUG;
95 else if (strcasecmp(p, "none") == 0)
96 watchdog_action = WDT_NONE;
97 else
98 return -1;
99
100 return 0;
101}
102
103static void watchdog_mon_event(const char *action)
104{
105 QObject *data;
106
107 data = qobject_from_jsonf("{ 'action': %s }", action);
108 monitor_protocol_event(QEVENT_WATCHDOG, data);
109 qobject_decref(data);
110}
111
112
113
114
115void watchdog_perform_action(void)
116{
117 switch(watchdog_action) {
118 case WDT_RESET:
119 watchdog_mon_event("reset");
120 qemu_system_reset_request();
121 break;
122
123 case WDT_SHUTDOWN:
124 watchdog_mon_event("shutdown");
125 qemu_system_powerdown_request();
126 break;
127
128 case WDT_POWEROFF:
129 watchdog_mon_event("poweroff");
130 exit(0);
131 break;
132
133 case WDT_PAUSE:
134 watchdog_mon_event("pause");
135 vm_stop(RUN_STATE_WATCHDOG);
136 break;
137
138 case WDT_DEBUG:
139 watchdog_mon_event("debug");
140 fprintf(stderr, "watchdog: timer fired\n");
141 break;
142
143 case WDT_NONE:
144 watchdog_mon_event("none");
145 break;
146 }
147}
148