mod_procfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /****************************************************************************
  2. * sched/module/mod_procfs.c
  3. *
  4. * Copyright (C) 2015, 2017 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/stat.h>
  40. #include <stdio.h>
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include <string.h>
  44. #include <fcntl.h>
  45. #include <assert.h>
  46. #include <errno.h>
  47. #include <debug.h>
  48. #include <nuttx/kmalloc.h>
  49. #include <nuttx/module.h>
  50. #include <nuttx/lib/modlib.h>
  51. #include <nuttx/fs/fs.h>
  52. #include <nuttx/fs/procfs.h>
  53. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
  54. !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
  55. /****************************************************************************
  56. * Pre-processor Definitions
  57. ****************************************************************************/
  58. /* Determines the size of an intermediate buffer that must be large enough
  59. * to handle the longest line generated by this logic.
  60. */
  61. #define MOD_LINELEN 64
  62. /****************************************************************************
  63. * Private Types
  64. ****************************************************************************/
  65. /* This structure describes one open "file" */
  66. struct modprocfs_file_s
  67. {
  68. struct procfs_file_s base; /* Base open file structure */
  69. /* Read helpers */
  70. FAR char *buffer; /* User buffer pointer */
  71. size_t buflen; /* Size of the user buffer */
  72. size_t remaining; /* Space remaining in user buffer */
  73. size_t totalsize; /* Total size returned by read() */
  74. off_t offset; /* Offset skip on output */
  75. char line[MOD_LINELEN]; /* Buffer for line formatting */
  76. };
  77. /****************************************************************************
  78. * Private Function Prototypes
  79. ****************************************************************************/
  80. /* File system methods */
  81. static int modprocfs_open(FAR struct file *filep, FAR const char *relpath,
  82. int oflags, mode_t mode);
  83. static int modprocfs_close(FAR struct file *filep);
  84. static ssize_t modprocfs_read(FAR struct file *filep, FAR char *buffer,
  85. size_t buflen);
  86. static int modprocfs_dup(FAR const struct file *oldp,
  87. FAR struct file *newp);
  88. static int modprocfs_stat(FAR const char *relpath, FAR struct stat *buf);
  89. /****************************************************************************
  90. * Public Data
  91. ****************************************************************************/
  92. /* See include/nutts/fs/procfs.h
  93. * We use the old-fashioned kind of initializers so that this will compile
  94. * with any compiler.
  95. */
  96. const struct procfs_operations module_operations =
  97. {
  98. modprocfs_open, /* open */
  99. modprocfs_close, /* close */
  100. modprocfs_read, /* read */
  101. NULL, /* write */
  102. modprocfs_dup, /* dup */
  103. NULL, /* opendir */
  104. NULL, /* closedir */
  105. NULL, /* readdir */
  106. NULL, /* rewinddir */
  107. modprocfs_stat /* stat */
  108. };
  109. /****************************************************************************
  110. * Private Functions
  111. ****************************************************************************/
  112. /****************************************************************************
  113. * Name: modprocfs_callback
  114. ****************************************************************************/
  115. static int modprocfs_callback(FAR struct module_s *modp, FAR void *arg)
  116. {
  117. FAR struct modprocfs_file_s *priv;
  118. size_t linesize;
  119. size_t copysize;
  120. DEBUGASSERT(modp != NULL && arg != NULL);
  121. priv = (FAR struct modprocfs_file_s *)arg;
  122. linesize = snprintf(priv->line, MOD_LINELEN, "%s,%p,%p,%p,%u,%p,%lu,%p,%lu\n",
  123. modp->modname, modp->initializer,
  124. modp->modinfo.uninitializer, modp->modinfo.arg,
  125. modp->modinfo.nexports, modp->alloc,
  126. (unsigned long)modp->textsize,
  127. (FAR uint8_t *)modp->alloc + modp->textsize,
  128. (unsigned long)modp->datasize);
  129. copysize = procfs_memcpy(priv->line, linesize, priv->buffer,
  130. priv->remaining, &priv->offset);
  131. priv->totalsize += copysize;
  132. priv->buffer += copysize;
  133. priv->remaining -= copysize;
  134. return (priv->totalsize >= priv->buflen) ? 1 : 0;
  135. }
  136. /****************************************************************************
  137. * Name: modprocfs_open
  138. ****************************************************************************/
  139. static int modprocfs_open(FAR struct file *filep, FAR const char *relpath,
  140. int oflags, mode_t mode)
  141. {
  142. FAR struct modprocfs_file_s *priv;
  143. finfo("Open '%s'\n", relpath);
  144. /* PROCFS is read-only. Any attempt to open with any kind of write
  145. * access is not permitted.
  146. *
  147. * REVISIT: Write-able proc files could be quite useful.
  148. */
  149. if (((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0))
  150. {
  151. ferr("ERROR: Only O_RDONLY supported\n");
  152. return -EACCES;
  153. }
  154. /* Allocate the open file structure */
  155. priv = (FAR struct modprocfs_file_s *)kmm_zalloc(sizeof(struct modprocfs_file_s));
  156. if (!priv)
  157. {
  158. ferr("ERROR: Failed to allocate file attributes\n");
  159. return -ENOMEM;
  160. }
  161. /* Save the open file structure as the open-specific state in
  162. * filep->f_priv.
  163. */
  164. filep->f_priv = (FAR void *)priv;
  165. return OK;
  166. }
  167. /****************************************************************************
  168. * Name: modprocfs_close
  169. ****************************************************************************/
  170. static int modprocfs_close(FAR struct file *filep)
  171. {
  172. FAR struct modprocfs_file_s *priv;
  173. /* Recover our private data from the struct file instance */
  174. priv = (FAR struct modprocfs_file_s *)filep->f_priv;
  175. DEBUGASSERT(priv);
  176. /* Release the file attributes structure */
  177. kmm_free(priv);
  178. filep->f_priv = NULL;
  179. return OK;
  180. }
  181. /****************************************************************************
  182. * Name: modprocfs_read
  183. ****************************************************************************/
  184. static ssize_t modprocfs_read(FAR struct file *filep, FAR char *buffer,
  185. size_t buflen)
  186. {
  187. FAR struct modprocfs_file_s *priv;
  188. int ret;
  189. finfo("buffer=%p buflen=%lu\n", buffer, (unsigned long)buflen);
  190. /* Recover our private data from the struct file instance */
  191. priv = (FAR struct modprocfs_file_s *)filep->f_priv;
  192. DEBUGASSERT(priv);
  193. /* Traverse all installed modules */
  194. priv->remaining = buflen;
  195. priv->totalsize = 0;
  196. priv->buffer = buffer;
  197. priv->buflen = buflen;
  198. priv->offset = filep->f_pos;
  199. ret = modlib_registry_foreach(modprocfs_callback, priv);
  200. if (ret >= 0)
  201. {
  202. filep->f_pos += priv->totalsize;
  203. return priv->totalsize;
  204. }
  205. return ret;
  206. }
  207. /****************************************************************************
  208. * Name: modprocfs_dup
  209. *
  210. * Description:
  211. * Duplicate open file data in the new file structure.
  212. *
  213. ****************************************************************************/
  214. static int modprocfs_dup(FAR const struct file *oldp, FAR struct file *newp)
  215. {
  216. FAR struct modprocfs_file_s *oldpriv;
  217. FAR struct modprocfs_file_s *newpriv;
  218. finfo("Dup %p->%p\n", oldp, newp);
  219. /* Recover our private data from the old struct file instance */
  220. oldpriv = (FAR struct modprocfs_file_s *)oldp->f_priv;
  221. DEBUGASSERT(oldpriv);
  222. /* Allocate a new container to hold the task and attribute selection */
  223. newpriv = (FAR struct modprocfs_file_s *)kmm_zalloc(sizeof(struct modprocfs_file_s));
  224. if (!newpriv)
  225. {
  226. ferr("ERROR: Failed to allocate file attributes\n");
  227. return -ENOMEM;
  228. }
  229. /* The copy the file attributes from the old attributes to the new */
  230. memcpy(newpriv, oldpriv, sizeof(struct modprocfs_file_s));
  231. /* Save the new attributes in the new file structure */
  232. newp->f_priv = (FAR void *)newpriv;
  233. return OK;
  234. }
  235. /****************************************************************************
  236. * Name: modprocfs_stat
  237. *
  238. * Description: Return information about a file or directory
  239. *
  240. ****************************************************************************/
  241. static int modprocfs_stat(FAR const char *relpath, FAR struct stat *buf)
  242. {
  243. memset(buf, 0, sizeof(struct stat));
  244. buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
  245. return OK;
  246. }
  247. /****************************************************************************
  248. * Public Functions
  249. ****************************************************************************/
  250. #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
  251. * !CONFIG_FS_PROCFS_EXCLUDE_MODULE */