All of lore.kernel.org
 help / color / mirror / Atom feed
* problem with script -e
@ 2012-06-30 16:17 Iain Paton
  2012-09-04 15:45 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Iain Paton @ 2012-06-30 16:17 UTC (permalink / raw)
  To: util-linux

Came across an unexpected feature with script today. Can't make my mind up if 
it's a bug or not, but it certainly makes some things difficult.

Easy to reproduce, as follows

1. create a shell script with the following contents

#!/bin/bash
sleep 5
exit 42

2. run it under script

$ time script -e -c ./tst.sh ; echo $?
Script started, file is typescript
Script done, file is typescript

real    0m5.320s
user    0m0.020s
sys     0m0.008s
42

results as expected..


3. make it fail

$ time script -e -c ./tst.sh < /dev/null ; echo $?
Script started, file is typescript
Script done, file is typescript

real    0m0.012s
user    0m0.004s
sys     0m0.003s
0

The script still runs in a forked process, the parent has just exited early.

You can redirect just about anything as stdin for script and get the same 
problem, doesn't have to be /dev/null.

Ok, so if I want to redirect a file into the thing running under script there's 
probably other ways to do it, but still.

I stumbled across this while trying to capture the output of something being 
ran remotely by a daemon where there's no controlling tty, stdin/stdout/stderr 
are all redirected to /dev/null and the scripts return code is being checked.

Run it normally and an error is logged, try running it under script to find 
out some details of the error and of course there's no error logged... 
At least until you look at the output.

Tracked it down to the doinput function that deals with read(STDIN_FILENO...
being greater than zero or less than zero, but if it returns 0 for EOF then it 
ignores the die == 0 condition and exits.
Seems that there's an implicit assumption that stdin will always be a tty for 
the script process - strace shows all sorts of -ENOTTY stuff when it's not.

I came up with the following that works for my use case, but maybe there's a 
better way ?

--- script.c.org	2011-08-29 09:31:19.000000000 +0100
+++ script.c	2012-06-30 16:24:42.896997422 +0100
@@ -295,18 +295,29 @@
 
 	fclose(fscript);
 
-	while (die == 0) {
-		if ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
-			ssize_t wrt = write(master, ibuf, cc);
-			if (wrt < 0) {
-				warn (_("write failed"));
-				fail();
+	if(isatty(STDIN_FILENO)) {
+
+		while (die == 0) {
+			if ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
+				ssize_t wrt = write(master, ibuf, cc);
+				if (wrt < 0) {
+					warn (_("write failed"));
+					fail();
+				}
 			}
+			else if (cc < 0 && errno == EINTR && resized)
+				resized = 0;
+			else
+				break;
+		}
+	} else {
+	
+		while(die == 0) {
+			
+			fd_set fs;
+			FD_ZERO(&fs);
+			select(1, &fs, &fs, &fs, NULL);
 		}
-		else if (cc < 0 && errno == EINTR && resized)
-			resized = 0;
-		else
-			break;
 	}
 
 	done();





^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: problem with script -e
  2012-06-30 16:17 problem with script -e Iain Paton
@ 2012-09-04 15:45 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2012-09-04 15:45 UTC (permalink / raw)
  To: Iain Paton; +Cc: util-linux

On Sat, Jun 30, 2012 at 05:17:36PM +0100, Iain Paton wrote:
> Came across an unexpected feature with script today. Can't make my mind up if 
> it's a bug or not, but it certainly makes some things difficult.

 Sorry for delay...

> Seems that there's an implicit assumption that stdin will always be a tty for 
> the script process - strace shows all sorts of -ENOTTY stuff when it's not.

 Yes, "script - make typescript of terminal sessions". It initializes
 a new pseudoterminal and makes connection between the current session
 and the new terminal.

> I came up with the following that works for my use case, but maybe there's a 
> better way ?
> 
> --- script.c.org	2011-08-29 09:31:19.000000000 +0100
> +++ script.c	2012-06-30 16:24:42.896997422 +0100
> @@ -295,18 +295,29 @@
>  
>  	fclose(fscript);
>  
> -	while (die == 0) {
> -		if ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
> -			ssize_t wrt = write(master, ibuf, cc);
> -			if (wrt < 0) {
> -				warn (_("write failed"));
> -				fail();
> +	if(isatty(STDIN_FILENO)) {
> +
> +		while (die == 0) {
> +			if ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
> +				ssize_t wrt = write(master, ibuf, cc);
> +				if (wrt < 0) {
> +					warn (_("write failed"));
> +					fail();
> +				}
>  			}
> +			else if (cc < 0 && errno == EINTR && resized)
> +				resized = 0;
> +			else
> +				break;
> +		}
> +	} else {
> +	
> +		while(die == 0) {
> +			
> +			fd_set fs;
> +			FD_ZERO(&fs);
> +			select(1, &fs, &fs, &fs, NULL);


 Hmm... what the others things like tcsetattr(), etc. Anyway, I'll try
 to play with the patch. It seems like interesting idea.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-09-04 15:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-30 16:17 problem with script -e Iain Paton
2012-09-04 15:45 ` Karel Zak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.