2015-11-27 20:53:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/procfs/net_procfs.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-11-27 20:53:33 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-27 20:53:33 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-11-27 20:53:33 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2021-08-01 10:54:08 +02:00
|
|
|
#include <fnmatch.h>
|
2015-11-27 23:39:14 +01:00
|
|
|
#include <libgen.h>
|
2015-11-27 20:53:33 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
2023-08-08 00:11:20 +02:00
|
|
|
#include <nuttx/lib/lib.h>
|
2015-11-27 20:53:33 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/procfs.h>
|
2015-11-27 23:39:14 +01:00
|
|
|
#include <nuttx/net/netdev.h>
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2015-11-27 23:39:14 +01:00
|
|
|
#include "netdev/netdev.h"
|
2015-11-27 20:53:33 +01:00
|
|
|
#include "procfs/procfs.h"
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
|
|
|
|
!defined(CONFIG_FS_PROCFS_EXCLUDE_NET)
|
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
2018-11-06 17:42:53 +01:00
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
/* Read statistics function type */
|
|
|
|
|
|
|
|
typedef CODE ssize_t (*read_stat_t)(FAR struct netprocfs_file_s *priv,
|
|
|
|
FAR char *buffer, size_t buflen);
|
|
|
|
|
|
|
|
struct netprocfs_entry_s
|
|
|
|
{
|
|
|
|
uint8_t type; /* Type of file */
|
|
|
|
FAR const char *name; /* File name */
|
|
|
|
union
|
|
|
|
{
|
|
|
|
read_stat_t stat; /* Read statistics hook */
|
|
|
|
FAR const struct procfs_operations *ops;
|
|
|
|
} u;
|
|
|
|
};
|
2017-08-11 22:50:39 +02:00
|
|
|
|
2015-11-27 20:53:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2015-11-27 20:53:33 +01:00
|
|
|
/* File system methods */
|
|
|
|
|
2020-07-02 16:03:27 +02:00
|
|
|
static int netprocfs_open(FAR struct file *filep,
|
|
|
|
FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode);
|
2015-11-27 20:53:33 +01:00
|
|
|
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,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir);
|
2015-11-27 20:53:33 +01:00
|
|
|
static int netprocfs_closedir(FAR struct fs_dirent_s *dir);
|
2022-07-26 08:56:41 +02:00
|
|
|
static int netprocfs_readdir(FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry);
|
2015-11-27 20:53:33 +01:00
|
|
|
static int netprocfs_rewinddir(FAR struct fs_dirent_s *dir);
|
|
|
|
|
|
|
|
static int netprocfs_stat(FAR const char *relpath, FAR struct stat *buf);
|
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-17 20:27:33 +02:00
|
|
|
extern const struct procfs_operations g_netroute_operations;
|
2022-09-04 15:39:06 +02:00
|
|
|
|
|
|
|
/* Netprocfs component mappings */
|
|
|
|
|
|
|
|
static const struct netprocfs_entry_s g_net_entries[] =
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
|
|
|
{
|
|
|
|
DTYPE_FILE, "stat",
|
|
|
|
{
|
|
|
|
netprocfs_read_netstats
|
|
|
|
}
|
|
|
|
},
|
|
|
|
# ifdef CONFIG_NET_MLD
|
|
|
|
{
|
|
|
|
DTYPE_FILE, "mld",
|
|
|
|
{
|
|
|
|
netprocfs_read_mldstats
|
|
|
|
}
|
|
|
|
},
|
|
|
|
# endif
|
2022-09-19 06:36:15 +02:00
|
|
|
# if defined(CONFIG_NET_TCP) && !defined(CONFIG_NET_TCP_NO_STACK)
|
2022-09-05 11:06:05 +02:00
|
|
|
{
|
|
|
|
DTYPE_FILE, "tcp",
|
|
|
|
{
|
|
|
|
netprocfs_read_tcpstats
|
|
|
|
}
|
|
|
|
},
|
|
|
|
# endif
|
2022-09-19 06:36:15 +02:00
|
|
|
# if defined(CONFIG_NET_UDP) && !defined(CONFIG_NET_UDP_NO_STACK)
|
2022-09-09 05:13:15 +02:00
|
|
|
{
|
|
|
|
DTYPE_FILE, "udp",
|
|
|
|
{
|
|
|
|
netprocfs_read_udpstats
|
|
|
|
}
|
|
|
|
},
|
|
|
|
# endif
|
2022-09-04 15:39:06 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ROUTE
|
|
|
|
{
|
|
|
|
DTYPE_DIRECTORY, "route",
|
|
|
|
{
|
2023-04-17 20:27:33 +02:00
|
|
|
(FAR void *)&g_netroute_operations
|
2022-09-04 15:39:06 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
DTYPE_FILE, "",
|
|
|
|
{
|
|
|
|
netprocfs_read_devstats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-27 20:53:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See include/nutts/fs/procfs.h
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
2023-04-17 20:27:33 +02:00
|
|
|
const struct procfs_operations g_net_operations =
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
|
|
|
netprocfs_open, /* open */
|
|
|
|
netprocfs_close, /* close */
|
|
|
|
netprocfs_read, /* read */
|
|
|
|
NULL, /* write */
|
2023-11-25 13:11:03 +01:00
|
|
|
NULL, /* poll */
|
2015-11-27 20:53:33 +01:00
|
|
|
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)
|
|
|
|
{
|
2022-09-04 15:39:06 +02:00
|
|
|
FAR struct net_driver_s *dev = NULL;
|
2015-11-27 20:53:33 +01:00
|
|
|
FAR struct netprocfs_file_s *priv;
|
2022-09-04 15:39:06 +02:00
|
|
|
int i;
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Open '%s'\n", relpath);
|
2015-11-27 20:53:33 +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) &&
|
2023-04-17 20:27:33 +02:00
|
|
|
(g_net_operations.write == NULL))
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Only O_RDONLY supported\n");
|
2015-11-27 20:53:33 +01:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
/* For each net entries */
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
for (i = 0; i < nitems(g_net_entries); i++)
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
2022-09-04 15:39:06 +02:00
|
|
|
if (strncmp(relpath + 4, g_net_entries[i].name,
|
|
|
|
strlen(g_net_entries[i].name)))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
if (g_net_entries[i].type == DTYPE_DIRECTORY)
|
|
|
|
{
|
|
|
|
return g_net_entries[i].u.ops->open(filep, relpath, oflags, mode);
|
|
|
|
}
|
2017-08-11 22:50:39 +02:00
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
break;
|
2015-11-27 23:39:14 +01:00
|
|
|
}
|
2022-09-04 15:39:06 +02:00
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
if (i == nitems(g_net_entries) - 1)
|
2015-11-27 23:39:14 +01:00
|
|
|
{
|
|
|
|
FAR char *devname;
|
|
|
|
FAR char *copy;
|
|
|
|
|
|
|
|
/* Otherwise, we need to search the list of registered network devices
|
|
|
|
* to determine if the name corresponds to a network device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
copy = strdup(relpath);
|
|
|
|
if (copy == NULL)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: strdup failed\n");
|
2015-11-27 23:39:14 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
devname = basename(copy);
|
|
|
|
dev = netdev_findbyname(devname);
|
2023-08-08 00:11:20 +02:00
|
|
|
lib_free(copy);
|
2015-11-27 23:39:14 +01:00
|
|
|
|
|
|
|
if (dev == NULL)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: relpath is '%s'\n", relpath);
|
2015-11-27 23:39:14 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2015-11-27 20:53:33 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 23:39:14 +01:00
|
|
|
/* Allocate the open file structure */
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
priv = (FAR struct netprocfs_file_s *)
|
|
|
|
kmm_zalloc(sizeof(struct netprocfs_file_s));
|
2015-11-27 20:53:33 +01:00
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
2015-11-27 20:53:33 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-11-27 23:39:14 +01:00
|
|
|
/* Initialize the open-file structure */
|
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
priv->dev = dev;
|
2022-09-04 15:39:06 +02:00
|
|
|
priv->entry = i;
|
2015-11-27 23:39:14 +01:00
|
|
|
|
|
|
|
/* Save the open file structure as the open-specific state in
|
|
|
|
* filep->f_priv.
|
|
|
|
*/
|
2015-11-27 20:53:33 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
FAR struct netprocfs_file_s *priv;
|
2022-09-04 15:39:06 +02:00
|
|
|
ssize_t nreturned = -EINVAL;
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("buffer=%p buflen=%lu\n", buffer, (unsigned long)buflen);
|
2015-11-27 20:53:33 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
priv = (FAR struct netprocfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
/* Read according to the sub-directory */
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
nreturned = g_net_entries[priv->entry].u.stat(priv,
|
|
|
|
buffer, buflen);
|
2015-11-27 20:53:33 +01:00
|
|
|
|
|
|
|
/* Update the file offset */
|
|
|
|
|
|
|
|
if (nreturned > 0)
|
|
|
|
{
|
|
|
|
filep->f_pos += nreturned;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nreturned;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
2015-11-27 20:53:33 +01:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2019-10-25 19:31:42 +02:00
|
|
|
newpriv = (FAR struct netprocfs_file_s *)
|
|
|
|
kmm_zalloc(sizeof(struct netprocfs_file_s));
|
2015-11-27 20:53:33 +01:00
|
|
|
if (!newpriv)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
2015-11-27 20:53:33 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* The copy the file attribute from the old attributes to the new */
|
2015-11-27 20:53:33 +01:00
|
|
|
|
|
|
|
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,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
|
|
|
FAR struct netprocfs_level1_s *level1;
|
2015-11-27 23:39:14 +01:00
|
|
|
int ndevs;
|
2017-08-11 22:50:39 +02:00
|
|
|
int ret;
|
2022-09-04 15:39:06 +02:00
|
|
|
int i;
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
|
2022-08-05 06:31:25 +02:00
|
|
|
DEBUGASSERT(relpath && dir);
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
/* Subdirectory ? */
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2022-09-26 17:12:32 +02:00
|
|
|
if (strlen(relpath) > 4)
|
2015-11-27 23:39:14 +01:00
|
|
|
{
|
2023-02-02 13:31:22 +01:00
|
|
|
for (i = 0; i < nitems(g_net_entries); i++)
|
2022-09-04 15:39:06 +02:00
|
|
|
{
|
2022-09-26 17:12:32 +02:00
|
|
|
if (strncmp(relpath + 4, g_net_entries[i].name,
|
|
|
|
strlen(g_net_entries[i].name)))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_net_entries[i].type == DTYPE_DIRECTORY)
|
|
|
|
{
|
|
|
|
return g_net_entries[i].u.ops->opendir(relpath, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2022-09-04 15:39:06 +02:00
|
|
|
}
|
2015-11-27 23:39:14 +01:00
|
|
|
}
|
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
/* Assume that path refers to the 1st level subdirectory. Allocate the
|
|
|
|
* level1 the dirent structure before checking.
|
2015-11-27 20:53:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
level1 = (FAR struct netprocfs_level1_s *)
|
|
|
|
kmm_zalloc(sizeof(struct netprocfs_level1_s));
|
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
if (level1 == NULL)
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate the level1 directory structure\n");
|
2015-11-27 20:53:33 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
level1->base.level = 1;
|
|
|
|
|
2020-12-01 07:57:38 +01:00
|
|
|
if (strcmp(relpath, "net") == 0 || strcmp(relpath, "net/") == 0)
|
2017-08-11 22:50:39 +02:00
|
|
|
{
|
|
|
|
/* Count the number of network devices */
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
ndevs = netdev_count();
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2018-11-06 17:42:53 +01:00
|
|
|
/* Initialize base structure components */
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
level1->base.nentries = ndevs;
|
2022-09-04 15:39:06 +02:00
|
|
|
|
|
|
|
/* Add other enabled net components, except netdev */
|
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
level1->base.nentries += nitems(g_net_entries) - 1;
|
2017-08-11 22:50:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* REVISIT: We really need to check if the relpath refers to a network
|
|
|
|
* device. In that case, we need to return -ENOTDIR. Otherwise, we
|
|
|
|
* should return -ENOENT.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ferr("ERROR: Bad relpath: %s\n", relpath);
|
|
|
|
ret = -ENOTDIR;
|
|
|
|
goto errout_with_alloc;
|
|
|
|
}
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
*dir = (FAR struct fs_dirent_s *)level1;
|
2015-11-27 20:53:33 +01:00
|
|
|
return OK;
|
2017-08-11 22:50:39 +02:00
|
|
|
|
|
|
|
errout_with_alloc:
|
|
|
|
kmm_free(level1);
|
|
|
|
return ret;
|
2015-11-27 20:53:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netprocfs_closedir
|
|
|
|
*
|
|
|
|
* Description: Close the directory listing
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int netprocfs_closedir(FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
DEBUGASSERT(dir);
|
|
|
|
kmm_free(dir);
|
2015-11-27 20:53:33 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netprocfs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
static int netprocfs_readdir(FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry)
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
|
|
|
FAR struct netprocfs_level1_s *level1;
|
2015-11-27 23:39:14 +01:00
|
|
|
FAR struct net_driver_s *dev;
|
2015-11-27 20:53:33 +01:00
|
|
|
int index;
|
2017-08-11 22:50:39 +02:00
|
|
|
int ret;
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
DEBUGASSERT(dir);
|
|
|
|
level1 = (FAR struct netprocfs_level1_s *)dir;
|
2017-08-11 22:50:39 +02:00
|
|
|
DEBUGASSERT(level1->base.level > 0);
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
/* Are we searching this directory? Or is it just an intermediate on the
|
|
|
|
* way to a sub-directory?
|
|
|
|
*/
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
if (level1->base.level == 1)
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
2017-08-11 22:50:39 +02:00
|
|
|
/* This directory.. Have we reached the end of the directory? */
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
index = level1->base.index;
|
|
|
|
DEBUGASSERT(index <= level1->base.nentries);
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
if (index >= level1->base.nentries)
|
|
|
|
{
|
2018-10-30 01:00:30 +01:00
|
|
|
/* We signal the end of the directory by returning the special
|
2017-08-11 22:50:39 +02:00
|
|
|
* error -ENOENT.
|
|
|
|
*/
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
finfo("Entry %d: End of directory\n", index);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
/* Process other enabled net components, except netdev */
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
if (index < nitems(g_net_entries) - 1)
|
2018-11-06 17:42:53 +01:00
|
|
|
{
|
2022-09-04 15:39:06 +02:00
|
|
|
entry->d_type = g_net_entries[index].type;
|
|
|
|
strlcpy(entry->d_name,
|
|
|
|
g_net_entries[index].name, sizeof(entry->d_name));
|
2018-11-06 17:42:53 +01:00
|
|
|
}
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
/* Net device entry */
|
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
else
|
|
|
|
{
|
2018-06-26 01:42:56 +02:00
|
|
|
int ifindex;
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2018-06-25 20:08:10 +02:00
|
|
|
#ifdef CONFIG_NETDEV_IFINDEX
|
2018-06-26 01:42:56 +02:00
|
|
|
/* For the first network device, ifindex will be zero. We have
|
|
|
|
* to take some special action to get the correct starting
|
|
|
|
* ifindex.
|
2018-06-25 20:08:10 +02:00
|
|
|
*/
|
|
|
|
|
2018-06-26 01:42:56 +02:00
|
|
|
if (level1->ifindex == 0)
|
|
|
|
{
|
|
|
|
ifindex = netdev_nextindex(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ifindex = netdev_nextindex(level1->ifindex);
|
|
|
|
}
|
|
|
|
|
2018-06-26 00:55:45 +02:00
|
|
|
if (ifindex < 0)
|
2018-06-25 20:08:10 +02:00
|
|
|
{
|
|
|
|
/* There are no more... one must have been unregistered */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2018-06-26 01:42:56 +02:00
|
|
|
|
|
|
|
level1->ifindex = ifindex + 1;
|
|
|
|
#else
|
2022-09-08 08:50:11 +02:00
|
|
|
/* Get the raw index, (why +2 ? The ifindex should remove device
|
|
|
|
* entry of last g_net_entries(-1) and start the devidx from 1)
|
|
|
|
*/
|
2018-06-26 01:42:56 +02:00
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
ifindex = index + 2 - nitems(g_net_entries);
|
2018-06-25 20:08:10 +02:00
|
|
|
#endif
|
2017-08-11 22:50:39 +02:00
|
|
|
/* Find the device corresponding to this device index */
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2018-06-26 00:55:45 +02:00
|
|
|
dev = netdev_findbyindex(ifindex);
|
|
|
|
if (dev == NULL)
|
|
|
|
{
|
|
|
|
/* What happened? */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2017-08-11 22:50:39 +02:00
|
|
|
|
|
|
|
/* Copy the device statistics file entry */
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_FILE;
|
2022-08-24 04:23:20 +02:00
|
|
|
strlcpy(entry->d_name, dev->d_ifname, sizeof(entry->d_name));
|
2017-08-11 22:50:39 +02:00
|
|
|
}
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
/* 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;
|
2015-11-27 20:53:33 +01:00
|
|
|
}
|
2017-08-11 22:50:39 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We are performing a directory search of one of the subdirectories
|
|
|
|
* and we must let the handler perform the read.
|
|
|
|
*/
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2017-08-11 22:50:39 +02:00
|
|
|
DEBUGASSERT(level1->base.procfsentry != NULL &&
|
|
|
|
level1->base.procfsentry->ops->readdir != NULL);
|
2015-11-27 23:39:14 +01:00
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
ret = level1->base.procfsentry->ops->readdir(dir, entry);
|
2017-08-11 22:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-11-27 20:53:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
DEBUGASSERT(dir);
|
|
|
|
priv = (FAR struct netprocfs_level1_s *)dir;
|
2015-11-27 20:53:33 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-09-04 15:39:06 +02:00
|
|
|
int i;
|
|
|
|
|
2015-11-27 23:39:14 +01:00
|
|
|
/* Check for the directory "net" */
|
2015-11-27 20:53:33 +01:00
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
buf->st_mode = 0;
|
|
|
|
|
2020-07-30 20:50:17 +02:00
|
|
|
if (strcmp(relpath, "net") == 0 || strcmp(relpath, "net/") == 0)
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
|
|
|
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
|
|
|
|
}
|
2015-11-27 23:39:14 +01:00
|
|
|
else
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
2023-02-02 13:31:22 +01:00
|
|
|
for (i = 0; i < nitems(g_net_entries); i++)
|
2022-09-04 15:39:06 +02:00
|
|
|
{
|
|
|
|
if (strcmp(relpath + 4, g_net_entries[i].name) == 0)
|
|
|
|
{
|
|
|
|
if (g_net_entries[i].type == DTYPE_FILE)
|
|
|
|
{
|
|
|
|
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
|
|
|
|
}
|
|
|
|
else if (g_net_entries[i].type == DTYPE_DIRECTORY)
|
|
|
|
{
|
|
|
|
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-06 17:42:53 +01:00
|
|
|
}
|
2017-08-11 22:50:39 +02:00
|
|
|
|
2022-09-04 15:39:06 +02:00
|
|
|
if (buf->st_mode == 0)
|
2015-11-27 20:53:33 +01:00
|
|
|
{
|
2015-11-27 23:39:14 +01:00
|
|
|
FAR struct net_driver_s *dev;
|
|
|
|
FAR char *devname;
|
|
|
|
FAR char *copy;
|
|
|
|
|
|
|
|
/* Otherwise, we need to search the list of registered network devices
|
|
|
|
* to determine if the name corresponds to a network device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
copy = strdup(relpath);
|
|
|
|
if (copy == NULL)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: strdup failed\n");
|
2015-11-27 23:39:14 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
devname = basename(copy);
|
|
|
|
dev = netdev_findbyname(devname);
|
2023-08-08 00:11:20 +02:00
|
|
|
lib_free(copy);
|
2015-11-27 23:39:14 +01:00
|
|
|
|
|
|
|
if (dev == NULL)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: relpath is '%s'\n", relpath);
|
2015-11-27 23:39:14 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
|
2015-11-27 20:53:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* File/directory size, access block size */
|
|
|
|
|
|
|
|
buf->st_size = 0;
|
2015-11-27 23:39:14 +01:00
|
|
|
buf->st_blksize = 0;
|
2015-11-27 20:53:33 +01:00
|
|
|
buf->st_blocks = 0;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-27 23:39:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: netprocfs_read_linegen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read and format procfs data using a line generation table.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the network procfs file structure
|
|
|
|
* buffer - The user-provided buffer into which device status will be
|
|
|
|
* returned.
|
|
|
|
* buflen - The size in bytes of the user provided buffer.
|
|
|
|
* gentab - Table of line generation functions
|
|
|
|
* nelems - The number of elements in the table
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned
|
|
|
|
* on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t netprocfs_read_linegen(FAR struct netprocfs_file_s *priv,
|
|
|
|
FAR char *buffer, size_t buflen,
|
|
|
|
FAR const linegen_t *gentab, int nelems)
|
|
|
|
{
|
|
|
|
size_t xfrsize;
|
|
|
|
ssize_t nreturned;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("buffer=%p buflen=%lu\n", buffer, (unsigned long)buflen);
|
2015-11-27 23:39:14 +01:00
|
|
|
|
|
|
|
/* Is there line data already buffered? */
|
|
|
|
|
|
|
|
nreturned = 0;
|
|
|
|
if (priv->linesize > 0)
|
|
|
|
{
|
|
|
|
/* Yes, how much can we transfer now? */
|
|
|
|
|
|
|
|
xfrsize = priv->linesize;
|
|
|
|
if (xfrsize > buflen)
|
|
|
|
{
|
|
|
|
xfrsize = buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer the data to the user buffer */
|
|
|
|
|
|
|
|
memcpy(buffer, &priv->line[priv->offset], xfrsize);
|
|
|
|
|
|
|
|
/* Update pointers, sizes, and offsets */
|
|
|
|
|
|
|
|
buffer += xfrsize;
|
|
|
|
buflen -= xfrsize;
|
|
|
|
|
|
|
|
priv->linesize -= xfrsize;
|
|
|
|
priv->offset += xfrsize;
|
|
|
|
nreturned = xfrsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until the user buffer is full or until all of the network
|
|
|
|
* statistics have been transferred. At this point we know that
|
|
|
|
* either:
|
|
|
|
*
|
|
|
|
* 1. The user buffer is full, and/or
|
|
|
|
* 2. All of the current line data has been transferred.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (buflen > 0 && priv->lineno < nelems)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* Read the next line into the working buffer */
|
|
|
|
|
|
|
|
len = gentab[priv->lineno](priv);
|
|
|
|
|
|
|
|
/* Update line-related information */
|
|
|
|
|
|
|
|
priv->lineno++;
|
|
|
|
priv->linesize = len;
|
|
|
|
priv->offset = 0;
|
|
|
|
|
|
|
|
/* Transfer data to the user buffer */
|
|
|
|
|
|
|
|
xfrsize = priv->linesize;
|
|
|
|
if (xfrsize > buflen)
|
|
|
|
{
|
|
|
|
xfrsize = buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, &priv->line[priv->offset], xfrsize);
|
|
|
|
|
|
|
|
/* Update pointers, sizes, and offsets */
|
|
|
|
|
|
|
|
buffer += xfrsize;
|
|
|
|
buflen -= xfrsize;
|
|
|
|
|
|
|
|
priv->linesize -= xfrsize;
|
|
|
|
priv->offset += xfrsize;
|
|
|
|
nreturned += xfrsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nreturned;
|
|
|
|
}
|
|
|
|
|
2015-11-27 20:53:33 +01:00
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
|
|
|
|
* !CONFIG_FS_PROCFS_EXCLUDE_NET */
|