fs_procfsiobinfo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /****************************************************************************
  2. * fs/procfs/fs_procfsiobinfo.c
  3. *
  4. * Copyright (C) 2019 Gregory Nutt. All rights reserved.
  5. * Author: Anthony Merlino <anthony@vergeaero.com>
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <fcntl.h>
  48. #include <assert.h>
  49. #include <errno.h>
  50. #include <debug.h>
  51. #include <nuttx/kmalloc.h>
  52. #include <nuttx/mm/iob.h>
  53. #include <nuttx/fs/fs.h>
  54. #include <nuttx/fs/procfs.h>
  55. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
  56. defined(CONFIG_MM_IOB) && !defined(CONFIG_FS_PROCFS_EXCLUDE_IOBINFO)
  57. /****************************************************************************
  58. * Pre-processor Definitions
  59. ****************************************************************************/
  60. /* Determines the size of an intermediate buffer that must be large enough
  61. * to handle the longest line generated by this logic.
  62. */
  63. #define IOBINFO_LINELEN 80
  64. /****************************************************************************
  65. * Private Types
  66. ****************************************************************************/
  67. /* This structure describes one open "file" */
  68. struct iobinfo_file_s
  69. {
  70. struct procfs_file_s base; /* Base open file structure */
  71. unsigned int linesize; /* Number of valid characters in line[] */
  72. char line[IOBINFO_LINELEN]; /* Pre-allocated buffer for formatted lines */
  73. };
  74. /****************************************************************************
  75. * Private Function Prototypes
  76. ****************************************************************************/
  77. /* File system methods */
  78. static int iobinfo_open(FAR struct file *filep, FAR const char *relpath,
  79. int oflags, mode_t mode);
  80. static int iobinfo_close(FAR struct file *filep);
  81. static ssize_t iobinfo_read(FAR struct file *filep, FAR char *buffer,
  82. size_t buflen);
  83. static int iobinfo_dup(FAR const struct file *oldp,
  84. FAR struct file *newp);
  85. static int iobinfo_stat(FAR const char *relpath, FAR struct stat *buf);
  86. /****************************************************************************
  87. * Private Data
  88. ****************************************************************************/
  89. /* CAUTION: The order of these entries and the preprocessor logic must match
  90. * logic found in the enum iob_user_e declaration found in iob.h
  91. */
  92. static FAR const char *g_iob_user_names[] =
  93. {
  94. #ifdef CONFIG_SYSLOG_BUFFER
  95. "syslog",
  96. #endif
  97. #ifdef CONFIG_IOB_UNITTEST
  98. "unittest",
  99. #endif
  100. #ifdef CONFIG_NET_6LOWPAN
  101. "sixlowpan",
  102. #endif
  103. #ifdef CONFIG_NET_ICMP_SOCKET
  104. "icmp_sock",
  105. #endif
  106. #ifdef CONFIG_NET_ICMPv6_SOCKET
  107. "icmpv6_sock",
  108. #endif
  109. #ifdef CONFIG_NET_UDP
  110. "udp_sock",
  111. #endif
  112. #ifdef CONFIG_NET_TCP
  113. "tcp_sock",
  114. #endif
  115. #ifdef CONFIG_NET_IEEE802154
  116. "ieee802154_sock",
  117. #endif
  118. #ifdef CONFIG_NET_BLUETOOTH
  119. "bluetooth_sock",
  120. #endif
  121. #ifdef CONFIG_NET_UDP_READAHEAD
  122. "udp_readahead",
  123. #endif
  124. #ifdef CONFIG_NET_UDP_WRITE_BUFFERS
  125. "udp_writebuffer",
  126. #endif
  127. #ifdef CONFIG_NET_TCP_READAHEAD
  128. "tcp_readahead",
  129. #endif
  130. #ifdef CONFIG_NET_TCP_WRITE_BUFFERS
  131. "tcp_writebuffer",
  132. #endif
  133. #ifdef CONFIG_NET_IPFORWARD
  134. "ipforward",
  135. #endif
  136. #ifdef CONFIG_WIRELESS_IEEE802154
  137. "rad802154",
  138. #endif
  139. #ifdef CONFIG_IEEE802154_MAC
  140. "mac802154",
  141. #endif
  142. #ifdef CONFIG_IEEE802154_MACDEV
  143. "mac802154_macdev",
  144. #endif
  145. #ifdef CONFIG_IEEE802154_NETDEV
  146. "mac802154_netdev",
  147. #endif
  148. #ifdef CONFIG_WL_SPIRIT
  149. "packetradio",
  150. #endif
  151. #ifdef CONFIG_WIRELESS_BLUETOOTH
  152. "bluetooth",
  153. #endif
  154. "global",
  155. };
  156. /****************************************************************************
  157. * Public Data
  158. ****************************************************************************/
  159. /* See fs_mount.c -- this structure is explicitly externed there.
  160. * We use the old-fashioned kind of initializers so that this will compile
  161. * with any compiler.
  162. */
  163. const struct procfs_operations iobinfo_operations =
  164. {
  165. iobinfo_open, /* open */
  166. iobinfo_close, /* close */
  167. iobinfo_read, /* read */
  168. NULL, /* write */
  169. iobinfo_dup, /* dup */
  170. NULL, /* opendir */
  171. NULL, /* closedir */
  172. NULL, /* readdir */
  173. NULL, /* rewinddir */
  174. iobinfo_stat /* stat */
  175. };
  176. /****************************************************************************
  177. * Private Functions
  178. ****************************************************************************/
  179. /****************************************************************************
  180. * Name: iobinfo_open
  181. ****************************************************************************/
  182. static int iobinfo_open(FAR struct file *filep, FAR const char *relpath,
  183. int oflags, mode_t mode)
  184. {
  185. FAR struct iobinfo_file_s *procfile;
  186. finfo("Open '%s'\n", relpath);
  187. /* PROCFS is read-only. Any attempt to open with any kind of write
  188. * access is not permitted.
  189. *
  190. * REVISIT: Write-able proc files could be quite useful.
  191. */
  192. if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
  193. {
  194. ferr("ERROR: Only O_RDONLY supported\n");
  195. return -EACCES;
  196. }
  197. /* "iobinfo" is the only acceptable value for the relpath */
  198. if (strcmp(relpath, "iobinfo") != 0)
  199. {
  200. ferr("ERROR: relpath is '%s'\n", relpath);
  201. return -ENOENT;
  202. }
  203. /* Allocate a container to hold the file attributes */
  204. procfile = (FAR struct iobinfo_file_s *)
  205. kmm_zalloc(sizeof(struct iobinfo_file_s));
  206. if (!procfile)
  207. {
  208. ferr("ERROR: Failed to allocate file attributes\n");
  209. return -ENOMEM;
  210. }
  211. /* Save the attributes as the open-specific state in filep->f_priv */
  212. filep->f_priv = (FAR void *)procfile;
  213. return OK;
  214. }
  215. /****************************************************************************
  216. * Name: iobinfo_close
  217. ****************************************************************************/
  218. static int iobinfo_close(FAR struct file *filep)
  219. {
  220. FAR struct iobinfo_file_s *procfile;
  221. /* Recover our private data from the struct file instance */
  222. procfile = (FAR struct iobinfo_file_s *)filep->f_priv;
  223. DEBUGASSERT(procfile);
  224. /* Release the file attributes structure */
  225. kmm_free(procfile);
  226. filep->f_priv = NULL;
  227. return OK;
  228. }
  229. /****************************************************************************
  230. * Name: iobinfo_read
  231. ****************************************************************************/
  232. static ssize_t iobinfo_read(FAR struct file *filep, FAR char *buffer,
  233. size_t buflen)
  234. {
  235. FAR struct iobinfo_file_s *iobfile;
  236. FAR struct iob_userstats_s *userstats;
  237. size_t linesize;
  238. size_t copysize;
  239. size_t totalsize;
  240. off_t offset;
  241. int i;
  242. finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
  243. DEBUGASSERT(filep != NULL && buffer != NULL && buflen > 0);
  244. offset = filep->f_pos;
  245. /* Recover our private data from the struct file instance */
  246. iobfile = (FAR struct iobinfo_file_s *)filep->f_priv;
  247. DEBUGASSERT(iobfile);
  248. /* The first line is the headers */
  249. linesize = snprintf(iobfile->line, IOBINFO_LINELEN,
  250. " TOTAL TOTAL\n");
  251. copysize = procfs_memcpy(iobfile->line, linesize, buffer, buflen,
  252. &offset);
  253. totalsize = copysize;
  254. if (totalsize < buflen)
  255. {
  256. buffer += copysize;
  257. buflen -= copysize;
  258. linesize = snprintf(iobfile->line, IOBINFO_LINELEN,
  259. " USER CONSUMED PRODUCED\n");
  260. copysize = procfs_memcpy(iobfile->line, linesize, buffer, buflen,
  261. &offset);
  262. totalsize += copysize;
  263. }
  264. /* Loop through each IOB user printing the usage statistics */
  265. for (i = 0; i < IOBUSER_GLOBAL; i++)
  266. {
  267. if (totalsize < buflen)
  268. {
  269. buffer += copysize;
  270. buflen -= copysize;
  271. userstats = iob_getuserstats(i);
  272. linesize = snprintf(iobfile->line, IOBINFO_LINELEN,
  273. "%-16s%16lu%16lu\n",
  274. g_iob_user_names[i],
  275. (unsigned long)userstats->totalconsumed,
  276. (unsigned long)userstats->totalproduced);
  277. copysize = procfs_memcpy(iobfile->line, linesize, buffer, buflen,
  278. &offset);
  279. totalsize += copysize;
  280. }
  281. }
  282. if (totalsize < buflen)
  283. {
  284. buffer += copysize;
  285. buflen -= copysize;
  286. userstats = iob_getuserstats(IOBUSER_GLOBAL);
  287. linesize = snprintf(iobfile->line, IOBINFO_LINELEN,
  288. "\n%-16s%16lu%16lu\n",
  289. g_iob_user_names[IOBUSER_GLOBAL],
  290. (unsigned long)userstats->totalconsumed,
  291. (unsigned long)userstats->totalproduced);
  292. copysize = procfs_memcpy(iobfile->line, linesize, buffer, buflen,
  293. &offset);
  294. totalsize += copysize;
  295. }
  296. /* Update the file offset */
  297. filep->f_pos += totalsize;
  298. return totalsize;
  299. }
  300. /****************************************************************************
  301. * Name: iobinfo_dup
  302. *
  303. * Description:
  304. * Duplicate open file data in the new file structure.
  305. *
  306. ****************************************************************************/
  307. static int iobinfo_dup(FAR const struct file *oldp, FAR struct file *newp)
  308. {
  309. FAR struct iobinfo_file_s *oldattr;
  310. FAR struct iobinfo_file_s *newattr;
  311. finfo("Dup %p->%p\n", oldp, newp);
  312. /* Recover our private data from the old struct file instance */
  313. oldattr = (FAR struct iobinfo_file_s *)oldp->f_priv;
  314. DEBUGASSERT(oldattr);
  315. /* Allocate a new container to hold the task and attribute selection */
  316. newattr = (FAR struct iobinfo_file_s *)
  317. kmm_malloc(sizeof(struct iobinfo_file_s));
  318. if (!newattr)
  319. {
  320. ferr("ERROR: Failed to allocate file attributes\n");
  321. return -ENOMEM;
  322. }
  323. /* The copy the file attributes from the old attributes to the new */
  324. memcpy(newattr, oldattr, sizeof(struct iobinfo_file_s));
  325. /* Save the new attributes in the new file structure */
  326. newp->f_priv = (FAR void *)newattr;
  327. return OK;
  328. }
  329. /****************************************************************************
  330. * Name: iobinfo_stat
  331. *
  332. * Description: Return information about a file or directory
  333. *
  334. ****************************************************************************/
  335. static int iobinfo_stat(FAR const char *relpath, FAR struct stat *buf)
  336. {
  337. /* "iobinfo" is the only acceptable value for the relpath */
  338. if (strcmp(relpath, "iobinfo") != 0)
  339. {
  340. ferr("ERROR: relpath is '%s'\n", relpath);
  341. return -ENOENT;
  342. }
  343. /* "iobinfo" is the name for a read-only file */
  344. memset(buf, 0, sizeof(struct stat));
  345. buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
  346. return OK;
  347. }
  348. #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
  349. * CONFIG_MM_IOB && !CONFIG_FS_PROCFS_EXCLUDE_IOBINFO */