nshlib: Add fdinfo to get information about the process associated fd

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
This commit is contained in:
chenrun1 2023-06-14 16:14:59 +08:00 committed by Xiang Xiao
parent ed370ec674
commit 415168dde6
4 changed files with 109 additions and 0 deletions

View File

@ -365,6 +365,11 @@ config NSH_DISABLE_EXPORT
bool "Disable export"
default DEFAULT_SMALL
config NSH_DISABLE_FDINFO
bool "Disable fdinfo"
default DEFAULT_SMALL
depends on FS_PROCFS
config NSH_DISABLE_FREE
bool "Disable free"
default DEFAULT_SMALL

View File

@ -951,6 +951,9 @@ void nsh_usbtrace(void);
#ifndef CONFIG_NSH_DISABLE_PS
int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
#endif
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_NSH_DISABLE_FDINFO)
int cmd_fdinfo(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
#endif
#ifndef CONFIG_NSH_DISABLE_XD
int cmd_xd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
#endif

View File

@ -234,6 +234,12 @@ static const struct cmdmap_s g_cmdmap[] =
CMD_MAP("false", cmd_false, 1, 1, NULL),
#endif
#ifdef CONFIG_FS_PROCFS
# ifndef CONFIG_NSH_DISABLE_FDINFO
CMD_MAP("fdinfo", cmd_fdinfo, 1, 2, "[pid]"),
# endif
#endif
#ifndef CONFIG_NSH_DISABLE_FREE
CMD_MAP("free", cmd_free, 1, 1, NULL),
#endif

View File

@ -25,6 +25,7 @@
#include <nuttx/config.h>
#include <sys/types.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdbool.h>
@ -376,6 +377,60 @@ static int ls_recursive(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
#endif /* !CONFIG_NSH_DISABLE_LS */
/****************************************************************************
* Name: fdinfo_callback
****************************************************************************/
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_NSH_DISABLE_FDINFO)
static int fdinfo_callback(FAR struct nsh_vtbl_s *vtbl,
FAR const char *dirpath,
FAR struct dirent *entryp, FAR void *pvarg)
{
FAR char *filepath;
int ret;
int i;
UNUSED(pvarg);
if (!DIRENT_ISDIRECTORY(entryp->d_type))
{
/* Not a directory, let's skip it */
return OK;
}
/* Check name */
for (i = 0; entryp->d_name[i] != '\0'; i++)
{
if (!isdigit(entryp->d_name[i]))
{
/* Name contains something other than a numeric character */
return OK;
}
}
/* Let's initialize all the information */
ret = asprintf(&filepath, "%s/%s/group/fd", dirpath, entryp->d_name);
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, "fdinfo", "asprintf", NSH_ERRNO);
}
nsh_output(vtbl, "\npid:%s", entryp->d_name);
ret = nsh_catfile(vtbl, "fdinfo", filepath);
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, "fdinfo", "nsh_catfaile", NSH_ERRNO);
}
free(filepath);
return ret;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@ -2238,3 +2293,43 @@ int cmd_truncate(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
}
#endif
#endif
/****************************************************************************
* Name: cmd_fdinfo
****************************************************************************/
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_NSH_DISABLE_FDINFO)
int cmd_fdinfo(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
UNUSED(argc);
if (argv[1] != NULL)
{
FAR char *fdpath = NULL;
int ret;
/* The directories of the processes are displayed numerically */
if (!isdigit(argv[1][0]))
{
nsh_error(vtbl, g_fmtcmdfailed, "fdinfo",
"not process id", NSH_ERRNO);
return ERROR;
}
ret = asprintf(&fdpath, "%s/%s/group/fd",
CONFIG_NSH_PROC_MOUNTPOINT, argv[1]);
if (ret < 0)
{
return ret;
}
ret = nsh_catfile(vtbl, argv[0], fdpath);
free(fdpath);
return ret;
}
return nsh_foreach_direntry(vtbl, "fdinfo", CONFIG_NSH_PROC_MOUNTPOINT,
fdinfo_callback, NULL);
}
#endif