fs_procfsuptime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /****************************************************************************
  2. * fs/procfs/fs_procfsuptime.c
  3. *
  4. * Copyright (C) 2013, 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 <time.h>
  46. #include <string.h>
  47. #include <fcntl.h>
  48. #include <assert.h>
  49. #include <errno.h>
  50. #include <debug.h>
  51. #include <nuttx/clock.h>
  52. #include <nuttx/kmalloc.h>
  53. #include <nuttx/fs/fs.h>
  54. #include <nuttx/fs/procfs.h>
  55. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
  56. #ifndef CONFIG_FS_PROCFS_EXCLUDE_UPTIME
  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 UPTIME_LINELEN 16
  64. /****************************************************************************
  65. * Private Types
  66. ****************************************************************************/
  67. /* This structure describes one open "file" */
  68. struct uptime_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[UPTIME_LINELEN]; /* Pre-allocated buffer for formatted lines */
  73. };
  74. /****************************************************************************
  75. * Private Function Prototypes
  76. ****************************************************************************/
  77. /* File system methods */
  78. static int uptime_open(FAR struct file *filep, FAR const char *relpath,
  79. int oflags, mode_t mode);
  80. static int uptime_close(FAR struct file *filep);
  81. static ssize_t uptime_read(FAR struct file *filep, FAR char *buffer,
  82. size_t buflen);
  83. static int uptime_dup(FAR const struct file *oldp,
  84. FAR struct file *newp);
  85. static int uptime_stat(FAR const char *relpath, FAR struct stat *buf);
  86. /****************************************************************************
  87. * Public Data
  88. ****************************************************************************/
  89. /* See fs_mount.c -- this structure is explicitly externed there.
  90. * We use the old-fashioned kind of initializers so that this will compile
  91. * with any compiler.
  92. */
  93. const struct procfs_operations uptime_operations =
  94. {
  95. uptime_open, /* open */
  96. uptime_close, /* close */
  97. uptime_read, /* read */
  98. NULL, /* write */
  99. uptime_dup, /* dup */
  100. NULL, /* opendir */
  101. NULL, /* closedir */
  102. NULL, /* readdir */
  103. NULL, /* rewinddir */
  104. uptime_stat /* stat */
  105. };
  106. /****************************************************************************
  107. * Private Functions
  108. ****************************************************************************/
  109. /****************************************************************************
  110. * Name: uptime_open
  111. ****************************************************************************/
  112. static int uptime_open(FAR struct file *filep, FAR const char *relpath,
  113. int oflags, mode_t mode)
  114. {
  115. FAR struct uptime_file_s *attr;
  116. finfo("Open '%s'\n", relpath);
  117. /* PROCFS is read-only. Any attempt to open with any kind of write
  118. * access is not permitted.
  119. *
  120. * REVISIT: Write-able proc files could be quite useful.
  121. */
  122. if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
  123. {
  124. ferr("ERROR: Only O_RDONLY supported\n");
  125. return -EACCES;
  126. }
  127. /* "uptime" is the only acceptable value for the relpath */
  128. if (strcmp(relpath, "uptime") != 0)
  129. {
  130. ferr("ERROR: relpath is '%s'\n", relpath);
  131. return -ENOENT;
  132. }
  133. /* Allocate a container to hold the file attributes */
  134. attr = kmm_zalloc(sizeof(struct uptime_file_s));
  135. if (!attr)
  136. {
  137. ferr("ERROR: Failed to allocate file attributes\n");
  138. return -ENOMEM;
  139. }
  140. /* Save the attributes as the open-specific state in filep->f_priv */
  141. filep->f_priv = (FAR void *)attr;
  142. return OK;
  143. }
  144. /****************************************************************************
  145. * Name: uptime_close
  146. ****************************************************************************/
  147. static int uptime_close(FAR struct file *filep)
  148. {
  149. FAR struct uptime_file_s *attr;
  150. /* Recover our private data from the struct file instance */
  151. attr = (FAR struct uptime_file_s *)filep->f_priv;
  152. DEBUGASSERT(attr);
  153. /* Release the file attributes structure */
  154. kmm_free(attr);
  155. filep->f_priv = NULL;
  156. return OK;
  157. }
  158. /****************************************************************************
  159. * Name: uptime_read
  160. ****************************************************************************/
  161. static ssize_t uptime_read(FAR struct file *filep, FAR char *buffer,
  162. size_t buflen)
  163. {
  164. FAR struct uptime_file_s *attr;
  165. size_t linesize;
  166. off_t offset;
  167. ssize_t ret;
  168. clock_t ticktime;
  169. #if defined(CONFIG_HAVE_DOUBLE) && defined(CONFIG_LIBC_FLOATINGPOINT)
  170. double now;
  171. #else
  172. # if defined(CONFIG_SYSTEM_TIME64)
  173. uint64_t sec;
  174. # else
  175. uint32_t sec;
  176. # endif
  177. unsigned int remainder;
  178. unsigned int csec;
  179. #endif
  180. finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
  181. /* Recover our private data from the struct file instance */
  182. attr = (FAR struct uptime_file_s *)filep->f_priv;
  183. DEBUGASSERT(attr);
  184. /* If f_pos is zero, then sample the system time. Otherwise, use
  185. * the cached system time from the previous read(). It is necessary
  186. * save the cached value in case, for example, the user is reading
  187. * the time one byte at a time. In that case, the time must remain
  188. * stable throughout the reads.
  189. */
  190. if (filep->f_pos == 0)
  191. {
  192. /* System time */
  193. ticktime = clock_systime_ticks() - INITIAL_SYSTEM_TIMER_TICKS;
  194. #if defined(CONFIG_HAVE_DOUBLE) && defined(CONFIG_LIBC_FLOATINGPOINT)
  195. /* Convert the up time to a seconds + hundredths of seconds string */
  196. now = (double)ticktime / (double)CLOCKS_PER_SEC;
  197. linesize = snprintf(attr->line, UPTIME_LINELEN, "%10.2f\n", now);
  198. #else
  199. /* Convert the system up time to seconds + hundredths of seconds */
  200. sec = ticktime / CLOCKS_PER_SEC;
  201. remainder = (unsigned int)(ticktime % CLOCKS_PER_SEC);
  202. csec = (100 * remainder + (CLOCKS_PER_SEC / 2)) / CLOCKS_PER_SEC;
  203. /* Make sure that rounding did not force the hundredths of
  204. * a second above 99
  205. */
  206. if (csec > 99)
  207. {
  208. sec++;
  209. csec -= 100;
  210. }
  211. /* Convert the seconds + hundredths of seconds to a string */
  212. #ifdef CONFIG_SYSTEM_TIME64
  213. linesize = snprintf(attr->line, UPTIME_LINELEN, "%7llu.%02u\n",
  214. sec, csec);
  215. #else
  216. linesize = snprintf(attr->line, UPTIME_LINELEN, "%7lu.%02u\n",
  217. (unsigned long)sec, csec);
  218. #endif
  219. #endif
  220. /* Save the linesize in case we are re-entered with f_pos > 0 */
  221. attr->linesize = linesize;
  222. }
  223. /* Transfer the system up time to user receive buffer */
  224. offset = filep->f_pos;
  225. ret = procfs_memcpy(attr->line, attr->linesize, buffer, buflen, &offset);
  226. /* Update the file offset */
  227. if (ret > 0)
  228. {
  229. filep->f_pos += ret;
  230. }
  231. return ret;
  232. }
  233. /****************************************************************************
  234. * Name: uptime_dup
  235. *
  236. * Description:
  237. * Duplicate open file data in the new file structure.
  238. *
  239. ****************************************************************************/
  240. static int uptime_dup(FAR const struct file *oldp, FAR struct file *newp)
  241. {
  242. FAR struct uptime_file_s *oldattr;
  243. FAR struct uptime_file_s *newattr;
  244. finfo("Dup %p->%p\n", oldp, newp);
  245. /* Recover our private data from the old struct file instance */
  246. oldattr = (FAR struct uptime_file_s *)oldp->f_priv;
  247. DEBUGASSERT(oldattr);
  248. /* Allocate a new container to hold the task and attribute selection */
  249. newattr = kmm_malloc(sizeof(struct uptime_file_s));
  250. if (!newattr)
  251. {
  252. ferr("ERROR: Failed to allocate file attributes\n");
  253. return -ENOMEM;
  254. }
  255. /* The copy the file attributes from the old attributes to the new */
  256. memcpy(newattr, oldattr, sizeof(struct uptime_file_s));
  257. /* Save the new attributes in the new file structure */
  258. newp->f_priv = (FAR void *)newattr;
  259. return OK;
  260. }
  261. /****************************************************************************
  262. * Name: uptime_stat
  263. *
  264. * Description: Return information about a file or directory
  265. *
  266. ****************************************************************************/
  267. static int uptime_stat(FAR const char *relpath, FAR struct stat *buf)
  268. {
  269. /* "uptime" is the only acceptable value for the relpath */
  270. if (strcmp(relpath, "uptime") != 0)
  271. {
  272. ferr("ERROR: relpath is '%s'\n", relpath);
  273. return -ENOENT;
  274. }
  275. /* "uptime" is the name for a read-only file */
  276. memset(buf, 0, sizeof(struct stat));
  277. buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
  278. return OK;
  279. }
  280. /****************************************************************************
  281. * Public Functions
  282. ****************************************************************************/
  283. #endif /* CONFIG_FS_PROCFS_EXCLUDE_PROCESS */
  284. #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */