devnull.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Implementation of the null device, "null:", which generates an
  31. * immediate EOF on read and throws away anything written to it.
  32. */
  33. #include <types.h>
  34. #include <kern/errno.h>
  35. #include <lib.h>
  36. #include <uio.h>
  37. #include <vfs.h>
  38. #include <device.h>
  39. /* For open() */
  40. static
  41. int
  42. nullopen(struct device *dev, int openflags)
  43. {
  44. (void)dev;
  45. (void)openflags;
  46. return 0;
  47. }
  48. /* For close() */
  49. static
  50. int
  51. nullclose(struct device *dev)
  52. {
  53. (void)dev;
  54. return 0;
  55. }
  56. /* For d_io() */
  57. static
  58. int
  59. nullio(struct device *dev, struct uio *uio)
  60. {
  61. /*
  62. * On write, discard everything without looking at it.
  63. * On read, do nothing, generating an immediate EOF.
  64. */
  65. (void)dev; // unused
  66. if (uio->uio_rw == UIO_WRITE) {
  67. uio->uio_resid = 0;
  68. }
  69. return 0;
  70. }
  71. /* For ioctl() */
  72. static
  73. int
  74. nullioctl(struct device *dev, int op, userptr_t data)
  75. {
  76. /*
  77. * No ioctls.
  78. */
  79. (void)dev;
  80. (void)op;
  81. (void)data;
  82. return EINVAL;
  83. }
  84. /*
  85. * Function to create and attach null:
  86. */
  87. void
  88. devnull_create(void)
  89. {
  90. int result;
  91. struct device *dev;
  92. dev = kmalloc(sizeof(*dev));
  93. if (dev==NULL) {
  94. panic("Could not add null device: out of memory\n");
  95. }
  96. dev->d_open = nullopen;
  97. dev->d_close = nullclose;
  98. dev->d_io = nullio;
  99. dev->d_ioctl = nullioctl;
  100. dev->d_blocks = 0;
  101. dev->d_blocksize = 1;
  102. dev->d_devnumber = 0; /* assigned by vfs_adddev */
  103. dev->d_data = NULL;
  104. result = vfs_adddev("null", dev, 0);
  105. if (result) {
  106. panic("Could not add null device: %s\n", strerror(result));
  107. }
  108. }