From 0fb32570ff7cc579c5ea2a25c71a5d3b49673aec Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 27 Nov 2015 17:47:09 -0600 Subject: [PATCH] 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. --- ChangeLog.txt | 3 ++ nshlib/Kconfig | 8 ++++- nshlib/nsh.h | 3 ++ nshlib/nsh_netcmds.c | 77 ++++++++++++++++++++++++++++++++++++++------ 4 files changed, 81 insertions(+), 10 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index a7c6c76d7..0073df8f4 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -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). diff --git a/nshlib/Kconfig b/nshlib/Kconfig index 3cdfec446..eb8a760d3 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -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" diff --git a/nshlib/nsh.h b/nshlib/nsh.h index f5405dbed..96481a085 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -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 /**************************************************************************** diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index 214d88829..45114dfa7 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -52,6 +52,7 @@ #include #include #include /* Needed for open */ +#include #include /* Needed for gethostbyname */ #include /* Needed for basename */ #include @@ -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; }