dev_null.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /****************************************************************************
  2. * drivers/dev_null.c
  3. *
  4. * Copyright (C) 2007, 2008, 2013, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdbool.h>
  41. #include <string.h>
  42. #include <poll.h>
  43. #include <errno.h>
  44. #include <nuttx/semaphore.h>
  45. #include <nuttx/fs/fs.h>
  46. #include <nuttx/drivers/drivers.h>
  47. /****************************************************************************
  48. * Private Function Prototypes
  49. ****************************************************************************/
  50. static ssize_t devnull_read(FAR struct file *filep, FAR char *buffer,
  51. size_t buflen);
  52. static ssize_t devnull_write(FAR struct file *filep, FAR const char *buffer,
  53. size_t buflen);
  54. static int devnull_poll(FAR struct file *filep, FAR struct pollfd *fds,
  55. bool setup);
  56. /****************************************************************************
  57. * Private Data
  58. ****************************************************************************/
  59. static const struct file_operations devnull_fops =
  60. {
  61. NULL, /* open */
  62. NULL, /* close */
  63. devnull_read, /* read */
  64. devnull_write, /* write */
  65. NULL, /* seek */
  66. NULL, /* ioctl */
  67. devnull_poll /* poll */
  68. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  69. , NULL /* unlink */
  70. #endif
  71. };
  72. /****************************************************************************
  73. * Private Functions
  74. ****************************************************************************/
  75. /****************************************************************************
  76. * Name: devnull_read
  77. ****************************************************************************/
  78. static ssize_t devnull_read(FAR struct file *filep, FAR char *buffer,
  79. size_t len)
  80. {
  81. return 0; /* Return EOF */
  82. }
  83. /****************************************************************************
  84. * Name: devnull_write
  85. ****************************************************************************/
  86. static ssize_t devnull_write(FAR struct file *filep, FAR const char *buffer,
  87. size_t len)
  88. {
  89. return len; /* Say that everything was written */
  90. }
  91. /****************************************************************************
  92. * Name: devnull_poll
  93. ****************************************************************************/
  94. static int devnull_poll(FAR struct file *filep, FAR struct pollfd *fds,
  95. bool setup)
  96. {
  97. if (setup)
  98. {
  99. fds->revents |= (fds->events & (POLLIN | POLLOUT));
  100. if (fds->revents != 0)
  101. {
  102. nxsem_post(fds->sem);
  103. }
  104. }
  105. return OK;
  106. }
  107. /****************************************************************************
  108. * Public Functions
  109. ****************************************************************************/
  110. /****************************************************************************
  111. * Name: devnull_register
  112. *
  113. * Description:
  114. * Register /dev/null
  115. *
  116. ****************************************************************************/
  117. void devnull_register(void)
  118. {
  119. (void)register_driver("/dev/null", &devnull_fops, 0666, NULL);
  120. }