adba872533384352e1218ee4174674ec50421d19
[qemu] / hw / watchdog.c
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
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  *
19  * By Richard W.M. Jones (rjones@redhat.com).
20  */
21
22 #include "qemu-common.h"
23 #include "qemu-option.h"
24 #include "qemu-config.h"
25 #include "sys-queue.h"
26 #include "sysemu.h"
27 #include "hw/watchdog.h"
28
29 /* Possible values for action parameter. */
30 #define WDT_RESET        1      /* Hard reset. */
31 #define WDT_SHUTDOWN     2      /* Shutdown. */
32 #define WDT_POWEROFF     3      /* Quit. */
33 #define WDT_PAUSE        4      /* Pause. */
34 #define WDT_DEBUG        5      /* Prints a message and continues running. */
35 #define WDT_NONE         6      /* Do nothing. */
36
37 static int watchdog_action = WDT_RESET;
38 static LIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
39
40 void watchdog_add_model(WatchdogTimerModel *model)
41 {
42     LIST_INSERT_HEAD(&watchdog_list, model, entry);
43 }
44
45 /* Returns:
46  *   0 = continue
47  *   1 = exit program with error
48  *   2 = exit program without error
49  */
50 int select_watchdog(const char *p)
51 {
52     WatchdogTimerModel *model;
53     QemuOpts *opts;
54
55     /* -watchdog ? lists available devices and exits cleanly. */
56     if (strcmp(p, "?") == 0) {
57         LIST_FOREACH(model, &watchdog_list, entry) {
58             fprintf(stderr, "\t%s\t%s\n",
59                      model->wdt_name, model->wdt_description);
60         }
61         return 2;
62     }
63
64     LIST_FOREACH(model, &watchdog_list, entry) {
65         if (strcasecmp(model->wdt_name, p) == 0) {
66             /* add the device */
67             opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
68             qemu_opt_set(opts, "driver", p);
69             return 0;
70         }
71     }
72
73     fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
74     LIST_FOREACH(model, &watchdog_list, entry) {
75         fprintf(stderr, "\t%s\t%s\n",
76                  model->wdt_name, model->wdt_description);
77     }
78     return 1;
79 }
80
81 int select_watchdog_action(const char *p)
82 {
83     if (strcasecmp(p, "reset") == 0)
84         watchdog_action = WDT_RESET;
85     else if (strcasecmp(p, "shutdown") == 0)
86         watchdog_action = WDT_SHUTDOWN;
87     else if (strcasecmp(p, "poweroff") == 0)
88         watchdog_action = WDT_POWEROFF;
89     else if (strcasecmp(p, "pause") == 0)
90         watchdog_action = WDT_PAUSE;
91     else if (strcasecmp(p, "debug") == 0)
92         watchdog_action = WDT_DEBUG;
93     else if (strcasecmp(p, "none") == 0)
94         watchdog_action = WDT_NONE;
95     else
96         return -1;
97
98     return 0;
99 }
100
101 /* This actually performs the "action" once a watchdog has expired,
102  * ie. reboot, shutdown, exit, etc.
103  */
104 void watchdog_perform_action(void)
105 {
106     switch(watchdog_action) {
107     case WDT_RESET:             /* same as 'system_reset' in monitor */
108         qemu_system_reset_request();
109         break;
110
111     case WDT_SHUTDOWN:          /* same as 'system_powerdown' in monitor */
112         qemu_system_powerdown_request();
113         break;
114
115     case WDT_POWEROFF:          /* same as 'quit' command in monitor */
116         exit(0);
117         break;
118
119     case WDT_PAUSE:             /* same as 'stop' command in monitor */
120         vm_stop(0);
121         break;
122
123     case WDT_DEBUG:
124         fprintf(stderr, "watchdog: timer fired\n");
125         break;
126
127     case WDT_NONE:
128         break;
129     }
130 }