voyager_thread.c 3.6 KB

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