From 80feb2432d9dfea262ea8a4fb3b9942bb384d44b Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Wed, 22 Feb 2023 13:13:45 +0800 Subject: [PATCH] nshlib: Shouldn't set TIOCSCTTY with STDOUT_FILENO directly to handle the shell redirection correctly Signed-off-by: Xiang Xiao --- nshlib/nsh_builtin.c | 4 ++-- nshlib/nsh_console.c | 33 ++++++++++++++++++++++++++------- nshlib/nsh_console.h | 2 ++ nshlib/nsh_fileapps.c | 4 ++-- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/nshlib/nsh_builtin.c b/nshlib/nsh_builtin.c index 877af0fc1..2e2c6d10f 100644 --- a/nshlib/nsh_builtin.c +++ b/nshlib/nsh_builtin.c @@ -136,7 +136,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, { /* Setup up to receive SIGINT if control-C entered. */ - tc = ioctl(STDOUT_FILENO, TIOCSCTTY, ret); + tc = nsh_ioctl(vtbl, TIOCSCTTY, ret); } /* Wait for the application to exit. We did lock the scheduler @@ -201,7 +201,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, if (vtbl->isctty && tc == 0) { - ioctl(STDOUT_FILENO, TIOCNOTTY); + nsh_ioctl(vtbl, TIOCNOTTY, 0); } } # ifndef CONFIG_NSH_DISABLEBG diff --git a/nshlib/nsh_console.c b/nshlib/nsh_console.c index e12a88c80..6494992b8 100644 --- a/nshlib/nsh_console.c +++ b/nshlib/nsh_console.c @@ -57,18 +57,20 @@ static FAR struct nsh_vtbl_s *nsh_consoleclone(FAR struct nsh_vtbl_s *vtbl); #endif static void nsh_consolerelease(FAR struct nsh_vtbl_s *vtbl); static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl, - FAR const void *buffer, size_t nbytes); + FAR const void *buffer, size_t nbytes); +static int nsh_consoleioctl(FAR struct nsh_vtbl_s *vtbl, + int cmd, unsigned long arg); static int nsh_consoleoutput(FAR struct nsh_vtbl_s *vtbl, - FAR const char *fmt, ...) printf_like(2, 3); + FAR const char *fmt, ...) printf_like(2, 3); static int nsh_erroroutput(FAR struct nsh_vtbl_s *vtbl, - FAR const char *fmt, ...) printf_like(2, 3); + FAR const char *fmt, ...) printf_like(2, 3); static FAR char *nsh_consolelinebuffer(FAR struct nsh_vtbl_s *vtbl); static void nsh_consoleredirect(FAR struct nsh_vtbl_s *vtbl, int fd, - FAR uint8_t *save); + FAR uint8_t *save); static void nsh_consoleundirect(FAR struct nsh_vtbl_s *vtbl, - FAR uint8_t *save); -static void nsh_consoleexit(FAR struct nsh_vtbl_s *vtbl, int exitstatus) - noreturn_function; + FAR uint8_t *save); +static void nsh_consoleexit(FAR struct nsh_vtbl_s *vtbl, + int exitstatus) anoreturn_function; /**************************************************************************** * Private Functions @@ -127,6 +129,22 @@ static ssize_t nsh_consolewrite(FAR struct nsh_vtbl_s *vtbl, return ret; } +/**************************************************************************** + * Name: nsh_consolewrite + * + * Description: + * Issue ioctl to the currently selected stream. + * + ****************************************************************************/ + +static int nsh_consoleioctl(FAR struct nsh_vtbl_s *vtbl, + int cmd, unsigned long arg) +{ + FAR struct console_stdio_s *pstate = (FAR struct console_stdio_s *)vtbl; + + return ioctl(OUTFD(pstate), cmd, arg); +} + /**************************************************************************** * Name: nsh_consoleoutput * @@ -347,6 +365,7 @@ FAR struct console_stdio_s *nsh_newconsole(bool isctty) #endif pstate->cn_vtbl.release = nsh_consolerelease; pstate->cn_vtbl.write = nsh_consolewrite; + pstate->cn_vtbl.ioctl = nsh_consoleioctl; pstate->cn_vtbl.output = nsh_consoleoutput; pstate->cn_vtbl.error = nsh_erroroutput; pstate->cn_vtbl.linebuffer = nsh_consolelinebuffer; diff --git a/nshlib/nsh_console.h b/nshlib/nsh_console.h index 249480bae..4b0037b2c 100644 --- a/nshlib/nsh_console.h +++ b/nshlib/nsh_console.h @@ -43,6 +43,7 @@ #define nsh_clone(v) (v)->clone(v) #define nsh_release(v) (v)->release(v) #define nsh_write(v,b,n) (v)->write(v,b,n) +#define nsh_ioctl(v,c,a) (v)->ioctl(v,c,a) #define nsh_linebuffer(v) (v)->linebuffer(v) #define nsh_redirect(v,f,s) (v)->redirect(v,f,s) #define nsh_undirect(v,s) (v)->undirect(v,s) @@ -103,6 +104,7 @@ struct nsh_vtbl_s void (*release)(FAR struct nsh_vtbl_s *vtbl); ssize_t (*write)(FAR struct nsh_vtbl_s *vtbl, FAR const void *buffer, size_t nbytes); + int (*ioctl)(FAR struct nsh_vtbl_s *vtbl, int cmd, unsigned long arg); int (*error)(FAR struct nsh_vtbl_s *vtbl, FAR const char *fmt, ...) printf_like(2, 3); int (*output)(FAR struct nsh_vtbl_s *vtbl, FAR const char *fmt, ...) diff --git a/nshlib/nsh_fileapps.c b/nshlib/nsh_fileapps.c index e017f6c6c..804c0062d 100644 --- a/nshlib/nsh_fileapps.c +++ b/nshlib/nsh_fileapps.c @@ -204,7 +204,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, { /* Setup up to receive SIGINT if control-C entered. */ - tc = ioctl(stdout->fs_fd, TIOCSCTTY, pid); + tc = nsh_ioctl(vtbl, TIOCSCTTY, pid); } /* Wait for the application to exit. We did lock the scheduler @@ -260,7 +260,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, if (vtbl->isctty && tc == 0) { - ioctl(stdout->fs_fd, TIOCNOTTY); + nsh_ioctl(vtbl, TIOCNOTTY, 0); } } # ifndef CONFIG_NSH_DISABLEBG