2013-12-14 17:34:08 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/procfs/fs_procfsuptime.c
|
|
|
|
*
|
2017-02-15 16:59:09 +01:00
|
|
|
* Copyright (C) 2013, 2017 Gregory Nutt. All rights reserved.
|
2013-12-14 17:34:08 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/clock.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/procfs.h>
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
|
2019-03-14 23:56:43 +01:00
|
|
|
#ifndef CONFIG_FS_PROCFS_EXCLUDE_UPTIME
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-12-01 20:01:16 +01:00
|
|
|
|
2014-07-02 16:54:53 +02:00
|
|
|
/* Determines the size of an intermediate buffer that must be large enough
|
2013-12-14 17:34:08 +01:00
|
|
|
* to handle the longest line generated by this logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define UPTIME_LINELEN 16
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure describes one open "file" */
|
|
|
|
|
|
|
|
struct uptime_file_s
|
|
|
|
{
|
|
|
|
struct procfs_file_s base; /* Base open file structure */
|
2013-12-15 14:16:26 +01:00
|
|
|
unsigned int linesize; /* Number of valid characters in line[] */
|
2013-12-14 17:34:08 +01:00
|
|
|
char line[UPTIME_LINELEN]; /* Pre-allocated buffer for formatted lines */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* File system methods */
|
|
|
|
|
|
|
|
static int uptime_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode);
|
|
|
|
static int uptime_close(FAR struct file *filep);
|
|
|
|
static ssize_t uptime_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
|
|
|
|
static int uptime_dup(FAR const struct file *oldp,
|
|
|
|
FAR struct file *newp);
|
|
|
|
|
|
|
|
static int uptime_stat(FAR const char *relpath, FAR struct stat *buf);
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2013-12-14 17:34:08 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See fs_mount.c -- this structure is explicitly externed there.
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct procfs_operations uptime_operations =
|
|
|
|
{
|
|
|
|
uptime_open, /* open */
|
|
|
|
uptime_close, /* close */
|
|
|
|
uptime_read, /* read */
|
2020-05-04 19:34:04 +02:00
|
|
|
NULL, /* write */
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
uptime_dup, /* dup */
|
|
|
|
|
|
|
|
NULL, /* opendir */
|
|
|
|
NULL, /* closedir */
|
|
|
|
NULL, /* readdir */
|
|
|
|
NULL, /* rewinddir */
|
|
|
|
|
|
|
|
uptime_stat /* stat */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uptime_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int uptime_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct uptime_file_s *attr;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Open '%s'\n", relpath);
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
/* PROCFS is read-only. Any attempt to open with any kind of write
|
|
|
|
* access is not permitted.
|
|
|
|
*
|
|
|
|
* REVISIT: Write-able proc files could be quite useful.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Only O_RDONLY supported\n");
|
2013-12-14 17:34:08 +01:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "uptime" is the only acceptable value for the relpath */
|
|
|
|
|
|
|
|
if (strcmp(relpath, "uptime") != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: relpath is '%s'\n", relpath);
|
2013-12-14 17:34:08 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a container to hold the file attributes */
|
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
attr = kmm_zalloc(sizeof(struct uptime_file_s));
|
2013-12-14 17:34:08 +01:00
|
|
|
if (!attr)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
2013-12-14 17:34:08 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the attributes as the open-specific state in filep->f_priv */
|
|
|
|
|
|
|
|
filep->f_priv = (FAR void *)attr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uptime_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int uptime_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct uptime_file_s *attr;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
attr = (FAR struct uptime_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(attr);
|
|
|
|
|
|
|
|
/* Release the file attributes structure */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(attr);
|
2013-12-14 17:34:08 +01:00
|
|
|
filep->f_priv = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uptime_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t uptime_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct uptime_file_s *attr;
|
|
|
|
size_t linesize;
|
|
|
|
off_t offset;
|
|
|
|
ssize_t ret;
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t ticktime;
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_HAVE_DOUBLE) && defined(CONFIG_LIBC_FLOATINGPOINT)
|
2013-12-14 22:25:23 +01:00
|
|
|
double now;
|
2013-12-14 17:34:08 +01:00
|
|
|
#else
|
2016-01-21 18:54:26 +01:00
|
|
|
# if defined(CONFIG_SYSTEM_TIME64)
|
|
|
|
uint64_t sec;
|
|
|
|
# else
|
|
|
|
uint32_t sec;
|
|
|
|
# endif
|
2013-12-14 22:25:23 +01:00
|
|
|
unsigned int remainder;
|
|
|
|
unsigned int csec;
|
2013-12-14 17:34:08 +01:00
|
|
|
#endif
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
attr = (FAR struct uptime_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(attr);
|
|
|
|
|
2013-12-14 22:25:23 +01:00
|
|
|
/* If f_pos is zero, then sample the system time. Otherwise, use
|
|
|
|
* the cached system time from the previous read(). It is necessary
|
|
|
|
* save the cached value in case, for example, the user is reading
|
|
|
|
* the time one byte at a time. In that case, the time must remain
|
|
|
|
* stable throughout the reads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (filep->f_pos == 0)
|
|
|
|
{
|
2016-01-21 18:54:26 +01:00
|
|
|
/* System time */
|
2013-12-14 22:25:23 +01:00
|
|
|
|
2020-05-04 16:15:10 +02:00
|
|
|
ticktime = clock_systime_ticks() - INITIAL_SYSTEM_TIMER_TICKS;
|
2013-12-14 22:25:23 +01:00
|
|
|
|
2013-12-14 17:34:08 +01:00
|
|
|
#if defined(CONFIG_HAVE_DOUBLE) && defined(CONFIG_LIBC_FLOATINGPOINT)
|
2020-05-04 19:34:04 +02:00
|
|
|
/* Convert the up time to a seconds + hundredths of seconds string */
|
2013-12-14 17:34:08 +01:00
|
|
|
|
2013-12-15 14:16:26 +01:00
|
|
|
now = (double)ticktime / (double)CLOCKS_PER_SEC;
|
|
|
|
linesize = snprintf(attr->line, UPTIME_LINELEN, "%10.2f\n", now);
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
#else
|
2013-12-15 14:16:26 +01:00
|
|
|
/* Convert the system up time to seconds + hundredths of seconds */
|
2013-12-14 22:25:23 +01:00
|
|
|
|
2013-12-15 14:16:26 +01:00
|
|
|
sec = ticktime / CLOCKS_PER_SEC;
|
|
|
|
remainder = (unsigned int)(ticktime % CLOCKS_PER_SEC);
|
|
|
|
csec = (100 * remainder + (CLOCKS_PER_SEC / 2)) / CLOCKS_PER_SEC;
|
2013-12-14 22:25:23 +01:00
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
/* Make sure that rounding did not force the hundredths of
|
|
|
|
* a second above 99
|
|
|
|
*/
|
2013-12-14 17:34:08 +01:00
|
|
|
|
2013-12-15 14:16:26 +01:00
|
|
|
if (csec > 99)
|
|
|
|
{
|
|
|
|
sec++;
|
|
|
|
csec -= 100;
|
|
|
|
}
|
2013-12-14 17:34:08 +01:00
|
|
|
|
2013-12-15 14:16:26 +01:00
|
|
|
/* Convert the seconds + hundredths of seconds to a string */
|
2013-12-14 17:34:08 +01:00
|
|
|
|
2015-06-05 05:51:07 +02:00
|
|
|
#ifdef CONFIG_SYSTEM_TIME64
|
2019-08-07 15:56:10 +02:00
|
|
|
linesize = snprintf(attr->line, UPTIME_LINELEN, "%7llu.%02u\n",
|
|
|
|
sec, csec);
|
2015-06-05 05:51:07 +02:00
|
|
|
#else
|
2019-08-07 15:56:10 +02:00
|
|
|
linesize = snprintf(attr->line, UPTIME_LINELEN, "%7lu.%02u\n",
|
|
|
|
(unsigned long)sec, csec);
|
2015-06-05 05:51:07 +02:00
|
|
|
#endif
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
#endif
|
2013-12-15 14:16:26 +01:00
|
|
|
/* Save the linesize in case we are re-entered with f_pos > 0 */
|
|
|
|
|
|
|
|
attr->linesize = linesize;
|
|
|
|
}
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
/* Transfer the system up time to user receive buffer */
|
|
|
|
|
|
|
|
offset = filep->f_pos;
|
2020-05-04 19:34:04 +02:00
|
|
|
ret = procfs_memcpy(attr->line, attr->linesize, buffer, buflen, &offset);
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
/* Update the file offset */
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
filep->f_pos += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uptime_dup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int uptime_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
|
|
|
FAR struct uptime_file_s *oldattr;
|
|
|
|
FAR struct uptime_file_s *newattr;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the old struct file instance */
|
|
|
|
|
|
|
|
oldattr = (FAR struct uptime_file_s *)oldp->f_priv;
|
|
|
|
DEBUGASSERT(oldattr);
|
|
|
|
|
|
|
|
/* Allocate a new container to hold the task and attribute selection */
|
|
|
|
|
2020-05-04 19:34:04 +02:00
|
|
|
newattr = kmm_malloc(sizeof(struct uptime_file_s));
|
2013-12-14 17:34:08 +01:00
|
|
|
if (!newattr)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
2013-12-14 17:34:08 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2013-12-14 22:25:23 +01:00
|
|
|
/* The copy the file attributes from the old attributes to the new */
|
2013-12-14 17:34:08 +01:00
|
|
|
|
2013-12-14 22:25:23 +01:00
|
|
|
memcpy(newattr, oldattr, sizeof(struct uptime_file_s));
|
2013-12-14 17:34:08 +01:00
|
|
|
|
|
|
|
/* Save the new attributes in the new file structure */
|
|
|
|
|
|
|
|
newp->f_priv = (FAR void *)newattr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uptime_stat
|
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
static int uptime_stat(FAR const char *relpath, FAR struct stat *buf)
|
2013-12-14 17:34:08 +01:00
|
|
|
{
|
|
|
|
/* "uptime" is the only acceptable value for the relpath */
|
|
|
|
|
|
|
|
if (strcmp(relpath, "uptime") != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: relpath is '%s'\n", relpath);
|
2013-12-14 17:34:08 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* "uptime" is the name for a read-only file */
|
|
|
|
|
2017-02-15 16:59:09 +01:00
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
|
2013-12-14 17:34:08 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#endif /* CONFIG_FS_PROCFS_EXCLUDE_PROCESS */
|
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */
|