manage.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Handle extern requests for shutdown, reboot and sysrq
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/err.h>
  6. #include <linux/reboot.h>
  7. #include <linux/sysrq.h>
  8. #include <xen/xenbus.h>
  9. #define SHUTDOWN_INVALID -1
  10. #define SHUTDOWN_POWEROFF 0
  11. #define SHUTDOWN_SUSPEND 2
  12. /* Code 3 is SHUTDOWN_CRASH, which we don't use because the domain can only
  13. * report a crash, not be instructed to crash!
  14. * HALT is the same as POWEROFF, as far as we're concerned. The tools use
  15. * the distinction when we return the reason code to them.
  16. */
  17. #define SHUTDOWN_HALT 4
  18. /* Ignore multiple shutdown requests. */
  19. static int shutting_down = SHUTDOWN_INVALID;
  20. static void shutdown_handler(struct xenbus_watch *watch,
  21. const char **vec, unsigned int len)
  22. {
  23. char *str;
  24. struct xenbus_transaction xbt;
  25. int err;
  26. if (shutting_down != SHUTDOWN_INVALID)
  27. return;
  28. again:
  29. err = xenbus_transaction_start(&xbt);
  30. if (err)
  31. return;
  32. str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
  33. /* Ignore read errors and empty reads. */
  34. if (XENBUS_IS_ERR_READ(str)) {
  35. xenbus_transaction_end(xbt, 1);
  36. return;
  37. }
  38. xenbus_write(xbt, "control", "shutdown", "");
  39. err = xenbus_transaction_end(xbt, 0);
  40. if (err == -EAGAIN) {
  41. kfree(str);
  42. goto again;
  43. }
  44. if (strcmp(str, "poweroff") == 0 ||
  45. strcmp(str, "halt") == 0)
  46. orderly_poweroff(false);
  47. else if (strcmp(str, "reboot") == 0)
  48. ctrl_alt_del();
  49. else {
  50. printk(KERN_INFO "Ignoring shutdown request: %s\n", str);
  51. shutting_down = SHUTDOWN_INVALID;
  52. }
  53. kfree(str);
  54. }
  55. static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
  56. unsigned int len)
  57. {
  58. char sysrq_key = '\0';
  59. struct xenbus_transaction xbt;
  60. int err;
  61. again:
  62. err = xenbus_transaction_start(&xbt);
  63. if (err)
  64. return;
  65. if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
  66. printk(KERN_ERR "Unable to read sysrq code in "
  67. "control/sysrq\n");
  68. xenbus_transaction_end(xbt, 1);
  69. return;
  70. }
  71. if (sysrq_key != '\0')
  72. xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
  73. err = xenbus_transaction_end(xbt, 0);
  74. if (err == -EAGAIN)
  75. goto again;
  76. if (sysrq_key != '\0')
  77. handle_sysrq(sysrq_key, NULL);
  78. }
  79. static struct xenbus_watch shutdown_watch = {
  80. .node = "control/shutdown",
  81. .callback = shutdown_handler
  82. };
  83. static struct xenbus_watch sysrq_watch = {
  84. .node = "control/sysrq",
  85. .callback = sysrq_handler
  86. };
  87. static int setup_shutdown_watcher(void)
  88. {
  89. int err;
  90. err = register_xenbus_watch(&shutdown_watch);
  91. if (err) {
  92. printk(KERN_ERR "Failed to set shutdown watcher\n");
  93. return err;
  94. }
  95. err = register_xenbus_watch(&sysrq_watch);
  96. if (err) {
  97. printk(KERN_ERR "Failed to set sysrq watcher\n");
  98. return err;
  99. }
  100. return 0;
  101. }
  102. static int shutdown_event(struct notifier_block *notifier,
  103. unsigned long event,
  104. void *data)
  105. {
  106. setup_shutdown_watcher();
  107. return NOTIFY_DONE;
  108. }
  109. static int __init setup_shutdown_event(void)
  110. {
  111. static struct notifier_block xenstore_notifier = {
  112. .notifier_call = shutdown_event
  113. };
  114. register_xenstore_notifier(&xenstore_notifier);
  115. return 0;
  116. }
  117. subsys_initcall(setup_shutdown_event);