Skip to contentSkip to conversation

script: Also flush writes to timing file.

PATCH In review Jesper Dahl Nyerup 3 participants 7 messages Feb 17, 2014 – Feb 23, 2014 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 script: Also flush writes to timing file.

If both -f and -t are given, flush the timing fd on each write, similar to the behavior on the script fd. This allows playback of still-running sessions, and reduces the risk of ending up with empty timing files when script(1) exits abnormally.

---
 term-utils/script.c | 2 ++
 1 file changed, 2 insertions(+)
term-utils/script.c +2 -0
index a4c2e0c..49f21b6 100644
@@ -442,6 +442,8 @@ dooutput(FILE *timingfd) {
 		}
 		if (fflg)
 			fflush(fscript);
+			if (tflg)
+				fflush(timingfd);
 		if (write_all(STDOUT_FILENO, obuf, cc)) {
 			warn (_("write failed"));
 			fail();
Signature · 1 line
1.8.4
#1 lore raw
Quoted 4 lines · Jesper Dahl Nyerup

If both -f and -t are given, flush the timing fd on each write, similar to the behavior on the script fd. This allows playback of still-running sessions, and reduces the risk of ending up with empty timing files when script(1) exits abnormally.

Hi Jesper,

Instead of flushing all the time how about checking write status at the end? See attached patch how that could work.

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/

From 6b1bb220173b69d1b3ea80153be08f7a8661775b Mon Sep 17 00:00:00 2001
From: Jesper Dahl Nyerup <[email protected]>
Date: Mon, 17 Feb 2014 22:43:06 +0100
Subject: [PATCH] script: Also flush writes to timing file.
Organization: lastminute.com

If both -f and -t are given, flush the timing fd on each write, similar to the behavior on the script fd. This allows playback of still-running sessions, and reduces the risk of ending up with empty timing files when script(1) exits abnormally.

[[email protected]: - check timing file write status in done()]

---
 term-utils/script.c | 42 ++++++++++++++++++++----------------------
 1 file changed, 20 insertions(+), 22 deletions(-)
term-utils/script.c +20 -22
index a4c2e0c..4b816ec 100644
@@ -78,8 +78,8 @@
 #define DEFAULT_OUTPUT "typescript"
 
 void finish(int);
-void done(void);
-void fail(void);
+void done(FILE *timingfd);
+void fail(FILE *timingfd);
 void resize(int);
 void fixtty(void);
 void getmaster(void);
@@ -237,7 +237,7 @@ main(int argc, char **argv) {
 
 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
 		warn(_("cannot open %s"), fname);
-		fail();
+		fail(timingfd);
 	}
 
 	shell = getenv("SHELL");
@@ -268,7 +268,7 @@ main(int argc, char **argv) {
 
 	if (child < 0) {
 		warn(_("fork failed"));
-		fail();
+		fail(timingfd);
 	}
 	if (child == 0) {
 
@@ -278,7 +278,7 @@ main(int argc, char **argv) {
 
 		if (child < 0) {
 			warn(_("fork failed"));
-			fail();
+			fail(timingfd);
 		}
 		if (child) {
 			if (!timingfd)
@@ -319,7 +319,7 @@ doinput(void) {
 		if ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
 			if (write_all(master, ibuf, cc)) {
 				warn (_("write failed"));
-				fail();
+				fail(NULL);
 			}
 		}
 		else if (cc < 0 && errno == EINTR && resized)
@@ -355,7 +355,7 @@ doinput(void) {
 
 		if (write_all(master, &c, 1)) {
 			warn (_("write failed"));
-			fail();
+			fail(NULL);
 		}
 
 		/* wait for "exit" message from shell before we print "Script
@@ -365,7 +365,7 @@ doinput(void) {
 
 	if (!die)
 		finish(0);	/* wait for childern */
-	done();
+	done(NULL);
 }
 
 void
@@ -438,19 +438,15 @@ dooutput(FILE *timingfd) {
 		}
 		if (fwrite_all(obuf, 1, cc, fscript)) {
 			warn (_("cannot write script file"));
-			fail();
+			fail(timingfd);
 		}
-		if (fflg)
-			fflush(fscript);
 		if (write_all(STDOUT_FILENO, obuf, cc)) {
 			warn (_("write failed"));
-			fail();
+			fail(timingfd);
 		}
 	} while(1);
 
-	if (close_stream(timingfd) != 0)
-		errx(EXIT_FAILURE, _("write error"));
-	done();
+	done(timingfd);
 }
 
 void
@@ -496,7 +492,7 @@ doshell(void) {
 			execlp(shname, "-i", NULL);
 	}
 	warn(_("failed to execute %s"), shell);
-	fail();
+	fail(NULL);
 }
 
 void
@@ -513,14 +509,14 @@ fixtty(void) {
 }
 
 void
-fail(void) {
+fail(FILE *timingfd) {
 
 	kill(0, SIGTERM);
-	done();
+	done(timingfd);
 }
 
 void __attribute__((__noreturn__))
-done(void) {
+done(FILE *timingfd) {
 	time_t tvec;
 
 	if (subchild) {
@@ -530,8 +526,10 @@ done(void) {
 		my_strftime(buf, sizeof buf, "%c\n", localtime(&tvec));
 		fprintf(fscript, _("\nScript done on %s"), buf);
 
+		if (timingfd && close_stream(timingfd) != 0)
+			warn(_("write error"));
 		if (close_stream(fscript) != 0)
-			errx(EXIT_FAILURE, _("write error"));
+			err(EXIT_FAILURE, _("write error"));
 		close(master);
 
 		master = -1;
@@ -574,7 +572,7 @@ getmaster(void) {
 
 	if (rc < 0) {
 		warn(_("openpty failed"));
-		fail();
+		fail(NULL);
 	}
 #else
 	char *pty, *bank, *cp;
@@ -614,7 +612,7 @@ getmaster(void) {
 	}
 	master = -1;
 	warn(_("out of pty's"));
-	fail();
+	fail(NULL);
 #endif /* not HAVE_LIBUTIL */
 }
 
Signature · 1 line
1.9.0
#2 lore raw
Quoted 8 lines · Sami Kerola
Quoted 4 lines · Jesper Dahl Nyerup

If both -f and -t are given, flush the timing fd on each write, similar to the behavior on the script fd. This allows playback of still-running sessions, and reduces the risk of ending up with empty timing files when script(1) exits abnormally.

Instead of flushing all the time how about checking write status at the end? See attached patch how that could work.

That's a good idea, and should take care of the scenario where an abnormal exit leads to empty timings The other use case however, scriptreplay(1)'ing transcripts while script(1) is still running, would not be improved upon.

Besides, the man page describes the -f option behavior as Flush output after each write. I would expect this to apply to the timing fd as well, and honoring this description was the main purpose of my patch.

I think it'd be great if both patches were applied.

J.

Signature · 3 lines
Jesper Dahl Nyerup
Systems Engineer
One.com, [email protected]
#3 lore raw

PATCH 1/1 script: Also flush writes to timing file.

In reply to Jesper Dahl Nyerup

If both -f and -t are given, flush the timing fd on each write, similar to the behavior on the script fd. This allows playback of still-running sessions, and reduces the risk of ending up with empty timing files when script(1) exits abnormally.

---
 term-utils/script.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
term-utils/script.c +4 -1
index a4c2e0c..d6e04d6 100644
@@ -440,8 +440,11 @@ dooutput(FILE *timingfd) {
 			warn (_("cannot write script file"));
 			fail();
 		}
-		if (fflg)
+		if (fflg) {
 			fflush(fscript);
+			if (tflg)
+				fflush(timingfd);
+		}
 		if (write_all(STDOUT_FILENO, obuf, cc)) {
 			warn (_("write failed"));
 			fail();
Signature · 1 line
1.8.4
#4 lore raw
Quoted 3 lines · Jesper Dahl Nyerup
---
 term-utils/script.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

I apologise.

This second patch is obviously the correct one. Please disregard my first submission in this thread.

J.

Signature · 3 lines
Jesper Dahl Nyerup
Systems Engineer
One.com, [email protected]
maintainer Applied
#5 lore raw
In reply to Jesper Dahl Nyerup
Quoted 2 lines · Jesper Dahl Nyerup
 term-utils/script.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Applied, thanks.

I have also made the files usage more robust and close all in done() (based on Sami's suggestion).

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#6 lore raw
Quoted 8 lines · Karel Zak
Quoted 2 lines · Jesper Dahl Nyerup
 term-utils/script.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Applied, thanks.

I have also made the files usage more robust and close all in done() (based on Sami's suggestion).

Thanks!

J.

Signature · 3 lines
Jesper Dahl Nyerup
Systems Engineer
One.com, [email protected]