procfs.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /****************************************************************************
  2. * include/nuttx/fs/procfs.h
  3. *
  4. * Copyright (C) 2013 Gregory Nutt. All rights reserved.
  5. * Author: Ken Pettit <pettitkd@gmail.com>
  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. #ifndef __INCLUDE_NUTTX_FS_PROCFS_H
  36. #define __INCLUDE_NUTTX_FS_PROCFS_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <nuttx/fs/fs.h>
  42. /****************************************************************************
  43. * Pre-processor Definitions
  44. ****************************************************************************/
  45. /* Data entry declaration prototypes ****************************************/
  46. /* Procfs operations are a subset of the mountpt_operations */
  47. struct procfs_operations
  48. {
  49. /* The procfs open method differs from the driver open method
  50. * because it receives (1) the inode that contains the procfs
  51. * private data, (2) the relative path into the procfs, and (3)
  52. * information to manage privileges.
  53. */
  54. int (*open)(FAR struct file *filep, FAR const char *relpath,
  55. int oflags, mode_t mode);
  56. /* The following methods must be identical in signature and position because
  57. * the struct file_operations and struct mountp_operations are treated like
  58. * unions.
  59. */
  60. int (*close)(FAR struct file *filep);
  61. ssize_t (*read)(FAR struct file *filep, FAR char *buffer, size_t buflen);
  62. ssize_t (*write)(FAR struct file *filep, FAR const char *buffer, size_t buflen);
  63. /* The two structures need not be common after this point. The following
  64. * are extended methods needed to deal with the unique needs of mounted
  65. * file systems.
  66. *
  67. * Additional open-file-specific procfs operations:
  68. */
  69. int (*dup)(FAR const struct file *oldp, FAR struct file *newp);
  70. /* Directory operations */
  71. int (*opendir)(FAR const char *relpath, FAR struct fs_dirent_s *dir);
  72. int (*closedir)(FAR struct fs_dirent_s *dir);
  73. int (*readdir)(FAR struct fs_dirent_s *dir);
  74. int (*rewinddir)(FAR struct fs_dirent_s *dir);
  75. /* Operations on paths */
  76. int (*stat)(FAR const char *relpath, FAR struct stat *buf);
  77. };
  78. /* Procfs handler prototypes ************************************************/
  79. /* These are the types of entries that may appear in the procfs: */
  80. enum procfs_entry_e
  81. {
  82. PROCFS_UNKOWN_TYPE = 0, /* Unknown type */
  83. PROCFS_FILE_TYPE, /* File type */
  84. PROCFS_DIR_TYPE, /* Directory type */
  85. };
  86. /* This is a procfs entry that each handler should provide to supply
  87. * specific operations for file and directory handling.
  88. */
  89. struct procfs_entry_s
  90. {
  91. FAR const char *pathpattern;
  92. FAR const struct procfs_operations *ops;
  93. uint8_t type;
  94. };
  95. /* Specifies the common elements for an open file in the procfs
  96. * file system. This structure should be sub-classed by handlers
  97. * to add their own specific data elements to the context.
  98. */
  99. struct procfs_file_s
  100. {
  101. FAR const struct procfs_entry_s *procfsentry;
  102. };
  103. /* The generic proc/ pseudo directory structure */
  104. struct procfs_dir_priv_s
  105. {
  106. uint8_t level; /* Directory level. Currently 0 or 1 */
  107. uint16_t index; /* Index to the next directory entry */
  108. uint16_t nentries; /* Number of directory entries */
  109. FAR const struct procfs_entry_s *procfsentry; /* Pointer to procfs handler entry */
  110. };
  111. /****************************************************************************
  112. * Public Function Prototypes
  113. ****************************************************************************/
  114. #ifdef __cplusplus
  115. #define EXTERN extern "C"
  116. extern "C"
  117. {
  118. #else
  119. #define EXTERN extern
  120. #endif
  121. /****************************************************************************
  122. * Name: procfs_memcpy
  123. *
  124. * Description:
  125. * procfs/ file data may be read by the user with different user buffer
  126. * sizes to receive the data. If the amount of data to be returned is
  127. * large or if the callers receive buffer is small, then multiple read
  128. * operations will be required.
  129. *
  130. * If multiple read operations are required, then each read operation will
  131. * be identical accept that file position (f_pos) will be incremented with
  132. * each read: f_pos must be incremented by the read method after each
  133. * read operation to provide the 'offset' for the next read.
  134. *
  135. * procfs_memcpy() is a helper function. Each read() method should
  136. * provide data in a local data buffer ('src' and 'srclen'). This
  137. * will transfer the data to the user receive buffer ('dest' and 'destlen'),
  138. * respecting both (1) the size of the destination buffer so that it will
  139. * write beyond the user receiver and (1) the file position, 'offset'.
  140. *
  141. * This function will skip over data until the under of bytes specified
  142. * by 'offset' have been skipped. Then it will transfer data from the
  143. * the procfs/ 'src' buffer into the user receive buffer. No more than
  144. * 'destlen' bytes will be transferred.
  145. *
  146. * Input Parameters:
  147. * src - The address of the intermediate procfs/ buffer containing the
  148. * data to be returned.
  149. * srclen - The number of bytes of data in the 'src' buffer
  150. * dest - The address of the user's receive buffer.
  151. * destlen - The size (in bytes) of the user's receive buffer.
  152. * offset - On input, this is the number of bytes to skip before returning
  153. * data; If bytes were skipped, this offset will be decremented.
  154. * Data will not be transferred until this offset decrements to
  155. * zero.
  156. *
  157. * Returned Value:
  158. * The number of bytes actually transferred into the user's receive buffer.
  159. *
  160. ****************************************************************************/
  161. size_t procfs_memcpy(FAR const char *src, size_t srclen,
  162. FAR char *dest, size_t destlen,
  163. off_t *offset);
  164. /****************************************************************************
  165. * Name: procfs_register
  166. *
  167. * Description:
  168. * Add a new entry to the procfs file system.
  169. *
  170. * NOTE: This function should be called *prior* to mounting the procfs
  171. * file system to prevent concurrency problems with the modification of
  172. * the procfs data set while it is in use.
  173. *
  174. * Input Parameters:
  175. * entry - Describes the entry to be registered.
  176. *
  177. * Returned Value:
  178. * Zero (OK) on success; a negated errno value on failure
  179. *
  180. ****************************************************************************/
  181. #ifdef CONFIG_FS_PROCFS_REGISTER
  182. int procfs_register(FAR const struct procfs_entry_s *entry);
  183. #endif
  184. #undef EXTERN
  185. #ifdef __cplusplus
  186. }
  187. #endif
  188. #endif /* __INCLUDE_NUTTX_FS_PROCFS_H */