From 0d39246b4eeb273b203e86ec23475b947ffec093 Mon Sep 17 00:00:00 2001 From: liqinhui Date: Wed, 9 Aug 2023 16:25:24 +0800 Subject: [PATCH] sim/posix: Add the host_system interface used to execute the host command Encapsulate the host system/popen interface to host_system. Signed-off-by: liqinhui --- arch/sim/src/nuttx-names.in | 1 + arch/sim/src/sim/posix/sim_hostmisc.c | 56 +++++++++++++++++++++++++++ arch/sim/src/sim/sim_internal.h | 1 + 3 files changed, 58 insertions(+) diff --git a/arch/sim/src/nuttx-names.in b/arch/sim/src/nuttx-names.in index bb4e201295..ace70ee1b0 100644 --- a/arch/sim/src/nuttx-names.in +++ b/arch/sim/src/nuttx-names.in @@ -143,6 +143,7 @@ NXSYMBOLS(strlen) NXSYMBOLS(strtol) NXSYMBOLS(sysconf) NXSYMBOLS(syslog) +NXSYMBOLS(system) NXSYMBOLS(tcgetattr) NXSYMBOLS(tcsetattr) NXSYMBOLS(unlink) diff --git a/arch/sim/src/sim/posix/sim_hostmisc.c b/arch/sim/src/sim/posix/sim_hostmisc.c index 8be18a05d7..b23403c24c 100644 --- a/arch/sim/src/sim/posix/sim_hostmisc.c +++ b/arch/sim/src/sim/posix/sim_hostmisc.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -108,6 +109,61 @@ int host_backtrace(void** array, int size) #endif } +/**************************************************************************** + * Name: host_system + * + * Description: + * Execute the command and get the result. + * + * Input Parameters: + * buf - return massage, which return info will be stored + * len - buf length + * fmt - the format of parameters + * ... - variable parameters. + * + * Returned Value: + * A nonnegative integer is returned on success. Otherwise, + * a negated errno value is returned to indicate the nature of the failure. + ****************************************************************************/ + +int host_system(char *buf, size_t len, const char *fmt, ...) +{ + FILE *fp; + int ret; + uint64_t flags; + char cmd[512]; + va_list vars; + + va_start(vars, fmt); + ret = vsnprintf(cmd, sizeof(cmd), fmt, vars); + va_end(vars); + if (ret <= 0 || ret > sizeof(cmd)) + { + return ret < 0 ? -errno : -EINVAL; + } + + if (buf == NULL) + { + ret = system(cmd); + } + else + { + flags = up_irq_save(); + fp = popen(cmd, "r"); + if (fp == NULL) + { + up_irq_restore(flags); + return -errno; + } + + ret = fread(buf, sizeof(char), len, fp); + pclose(fp); + up_irq_restore(flags); + } + + return ret < 0 ? -errno : ret; +} + /**************************************************************************** * Name: host_init_cwd ****************************************************************************/ diff --git a/arch/sim/src/sim/sim_internal.h b/arch/sim/src/sim/sim_internal.h index ef78bb891b..47b0084a22 100644 --- a/arch/sim/src/sim/sim_internal.h +++ b/arch/sim/src/sim/sim_internal.h @@ -163,6 +163,7 @@ void *sim_doirq(int irq, void *regs); void host_abort(int status); int host_backtrace(void** array, int size); +int host_system(char *buf, size_t len, const char *fmt, ...); #ifdef CONFIG_SIM_IMAGEPATH_AS_CWD void host_init_cwd(void);