harddog_kern.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* UML hardware watchdog, shamelessly stolen from:
  2. *
  3. * SoftDog 0.05: A Software Watchdog Device
  4. *
  5. * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
  6. * http://www.redhat.com
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  14. * warranty for any of this software. This material is provided
  15. * "AS-IS" and at no charge.
  16. *
  17. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  18. *
  19. * Software only watchdog driver. Unlike its big brother the WDT501P
  20. * driver this won't always recover a failed machine.
  21. *
  22. * 03/96: Angelo Haritsis <ah@doc.ic.ac.uk> :
  23. * Modularised.
  24. * Added soft_margin; use upon insmod to change the timer delay.
  25. * NB: uses same minor as wdt (WATCHDOG_MINOR); we could use separate
  26. * minors.
  27. *
  28. * 19980911 Alan Cox
  29. * Made SMP safe for 2.3.x
  30. *
  31. * 20011127 Joel Becker (jlbec@evilplan.org>
  32. * Added soft_noboot; Allows testing the softdog trigger without
  33. * requiring a recompile.
  34. * Added WDIOC_GETTIMEOUT and WDIOC_SETTIMOUT.
  35. */
  36. #include <linux/module.h>
  37. #include <linux/config.h>
  38. #include <linux/types.h>
  39. #include <linux/kernel.h>
  40. #include <linux/fs.h>
  41. #include <linux/mm.h>
  42. #include <linux/miscdevice.h>
  43. #include <linux/watchdog.h>
  44. #include <linux/reboot.h>
  45. #include <linux/smp_lock.h>
  46. #include <linux/init.h>
  47. #include <asm/uaccess.h>
  48. #include "helper.h"
  49. #include "mconsole.h"
  50. MODULE_LICENSE("GPL");
  51. /* Locked by the BKL in harddog_open and harddog_release */
  52. static int timer_alive;
  53. static int harddog_in_fd = -1;
  54. static int harddog_out_fd = -1;
  55. /*
  56. * Allow only one person to hold it open
  57. */
  58. extern int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock);
  59. static int harddog_open(struct inode *inode, struct file *file)
  60. {
  61. int err;
  62. char *sock = NULL;
  63. lock_kernel();
  64. if(timer_alive)
  65. return -EBUSY;
  66. #ifdef CONFIG_HARDDOG_NOWAYOUT
  67. __module_get(THIS_MODULE);
  68. #endif
  69. #ifdef CONFIG_MCONSOLE
  70. sock = mconsole_notify_socket();
  71. #endif
  72. err = start_watchdog(&harddog_in_fd, &harddog_out_fd, sock);
  73. if(err) return(err);
  74. timer_alive = 1;
  75. unlock_kernel();
  76. return nonseekable_open(inode, file);
  77. }
  78. extern void stop_watchdog(int in_fd, int out_fd);
  79. static int harddog_release(struct inode *inode, struct file *file)
  80. {
  81. /*
  82. * Shut off the timer.
  83. */
  84. lock_kernel();
  85. stop_watchdog(harddog_in_fd, harddog_out_fd);
  86. harddog_in_fd = -1;
  87. harddog_out_fd = -1;
  88. timer_alive=0;
  89. unlock_kernel();
  90. return 0;
  91. }
  92. extern int ping_watchdog(int fd);
  93. static ssize_t harddog_write(struct file *file, const char *data, size_t len,
  94. loff_t *ppos)
  95. {
  96. /*
  97. * Refresh the timer.
  98. */
  99. if(len)
  100. return(ping_watchdog(harddog_out_fd));
  101. return 0;
  102. }
  103. static int harddog_ioctl(struct inode *inode, struct file *file,
  104. unsigned int cmd, unsigned long arg)
  105. {
  106. static struct watchdog_info ident = {
  107. WDIOC_SETTIMEOUT,
  108. 0,
  109. "UML Hardware Watchdog"
  110. };
  111. switch (cmd) {
  112. default:
  113. return -ENOTTY;
  114. case WDIOC_GETSUPPORT:
  115. if(copy_to_user((struct harddog_info *)arg, &ident,
  116. sizeof(ident)))
  117. return -EFAULT;
  118. return 0;
  119. case WDIOC_GETSTATUS:
  120. case WDIOC_GETBOOTSTATUS:
  121. return put_user(0,(int *)arg);
  122. case WDIOC_KEEPALIVE:
  123. return(ping_watchdog(harddog_out_fd));
  124. }
  125. }
  126. static struct file_operations harddog_fops = {
  127. .owner = THIS_MODULE,
  128. .write = harddog_write,
  129. .ioctl = harddog_ioctl,
  130. .open = harddog_open,
  131. .release = harddog_release,
  132. };
  133. static struct miscdevice harddog_miscdev = {
  134. .minor = WATCHDOG_MINOR,
  135. .name = "watchdog",
  136. .fops = &harddog_fops,
  137. };
  138. static char banner[] __initdata = KERN_INFO "UML Watchdog Timer\n";
  139. static int __init harddog_init(void)
  140. {
  141. int ret;
  142. ret = misc_register(&harddog_miscdev);
  143. if (ret)
  144. return ret;
  145. printk(banner);
  146. return(0);
  147. }
  148. static void __exit harddog_exit(void)
  149. {
  150. misc_deregister(&harddog_miscdev);
  151. }
  152. module_init(harddog_init);
  153. module_exit(harddog_exit);
  154. /*
  155. * Overrides for Emacs so that we follow Linus's tabbing style.
  156. * Emacs will notice this stuff at the end of the file and automatically
  157. * adjust the settings for this buffer only. This must remain at the end
  158. * of the file.
  159. * ---------------------------------------------------------------------------
  160. * Local variables:
  161. * c-file-style: "linux"
  162. * End:
  163. */