net/: Move net_procfs.c to net/procfs/. Partition logic a little better to make space to support net device procfs as well

This commit is contained in:
Gregory Nutt 2015-11-27 13:53:33 -06:00
parent e4236941c6
commit 9d8462aa23
8 changed files with 650 additions and 407 deletions

2
TODO
View File

@ -444,7 +444,7 @@ o Kernel/Protected Build
ps sched_foreach()
ifup netdev_foreach()
ifdown netdev_foreach()
ifconfig netdev_foreach(), g_netstats
ifconfig netdev_foreach()
ping icmp_ping()
The busybox mkfatfs does not involve any OS calls; it does

View File

@ -275,6 +275,7 @@ source "net/igmp/Kconfig"
source "net/arp/Kconfig"
source "net/loopback/Kconfig"
source "net/iob/Kconfig"
source "net/procfs/Kconfig"
source "net/utils/Kconfig"
config NET_STATISTICS

View File

@ -42,12 +42,6 @@ ifeq ($(CONFIG_NET),y)
NET_ASRCS =
NET_CSRCS = net_initialize.c
ifeq ($(CONFIG_FS_PROCFS),y)
ifneq ($(CONFIG_FS_PROCFS_EXCLUDE_NET),y)
NET_CSRCS += net_procfs.c
endif
endif
# Socket support
SOCK_ASRCS =
@ -76,6 +70,7 @@ include udp/Make.defs
include devif/Make.defs
include loopback/Make.defs
include route/Make.defs
include procfs/Make.defs
include utils/Make.defs
endif

4
net/procfs/Kconfig Normal file
View File

@ -0,0 +1,4 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

57
net/procfs/Make.defs Normal file
View File

@ -0,0 +1,57 @@
############################################################################
# net/procfs/Make.defs
#
# Copyright (C) 2015 Gregory Nutt. All rights reserved.
# 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.
#
############################################################################
# Network procfs support
ifneq ($(CONFIG_DISABLE_MOUNTPOINT),y)
ifeq ($(CONFIG_FS_PROCFS),y)
ifneq ($(CONFIG_FS_PROCFS_EXCLUDE_NET),y)
NET_CSRCS += net_procfs.c
# General network statistics
ifeq ($(CONFIG_NET_STATISTICS),y)
NET_CSRCS += net_statistics.c
endif
# Include packet socket build support
DEPPATH += --dep-path procfs
VPATH += :procfs
endif # CONFIG_FS_PROCFS_EXCLUDE_NET
endif # CONFIG_FS_PROCFS
endif # CONFIG_DISABLE_MOUNTPOINT

427
net/procfs/net_procfs.c Normal file
View File

@ -0,0 +1,427 @@
/****************************************************************************
* net/procfs/net_procfs.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* 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/statfs.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h>
#include <nuttx/fs/dirent.h>
#include "procfs/procfs.h"
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
!defined(CONFIG_FS_PROCFS_EXCLUDE_NET)
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* File system methods */
static int netprocfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode);
static int netprocfs_close(FAR struct file *filep);
static ssize_t netprocfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static int netprocfs_dup(FAR const struct file *oldp,
FAR struct file *newp);
static int netprocfs_opendir(FAR const char *relpath,
FAR struct fs_dirent_s *dir);
static int netprocfs_closedir(FAR struct fs_dirent_s *dir);
static int netprocfs_readdir(FAR struct fs_dirent_s *dir);
static int netprocfs_rewinddir(FAR struct fs_dirent_s *dir);
static int netprocfs_stat(FAR const char *relpath, FAR struct stat *buf);
/****************************************************************************
* Public Data
****************************************************************************/
/* See include/nutts/fs/procfs.h
* We use the old-fashioned kind of initializers so that this will compile
* with any compiler.
*/
const struct procfs_operations net_procfsoperations =
{
netprocfs_open, /* open */
netprocfs_close, /* close */
netprocfs_read, /* read */
NULL, /* write */
netprocfs_dup, /* dup */
netprocfs_opendir, /* opendir */
netprocfs_closedir, /* closedir */
netprocfs_readdir, /* readdir */
netprocfs_rewinddir, /* rewinddir */
netprocfs_stat /* stat */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: netprocfs_open
****************************************************************************/
static int netprocfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct netprocfs_file_s *priv;
fvdbg("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) &&
(net_procfsoperations.write == NULL))
{
fdbg("ERROR: Only O_RDONLY supported\n");
return -EACCES;
}
/* "net/stat" is the only acceptable value for the relpath */
if (strcmp(relpath, "net/stat") != 0)
{
fdbg("ERROR: relpath is '%s'\n", relpath);
return -ENOENT;
}
/* Allocate a container to hold the task and attribute selection */
priv = (FAR struct netprocfs_file_s *)kmm_zalloc(sizeof(struct netprocfs_file_s));
if (!priv)
{
fdbg("ERROR: Failed to allocate file attributes\n");
return -ENOMEM;
}
/* Save the index as the open-specific state in filep->f_priv */
filep->f_priv = (FAR void *)priv;
return OK;
}
/****************************************************************************
* Name: netprocfs_close
****************************************************************************/
static int netprocfs_close(FAR struct file *filep)
{
FAR struct netprocfs_file_s *priv;
/* Recover our private data from the struct file instance */
priv = (FAR struct netprocfs_file_s *)filep->f_priv;
DEBUGASSERT(priv);
/* Release the file attributes structure */
kmm_free(priv);
filep->f_priv = NULL;
return OK;
}
/****************************************************************************
* Name: netprocfs_read
****************************************************************************/
static ssize_t netprocfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
#ifdef CONFIG_NET_STATISTICS
FAR struct netprocfs_file_s *priv;
ssize_t nreturned;
fvdbg("buffer=%p buflen=%lu\n", buffer, (unsigned long)buflen);
/* Recover our private data from the struct file instance */
priv = (FAR struct netprocfs_file_s *)filep->f_priv;
DEBUGASSERT(priv);
/* Read the network layer statistics */
nreturned = net_readstats(priv, buffer, buflen);
/* Update the file offset */
if (nreturned > 0)
{
filep->f_pos += nreturned;
}
return nreturned;
#else
return 0;
#endif
}
/****************************************************************************
* Name: netprocfs_dup
*
* Description:
* Duplicate open file data in the new file structure.
*
****************************************************************************/
static int netprocfs_dup(FAR const struct file *oldp, FAR struct file *newp)
{
FAR struct netprocfs_file_s *oldpriv;
FAR struct netprocfs_file_s *newpriv;
fvdbg("Dup %p->%p\n", oldp, newp);
/* Recover our private data from the old struct file instance */
oldpriv = (FAR struct netprocfs_file_s *)oldp->f_priv;
DEBUGASSERT(oldpriv);
/* Allocate a new container to hold the task and attribute selection */
newpriv = (FAR struct netprocfs_file_s *)kmm_zalloc(sizeof(struct netprocfs_file_s));
if (!newpriv)
{
fdbg("ERROR: Failed to allocate file attributes\n");
return -ENOMEM;
}
/* The copy the file attribtes from the old attributes to the new */
memcpy(newpriv, oldpriv, sizeof(struct netprocfs_file_s));
/* Save the new attributes in the new file structure */
newp->f_priv = (FAR void *)newpriv;
return OK;
}
/****************************************************************************
* Name: netprocfs_opendir
*
* Description:
* Open a directory for read access
*
****************************************************************************/
static int netprocfs_opendir(FAR const char *relpath,
FAR struct fs_dirent_s *dir)
{
FAR struct netprocfs_level1_s *level1;
fvdbg("relpath: \"%s\"\n", relpath ? relpath : "NULL");
DEBUGASSERT(relpath && dir && !dir->u.procfs);
/* The path refers to the 1st level sbdirectory. Allocate the level1
* dirent structure.
*/
level1 = (FAR struct netprocfs_level1_s *)
kmm_zalloc(sizeof(struct netprocfs_level1_s));
if (!level1)
{
fdbg("ERROR: Failed to allocate the level1 directory structure\n");
return -ENOMEM;
}
/* Initialze base structure components */
level1->base.level = 1;
level1->base.nentries = 1;
level1->base.index = 0;
dir->u.procfs = (FAR void *) level1;
return OK;
}
/****************************************************************************
* Name: netprocfs_closedir
*
* Description: Close the directory listing
*
****************************************************************************/
static int netprocfs_closedir(FAR struct fs_dirent_s *dir)
{
FAR struct netprocfs_level1_s *priv;
DEBUGASSERT(dir && dir->u.procfs);
priv = dir->u.procfs;
if (priv)
{
kmm_free(priv);
}
dir->u.procfs = NULL;
return OK;
}
/****************************************************************************
* Name: netprocfs_readdir
*
* Description: Read the next directory entry
*
****************************************************************************/
static int netprocfs_readdir(FAR struct fs_dirent_s *dir)
{
FAR struct netprocfs_level1_s *level1;
int index;
int ret;
DEBUGASSERT(dir && dir->u.procfs);
level1 = dir->u.procfs;
/* Have we reached the end of the directory */
index = level1->base.index;
if (index >= level1->base.nentries)
{
/* We signal the end of the directory by returning the special
* error -ENOENT
*/
fvdbg("Entry %d: End of directory\n", index);
ret = -ENOENT;
}
/* Currently only one element in the directory */
else
{
DEBUGASSERT(level1->base.level == 1);
DEBUGASSERT(level1->base.index <= 1);
/* Copy the one supported directory entry */
dir->fd_dir.d_type = DTYPE_FILE;
strncpy(dir->fd_dir.d_name, "stat", NAME_MAX + 1);
/* Set up the next directory entry offset. NOTE that we could use the
* standard f_pos instead of our own private index.
*/
level1->base.index = index + 1;
ret = OK;
}
return ret;
}
/****************************************************************************
* Name: netprocfs_rewindir
*
* Description: Reset directory read to the first entry
*
****************************************************************************/
static int netprocfs_rewinddir(FAR struct fs_dirent_s *dir)
{
FAR struct netprocfs_level1_s *priv;
DEBUGASSERT(dir && dir->u.procfs);
priv = dir->u.procfs;
priv->base.index = 0;
return OK;
}
/****************************************************************************
* Name: netprocfs_stat
*
* Description: Return information about a file or directory
*
****************************************************************************/
static int netprocfs_stat(FAR const char *relpath, FAR struct stat *buf)
{
/* "net" and "net/stat" are the only acceptable values for the relpath */
if (strcmp(relpath, "net") == 0)
{
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
}
else if (strcmp(relpath, "net/stat") == 0)
{
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
}
else
{
fdbg("ERROR: relpath is '%s'\n", relpath);
return -ENOENT;
}
/* File/directory size, access block size */
buf->st_size = 0;
buf->st_blksize = 512;
buf->st_blocks = 0;
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
* !CONFIG_FS_PROCFS_EXCLUDE_NET */

View File

@ -1,5 +1,5 @@
/****************************************************************************
* net/net_procfs.c
* net/procfs/net_statisticsc
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
@ -40,76 +40,22 @@
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/statfs.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/sched.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h>
#include <nuttx/fs/dirent.h>
#include <nuttx/net/netstats.h>
#include <arch/irq.h>
#include "procfs/procfs.h"
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
/****************************************************************************
* 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 NET_LINELEN 64
/****************************************************************************
* Private Types
****************************************************************************/
/* This enumeration identifies all of the thread attributes that can be
* accessed via the procfs file system.
*/
/* This structure describes one open "file" */
struct netprocfs_file_s
{
struct procfs_file_s base; /* Base open file structure */
uint8_t lineno; /* Line number */
uint8_t linesize; /* Number of valid characters in line[] */
uint8_t offset; /* Offset to first valid character in line[] */
char line[NET_LINELEN]; /* Pre-allocated buffer for formatted lines */
};
/* Level 1 is the directory of attributes */
struct netprocfs_level1_s
{
struct procfs_dir_priv_s base; /* Base directory private data */
};
/* Line generating function type */
typedef int (*linegen_t)(FAR struct netprocfs_file_s *netfile);
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
!defined(CONFIG_FS_PROCFS_EXCLUDE_NET) && defined(CONFIG_NET_STATISTICS)
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Line generating functions */
#ifdef CONFIG_NET_STATISTICS
static int netprocfs_header(FAR struct netprocfs_file_s *netfile);
static int netprocfs_received(FAR struct netprocfs_file_s *netfile);
static int netprocfs_dropped(FAR struct netprocfs_file_s *netfile);
@ -129,26 +75,6 @@ static int netprocfs_sent(FAR struct netprocfs_file_s *netfile);
#ifdef CONFIG_NET_TCP
static int netprocfs_retransmissions(FAR struct netprocfs_file_s *netfile);
#endif /* CONFIG_NET_TCP */
#endif /* CONFIG_NET_STATISTICS */
/* File system methods */
static int netprocfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode);
static int netprocfs_close(FAR struct file *filep);
static ssize_t netprocfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static int netprocfs_dup(FAR const struct file *oldp,
FAR struct file *newp);
static int netprocfs_opendir(FAR const char *relpath,
FAR struct fs_dirent_s *dir);
static int netprocfs_closedir(FAR struct fs_dirent_s *dir);
static int netprocfs_readdir(FAR struct fs_dirent_s *dir);
static int netprocfs_rewinddir(FAR struct fs_dirent_s *dir);
static int netprocfs_stat(FAR const char *relpath, FAR struct stat *buf);
/****************************************************************************
* Private Data
@ -156,7 +82,6 @@ static int netprocfs_stat(FAR const char *relpath, FAR struct stat *buf);
/* Line generating functions */
#ifdef CONFIG_NET_STATISTICS
static const linegen_t g_linegen[] =
{
netprocfs_header,
@ -187,32 +112,6 @@ static const linegen_t g_linegen[] =
};
#define NSTAT_LINES (sizeof(g_linegen) / sizeof(linegen_t))
#endif /* CONFIG_NET_STATISTICS */
/****************************************************************************
* Public Data
****************************************************************************/
/* See include/nutts/fs/procfs.h
* We use the old-fashioned kind of initializers so that this will compile
* with any compiler.
*/
const struct procfs_operations net_procfsoperations =
{
netprocfs_open, /* open */
netprocfs_close, /* close */
netprocfs_read, /* read */
NULL, /* write */
netprocfs_dup, /* dup */
netprocfs_opendir, /* opendir */
netprocfs_closedir, /* closedir */
netprocfs_readdir, /* readdir */
netprocfs_rewinddir, /* rewinddir */
netprocfs_stat /* stat */
};
/****************************************************************************
* Private Functions
@ -533,90 +432,34 @@ static int netprocfs_retransmissions(FAR struct netprocfs_file_s *netfile)
#endif /* CONFIG_NET_STATISTICS && CONFIG_NET_TCP */
/****************************************************************************
* Name: netprocfs_open
* Public Functions
****************************************************************************/
static int netprocfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct netprocfs_file_s *priv;
fvdbg("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) &&
(net_procfsoperations.write == NULL))
{
fdbg("ERROR: Only O_RDONLY supported\n");
return -EACCES;
}
/* "net/stat" is the only acceptable value for the relpath */
if (strcmp(relpath, "net/stat") != 0)
{
fdbg("ERROR: relpath is '%s'\n", relpath);
return -ENOENT;
}
/* Allocate a container to hold the task and attribute selection */
priv = (FAR struct netprocfs_file_s *)kmm_zalloc(sizeof(struct netprocfs_file_s));
if (!priv)
{
fdbg("ERROR: Failed to allocate file attributes\n");
return -ENOMEM;
}
/* Save the index as the open-specific state in filep->f_priv */
filep->f_priv = (FAR void *)priv;
return OK;
}
/****************************************************************************
* Name: netprocfs_close
* Name: net_readstats
*
* Description:
* Read and format network layer statistics.
*
* Input Parameters:
* priv - A reference to the network procfs file structure
* buffer - The user-provided buffer into which network status will be
* returned.
* bulen - The size in bytes of the user provided buffer.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned
* on failure.
*
****************************************************************************/
static int netprocfs_close(FAR struct file *filep)
ssize_t net_readstats(FAR struct netprocfs_file_s *priv, FAR char *buffer,
size_t buflen)
{
FAR struct netprocfs_file_s *priv;
/* Recover our private data from the struct file instance */
priv = (FAR struct netprocfs_file_s *)filep->f_priv;
DEBUGASSERT(priv);
/* Release the file attributes structure */
kmm_free(priv);
filep->f_priv = NULL;
return OK;
}
/****************************************************************************
* Name: netprocfs_read
****************************************************************************/
static ssize_t netprocfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
#ifdef CONFIG_NET_STATISTICS
FAR struct netprocfs_file_s *priv;
size_t xfrsize;
ssize_t nreturned;
fvdbg("buffer=%p buflen=%d\n", buffer, (int)buflen);
/* Recover our private data from the struct file instance */
priv = (FAR struct netprocfs_file_s *)filep->f_priv;
DEBUGASSERT(priv);
fvdbg("buffer=%p buflen=%lu\n", buffer, (unsigned long)buflen);
/* Is there line data already buffered? */
@ -687,226 +530,8 @@ static ssize_t netprocfs_read(FAR struct file *filep, FAR char *buffer,
nreturned += xfrsize;
}
/* Update the file offset */
if (nreturned > 0)
{
filep->f_pos += nreturned;
}
return nreturned;
#else
return 0;
#endif
}
/****************************************************************************
* Name: netprocfs_dup
*
* Description:
* Duplicate open file data in the new file structure.
*
****************************************************************************/
static int netprocfs_dup(FAR const struct file *oldp, FAR struct file *newp)
{
FAR struct netprocfs_file_s *oldpriv;
FAR struct netprocfs_file_s *newpriv;
fvdbg("Dup %p->%p\n", oldp, newp);
/* Recover our private data from the old struct file instance */
oldpriv = (FAR struct netprocfs_file_s *)oldp->f_priv;
DEBUGASSERT(oldpriv);
/* Allocate a new container to hold the task and attribute selection */
newpriv = (FAR struct netprocfs_file_s *)kmm_zalloc(sizeof(struct netprocfs_file_s));
if (!newpriv)
{
fdbg("ERROR: Failed to allocate file attributes\n");
return -ENOMEM;
}
/* The copy the file attribtes from the old attributes to the new */
memcpy(newpriv, oldpriv, sizeof(struct netprocfs_file_s));
/* Save the new attributes in the new file structure */
newp->f_priv = (FAR void *)newpriv;
return OK;
}
/****************************************************************************
* Name: netprocfs_opendir
*
* Description:
* Open a directory for read access
*
****************************************************************************/
static int netprocfs_opendir(FAR const char *relpath,
FAR struct fs_dirent_s *dir)
{
FAR struct netprocfs_level1_s *level1;
fvdbg("relpath: \"%s\"\n", relpath ? relpath : "NULL");
DEBUGASSERT(relpath && dir && !dir->u.procfs);
/* The path refers to the 1st level sbdirectory. Allocate the level1
* dirent structure.
*/
level1 = (FAR struct netprocfs_level1_s *)
kmm_zalloc(sizeof(struct netprocfs_level1_s));
if (!level1)
{
fdbg("ERROR: Failed to allocate the level1 directory structure\n");
return -ENOMEM;
}
/* Initialze base structure components */
level1->base.level = 1;
level1->base.nentries = 1;
level1->base.index = 0;
dir->u.procfs = (FAR void *) level1;
return OK;
}
/****************************************************************************
* Name: netprocfs_closedir
*
* Description: Close the directory listing
*
****************************************************************************/
static int netprocfs_closedir(FAR struct fs_dirent_s *dir)
{
FAR struct netprocfs_level1_s *priv;
DEBUGASSERT(dir && dir->u.procfs);
priv = dir->u.procfs;
if (priv)
{
kmm_free(priv);
}
dir->u.procfs = NULL;
return OK;
}
/****************************************************************************
* Name: netprocfs_readdir
*
* Description: Read the next directory entry
*
****************************************************************************/
static int netprocfs_readdir(FAR struct fs_dirent_s *dir)
{
FAR struct netprocfs_level1_s *level1;
int index;
int ret;
DEBUGASSERT(dir && dir->u.procfs);
level1 = dir->u.procfs;
/* Have we reached the end of the directory */
index = level1->base.index;
if (index >= level1->base.nentries)
{
/* We signal the end of the directory by returning the special
* error -ENOENT
*/
fvdbg("Entry %d: End of directory\n", index);
ret = -ENOENT;
}
/* Currently only one element in the directory */
else
{
DEBUGASSERT(level1->base.level == 1);
DEBUGASSERT(level1->base.index <= 1);
/* Copy the one supported directory entry */
dir->fd_dir.d_type = DTYPE_FILE;
strncpy(dir->fd_dir.d_name, "stat", NAME_MAX + 1);
/* Set up the next directory entry offset. NOTE that we could use the
* standard f_pos instead of our own private index.
*/
level1->base.index = index + 1;
ret = OK;
}
return ret;
}
/****************************************************************************
* Name: netprocfs_rewindir
*
* Description: Reset directory read to the first entry
*
****************************************************************************/
static int netprocfs_rewinddir(FAR struct fs_dirent_s *dir)
{
FAR struct netprocfs_level1_s *priv;
DEBUGASSERT(dir && dir->u.procfs);
priv = dir->u.procfs;
priv->base.index = 0;
return OK;
}
/****************************************************************************
* Name: netprocfs_stat
*
* Description: Return information about a file or directory
*
****************************************************************************/
static int netprocfs_stat(FAR const char *relpath, FAR struct stat *buf)
{
/* "net" and "net/stat" are the only acceptable values for the relpath */
if (strcmp(relpath, "net") == 0)
{
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
}
else if (strcmp(relpath, "net/stat") == 0)
{
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
}
else
{
fdbg("ERROR: relpath is '%s'\n", relpath);
return -ENOENT;
}
/* File/directory size, access block size */
buf->st_size = 0;
buf->st_blksize = 512;
buf->st_blocks = 0;
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
* !CONFIG_FS_PROCFS_EXCLUDE_NET */

134
net/procfs/procfs.h Normal file
View File

@ -0,0 +1,134 @@
/****************************************************************************
* net/procfs/procfs.h
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* 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.
*
****************************************************************************/
#ifndef __NET_PROCFS_PROCFS_H
#define __NET_PROCFS_PROCFS_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <nuttx/fs/procfs.h>
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_NET)
/****************************************************************************
* 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 NET_LINELEN 64
/****************************************************************************
* Public Type Definitions
****************************************************************************/
/* This enumeration identifies all of the thread attributes that can be
* accessed via the procfs file system.
*/
/* This structure describes one open "file" */
struct netprocfs_file_s
{
struct procfs_file_s base; /* Base open file structure */
uint8_t lineno; /* Line number */
uint8_t linesize; /* Number of valid characters in line[] */
uint8_t offset; /* Offset to first valid character in line[] */
char line[NET_LINELEN]; /* Pre-allocated buffer for formatted lines */
};
/* Level 1 is the directory of attributes */
struct netprocfs_level1_s
{
struct procfs_dir_priv_s base; /* Base directory private data */
};
/* Line generating function type */
typedef int (*linegen_t)(FAR struct netprocfs_file_s *netfile);
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef __cplusplus
# define EXTERN extern "C"
extern "C"
{
#else
# define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: net_readstats
*
* Description:
* Read and format network layer statistics.
*
* Input Parameters:
* priv - A reference to the network procfs file structure
* buffer - The user-provided buffer into which network status will be
* returned.
* bulen - The size in bytes of the user provided buffer.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is returned
* on failure.
*
****************************************************************************/
#ifdef CONFIG_NET_STATISTICS
ssize_t net_readstats(FAR struct netprocfs_file_s *priv, FAR char *buffer,
size_t buflen);
#endif
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_FS_PROCFS && !CONFIG_FS_PROCFS_EXCLUDE_NET */
#endif /* __NET_PROCFS_PROCFS_H */