watchdog.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "qemu-common.h"
  22. #include "qemu/option.h"
  23. #include "qemu/config-file.h"
  24. #include "qemu/queue.h"
  25. #include "qapi/qmp/types.h"
  26. #include "monitor/monitor.h"
  27. #include "sysemu/sysemu.h"
  28. #include "hw/watchdog.h"
  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. static int watchdog_action = WDT_RESET;
  37. static QLIST_HEAD(watchdog_list, WatchdogTimerModel) watchdog_list;
  38. void watchdog_add_model(WatchdogTimerModel *model)
  39. {
  40. QLIST_INSERT_HEAD(&watchdog_list, model, entry);
  41. }
  42. /* Returns:
  43. * 0 = continue
  44. * 1 = exit program with error
  45. * 2 = exit program without error
  46. */
  47. int select_watchdog(const char *p)
  48. {
  49. WatchdogTimerModel *model;
  50. QemuOpts *opts;
  51. /* -watchdog ? lists available devices and exits cleanly. */
  52. if (is_help_option(p)) {
  53. QLIST_FOREACH(model, &watchdog_list, entry) {
  54. fprintf(stderr, "\t%s\t%s\n",
  55. model->wdt_name, model->wdt_description);
  56. }
  57. return 2;
  58. }
  59. QLIST_FOREACH(model, &watchdog_list, entry) {
  60. if (strcasecmp(model->wdt_name, p) == 0) {
  61. /* add the device */
  62. opts = qemu_opts_create_nofail(qemu_find_opts("device"));
  63. qemu_opt_set(opts, "driver", p);
  64. return 0;
  65. }
  66. }
  67. fprintf(stderr, "Unknown -watchdog device. Supported devices are:\n");
  68. QLIST_FOREACH(model, &watchdog_list, entry) {
  69. fprintf(stderr, "\t%s\t%s\n",
  70. model->wdt_name, model->wdt_description);
  71. }
  72. return 1;
  73. }
  74. int select_watchdog_action(const char *p)
  75. {
  76. if (strcasecmp(p, "reset") == 0)
  77. watchdog_action = WDT_RESET;
  78. else if (strcasecmp(p, "shutdown") == 0)
  79. watchdog_action = WDT_SHUTDOWN;
  80. else if (strcasecmp(p, "poweroff") == 0)
  81. watchdog_action = WDT_POWEROFF;
  82. else if (strcasecmp(p, "pause") == 0)
  83. watchdog_action = WDT_PAUSE;
  84. else if (strcasecmp(p, "debug") == 0)
  85. watchdog_action = WDT_DEBUG;
  86. else if (strcasecmp(p, "none") == 0)
  87. watchdog_action = WDT_NONE;
  88. else
  89. return -1;
  90. return 0;
  91. }
  92. static void watchdog_mon_event(const char *action)
  93. {
  94. QObject *data;
  95. data = qobject_from_jsonf("{ 'action': %s }", action);
  96. monitor_protocol_event(QEVENT_WATCHDOG, data);
  97. qobject_decref(data);
  98. }
  99. /* This actually performs the "action" once a watchdog has expired,
  100. * ie. reboot, shutdown, exit, etc.
  101. */
  102. void watchdog_perform_action(void)
  103. {
  104. switch(watchdog_action) {
  105. case WDT_RESET: /* same as 'system_reset' in monitor */
  106. watchdog_mon_event("reset");
  107. qemu_system_reset_request();
  108. break;
  109. case WDT_SHUTDOWN: /* same as 'system_powerdown' in monitor */
  110. watchdog_mon_event("shutdown");
  111. qemu_system_powerdown_request();
  112. break;
  113. case WDT_POWEROFF: /* same as 'quit' command in monitor */
  114. watchdog_mon_event("poweroff");
  115. exit(0);
  116. break;
  117. case WDT_PAUSE: /* same as 'stop' command in monitor */
  118. watchdog_mon_event("pause");
  119. vm_stop(RUN_STATE_WATCHDOG);
  120. break;
  121. case WDT_DEBUG:
  122. watchdog_mon_event("debug");
  123. fprintf(stderr, "watchdog: timer fired\n");
  124. break;
  125. case WDT_NONE:
  126. watchdog_mon_event("none");
  127. break;
  128. }
  129. }