poweroff.c 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * poweroff.c - sysrq handler to gracefully power down machine.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/sysrq.h>
  7. #include <linux/init.h>
  8. #include <linux/pm.h>
  9. #include <linux/workqueue.h>
  10. #include <linux/reboot.h>
  11. #include <linux/cpumask.h>
  12. /*
  13. * When the user hits Sys-Rq o to power down the machine this is the
  14. * callback we use.
  15. */
  16. static void do_poweroff(struct work_struct *dummy)
  17. {
  18. kernel_power_off();
  19. }
  20. static DECLARE_WORK(poweroff_work, do_poweroff);
  21. static void handle_poweroff(int key)
  22. {
  23. /* run sysrq poweroff on boot cpu */
  24. schedule_work_on(cpumask_first(cpu_online_mask), &poweroff_work);
  25. }
  26. static const struct sysrq_key_op sysrq_poweroff_op = {
  27. .handler = handle_poweroff,
  28. .help_msg = "poweroff(o)",
  29. .action_msg = "Power Off",
  30. .enable_mask = SYSRQ_ENABLE_BOOT,
  31. };
  32. static int __init pm_sysrq_init(void)
  33. {
  34. register_sysrq_key('o', &sysrq_poweroff_op);
  35. return 0;
  36. }
  37. subsys_initcall(pm_sysrq_init);