fs_procfskmm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /****************************************************************************
  2. * fs/procfs/fs_procfskmm.c
  3. *
  4. * Copyright (C) 2016-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/types.h>
  40. #include <sys/stat.h>
  41. #include <stdint.h>
  42. #include <stdbool.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <fcntl.h>
  47. #include <assert.h>
  48. #include <errno.h>
  49. #include <debug.h>
  50. #include <nuttx/kmalloc.h>
  51. #include <nuttx/mm/mm.h>
  52. #include <nuttx/fs/fs.h>
  53. #include <nuttx/fs/procfs.h>
  54. #if defined(CONFIG_MM_KERNEL_HEAP) && defined(CONFIG_FS_PROCFS) && \
  55. !defined(CONFIG_FS_PROCFS_EXCLUDE_KMM)
  56. /****************************************************************************
  57. * Pre-processor Definitions
  58. ****************************************************************************/
  59. /* Determines the size of an intermediate buffer that must be large enough
  60. * to handle the longest line generated by this logic.
  61. */
  62. #define KMM_LINELEN 54
  63. /****************************************************************************
  64. * Private Types
  65. ****************************************************************************/
  66. /* This structure describes one open "file" */
  67. struct kmm_file_s
  68. {
  69. struct procfs_file_s base; /* Base open file structure */
  70. unsigned int linesize; /* Number of valid characters in line[] */
  71. char line[KMM_LINELEN]; /* Pre-allocated buffer for formatted lines */
  72. };
  73. /****************************************************************************
  74. * Private Function Prototypes
  75. ****************************************************************************/
  76. /* File system methods */
  77. static int kmm_open(FAR struct file *filep, FAR const char *relpath,
  78. int oflags, mode_t mode);
  79. static int kmm_close(FAR struct file *filep);
  80. static ssize_t kmm_read(FAR struct file *filep, FAR char *buffer,
  81. size_t buflen);
  82. static int kmm_dup(FAR const struct file *oldp,
  83. FAR struct file *newp);
  84. static int kmm_stat(FAR const char *relpath, FAR struct stat *buf);
  85. /****************************************************************************
  86. * Private Data
  87. ****************************************************************************/
  88. /****************************************************************************
  89. * Public Data
  90. ****************************************************************************/
  91. /* See fs_mount.c -- this structure is explicitly externed there.
  92. * We use the old-fashioned kind of initializers so that this will compile
  93. * with any compiler.
  94. */
  95. const struct procfs_operations kmm_operations =
  96. {
  97. kmm_open, /* open */
  98. kmm_close, /* close */
  99. kmm_read, /* read */
  100. NULL, /* write */
  101. kmm_dup, /* dup */
  102. NULL, /* opendir */
  103. NULL, /* closedir */
  104. NULL, /* readdir */
  105. NULL, /* rewinddir */
  106. kmm_stat /* stat */
  107. };
  108. /****************************************************************************
  109. * Private Functions
  110. ****************************************************************************/
  111. /****************************************************************************
  112. * Name: kmm_open
  113. ****************************************************************************/
  114. static int kmm_open(FAR struct file *filep, FAR const char *relpath,
  115. int oflags, mode_t mode)
  116. {
  117. FAR struct kmm_file_s *procfile;
  118. finfo("Open '%s'\n", relpath);
  119. /* PROCFS is read-only. Any attempt to open with any kind of write
  120. * access is not permitted.
  121. *
  122. * REVISIT: Write-able proc files could be quite useful.
  123. */
  124. if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
  125. {
  126. ferr("ERROR: Only O_RDONLY supported\n");
  127. return -EACCES;
  128. }
  129. /* "kmm" is the only acceptable value for the relpath */
  130. if (strcmp(relpath, "kmm") != 0)
  131. {
  132. ferr("ERROR: relpath is '%s'\n", relpath);
  133. return -ENOENT;
  134. }
  135. /* Allocate a container to hold the file attributes */
  136. procfile = (FAR struct kmm_file_s *)kmm_zalloc(sizeof(struct kmm_file_s));
  137. if (!procfile)
  138. {
  139. ferr("ERROR: Failed to allocate file attributes\n");
  140. return -ENOMEM;
  141. }
  142. /* Save the attributes as the open-specific state in filep->f_priv */
  143. filep->f_priv = (FAR void *)procfile;
  144. return OK;
  145. }
  146. /****************************************************************************
  147. * Name: kmm_close
  148. ****************************************************************************/
  149. static int kmm_close(FAR struct file *filep)
  150. {
  151. FAR struct kmm_file_s *procfile;
  152. /* Recover our private data from the struct file instance */
  153. procfile = (FAR struct kmm_file_s *)filep->f_priv;
  154. DEBUGASSERT(procfile);
  155. /* Release the file attributes structure */
  156. kmm_free(procfile);
  157. filep->f_priv = NULL;
  158. return OK;
  159. }
  160. /****************************************************************************
  161. * Name: kmm_read
  162. ****************************************************************************/
  163. static ssize_t kmm_read(FAR struct file *filep, FAR char *buffer,
  164. size_t buflen)
  165. {
  166. FAR struct kmm_file_s *procfile;
  167. struct mallinfo mem;
  168. size_t linesize;
  169. size_t copysize;
  170. size_t totalsize;
  171. off_t offset;
  172. finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
  173. DEBUGASSERT(filep != NULL && buffer != NULL && buflen > 0);
  174. offset = filep->f_pos;
  175. /* Recover our private data from the struct file instance */
  176. procfile = (FAR struct kmm_file_s *)filep->f_priv;
  177. DEBUGASSERT(procfile);
  178. /* The first line is the headers */
  179. linesize = snprintf(procfile->line, KMM_LINELEN,
  180. " total used free largest\n");
  181. copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen,
  182. &offset);
  183. totalsize = copysize;
  184. if (totalsize < buflen)
  185. {
  186. buffer += copysize;
  187. buflen -= copysize;
  188. /* The second line is the memory data */
  189. #ifdef CONFIG_CAN_PASS_STRUCTS
  190. mem = kmm_mallinfo();
  191. #else
  192. (void)kmm_mallinfo(&mem);
  193. #endif
  194. linesize = snprintf(procfile->line, KMM_LINELEN,
  195. "Mem: %11d%11d%11d%11d\n",
  196. mem.arena, mem.uordblks, mem.fordblks,
  197. mem.mxordblk);
  198. copysize = procfs_memcpy(procfile->line, linesize, buffer, buflen,
  199. &offset);
  200. totalsize += copysize;
  201. }
  202. /* Update the file offset */
  203. filep->f_pos += totalsize;
  204. return totalsize;
  205. }
  206. /****************************************************************************
  207. * Name: kmm_dup
  208. *
  209. * Description:
  210. * Duplicate open file data in the new file structure.
  211. *
  212. ****************************************************************************/
  213. static int kmm_dup(FAR const struct file *oldp, FAR struct file *newp)
  214. {
  215. FAR struct kmm_file_s *oldattr;
  216. FAR struct kmm_file_s *newattr;
  217. finfo("Dup %p->%p\n", oldp, newp);
  218. /* Recover our private data from the old struct file instance */
  219. oldattr = (FAR struct kmm_file_s *)oldp->f_priv;
  220. DEBUGASSERT(oldattr);
  221. /* Allocate a new container to hold the task and attribute selection */
  222. newattr = (FAR struct kmm_file_s *)kmm_malloc(sizeof(struct kmm_file_s));
  223. if (!newattr)
  224. {
  225. ferr("ERROR: Failed to allocate file attributes\n");
  226. return -ENOMEM;
  227. }
  228. /* The copy the file attributes from the old attributes to the new */
  229. memcpy(newattr, oldattr, sizeof(struct kmm_file_s));
  230. /* Save the new attributes in the new file structure */
  231. newp->f_priv = (FAR void *)newattr;
  232. return OK;
  233. }
  234. /****************************************************************************
  235. * Name: kmm_stat
  236. *
  237. * Description: Return information about a file or directory
  238. *
  239. ****************************************************************************/
  240. static int kmm_stat(FAR const char *relpath, FAR struct stat *buf)
  241. {
  242. /* "kmm" is the only acceptable value for the relpath */
  243. if (strcmp(relpath, "kmm") != 0)
  244. {
  245. ferr("ERROR: relpath is '%s'\n", relpath);
  246. return -ENOENT;
  247. }
  248. /* "kmm" is the name for a read-only file */
  249. memset(buf, 0, sizeof(struct stat));
  250. buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
  251. return OK;
  252. }
  253. /****************************************************************************
  254. * Public Functions
  255. ****************************************************************************/
  256. #endif /* CONFIG_MM_KERNEL_HEAP && CONFIG_FS_PROCFS && !CONFIG_FS_PROCFS_EXCLUDE_KMM */