From 32b992971bf4a63f2121a8e89a7c9599ce13f886 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 27 Nov 2015 13:04:11 -0600 Subject: [PATCH] apps/nshlib: The 'ifconfig' command now uses /proc/net/stat to show network statistics. A consequence of this is that you cannot view network statistics if the procfs is not enabled and mounted at /proc --- ChangeLog.txt | 4 + nshlib/Makefile | 4 + nshlib/nsh.h | 21 +++++ nshlib/nsh_fscmds.c | 127 +--------------------------- nshlib/nsh_fsutils.c | 196 +++++++++++++++++++++++++++++++++++++++++++ nshlib/nsh_netcmds.c | 183 +--------------------------------------- 6 files changed, 232 insertions(+), 303 deletions(-) create mode 100644 nshlib/nsh_fsutils.c diff --git a/ChangeLog.txt b/ChangeLog.txt index d07a2f1c2..6ce50e4ab 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1466,4 +1466,8 @@ calls (2015-11-25). * apps/nshlib: If CONFIG_NETDEV_STATISTICS=y, then print the network driver statistics in the ifconfig (15-11-26). + * apps/nshlib: The 'ifconfig' command now uses /proc/net/stat to + show network statistics. A consequence of this is that you cannot + view network statistics if the procfs is not enabled and mounted + at /proc (2015-11-27). diff --git a/nshlib/Makefile b/nshlib/Makefile index 4d2d6a8ca..2b25750c3 100644 --- a/nshlib/Makefile +++ b/nshlib/Makefile @@ -50,6 +50,10 @@ else CSRCS += nsh_session.c endif +ifneq ($(CONFIG_NFILE_DESCRIPTORS),0) +CSRCS += nsh_fsutils.c +endif + ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) CSRCS += nsh_builtin.c endif diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 1b705f506..a1e900647 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -1110,4 +1110,25 @@ int nsh_extmatch_count(FAR char *name, FAR int *matches, int namelen); FAR const char *nsh_extmatch_getname(int index); #endif +/**************************************************************************** + * Name: nsh_catfile + * + * Description: + * Dump the contents of a file to the current NSH terminal. + * + * Input Paratemets: + * vtbl - session vtbl + * cmd - NSH command name to use in error reporting + * filepath - The full path to the file to be dumped + * + * Returned Value: + * Zero (OK) on success; -1 (ERROR) on failure. + * + ****************************************************************************/ + +#if CONFIG_NFILE_DESCRIPTORS > 0 +int nsh_catfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, + FAR const char *filepath); +#endif + #endif /* __APPS_NSHLIB_NSH_H */ diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c index 66e523774..1024d1995 100644 --- a/nshlib/nsh_fscmds.c +++ b/nshlib/nsh_fscmds.c @@ -420,127 +420,6 @@ static int ls_recursive(FAR struct nsh_vtbl_s *vtbl, const char *dirpath, #endif /* CONFIG_NFILE_DESCRIPTORS > 0 && !CONFIG_NSH_DISABLE_LS */ -/**************************************************************************** - * Name: cat_common - ****************************************************************************/ - -#if CONFIG_NFILE_DESCRIPTORS > 0 -#ifndef CONFIG_NSH_DISABLE_CAT -static int cat_common(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, - FAR const char *filename) -{ - FAR char *buffer; - int fd; - int ret = OK; - - /* Open the file for reading */ - - fd = open(filename, O_RDONLY); - if (fd < 0) - { - nsh_output(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO); - return ERROR; - } - - buffer = (FAR char *)malloc(IOBUFFERSIZE); - if(buffer == NULL) - { - (void)close(fd); - nsh_output(vtbl, g_fmtcmdfailed, cmd, "malloc", NSH_ERRNO); - return ERROR; - } - - /* And just dump it byte for byte into stdout */ - - for (;;) - { - int nbytesread = read(fd, buffer, IOBUFFERSIZE); - - /* Check for read errors */ - - if (nbytesread < 0) - { - int errval = errno; - - /* EINTR is not an error (but will stop stop the cat) */ - -#ifndef CONFIG_DISABLE_SIGNALS - if (errval == EINTR) - { - nsh_output(vtbl, g_fmtsignalrecvd, cmd); - } - else -#endif - { - nsh_output(vtbl, g_fmtcmdfailed, cmd, "read", NSH_ERRNO_OF(errval)); - } - - ret = ERROR; - break; - } - - /* Check for data successfully read */ - - else if (nbytesread > 0) - { - int nbyteswritten = 0; - - while (nbyteswritten < nbytesread) - { - ssize_t n = nsh_write(vtbl, buffer, nbytesread); - if (n < 0) - { - int errval = errno; - - /* EINTR is not an error (but will stop stop the cat) */ - -#ifndef CONFIG_DISABLE_SIGNALS - if (errval == EINTR) - { - nsh_output(vtbl, g_fmtsignalrecvd, cmd); - } - else -#endif - { - nsh_output(vtbl, g_fmtcmdfailed, cmd, "write", NSH_ERRNO); - } - - ret = ERROR; - break; - } - else - { - nbyteswritten += n; - } - } - } - - /* Otherwise, it is the end of file */ - - else - { - break; - } - } - - /* Make sure that the following NSH prompt appears on a new line. If the - * file ends in a newline, then this will print an extra blank line - * before the prompt, but that is preferable to the case where there is - * no newline and the NSH prompt appears on the same line as the cat'ed - * file. - */ - - nsh_output(vtbl, "\n"); - - /* Close the input file and return the result */ - - (void)close(fd); - free(buffer); - return ret; -} -#endif -#endif - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -632,7 +511,7 @@ int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { /* Dump the file to the console */ - ret = cat_common(vtbl, argv[0], fullpath); + ret = nsh_catfile(vtbl, argv[0], fullpath); /* Free the allocated full path */ @@ -653,7 +532,7 @@ int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) defined(CONFIG_RAMLOG_SYSLOG) && !defined(CONFIG_NSH_DISABLE_DMESG) int cmd_dmesg(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { - return cat_common(vtbl, argv[0], CONFIG_SYSLOG_DEVPATH); + return nsh_catfile(vtbl, argv[0], CONFIG_SYSLOG_DEVPATH); } #endif @@ -669,7 +548,7 @@ int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) char *srcpath = NULL; char *destpath = NULL; char *allocpath = NULL; - int oflags = O_WRONLY|O_CREAT|O_TRUNC; + int oflags = O_WRONLY | O_CREAT | O_TRUNC; int rdfd; int wrfd; int ret = ERROR; diff --git a/nshlib/nsh_fsutils.c b/nshlib/nsh_fsutils.c new file mode 100644 index 000000000..f88455925 --- /dev/null +++ b/nshlib/nsh_fsutils.c @@ -0,0 +1,196 @@ +/**************************************************************************** + * apps/nshlib/nsh_fsutils.c + * + * Copyright (C) 2015 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 + +#if 0 +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#include "nsh.h" +#include "nsh_console.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nsh_catfile + * + * Description: + * Dump the contents of a file to the current NSH terminal. + * + * Input Paratemets: + * vtbl - session vtbl + * cmd - NSH command name to use in error reporting + * filepath - The full path to the file to be dumped + * + * Returned Value: + * Zero (OK) on success; -1 (ERROR) on failure. + * + ****************************************************************************/ + +#if CONFIG_NFILE_DESCRIPTORS > 0 +int nsh_catfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, + FAR const char *filepath) +{ + FAR char *buffer; + int fd; + int ret = OK; + + /* Open the file for reading */ + + fd = open(filepath, O_RDONLY); + if (fd < 0) + { + nsh_output(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO); + return ERROR; + } + + buffer = (FAR char *)malloc(IOBUFFERSIZE); + if(buffer == NULL) + { + (void)close(fd); + nsh_output(vtbl, g_fmtcmdfailed, cmd, "malloc", NSH_ERRNO); + return ERROR; + } + + /* And just dump it byte for byte into stdout */ + + for (;;) + { + int nbytesread = read(fd, buffer, IOBUFFERSIZE); + + /* Check for read errors */ + + if (nbytesread < 0) + { + int errval = errno; + + /* EINTR is not an error (but will stop stop the cat) */ + +#ifndef CONFIG_DISABLE_SIGNALS + if (errval == EINTR) + { + nsh_output(vtbl, g_fmtsignalrecvd, cmd); + } + else +#endif + { + nsh_output(vtbl, g_fmtcmdfailed, cmd, "read", NSH_ERRNO_OF(errval)); + } + + ret = ERROR; + break; + } + + /* Check for data successfully read */ + + else if (nbytesread > 0) + { + int nbyteswritten = 0; + + while (nbyteswritten < nbytesread) + { + ssize_t n = nsh_write(vtbl, buffer, nbytesread); + if (n < 0) + { + int errval = errno; + + /* EINTR is not an error (but will stop stop the cat) */ + +#ifndef CONFIG_DISABLE_SIGNALS + if (errval == EINTR) + { + nsh_output(vtbl, g_fmtsignalrecvd, cmd); + } + else +#endif + { + nsh_output(vtbl, g_fmtcmdfailed, cmd, "write", NSH_ERRNO); + } + + ret = ERROR; + break; + } + else + { + nbyteswritten += n; + } + } + } + + /* Otherwise, it is the end of file */ + + else + { + break; + } + } + + /* Make sure that the following NSH prompt appears on a new line. If the + * file ends in a newline, then this will print an extra blank line + * before the prompt, but that is preferable to the case where there is + * no newline and the NSH prompt appears on the same line as the cat'ed + * file. + */ + + nsh_output(vtbl, "\n"); + + /* Close the input file and return the result */ + + (void)close(fd); + free(buffer); + return ret; +} +#endif + diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index be3618043..58e24e60e 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -274,187 +274,12 @@ errout: * Name: net_statistics ****************************************************************************/ -#if defined(CONFIG_NET_STATISTICS) && !defined(CONFIG_NSH_DISABLE_IFCONFIG) +#if defined(CONFIG_NET_STATISTICS) && defined(CONFIG_FS_PROCFS) && \ + !defined(CONFIG_FS_PROCFS_EXCLUDE_NET) && \ + !defined(CONFIG_NSH_DISABLE_IFCONFIG) static inline void net_statistics(FAR struct nsh_vtbl_s *vtbl) { - /* Headings */ - - nsh_output(vtbl, " "); -#ifdef CONFIG_NET_IPv4 - nsh_output(vtbl, " IPv4"); -#endif -#ifdef CONFIG_NET_IPv6 - nsh_output(vtbl, " IPv6"); -#endif -#ifdef CONFIG_NET_TCP - nsh_output(vtbl, " TCP"); -#endif -#ifdef CONFIG_NET_UDP - nsh_output(vtbl, " UDP"); -#endif -#ifdef CONFIG_NET_ICMP - nsh_output(vtbl, " ICMP"); -#endif -#ifdef CONFIG_NET_ICMPv6 - nsh_output(vtbl, " ICMPv6"); -#endif - nsh_output(vtbl, "\n"); - - /* Received packets */ - - nsh_output(vtbl, "Received "); -#ifdef CONFIG_NET_IPv4 - nsh_output(vtbl, " %04x", g_netstats.ipv4.recv); -#endif -#ifdef CONFIG_NET_IPv6 - nsh_output(vtbl, " %04x", g_netstats.ipv6.recv); -#endif -#ifdef CONFIG_NET_TCP - nsh_output(vtbl, " %04x", g_netstats.tcp.recv); -#endif -#ifdef CONFIG_NET_UDP - nsh_output(vtbl, " %04x", g_netstats.udp.recv); -#endif -#ifdef CONFIG_NET_ICMP - nsh_output(vtbl, " %04x", g_netstats.icmp.recv); -#endif -#ifdef CONFIG_NET_ICMPv6 - nsh_output(vtbl, " %04x", g_netstats.icmpv6.recv); -#endif - nsh_output(vtbl, "\n"); - - /* Dropped packets */ - - nsh_output(vtbl, "Dropped "); -#ifdef CONFIG_NET_IPv4 - nsh_output(vtbl, " %04x", g_netstats.ipv4.drop); -#endif -#ifdef CONFIG_NET_IPv6 - nsh_output(vtbl, " %04x", g_netstats.ipv6.drop); -#endif -#ifdef CONFIG_NET_TCP - nsh_output(vtbl, " %04x", g_netstats.tcp.drop); -#endif -#ifdef CONFIG_NET_UDP - nsh_output(vtbl, " %04x", g_netstats.udp.drop); -#endif -#ifdef CONFIG_NET_ICMP - nsh_output(vtbl, " %04x", g_netstats.icmp.drop); -#endif -#ifdef CONFIG_NET_ICMPv6 - nsh_output(vtbl, " %04x", g_netstats.icmpv6.drop); -#endif - nsh_output(vtbl, "\n"); - - /* Dropped IP packets */ - -#ifdef CONFIG_NET_IPv4 - nsh_output(vtbl, " IPv4 VHL: %04x Frg: %04x\n", - g_netstats.ipv4.vhlerr, g_netstats.ipv4.fragerr); -#endif -#ifdef CONFIG_NET_IPv6 - nsh_output(vtbl, " IPv6 VHL: %04x\n", - g_netstats.ipv6.vhlerr); -#endif - - /* Checksum errors */ - - nsh_output(vtbl, " Checksum "); -#ifdef CONFIG_NET_IPv4 - nsh_output(vtbl, " %04x", g_netstats.ipv4.chkerr); -#endif -#ifdef CONFIG_NET_IPv6 - nsh_output(vtbl, " ----"); -#endif -#ifdef CONFIG_NET_TCP - nsh_output(vtbl, " %04x", g_netstats.tcp.chkerr); -#endif -#ifdef CONFIG_NET_UDP - nsh_output(vtbl, " %04x", g_netstats.udp.chkerr); -#endif -#ifdef CONFIG_NET_ICMP - nsh_output(vtbl, " ----"); -#endif -#ifdef CONFIG_NET_ICMPv6 - nsh_output(vtbl, " ----"); -#endif - nsh_output(vtbl, "\n"); - -#ifdef CONFIG_NET_TCP - nsh_output(vtbl, " TCP ACK: %04x SYN: %04x\n", - g_netstats.tcp.ackerr, g_netstats.tcp.syndrop); - nsh_output(vtbl, " RST: %04x %04x\n", - g_netstats.tcp.rst, g_netstats.tcp.synrst); -#endif - - /* Prototype errors */ - - nsh_output(vtbl, " Type "); -#ifdef CONFIG_NET_IPv4 - nsh_output(vtbl, " %04x", g_netstats.ipv4.protoerr); -#endif -#ifdef CONFIG_NET_IPv6 - nsh_output(vtbl, " %04x", g_netstats.ipv6.protoerr); -#endif -#ifdef CONFIG_NET_TCP - nsh_output(vtbl, " ----"); -#endif -#ifdef CONFIG_NET_UDP - nsh_output(vtbl, " ----"); -#endif -#ifdef CONFIG_NET_ICMP - nsh_output(vtbl, " %04x", g_netstats.icmp.typeerr); -#endif -#ifdef CONFIG_NET_ICMPv6 - nsh_output(vtbl, " %04x", g_netstats.icmpv6.typeerr); -#endif - nsh_output(vtbl, "\n"); - - /* Sent packets */ - - nsh_output(vtbl, "Sent "); -#ifdef CONFIG_NET_IPv4 - nsh_output(vtbl, " %04x", g_netstats.ipv4.sent); -#endif -#ifdef CONFIG_NET_IPv6 - nsh_output(vtbl, " %04x", g_netstats.ipv6.sent); -#endif -#ifdef CONFIG_NET_TCP - nsh_output(vtbl, " %04x", g_netstats.tcp.sent); -#endif -#ifdef CONFIG_NET_UDP - nsh_output(vtbl, " %04x", g_netstats.udp.sent); -#endif -#ifdef CONFIG_NET_ICMP - nsh_output(vtbl, " %04x", g_netstats.icmp.sent); -#endif -#ifdef CONFIG_NET_ICMPv6 - nsh_output(vtbl, " %04x", g_netstats.icmpv6.sent); -#endif - nsh_output(vtbl, "\n"); - - /* TCP retransmissions */ - -#ifdef CONFIG_NET_TCP - nsh_output(vtbl, " Rexmit "); -#ifdef CONFIG_NET_IPv4 - nsh_output(vtbl, " ----"); -#endif -#ifdef CONFIG_NET_IPv6 - nsh_output(vtbl, " ----"); -#endif - nsh_output(vtbl, " %04x", g_netstats.tcp.rexmit); -#ifdef CONFIG_NET_UDP - nsh_output(vtbl, " ----"); -#endif -#ifdef CONFIG_NET_ICMP - nsh_output(vtbl, " ----"); -#endif -#ifdef CONFIG_NET_ICMPv6 - nsh_output(vtbl, " ----"); -#endif - nsh_output(vtbl, "\n"); -#endif /* CONFIG_NET_TCP */ + (void)nsh_catfile(vtbl, "ifconfig", "/proc/net/stat"); } #else # define net_statistics(vtbl)