]>
Commit | Line | Data |
---|---|---|
9dd986cc RJ |
1 | /* |
2 | * Virtual hardware watchdog. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version 2 | |
9 | * of the License, or (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
8167ee88 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
9dd986cc RJ |
18 | * |
19 | * By Richard W.M. Jones ([email protected]). | |
20 | */ | |
21 | ||
22 | #include "qemu-common.h" | |
1de7afc9 PB |
23 | #include "qemu/option.h" |
24 | #include "qemu/config-file.h" | |
25 | #include "qemu/queue.h" | |
7b1b5d19 | 26 | #include "qapi/qmp/types.h" |
83c9089e | 27 | #include "monitor/monitor.h" |
9c17d615 | 28 | #include "sysemu/sysemu.h" |
0d09e41a | 29 | #include "sysemu/watchdog.h" |
9dd986cc | 30 | |
88b3be20 MA |
31 | /* Possible values for action parameter. */ |
32 | #define WDT_RESET 1 /* Hard reset. */ | |
33 | #define WDT_SHUTDOWN 2 /* Shutdown. */ | |
34 | #define WDT_POWEROFF 3 /* Quit. */ | |
35 | #define WDT_PAUSE 4 /* Pause. */ | |
36 | #define WDT_DEBUG 5 /* Prints a message and continues running. */ | |
37 | #define WDT_NONE 6 /* Do nothing. */ | |
38 | ||
88b3be20 | 39 | static int watchdog_action = WDT_RESET; |
72cf2d4f | 40 | static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list; |
9dd986cc RJ |
41 | |
42 | void watchdog_add_model(WatchdogTimerModel *model) | |
43 | { | |
72cf2d4f | 44 | QLIST_INSERT_HEAD(&watchdog_list, model, entry); |
9dd986cc RJ |
45 | } |
46 | ||
47 | /* Returns: | |
48 | * 0 = continue | |
49 | * 1 = exit program with error | |
50 | * 2 = exit program without error | |
51 | */ | |
52 | int select_watchdog(const char *p) | |
53 | { | |
54 | WatchdogTimerModel *model; | |
09aaa160 | 55 | QemuOpts *opts; |
9dd986cc RJ |
56 | |
57 | /* -watchdog ? lists available devices and exits cleanly. */ | |
c8057f95 | 58 | if (is_help_option(p)) { |
72cf2d4f | 59 | QLIST_FOREACH(model, &watchdog_list, entry) { |
9dd986cc RJ |
60 | fprintf(stderr, "\t%s\t%s\n", |
61 | model->wdt_name, model->wdt_description); | |
62 | } | |
63 | return 2; | |
64 | } | |
65 | ||
72cf2d4f | 66 | QLIST_FOREACH(model, &watchdog_list, entry) { |
9dd986cc | 67 | if (strcasecmp(model->wdt_name, p) == 0) { |
09aaa160 | 68 | /* add the device */ |
87ea75d5 PC |
69 | opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
70 | &error_abort); | |
09aaa160 | 71 | qemu_opt_set(opts, "driver", p); |
9dd986cc RJ |
72 | return 0; |
73 | } | |
74 | } | |
75 | ||
76 | fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n"); | |
72cf2d4f | 77 | QLIST_FOREACH(model, &watchdog_list, entry) { |
9dd986cc RJ |
78 | fprintf(stderr, "\t%s\t%s\n", |
79 | model->wdt_name, model->wdt_description); | |
80 | } | |
81 | return 1; | |
82 | } | |
83 | ||
84 | int select_watchdog_action(const char *p) | |
85 | { | |
86 | if (strcasecmp(p, "reset") == 0) | |
87 | watchdog_action = WDT_RESET; | |
88 | else if (strcasecmp(p, "shutdown") == 0) | |
89 | watchdog_action = WDT_SHUTDOWN; | |
90 | else if (strcasecmp(p, "poweroff") == 0) | |
91 | watchdog_action = WDT_POWEROFF; | |
92 | else if (strcasecmp(p, "pause") == 0) | |
93 | watchdog_action = WDT_PAUSE; | |
94 | else if (strcasecmp(p, "debug") == 0) | |
95 | watchdog_action = WDT_DEBUG; | |
96 | else if (strcasecmp(p, "none") == 0) | |
97 | watchdog_action = WDT_NONE; | |
98 | else | |
99 | return -1; | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
9eedeb3b LC |
104 | static void watchdog_mon_event(const char *action) |
105 | { | |
106 | QObject *data; | |
107 | ||
108 | data = qobject_from_jsonf("{ 'action': %s }", action); | |
109 | monitor_protocol_event(QEVENT_WATCHDOG, data); | |
110 | qobject_decref(data); | |
111 | } | |
112 | ||
9dd986cc RJ |
113 | /* This actually performs the "action" once a watchdog has expired, |
114 | * ie. reboot, shutdown, exit, etc. | |
115 | */ | |
116 | void watchdog_perform_action(void) | |
117 | { | |
118 | switch(watchdog_action) { | |
119 | case WDT_RESET: /* same as 'system_reset' in monitor */ | |
9eedeb3b | 120 | watchdog_mon_event("reset"); |
9dd986cc RJ |
121 | qemu_system_reset_request(); |
122 | break; | |
123 | ||
124 | case WDT_SHUTDOWN: /* same as 'system_powerdown' in monitor */ | |
9eedeb3b | 125 | watchdog_mon_event("shutdown"); |
9dd986cc RJ |
126 | qemu_system_powerdown_request(); |
127 | break; | |
128 | ||
129 | case WDT_POWEROFF: /* same as 'quit' command in monitor */ | |
9eedeb3b | 130 | watchdog_mon_event("poweroff"); |
9dd986cc | 131 | exit(0); |
9dd986cc RJ |
132 | |
133 | case WDT_PAUSE: /* same as 'stop' command in monitor */ | |
9eedeb3b | 134 | watchdog_mon_event("pause"); |
0461d5a6 | 135 | vm_stop(RUN_STATE_WATCHDOG); |
9dd986cc RJ |
136 | break; |
137 | ||
138 | case WDT_DEBUG: | |
9eedeb3b | 139 | watchdog_mon_event("debug"); |
9dd986cc RJ |
140 | fprintf(stderr, "watchdog: timer fired\n"); |
141 | break; | |
142 | ||
143 | case WDT_NONE: | |
9eedeb3b | 144 | watchdog_mon_event("none"); |
9dd986cc RJ |
145 | break; |
146 | } | |
147 | } |