voyager_thread.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <asm/desc.h>
  26. #include <asm/voyager.h>
  27. #include <asm/vic.h>
  28. #include <asm/mtrr.h>
  29. #include <asm/msr.h>
  30. #define THREAD_NAME "kvoyagerd"
  31. /* external variables */
  32. int kvoyagerd_running = 0;
  33. DECLARE_MUTEX_LOCKED(kvoyagerd_sem);
  34. static int thread(void *);
  35. static __u8 set_timeout = 0;
  36. /* Start the machine monitor thread. Return 1 if OK, 0 if fail */
  37. static int __init
  38. voyager_thread_start(void)
  39. {
  40. if(kernel_thread(thread, NULL, CLONE_KERNEL) < 0) {
  41. /* This is serious, but not fatal */
  42. printk(KERN_ERR "Voyager: Failed to create system monitor thread!!!\n");
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. static int
  48. execute(const char *string)
  49. {
  50. int ret;
  51. char *envp[] = {
  52. "HOME=/",
  53. "TERM=linux",
  54. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  55. NULL,
  56. };
  57. char *argv[] = {
  58. "/bin/bash",
  59. "-c",
  60. (char *)string,
  61. NULL,
  62. };
  63. if ((ret = call_usermodehelper(argv[0], argv, envp, 1)) != 0) {
  64. printk(KERN_ERR "Voyager failed to run \"%s\": %i\n",
  65. string, ret);
  66. }
  67. return ret;
  68. }
  69. static void
  70. check_from_kernel(void)
  71. {
  72. if(voyager_status.switch_off) {
  73. /* FIXME: This should be configureable via proc */
  74. execute("umask 600; echo 0 > /etc/initrunlvl; kill -HUP 1");
  75. } else if(voyager_status.power_fail) {
  76. VDEBUG(("Voyager daemon detected AC power failure\n"));
  77. /* FIXME: This should be configureable via proc */
  78. execute("umask 600; echo F > /etc/powerstatus; kill -PWR 1");
  79. set_timeout = 1;
  80. }
  81. }
  82. static void
  83. check_continuing_condition(void)
  84. {
  85. if(voyager_status.power_fail) {
  86. __u8 data;
  87. voyager_cat_psi(VOYAGER_PSI_SUBREAD,
  88. VOYAGER_PSI_AC_FAIL_REG, &data);
  89. if((data & 0x1f) == 0) {
  90. /* all power restored */
  91. printk(KERN_NOTICE "VOYAGER AC power restored, cancelling shutdown\n");
  92. /* FIXME: should be user configureable */
  93. execute("umask 600; echo O > /etc/powerstatus; kill -PWR 1");
  94. set_timeout = 0;
  95. }
  96. }
  97. }
  98. static void
  99. wakeup(unsigned long unused)
  100. {
  101. up(&kvoyagerd_sem);
  102. }
  103. static int
  104. thread(void *unused)
  105. {
  106. struct timer_list wakeup_timer;
  107. kvoyagerd_running = 1;
  108. daemonize(THREAD_NAME);
  109. set_timeout = 0;
  110. init_timer(&wakeup_timer);
  111. sigfillset(&current->blocked);
  112. printk(KERN_NOTICE "Voyager starting monitor thread\n");
  113. for(;;) {
  114. down_interruptible(&kvoyagerd_sem);
  115. VDEBUG(("Voyager Daemon awoken\n"));
  116. if(voyager_status.request_from_kernel == 0) {
  117. /* probably awoken from timeout */
  118. check_continuing_condition();
  119. } else {
  120. check_from_kernel();
  121. voyager_status.request_from_kernel = 0;
  122. }
  123. if(set_timeout) {
  124. del_timer(&wakeup_timer);
  125. wakeup_timer.expires = HZ + jiffies;
  126. wakeup_timer.function = wakeup;
  127. add_timer(&wakeup_timer);
  128. }
  129. }
  130. }
  131. static void __exit
  132. voyager_thread_stop(void)
  133. {
  134. /* FIXME: do nothing at the moment */
  135. }
  136. module_init(voyager_thread_start);
  137. //module_exit(voyager_thread_stop);