voyager_thread.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* -*- mode: c; c-basic-offset: 8 -*- */
  2. /* Copyright (C) 2001
  3. *
  4. * Author: J.E.J.Bottomley@HansenPartnership.com
  5. *
  6. * linux/arch/i386/kernel/voyager_thread.c
  7. *
  8. * This module provides the machine status monitor thread for the
  9. * voyager architecture. This allows us to monitor the machine
  10. * environment (temp, voltage, fan function) and the front panel and
  11. * internal UPS. If a fault is detected, this thread takes corrective
  12. * action (usually just informing init)
  13. * */
  14. #include <linux/module.h>
  15. #include <linux/mm.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/delay.h>
  18. #include <linux/mc146818rtc.h>
  19. #include <linux/init.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/kmod.h>
  22. #include <linux/completion.h>
  23. #include <linux/sched.h>
  24. #include <linux/kthread.h>
  25. #include <asm/desc.h>
  26. #include <asm/voyager.h>
  27. #include <asm/vic.h>
  28. #include <asm/mtrr.h>
  29. #include <asm/msr.h>
  30. struct task_struct *voyager_thread;
  31. static __u8 set_timeout;
  32. static int execute(const char *string)
  33. {
  34. int ret;
  35. char *envp[] = {
  36. "HOME=/",
  37. "TERM=linux",
  38. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  39. NULL,
  40. };
  41. char *argv[] = {
  42. "/bin/bash",
  43. "-c",
  44. (char *)string,
  45. NULL,
  46. };
  47. if ((ret =
  48. call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC)) != 0) {
  49. printk(KERN_ERR "Voyager failed to run \"%s\": %i\n", string,
  50. ret);
  51. }
  52. return ret;
  53. }
  54. static void check_from_kernel(void)
  55. {
  56. if (voyager_status.switch_off) {
  57. /* FIXME: This should be configurable via proc */
  58. execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1");
  59. } else if (voyager_status.power_fail) {
  60. VDEBUG(("Voyager daemon detected AC power failure\n"));
  61. /* FIXME: This should be configureable via proc */
  62. execute("umask 600; echo F > /etc/powerstatus; kill -PWR 1");
  63. set_timeout = 1;
  64. }
  65. }
  66. static void check_continuing_condition(void)
  67. {
  68. if (voyager_status.power_fail) {
  69. __u8 data;
  70. voyager_cat_psi(VOYAGER_PSI_SUBREAD,
  71. VOYAGER_PSI_AC_FAIL_REG, &data);
  72. if ((data & 0x1f) == 0) {
  73. /* all power restored */
  74. printk(KERN_NOTICE
  75. "VOYAGER AC power restored, cancelling shutdown\n");
  76. /* FIXME: should be user configureable */
  77. execute
  78. ("umask 600; echo O > /etc/powerstatus; kill -PWR 1");
  79. set_timeout = 0;
  80. }
  81. }
  82. }
  83. static int thread(void *unused)
  84. {
  85. printk(KERN_NOTICE "Voyager starting monitor thread\n");
  86. for (;;) {
  87. set_current_state(TASK_INTERRUPTIBLE);
  88. schedule_timeout(set_timeout ? HZ : MAX_SCHEDULE_TIMEOUT);
  89. VDEBUG(("Voyager Daemon awoken\n"));
  90. if (voyager_status.request_from_kernel == 0) {
  91. /* probably awoken from timeout */
  92. check_continuing_condition();
  93. } else {
  94. check_from_kernel();
  95. voyager_status.request_from_kernel = 0;
  96. }
  97. }
  98. }
  99. static int __init voyager_thread_start(void)
  100. {
  101. voyager_thread = kthread_run(thread, NULL, "kvoyagerd");
  102. if (IS_ERR(voyager_thread)) {
  103. printk(KERN_ERR
  104. "Voyager: Failed to create system monitor thread.\n");
  105. return PTR_ERR(voyager_thread);
  106. }
  107. return 0;
  108. }
  109. static void __exit voyager_thread_stop(void)
  110. {
  111. kthread_stop(voyager_thread);
  112. }
  113. module_init(voyager_thread_start);
  114. module_exit(voyager_thread_stop);