]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
script: fix ECHO use, improve shell exec
authorKarel Zak <kzak@redhat.com>
Thu, 21 Nov 2019 11:28:51 +0000 (12:28 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Nov 2019 11:28:51 +0000 (12:28 +0100)
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 <kzak@redhat.com>
include/pty-session.h
lib/pty-session.c
term-utils/script.c

index 2adad102ec17ff4df458a2928e4ad6a87f0afafb..30932d03515064f49f01ffc545a588367aef6aca 100644 (file)
@@ -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);
index 5486fd06883c8576b1f30426b1cb0953f1a9b442..a7fb7d40b5cf52c82462a7eb818a6e97cdd451ca 100644 (file)
@@ -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;
        }
index 8d0b45f646e2c10b9b27431f9beb6e6ffb46142c..f9494fd507ee33f269d1b2509994d60bbc6e50b9 100644 (file)
@@ -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;
        }