On 17 February 2014 21:43, Jesper Dahl Nyerup <nyerup@xxxxxxx> wrote: > 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. -- Sami Kerola http://www.iki.fi/kerolasa/
From 6b1bb220173b69d1b3ea80153be08f7a8661775b Mon Sep 17 00:00:00 2001 From: Jesper Dahl Nyerup <nyerup@xxxxxxx> 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. [kerolasa@xxxxxx: - check timing file write status in done()] Signed-off-by: Jesper Dahl Nyerup <nyerup@xxxxxxx> Signed-off-by: Sami Kerola <kerolasa@xxxxxx> --- term-utils/script.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/term-utils/script.c b/term-utils/script.c index a4c2e0c..4b816ec 100644 --- a/term-utils/script.c +++ b/term-utils/script.c @@ -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 */ } -- 1.9.0