From 82e0b0328bec0b281cfd2b89dd1a8e123b90950a Mon Sep 17 00:00:00 2001 From: iuhaitao Date: Thu, 8 Nov 2018 10:13:45 -0600 Subject: [PATCH] fs/procfs: Add /proc/version support to get version info --- fs/procfs/Kconfig | 4 + fs/procfs/Make.defs | 7 +- fs/procfs/fs_procfs.c | 5 + fs/procfs/fs_procfsversion.c | 323 +++++++++++++++++++++++++++++++++++ 4 files changed, 338 insertions(+), 1 deletion(-) create mode 100644 fs/procfs/fs_procfsversion.c diff --git a/fs/procfs/Kconfig b/fs/procfs/Kconfig index f4cc974551..784725ad3b 100644 --- a/fs/procfs/Kconfig +++ b/fs/procfs/Kconfig @@ -78,6 +78,10 @@ config FS_PROCFS_EXCLUDE_UPTIME bool "Exclude uptime" default n +config FS_PROCFS_EXCLUDE_VERSION + bool "Exclude version" + default n + config FS_PROCFS_EXCLUDE_CPULOAD bool "Exclude CPU load" default n diff --git a/fs/procfs/Make.defs b/fs/procfs/Make.defs index a9382de318..c322f215ff 100644 --- a/fs/procfs/Make.defs +++ b/fs/procfs/Make.defs @@ -38,11 +38,16 @@ ifeq ($(CONFIG_FS_PROCFS),y) ASRCS += CSRCS += fs_procfs.c fs_procfsutil.c fs_procfsproc.c fs_procfsuptime.c -CSRCS += fs_procfscpuload.c fs_procfsmeminfo.c +CSRCS += fs_procfscpuload.c fs_procfsmeminfo.c fs_procfsversion.c # Include procfs build support DEPPATH += --dep-path procfs VPATH += :procfs +# To ensure version information is newest, +# add fs_procfsversion to phony target for force rebuild + +.PHONY: fs_procfsversion$(OBJEXT) + endif diff --git a/fs/procfs/fs_procfs.c b/fs/procfs/fs_procfs.c index 931684ba50..de73dfba99 100644 --- a/fs/procfs/fs_procfs.c +++ b/fs/procfs/fs_procfs.c @@ -82,6 +82,7 @@ extern const struct procfs_operations cpuload_operations; extern const struct procfs_operations meminfo_operations; extern const struct procfs_operations module_operations; extern const struct procfs_operations uptime_operations; +extern const struct procfs_operations version_operations; /* This is not good. These are implemented in other sub-systems. Having to * deal with them here is not a good coupling. What is really needed is a @@ -173,6 +174,10 @@ static const struct procfs_entry_s g_procfs_entries[] = #if !defined(CONFIG_FS_PROCFS_EXCLUDE_UPTIME) { "uptime", &uptime_operations, PROCFS_FILE_TYPE }, #endif + +#if !defined(CONFIG_FS_PROCFS_EXCLUDE_VERSION) + { "version", &version_operations, PROCFS_FILE_TYPE }, +#endif }; #ifdef CONFIG_FS_PROCFS_REGISTER diff --git a/fs/procfs/fs_procfsversion.c b/fs/procfs/fs_procfsversion.c new file mode 100644 index 0000000000..dd901c936b --- /dev/null +++ b/fs/procfs/fs_procfsversion.c @@ -0,0 +1,323 @@ +/**************************************************************************** + * fs/procfs/fs_procfsversion.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) +#ifndef CONFIG_FS_PROCFS_EXCLUDE_PROCESS + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Determines the size of an intermediate buffer that must be large enough + * to handle the longest line generated by this logic. + */ + +#define VERSION_LINELEN 128 + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* This structure describes one open "file" */ + +struct version_file_s +{ + struct procfs_file_s base; /* Base open file structure */ + unsigned int linesize; /* Number of valid characters in line[] */ + char line[VERSION_LINELEN]; /* Pre-allocated buffer for formatted lines */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* File system methods */ + +static int version_open(FAR struct file *filep, FAR const char *relpath, + int oflags, mode_t mode); +static int version_close(FAR struct file *filep); +static ssize_t version_read(FAR struct file *filep, FAR char *buffer, + size_t buflen); + +static int version_dup(FAR const struct file *oldp, + FAR struct file *newp); + +static int version_stat(FAR const char *relpath, FAR struct stat *buf); + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* 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 version_operations = +{ + version_open, /* open */ + version_close, /* close */ + version_read, /* read */ + NULL, /* write */ + + version_dup, /* dup */ + + NULL, /* opendir */ + NULL, /* closedir */ + NULL, /* readdir */ + NULL, /* rewinddir */ + + version_stat /* stat */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: version_open + ****************************************************************************/ + +static int version_open(FAR struct file *filep, FAR const char *relpath, + int oflags, mode_t mode) +{ + FAR struct version_file_s *attr; + + finfo("Open '%s'\n", relpath); + + /* 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) + { + ferr("ERROR: Only O_RDONLY supported\n"); + return -EACCES; + } + + /* "version" is the only acceptable value for the relpath */ + + if (strcmp(relpath, "version") != 0) + { + ferr("ERROR: relpath is '%s'\n", relpath); + return -ENOENT; + } + + /* Allocate a container to hold the file attributes */ + + attr = (FAR struct version_file_s *) + kmm_zalloc(sizeof(struct version_file_s)); + + if (attr == NULL) + { + ferr("ERROR: Failed to allocate file attributes\n"); + return -ENOMEM; + } + + /* Save the attributes as the open-specific state in filep->f_priv */ + + filep->f_priv = (FAR void *)attr; + return OK; +} + +/**************************************************************************** + * Name: version_close + ****************************************************************************/ + +static int version_close(FAR struct file *filep) +{ + FAR struct version_file_s *attr; + + /* Recover our private data from the struct file instance */ + + attr = (FAR struct version_file_s *)filep->f_priv; + DEBUGASSERT(attr); + + /* Release the file attributes structure */ + + kmm_free(attr); + filep->f_priv = NULL; + return OK; +} + +/**************************************************************************** + * Name: version_read + ****************************************************************************/ + +static ssize_t version_read(FAR struct file *filep, FAR char *buffer, + size_t buflen) +{ + FAR struct version_file_s *attr; + size_t linesize; + off_t offset; + ssize_t ret; + + finfo("buffer=%p buflen=%d\n", buffer, (int)buflen); + + /* Recover our private data from the struct file instance */ + + attr = (FAR struct version_file_s *)filep->f_priv; + DEBUGASSERT(attr); + + if (filep->f_pos == 0) + { +#if defined(__DATE__) && defined(__TIME__) + linesize = snprintf(attr->line, VERSION_LINELEN, + "NuttX version %s %s %s %s\n", + CONFIG_VERSION_STRING, CONFIG_VERSION_BUILD, + __DATE__, __TIME__); +#else + linesize = snprintf(attr->line, VERSION_LINELEN, + "NuttX version %s %s\n", + CONFIG_VERSION_STRING, CONFIG_VERSION_BUILD); +#endif + + /* Save the linesize in case we are re-entered with f_pos > 0 */ + + attr->linesize = linesize; + } + + offset = filep->f_pos; + ret = procfs_memcpy(attr->line, attr->linesize, buffer, buflen, &offset); + + /* Update the file offset */ + + if (ret > 0) + { + filep->f_pos += ret; + } + + return ret; +} + +/**************************************************************************** + * Name: version_dup + * + * Description: + * Duplicate open file data in the new file structure. + * + ****************************************************************************/ + +static int version_dup(FAR const struct file *oldp, FAR struct file *newp) +{ + FAR struct version_file_s *oldattr; + FAR struct version_file_s *newattr; + + finfo("Dup %p->%p\n", oldp, newp); + + /* Recover our private data from the old struct file instance */ + + oldattr = (FAR struct version_file_s *)oldp->f_priv; + DEBUGASSERT(oldattr); + + /* Allocate a new container to hold the task and attribute selection */ + + newattr = (FAR struct version_file_s *) + kmm_malloc(sizeof(struct version_file_s)); + + if (!newattr) + { + ferr("ERROR: Failed to allocate file attributes\n"); + return -ENOMEM; + } + + /* The copy the file attributes from the old attributes to the new */ + + memcpy(newattr, oldattr, sizeof(struct version_file_s)); + + /* Save the new attributes in the new file structure */ + + newp->f_priv = (FAR void *)newattr; + return OK; +} + +/**************************************************************************** + * Name: version_stat + * + * Description: Return information about a file or directory + * + ****************************************************************************/ + +static int version_stat(FAR const char *relpath, FAR struct stat *buf) +{ + /* "version" is the only acceptable value for the relpath */ + + if (strcmp(relpath, "version") != 0) + { + ferr("ERROR: relpath is '%s'\n", relpath); + return -ENOENT; + } + + /* "version" is the name for a read-only file */ + + memset(buf, 0, sizeof(struct stat)); + buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR; + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#endif /* CONFIG_FS_PROCFS_EXCLUDE_PROCESS */ +#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */ +