apps/nshlib: Remove all references to internal OS interface netdev_foreach(). Logic in ifconfig, ifup, and ifown now use the procfs file system to examine networking status.

This commit is contained in:
Gregory Nutt 2015-11-27 17:47:09 -06:00
parent bb88ff9b12
commit 0fb32570ff
4 changed files with 81 additions and 10 deletions

View File

@ -1471,3 +1471,6 @@
network statistics. A consequence of this is that you cannot view
this network information if the procfs is not enabled and mounted at
/proc (2015-11-27).
* apps/nshlib: Remove all references to internal OS interface
netdev_foreach(). Logic in ifconfig, ifup, and ifown now use the
procfs file system to examine networking status. (2015-11-27).

View File

@ -245,7 +245,13 @@ config NSH_DISABLE_HEXDUMP
config NSH_DISABLE_IFCONFIG
bool "Disable ifconfig"
default n
default y if !FS_PROCFS || FS_PROCFS_EXCLUDE_NET
default n if FS_PROCFS && !FS_PROCFS_EXCLUDE_NET
config NSH_DISABLE_IFUPDOWN
bool "Disable ifup/down"
default y if !FS_PROCFS || FS_PROCFS_EXCLUDE_NET
default n if FS_PROCFS && !FS_PROCFS_EXCLUDE_NET
config NSH_DISABLE_KILL
bool "Disable kill"

View File

@ -616,6 +616,9 @@
#if !defined(CONFIG_FS_PROCFS) || defined(CONFIG_FS_PROCFS_EXCLUDE_NET)
# undef CONFIG_NSH_DISABLE_IFCONFIG /* 'ifconfig' depends on network procfs */
# define CONFIG_NSH_DISABLE_IFCONFIG 1
# undef CONFIG_NSH_DISABLE_IFUPDOWN /* 'ifup/down' depend on network procfs */
# define CONFIG_NSH_DISABLE_IFUPDOWN 1
#endif
/****************************************************************************

View File

@ -52,6 +52,7 @@
#include <string.h>
#include <sched.h>
#include <fcntl.h> /* Needed for open */
#include <dirent.h>
#include <netdb.h> /* Needed for gethostbyname */
#include <libgen.h> /* Needed for basename */
#include <errno.h>
@ -143,6 +144,9 @@ struct tftpc_args_s
};
#endif
typedef int (*nsh_netdev_callback_t)(FAR struct nsh_vtbl_s *vtbl,
FAR char *devname);
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
@ -290,16 +294,15 @@ static inline void net_statistics(FAR struct nsh_vtbl_s *vtbl)
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_IFUPDOWN) || !defined(CONFIG_NSH_DISABLE_IFCONFIG)
static int ifconfig_callback(FAR struct net_driver_s *dev, void *arg)
static int ifconfig_callback(FAR struct nsh_vtbl_s *vtbl, FAR char *devname)
{
FAR struct nsh_vtbl_s *vtbl = (FAR struct nsh_vtbl_s *)arg;
char buffer[IFNAMSIZ + 12];
DEBUGASSERT(dev != NULL && vtbl != NULL);
DEBUGASSERT(vtbl != NULL && devname != NULL);
/* Construct the full path to the /proc/net entry for this device */
snprintf(buffer, IFNAMSIZ + 12, "/proc/net/%s", dev->d_ifname);
snprintf(buffer, IFNAMSIZ + 12, "/proc/net/%s", devname);
(void)nsh_catfile(vtbl, "ifconfig", buffer);
return OK;
@ -573,6 +576,58 @@ static void wget_callback(FAR char **buffer, int offset, int datend,
#endif
#endif
/****************************************************************************
* Name: nsh_foreach_netdev
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_IFUPDOWN) || !defined(CONFIG_NSH_DISABLE_IFCONFIG)
static int nsh_foreach_netdev(nsh_netdev_callback_t callback,
FAR struct nsh_vtbl_s *vtbl,
FAR char *cmd)
{
FAR struct dirent *entry;
FAR DIR *dir;
int ret = OK;
/* Open the /proc/net directory */
dir = opendir("/proc/net");
if (dir == NULL)
{
nsh_output(vtbl, g_fmtcmdfailed, cmd, "opendir", NSH_ERRNO);
return ERROR;
}
/* Look for device configuration "regular" files */
while ((entry = readdir(dir)) != NULL)
{
/* Skip anything that is not a regular file and skip the file
* /proc/dev/stat which does not correspond to a network driver.
*/
if (entry->d_type == DTYPE_FILE &&
strcmp(entry->d_name, "stat") != 0)
{
/* Performt he callback. It returns any non-zero value, then
* terminate the serach.
*/
ret = callback(vtbl, entry->d_name);
if (ret != OK)
{
break;
}
}
}
/* Close the directory */
(void)closedir(dir);
return ret;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@ -632,8 +687,7 @@ int cmd_ifup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
if (argc != 2)
{
nsh_output(vtbl, "Please select nic_name:\n");
netdev_foreach(ifconfig_callback, vtbl);
return OK;
return nsh_foreach_netdev(ifconfig_callback, vtbl, "ifup");
}
intf = argv[1];
@ -656,8 +710,7 @@ int cmd_ifdown(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
if (argc != 2)
{
nsh_output(vtbl, "Please select nic_name:\n");
netdev_foreach(ifconfig_callback, vtbl);
return OK;
return nsh_foreach_netdev(ifconfig_callback, vtbl, "ifdown");
}
intf = argv[1];
@ -701,6 +754,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
#if defined(CONFIG_NSH_DHCPC)
FAR void *handle;
#endif
int ret;
/* With one or no arguments, ifconfig simply shows the status of Ethernet
* device:
@ -711,7 +765,12 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
if (argc <= 2)
{
netdev_foreach(ifconfig_callback, vtbl);
ret = nsh_foreach_netdev(ifconfig_callback, vtbl, "ifconfig");
if (ret < 0)
{
return ERROR;
}
net_statistics(vtbl);
return OK;
}