From: Karel Zak Date: Thu, 21 Nov 2019 11:28:51 +0000 (+0100) Subject: script: fix ECHO use, improve shell exec X-Git-Tag: v2.35-rc1~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4169bcb766dd6cdbcbb7c43fe91bb7bc450a0078;p=thirdparty%2Futil-linux.git script: fix ECHO use, improve shell exec For tools like su(1) is ECHO flag unexpected for use-case like echo 'date' | su - user but script(1) need the echo to keep input recorded. The patch also return execlp() use to script(1) code. Signed-off-by: Karel Zak --- diff --git a/include/pty-session.h b/include/pty-session.h index 2adad102ec..30932d0351 100644 --- a/include/pty-session.h +++ b/include/pty-session.h @@ -75,13 +75,15 @@ struct ul_pty { struct timeval next_callback_time; - unsigned int isterm:1; /* is stdin terminal? */ + unsigned int isterm:1, /* is stdin terminal? */ + keep_slave_echo:1; /* keep ECHO on stdin */ }; void ul_pty_init_debug(int mask); struct ul_pty *ul_new_pty(int is_stdin_tty); void ul_free_pty(struct ul_pty *pty); +void ul_pty_keep_slave_echo(struct ul_pty *pty, int enable); int ul_pty_get_delivered_signal(struct ul_pty *pty); void ul_pty_set_callback_data(struct ul_pty *pty, void *data); diff --git a/lib/pty-session.c b/lib/pty-session.c index 5486fd0688..a7fb7d40b5 100644 --- a/lib/pty-session.c +++ b/lib/pty-session.c @@ -71,6 +71,11 @@ void ul_free_pty(struct ul_pty *pty) free(pty); } +void ul_pty_keep_slave_echo(struct ul_pty *pty, int enable) +{ + assert(pty); + pty->keep_slave_echo = enable ? 1 : 0; +} int ul_pty_get_delivered_signal(struct ul_pty *pty) { @@ -175,8 +180,10 @@ int ul_pty_setup(struct ul_pty *pty) if (!rc) { tcgetattr(pty->slave, &slave_attrs); - slave_attrs.c_lflag &= ~ECHO; - tcsetattr(pty->slave, TCSANOW, &slave_attrs); + if (!pty->keep_slave_echo) { + slave_attrs.c_lflag &= ~ECHO; + tcsetattr(pty->slave, TCSANOW, &slave_attrs); + } } else goto done; } diff --git a/term-utils/script.c b/term-utils/script.c index 8d0b45f646..f9494fd507 100644 --- a/term-utils/script.c +++ b/term-utils/script.c @@ -875,6 +875,13 @@ int main(int argc, char **argv) if (!ctl.pty) err(EXIT_FAILURE, "failed to allocate PTY handler"); + if (!ctl.isterm) + /* We keep ECHO flag for 'echo "date" | script' otherwise the + * input is no visible (output is completely comtroled by + * shell/command, we do not write anything there). + */ + ul_pty_keep_slave_echo(ctl.pty, 1); + ul_pty_set_callback_data(ctl.pty, (void *) &ctl); cb = ul_pty_get_callbacks(ctl.pty); cb->child_die = callback_child_die; @@ -923,10 +930,18 @@ int main(int argc, char **argv) shname = strrchr(shell, '/'); shname = shname ? shname + 1 : shell; - if (command) - execl(shell, shname, "-c", command, NULL); - else - execl(shell, shname, "-i", NULL); + if (access(shell, X_OK) == 0) { + if (command) + execl(shell, shname, "-c", command, NULL); + else + execl(shell, shname, "-i", NULL); + } else { + if (command) + execlp(shname, "-c", command, NULL); + else + execlp(shname, "-i", NULL); + } + err(EXIT_FAILURE, "failed to execute %s", shell); break; }