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 <liqinhui@xiaomi.com>
This commit is contained in:
liqinhui
2023-08-09 16:25:24 +08:00
committed by Xiang Xiao
parent 0100e8338e
commit 0d39246b4e
3 changed files with 58 additions and 0 deletions
+1
View File
@@ -143,6 +143,7 @@ NXSYMBOLS(strlen)
NXSYMBOLS(strtol)
NXSYMBOLS(sysconf)
NXSYMBOLS(syslog)
NXSYMBOLS(system)
NXSYMBOLS(tcgetattr)
NXSYMBOLS(tcsetattr)
NXSYMBOLS(unlink)
+56
View File
@@ -29,6 +29,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/wait.h>
#include <sys/types.h>
@@ -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
****************************************************************************/
+1
View File
@@ -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);