harddog_kern.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* UML hardware watchdog, shamelessly stolen from:
  2. *
  3. * SoftDog 0.05: A Software Watchdog Device
  4. *
  5. * (c) Copyright 1996 Alan Cox <[email protected]>, 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 <[email protected]>
  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 <[email protected]> :
  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 ([email protected]>
  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/types.h>
  38. #include <linux/kernel.h>
  39. #include <linux/fs.h>
  40. #include <linux/mm.h>
  41. #include <linux/miscdevice.h>
  42. #include <linux/watchdog.h>
  43. #include <linux/reboot.h>
  44. #include <linux/mutex.h>
  45. #include <linux/init.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/uaccess.h>
  48. #include "mconsole.h"
  49. #include "harddog.h"
  50. MODULE_LICENSE("GPL");
  51. static DEFINE_MUTEX(harddog_mutex);
  52. static DEFINE_SPINLOCK(lock);
  53. static int timer_alive;
  54. static int harddog_in_fd = -1;
  55. static int harddog_out_fd = -1;
  56. /*
  57. * Allow only one person to hold it open
  58. */
  59. static int harddog_open(struct inode *inode, struct file *file)
  60. {
  61. int err = -EBUSY;
  62. char *sock = NULL;
  63. mutex_lock(&harddog_mutex);
  64. spin_lock(&lock);
  65. if(timer_alive)
  66. goto err;
  67. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  68. __module_get(THIS_MODULE);
  69. #endif
  70. #ifdef CONFIG_MCONSOLE
  71. sock = mconsole_notify_socket();
  72. #endif
  73. err = start_watchdog(&harddog_in_fd, &harddog_out_fd, sock);
  74. if(err)
  75. goto err;
  76. timer_alive = 1;
  77. spin_unlock(&lock);
  78. mutex_unlock(&harddog_mutex);
  79. return stream_open(inode, file);
  80. err:
  81. spin_unlock(&lock);
  82. mutex_unlock(&harddog_mutex);
  83. return err;
  84. }
  85. static int harddog_release(struct inode *inode, struct file *file)
  86. {
  87. /*
  88. * Shut off the timer.
  89. */
  90. spin_lock(&lock);
  91. stop_watchdog(harddog_in_fd, harddog_out_fd);
  92. harddog_in_fd = -1;
  93. harddog_out_fd = -1;
  94. timer_alive=0;
  95. spin_unlock(&lock);
  96. return 0;
  97. }
  98. static ssize_t harddog_write(struct file *file, const char __user *data, size_t len,
  99. loff_t *ppos)
  100. {
  101. /*
  102. * Refresh the timer.
  103. */
  104. if(len)
  105. return ping_watchdog(harddog_out_fd);
  106. return 0;
  107. }
  108. static int harddog_ioctl_unlocked(struct file *file,
  109. unsigned int cmd, unsigned long arg)
  110. {
  111. void __user *argp= (void __user *)arg;
  112. static struct watchdog_info ident = {
  113. WDIOC_SETTIMEOUT,
  114. 0,
  115. "UML Hardware Watchdog"
  116. };
  117. switch (cmd) {
  118. default:
  119. return -ENOTTY;
  120. case WDIOC_GETSUPPORT:
  121. if(copy_to_user(argp, &ident, sizeof(ident)))
  122. return -EFAULT;
  123. return 0;
  124. case WDIOC_GETSTATUS:
  125. case WDIOC_GETBOOTSTATUS:
  126. return put_user(0,(int __user *)argp);
  127. case WDIOC_KEEPALIVE:
  128. return ping_watchdog(harddog_out_fd);
  129. }
  130. }
  131. static long harddog_ioctl(struct file *file,
  132. unsigned int cmd, unsigned long arg)
  133. {
  134. long ret;
  135. mutex_lock(&harddog_mutex);
  136. ret = harddog_ioctl_unlocked(file, cmd, arg);
  137. mutex_unlock(&harddog_mutex);
  138. return ret;
  139. }
  140. static const struct file_operations harddog_fops = {
  141. .owner = THIS_MODULE,
  142. .write = harddog_write,
  143. .unlocked_ioctl = harddog_ioctl,
  144. .compat_ioctl = compat_ptr_ioctl,
  145. .open = harddog_open,
  146. .release = harddog_release,
  147. .llseek = no_llseek,
  148. };
  149. static struct miscdevice harddog_miscdev = {
  150. .minor = WATCHDOG_MINOR,
  151. .name = "watchdog",
  152. .fops = &harddog_fops,
  153. };
  154. module_misc_device(harddog_miscdev);