voyager_thread.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/smp_lock.h>
  20. #include <linux/init.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/kmod.h>
  23. #include <linux/completion.h>
  24. #include <linux/sched.h>
  25. #include <linux/kthread.h>
  26. #include <asm/desc.h>
  27. #include <asm/voyager.h>
  28. #include <asm/vic.h>
  29. #include <asm/mtrr.h>
  30. #include <asm/msr.h>
  31. struct task_struct *voyager_thread;
  32. static __u8 set_timeout;
  33. static int
  34. execute(const char *string)
  35. {
  36. int ret;
  37. char *envp[] = {
  38. "HOME=/",
  39. "TERM=linux",
  40. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  41. NULL,
  42. };
  43. char *argv[] = {
  44. "/bin/bash",
  45. "-c",
  46. (char *)string,
  47. NULL,
  48. };
  49. if ((ret = call_usermodehelper(argv[0], argv, envp, 1)) != 0) {
  50. printk(KERN_ERR "Voyager failed to run \"%s\": %i\n",
  51. string, ret);
  52. }
  53. return ret;
  54. }
  55. static void
  56. check_from_kernel(void)
  57. {
  58. if(voyager_status.switch_off) {
  59. /* FIXME: This should be configureable via proc */
  60. execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1");
  61. } else if(voyager_status.power_fail) {
  62. VDEBUG(("Voyager daemon detected AC power failure\n"));
  63. /* FIXME: This should be configureable via proc */
  64. execute("umask 600; echo F > /etc/powerstatus; kill -PWR 1");
  65. set_timeout = 1;
  66. }
  67. }
  68. static void
  69. check_continuing_condition(void)
  70. {
  71. if(voyager_status.power_fail) {
  72. __u8 data;
  73. voyager_cat_psi(VOYAGER_PSI_SUBREAD,
  74. VOYAGER_PSI_AC_FAIL_REG, &data);
  75. if((data & 0x1f) == 0) {
  76. /* all power restored */
  77. printk(KERN_NOTICE "VOYAGER AC power restored, cancelling shutdown\n");
  78. /* FIXME: should be user configureable */
  79. execute("umask 600; echo O > /etc/powerstatus; kill -PWR 1");
  80. set_timeout = 0;
  81. }
  82. }
  83. }
  84. static int
  85. thread(void *unused)
  86. {
  87. printk(KERN_NOTICE "Voyager starting monitor thread\n");
  88. for (;;) {
  89. set_current_state(TASK_INTERRUPTIBLE);
  90. schedule_timeout(set_timeout ? HZ : MAX_SCHEDULE_TIMEOUT);
  91. VDEBUG(("Voyager Daemon awoken\n"));
  92. if(voyager_status.request_from_kernel == 0) {
  93. /* probably awoken from timeout */
  94. check_continuing_condition();
  95. } else {
  96. check_from_kernel();
  97. voyager_status.request_from_kernel = 0;
  98. }
  99. }
  100. }
  101. static int __init
  102. voyager_thread_start(void)
  103. {
  104. voyager_thread = kthread_run(thread, NULL, "kvoyagerd");
  105. if (IS_ERR(voyager_thread)) {
  106. printk(KERN_ERR "Voyager: Failed to create system monitor thread.\n");
  107. return PTR_ERR(voyager_thread);
  108. }
  109. return 0;
  110. }
  111. static void __exit
  112. voyager_thread_stop(void)
  113. {
  114. kthread_stop(voyager_thread);
  115. }
  116. module_init(voyager_thread_start);
  117. module_exit(voyager_thread_stop);