voyager_thread.c 3.5 KB

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