voyager_thread.c 3.0 KB

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