]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - term-utils/script.c
script: add --log-in
[thirdparty/util-linux.git] / term-utils / script.c
index 7aaf6da7bbf8c12bbfa7a824331f71d476655956..9b5aa778d8f513d328f6524654da3914ce361302 100644 (file)
@@ -12,8 +12,8 @@
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
+ *      This product includes software developed by the University of
+ *      California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
@@ -41,9 +41,6 @@
  * - fixed a rare deadlock after child termination
  */
 
-/*
- * script
- */
 #include <stdio.h>
 #include <stdlib.h>
 #include <paths.h>
 #include <stddef.h>
 #include <sys/wait.h>
 #include <poll.h>
+#include <sys/signalfd.h>
+#include <assert.h>
+#include <inttypes.h>
 
 #include "closestream.h"
 #include "nls.h"
 #include "c.h"
 #include "ttyutils.h"
 #include "all-io.h"
+#include "monotonic.h"
+#include "timeutils.h"
+#include "strutils.h"
+#include "xalloc.h"
+#include "optutils.h"
+
+#include "debug.h"
+
+static UL_DEBUG_DEFINE_MASK(script);
+UL_DEBUG_DEFINE_MASKNAMES(script) = UL_DEBUG_EMPTY_MASKNAMES;
+
+#define SCRIPT_DEBUG_INIT      (1 << 1)
+#define SCRIPT_DEBUG_POLL      (1 << 2)
+#define SCRIPT_DEBUG_SIGNAL    (1 << 3)
+#define SCRIPT_DEBUG_IO                (1 << 4)
+#define SCRIPT_DEBUG_MISC      (1 << 5)
+#define SCRIPT_DEBUG_ALL       0xFFFF
+
+#define DBG(m, x)       __UL_DBG(script, SCRIPT_DEBUG_, m, x)
+#define ON_DBG(m, x)    __UL_DBG_CALL(script, SCRIPT_DEBUG_, m, x)
 
 #if defined(HAVE_LIBUTIL) && defined(HAVE_PTY_H)
 # include <pty.h>
 # include <utempter.h>
 #endif
 
-#define DEFAULT_OUTPUT "typescript"
-
-char   *shell;
-FILE   *fscript;
-FILE   *timingfd;
-int    master = -1;
-int    slave;
-pid_t  child;
-pid_t  subchild;
-int    childstatus;
-char   *fname;
-
-struct termios tt;
-struct winsize win;
-int    lb;
-int    l;
+#define DEFAULT_TYPESCRIPT_FILENAME "typescript"
+
+enum {
+       SCRIPT_FMT_RAW = 1,             /* raw slave/master data */
+       SCRIPT_FMT_TIMING_SIMPLE,       /* timing info in classic "<time> <delta>" format */
+};
+
+struct script_log {
+       FILE    *fp;                    /* file pointer (handler) */
+       int     format;                 /* SCRIPT_FMT_* */
+       char    *filename;              /* on command line specified name */
+       struct timeval oldtime;         /* previous entry log time */
+};
+
+struct script_stream {
+       struct timeval oldtime;         /* last update */
+       struct script_log **logs;       /* logs where to write data from stream */
+       size_t nlogs;                   /* number of logs */
+};
+
+struct script_control {
+       char *shell;            /* shell to be executed */
+       char *command;          /* command to be executed */
+       uint64_t outsz;         /* current output files size */
+       uint64_t maxsz;         /* maximum output files size */
+
+       int master;             /* pseudoterminal master file descriptor */
+       int slave;              /* pseudoterminal slave file descriptor */
+
+       struct script_stream    out;    /* output */
+       struct script_stream    in;     /* input */
+
+       int poll_timeout;       /* poll() timeout, used in end of execution */
+       pid_t child;            /* child pid */
+       int childstatus;        /* child process exit value */
+       struct termios attrs;   /* slave terminal runtime attributes */
+       struct winsize win;     /* terminal window size */
 #if !HAVE_LIBUTIL || !HAVE_PTY_H
-char   line[] = "/dev/ptyXX";
+       char *line;             /* terminal line */
 #endif
-int    aflg = 0;
-char   *cflg = NULL;
-int    eflg = 0;
-int    fflg = 0;
-int    qflg = 0;
-int    tflg = 0;
-int    forceflg = 0;
-int    isterm;
-
-sigset_t block_mask, unblock_mask;
-
-int die;
-int resized;
+       unsigned int
+        append:1,              /* append output */
+        rc_wanted:1,           /* return child exit value */
+        flush:1,               /* flush after each write */
+        quiet:1,               /* suppress most output */
+        force:1,               /* write output to links */
+        isterm:1,              /* is child process running as terminal */
+        die:1;                 /* terminate program */
+
+       sigset_t sigset;        /* catch SIGCHLD and SIGWINCH with signalfd() */
+       sigset_t sigorg;        /* original signal mask */
+       int sigfd;              /* file descriptor for signalfd() */
+};
+
+static void restore_tty(struct script_control *ctl, int mode);
+static void __attribute__((__noreturn__)) fail(struct script_control *ctl);
+
+static void script_init_debug(void)
+{
+       __UL_INIT_DEBUG_FROM_ENV(script, SCRIPT_DEBUG_, 0, SCRIPT_DEBUG);
+}
 
 /*
  * For tests we want to be able to control time output
@@ -118,356 +167,627 @@ int resized;
 static inline time_t script_time(time_t *t)
 {
        const char *str = getenv("SCRIPT_TEST_SECOND_SINCE_EPOCH");
-       time_t sec;
+       int64_t sec;
 
-       if (str && sscanf(str, "%ld", &sec) == 1)
-               return sec;
-       return time(t);
+       if (!str || sscanf(str, "%"SCNi64, &sec) != 1)
+               return time(t);
+       if (t)
+               *t = (time_t)sec;
+       return (time_t)sec;
 }
 #else  /* !TEST_SCRIPT */
 # define script_time(x) time(x)
 #endif
 
-static void __attribute__((__noreturn__)) usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
-       fprintf(out,
-             _(" %s [options] [file]\n"), program_invocation_short_name);
+       fprintf(out, _(" %s [options] [file]\n"), program_invocation_short_name);
 
        fputs(USAGE_SEPARATOR, out);
        fputs(_("Make a typescript of a terminal session.\n"), out);
 
        fputs(USAGE_OPTIONS, out);
-       fputs(_(" -a, --append            append the output\n"
-               " -c, --command <command> run command rather than interactive shell\n"
-               " -e, --return            return exit code of the child process\n"
-               " -f, --flush             run flush after each write\n"
-               "     --force             use output file even when it is a link\n"
-               " -q, --quiet             be quiet\n"
-               " -t, --timing[=<file>]   output timing data to stderr (or to FILE)\n"
-               " -V, --version           output version information and exit\n"
-               " -h, --help              display this help and exit\n\n"), out);
-
-       fprintf(out, USAGE_MAN_TAIL("script(1)"));
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       fputs(_(" -I, --log-in <file>           log stdin to file\n"), out);
+       fputs(_(" -O, --log-out <file>          log stdout to file (default)\n"), out);
+       fputs(_(" -T, --log-timing <file>       log timing information to file\n"), out);
+       fputs(_(" -a, --append                  append the output\n"), out);
+       fputs(_(" -c, --command <command>       run command rather than interactive shell\n"), out);
+       fputs(_(" -e, --return                  return exit code of the child process\n"), out);
+       fputs(_(" -f, --flush                   run flush after each write\n"), out);
+       fputs(_("     --force                   use output file even when it is a link\n"), out);
+       fputs(_(" -o, --output-limit <size>     terminate if output files exceed size\n"), out);
+       fputs(_(" -q, --quiet                   be quiet\n"), out);
+       fputs(_(" -t[<file>], --timing[=<file>] deprecated alias to -T (default file is stderr)\n"), out);
+
+       fputs(USAGE_SEPARATOR, out);
+       printf(USAGE_HELP_OPTIONS(31));
+       printf(USAGE_MAN_TAIL("script(1)"));
+
+       exit(EXIT_SUCCESS);
 }
 
-static void die_if_link(char *fn)
+static struct script_log *get_log_by_name(struct script_stream *stream,
+                                         const char *name)
 {
-       struct stat s;
+       size_t i;
 
-       if (forceflg)
-               return;
-       if (lstat(fn, &s) == 0 && (S_ISLNK(s.st_mode) || s.st_nlink > 1))
-               errx(EXIT_FAILURE,
-                    _("output file `%s' is a link\n"
-                      "Use --force if you really want to use it.\n"
-                      "Program not started."), fn);
+       for (i = 0; i < stream->nlogs; i++) {
+               struct script_log *log = stream->logs[i];
+               if (strcmp(log->filename, name) == 0)
+                       return log;
+       }
+       return NULL;
 }
 
-/*
- * Stop extremely silly gcc complaint on %c:
- *  warning: `%c' yields only last 2 digits of year in some locales
- */
-static void my_strftime(char *buf, size_t len, const char *fmt, const struct tm *tm)
+static struct script_log *log_associate(struct script_control *ctl,
+                                       struct script_stream *stream,
+                                       const char *filename, int format)
 {
-       strftime(buf, len, fmt, tm);
+       struct script_log *log;
+
+       assert(ctl);
+       assert(filename);
+       assert(stream);
+
+       log = get_log_by_name(stream, filename);
+       if (log)
+               return log;     /* already defined */
+
+       log = get_log_by_name(stream == &ctl->out ? &ctl->in : &ctl->out, filename);
+       if (!log) {
+               /* create a new log */
+               log = xcalloc(1, sizeof(*log));
+               log->filename = xstrdup(filename);
+               log->format = format;
+       }
+
+       /* add log to the stream */
+       stream->logs = xrealloc(stream->logs,
+                       (stream->nlogs + 1) * sizeof(log));
+       stream->logs[stream->nlogs] = log;
+       stream->nlogs++;
+
+       return log;
 }
 
-static void __attribute__((__noreturn__)) done(void)
+static void log_close(struct script_control *ctl __attribute__((unused)),
+                     struct script_log *log,
+                     const char *msg,
+                     int status)
 {
-       time_t tvec;
-
-       if (subchild) {
-               /* output process */
-               if (fscript) {
-                       if (!qflg) {
-                               char buf[BUFSIZ];
-                               tvec = time((time_t *)NULL);
-                               my_strftime(buf, sizeof buf, "%c\n", localtime(&tvec));
-                               fprintf(fscript, _("\nScript done on %s"), buf);
-                       }
-                       if (close_stream(fscript) != 0)
-                               errx(EXIT_FAILURE, _("write error"));
-                       fscript = NULL;
-               }
-               if (timingfd && close_stream(timingfd) != 0)
-                       errx(EXIT_FAILURE, _("write error"));
-               timingfd = NULL;
+       DBG(MISC, ul_debug("closing %s", log->filename));
 
-               close(master);
-               master = -1;
-       } else {
-               /* input process */
-               if (isterm)
-                       tcsetattr(STDIN_FILENO, TCSADRAIN, &tt);
-               if (!qflg)
-                       printf(_("Script done, file is %s\n"), fname);
-#ifdef HAVE_LIBUTEMPTER
-               if (master >= 0)
-                       utempter_remove_record(master);
-#endif
-               kill(child, SIGTERM);   /* make sure we don't create orphans */
-       }
+       switch (log->format) {
+       case SCRIPT_FMT_RAW:
+       {
+               char buf[FORMAT_TIMESTAMP_MAX];
+               time_t tvec = script_time((time_t *)NULL);
 
-       if(eflg) {
-               if (WIFSIGNALED(childstatus))
-                       exit(WTERMSIG(childstatus) + 0x80);
+               strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf));
+               if (msg)
+                       fprintf(log->fp, _("\nScript done on %s [<%s>]\n"), buf, msg);
                else
-                       exit(WEXITSTATUS(childstatus));
+                       fprintf(log->fp, _("\nScript done on %s [COMMAND_EXIT_CODE=\"%d\"]\n"), buf, status);
+               break;
        }
-       exit(EXIT_SUCCESS);
+       case SCRIPT_FMT_TIMING_SIMPLE:
+               break;
+       }
+
+       if (close_stream(log->fp) != 0)
+               err(EXIT_FAILURE, "write failed: %s", log->filename);
+
+       log->fp = NULL;
 }
 
-static void
-fail(void) {
+static void log_start(struct script_control *ctl,
+                     struct script_log *log)
+{
+
+       assert(log->fp == NULL);
 
-       kill(0, SIGTERM);
-       done();
+       DBG(MISC, ul_debug("opening %s", log->filename));
+
+       /* open the log */
+       log->fp = fopen(log->filename,
+                       ctl->append && log->format == SCRIPT_FMT_RAW ?
+                               "a" UL_CLOEXECSTR :
+                               "w" UL_CLOEXECSTR);
+       if (!log->fp) {
+               restore_tty(ctl, TCSANOW);
+               warn(_("cannot open %s"), log->filename);
+               fail(ctl);
+       }
+
+       /* write header, etc. */
+       switch (log->format) {
+       case SCRIPT_FMT_RAW:
+       {
+               char buf[FORMAT_TIMESTAMP_MAX];
+               time_t tvec = script_time((time_t *)NULL);
+
+               strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf));
+               fprintf(log->fp, _("Script started on %s ["), buf);
+
+               if (ctl->isterm) {
+                       int cols = 0, lines = 0;
+                       const char *tty = NULL, *term = NULL;
+
+                       get_terminal_dimension(&cols, &lines);
+                       get_terminal_name(&tty, NULL, NULL);
+                       get_terminal_type(&term);
+
+                       if (term)
+                               fprintf(log->fp, "TERM=\"%s\" ", term);
+                       if (tty)
+                               fprintf(log->fp, "TTY=\"%s\" ", tty);
+
+                       fprintf(log->fp, "COLUMNS=\"%d\" LINES=\"%d\"", cols, lines);
+               } else
+                       fprintf(log->fp, _("<not executed on terminal>"));
+
+               fputs("]\n", log->fp);
+               break;
+       }
+       case SCRIPT_FMT_TIMING_SIMPLE:
+               gettime_monotonic(&log->oldtime);
+               break;
+       }
+}
+
+static size_t log_write(struct script_control *ctl,
+                     struct script_log *log,
+                     char *obuf, size_t bytes)
+{
+       if (!log->fp)
+               return 0;
+
+       DBG(IO, ul_debug("  writining %s", log->filename));
+
+       switch (log->format) {
+       case SCRIPT_FMT_RAW:
+               if (fwrite_all(obuf, 1, bytes, log->fp)) {
+                       warn(_("cannot write %s"), log->filename);
+                       fail(ctl);
+               }
+               break;
+       case SCRIPT_FMT_TIMING_SIMPLE:
+       {
+               struct timeval now, delta;
+               int sz;
+
+               DBG(IO, ul_debug("  writing timing info"));
+
+               gettime_monotonic(&now);
+               timersub(&now, &log->oldtime, &delta);
+               sz = fprintf(log->fp, "%ld.%06ld %zd\n",
+                       (long)delta.tv_sec, (long)delta.tv_usec, bytes);
+               log->oldtime = now;
+               bytes = sz > 0 ? sz : 0;
+               break;
+       }
+       default:
+               break;
+       }
+
+       if (ctl->flush)
+               fflush(log->fp);
+
+       return bytes;
 }
 
+static uint64_t log_stream_activity(
+                       struct script_control *ctl,
+                       struct script_stream *stream,
+                       char *buf, size_t bytes)
+{
+       size_t i;
+       uint64_t outsz = 0;
+
+       for (i = 0; i < stream->nlogs; i++)
+               outsz += log_write(ctl, stream->logs[i], buf, bytes);
 
-static void wait_for_empty_fd(int fd)
+       return outsz;
+}
+
+
+static void die_if_link(struct script_control *ctl, const char *filename)
 {
-       struct pollfd fds[] = {
-               { .fd = fd, .events = POLLIN }
-       };
+       struct stat s;
 
-       while (die == 0 && poll(fds, 1, 100) == 1);
+       if (ctl->force)
+               return;
+       if (lstat(filename, &s) == 0 && (S_ISLNK(s.st_mode) || s.st_nlink > 1))
+               errx(EXIT_FAILURE,
+                    _("output file `%s' is a link\n"
+                      "Use --force if you really want to use it.\n"
+                      "Program not started."), filename);
 }
 
-static void finish(int wait)
+static void restore_tty(struct script_control *ctl, int mode)
 {
-       int status;
-       pid_t pid;
-       int errsv = errno;
-       int options = wait ? 0 : WNOHANG;
+       struct termios rtt;
 
-       while ((pid = wait3(&status, options, 0)) > 0)
-               if (pid == child) {
-                       childstatus = status;
-                       die = 1;
-               }
+       if (!ctl->isterm)
+               return;
 
-       errno = errsv;
+       rtt = ctl->attrs;
+       tcsetattr(STDIN_FILENO, mode, &rtt);
 }
 
-static void doinput(void)
+static void enable_rawmode_tty(struct script_control *ctl)
 {
-       int errsv = 0;
-       ssize_t cc = 0;
-       char ibuf[BUFSIZ];
-       fd_set readfds;
+       struct termios rtt;
 
-       /* close things irrelevant for this process */
-       if (fscript)
-               fclose(fscript);
-       if (timingfd)
-               fclose(timingfd);
-       fscript = timingfd = NULL;
+       if (!ctl->isterm)
+               return;
 
-       FD_ZERO(&readfds);
+       rtt = ctl->attrs;
+       cfmakeraw(&rtt);
+       rtt.c_lflag &= ~ECHO;
+       tcsetattr(STDIN_FILENO, TCSANOW, &rtt);
+}
 
-       /* block SIGCHLD */
-       sigprocmask(SIG_SETMASK, &block_mask, &unblock_mask);
+static void __attribute__((__noreturn__)) done_log(struct script_control *ctl, const char *msg)
+{
+       int status;
+       size_t i;
 
-       while (die == 0) {
-               FD_SET(STDIN_FILENO, &readfds);
+       DBG(MISC, ul_debug("done!"));
 
-               errno = 0;
-               /* wait for input or signal (including SIGCHLD) */
-               if ((cc = pselect(STDIN_FILENO + 1, &readfds, NULL, NULL, NULL,
-                       &unblock_mask)) > 0) {
+       restore_tty(ctl, TCSADRAIN);
 
-                       if ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
-                               if (write_all(master, ibuf, cc)) {
-                                       warn (_("write failed"));
-                                       fail();
-                               }
-                       }
-               }
+       if (WIFSIGNALED(ctl->childstatus))
+               status = WTERMSIG(ctl->childstatus) + 0x80;
+       else
+               status = WEXITSTATUS(ctl->childstatus);
 
-               if (cc < 0 && errno == EINTR && resized)
-               {
-                       /* transmit window change information to the child */
-                       if (isterm) {
-                               ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&win);
-                               ioctl(slave, TIOCSWINSZ, (char *)&win);
-                       }
-                       resized = 0;
 
-               } else if (cc <= 0 && errno != EINTR) {
-                       errsv = errno;
-                       break;
-               }
-       }
+       DBG(MISC, ul_debug(" status=%d", status));
 
-       /* unblock SIGCHLD */
-       sigprocmask(SIG_SETMASK, &unblock_mask, NULL);
-
-       /* To be sure that we don't miss any data */
-       wait_for_empty_fd(slave);
-       wait_for_empty_fd(master);
-
-       if (die == 0 && cc == 0 && errsv == 0) {
-               /*
-                * Forward EOF from stdin (detected by read() above) to slave
-                * (shell) to correctly terminate the session. It seems we have
-                * to wait for empty terminal FDs otherwise EOF maybe ignored
-                * (why?) and typescript is incomplete.      -- kzak Dec-2013
-                *
-                * We usually use this when stdin is not a tty, for example:
-                * echo "ps" | script
-                */
-               char c = DEF_EOF;
-
-               if (write_all(master, &c, sizeof(char))) {
-                       warn (_("write failed"));
-                       fail();
-               }
+       /* close all output logs */
+       for (i = 0; i < ctl->out.nlogs; i++)
+               log_close(ctl, ctl->out.logs[i], msg, status);
 
-               /* wait for "exit" message from shell before we print "Script
-                * done" in done() */
-               wait_for_empty_fd(master);
-       }
+       /* close all input logs */
+       for (i = 0; i < ctl->in.nlogs; i++)
+               log_close(ctl, ctl->in.logs[i], msg, status);
+
+       if (!ctl->quiet)
+               printf(_("Script done.\n"));
 
-       if (!die)
-               finish(1);      /* wait for children */
-       done();
+#ifdef HAVE_LIBUTEMPTER
+       if (ctl->master >= 0)
+               utempter_remove_record(ctl->master);
+#endif
+       kill(ctl->child, SIGTERM);      /* make sure we don't create orphans */
+       exit(ctl->rc_wanted ? status : EXIT_SUCCESS);
 }
 
-static void sig_finish(int dummy __attribute__ ((__unused__)))
+static void __attribute__((__noreturn__)) done(struct script_control *ctl)
 {
-       finish(0);
+       done_log(ctl, NULL);
 }
 
-static void resize(int dummy __attribute__ ((__unused__)))
+static void __attribute__((__noreturn__)) fail(struct script_control *ctl)
 {
-       resized = 1;
+       DBG(MISC, ul_debug("fail!"));
+       kill(0, SIGTERM);
+       done(ctl);
 }
 
-static void dooutput(void)
+static void wait_for_child(struct script_control *ctl, int wait)
 {
-       ssize_t cc;
-       char obuf[BUFSIZ];
-       struct timeval tv;
-       double oldtime=time(NULL), newtime;
-       int errsv = 0;
-       fd_set readfds;
-
-       close(STDIN_FILENO);
-#ifdef HAVE_LIBUTIL
-       close(slave);
-#endif
-       if (tflg && !timingfd)
-               timingfd = fdopen(STDERR_FILENO, "w");
+       int status;
+       pid_t pid;
+       int options = wait ? 0 : WNOHANG;
 
-       if (!qflg) {
-               time_t tvec = script_time((time_t *)NULL);
-               my_strftime(obuf, sizeof obuf, "%c\n", localtime(&tvec));
-               fprintf(fscript, _("Script started on %s"), obuf);
+       DBG(MISC, ul_debug("waiting for child"));
+
+       while ((pid = wait3(&status, options, NULL)) > 0)
+               if (pid == ctl->child)
+                       ctl->childstatus = status;
+}
+
+/* data from master to stdout */
+static void write_output(struct script_control *ctl, char *obuf,
+                           ssize_t bytes)
+{
+       DBG(IO, ul_debug("  writing to output"));
+
+       if (write_all(STDOUT_FILENO, obuf, bytes)) {
+               DBG(IO, ul_debug("  writing output *failed*"));
+               warn(_("write failed"));
+               fail(ctl);
        }
+}
 
-       FD_ZERO(&readfds);
+static int write_to_shell(struct script_control *ctl,
+                         char *buf, size_t bufsz)
+{
+       return write_all(ctl->master, buf, bufsz);
+}
 
-       do {
-               if (die || errsv == EINTR) {
-                       struct pollfd fds[] = {{ .fd = master, .events = POLLIN }};
-                       if (poll(fds, 1, 50) <= 0)
-                               break;
-               }
+/*
+ * The script(1) is usually faster than shell, so it's a good idea to wait until
+ * the previous message has been already read by shell from slave before we
+ * write to master. This is necessary especially for EOF situation when we can
+ * send EOF to master before shell is fully initialized, to workaround this
+ * problem we wait until slave is empty. For example:
+ *
+ *   echo "date" | script
+ *
+ * Unfortunately, the child (usually shell) can ignore stdin at all, so we
+ * don't wait forever to avoid dead locks...
+ *
+ * Note that script is primarily designed for interactive sessions as it
+ * maintains master+slave tty stuff within the session. Use pipe to write to
+ * script(1) and assume non-interactive (tee-like) behavior is NOT well
+ * supported.
+ */
+static void write_eof_to_shell(struct script_control *ctl)
+{
+       unsigned int tries = 0;
+       struct pollfd fds[] = {
+                  { .fd = ctl->slave, .events = POLLIN }
+       };
+       char c = DEF_EOF;
 
-               /* block SIGCHLD */
-               sigprocmask(SIG_SETMASK, &block_mask, &unblock_mask);
+       DBG(IO, ul_debug(" waiting for empty slave"));
+       while (poll(fds, 1, 10) == 1 && tries < 8) {
+               DBG(IO, ul_debug("   slave is not empty"));
+               xusleep(250000);
+               tries++;
+       }
+       if (tries < 8)
+               DBG(IO, ul_debug("   slave is empty now"));
 
-               FD_SET(master, &readfds);
-               errno = 0;
+       DBG(IO, ul_debug(" sending EOF to master"));
+       write_to_shell(ctl, &c, sizeof(char));
+}
+
+static void handle_io(struct script_control *ctl, int fd, int *eof)
+{
+       char buf[BUFSIZ];
+       ssize_t bytes;
+       DBG(IO, ul_debug("%d FD active", fd));
+       *eof = 0;
+
+       /* read from active FD */
+       bytes = read(fd, buf, sizeof(buf));
+       if (bytes < 0) {
+               if (errno == EAGAIN || errno == EINTR)
+                       return;
+               fail(ctl);
+       }
+
+       if (bytes == 0) {
+               *eof = 1;
+               return;
+       }
 
-               /* wait for input or signal (including SIGCHLD) */
-               if ((cc = pselect(master+1, &readfds, NULL, NULL, NULL,
-                       &unblock_mask)) > 0) {
+       /* from stdin (user) to command */
+       if (fd == STDIN_FILENO) {
+               DBG(IO, ul_debug(" stdin --> master %zd bytes", bytes));
 
-                       cc = read(master, obuf, sizeof (obuf));
+               if (write_to_shell(ctl, buf, bytes)) {
+                       warn(_("write failed"));
+                       fail(ctl);
                }
-               errsv = errno;
+               /* without sync write_output() will write both input &
+                * shell output that looks like double echoing */
+               fdatasync(ctl->master);
+               ctl->outsz += log_stream_activity(ctl, &ctl->in, buf, (size_t) bytes);
+
+       /* from command (master) to stdout and log */
+       } else if (fd == ctl->master) {
+               DBG(IO, ul_debug(" master --> stdout %zd bytes", bytes));
+               write_output(ctl, buf, bytes);
+               ctl->outsz += log_stream_activity(ctl, &ctl->out, buf, (size_t) bytes);
+       }
 
-               /* unblock SIGCHLD */
-               sigprocmask(SIG_SETMASK, &unblock_mask, NULL);
+       /* check output limit */
+       if (ctl->maxsz != 0 && ctl->outsz >= ctl->maxsz) {
+               if (!ctl->quiet)
+                       printf(_("Script terminated, max output files size %"PRIu64" exceeded.\n"), ctl->maxsz);
+               DBG(IO, ul_debug("output size %"PRIu64", exceeded limit %"PRIu64, ctl->outsz, ctl->maxsz));
+               done_log(ctl, _("max output size exceeded"));
+       }
+}
 
-               if (tflg)
-                       gettimeofday(&tv, NULL);
+static void handle_signal(struct script_control *ctl, int fd)
+{
+       struct signalfd_siginfo info;
+       ssize_t bytes;
 
-               if (errsv == EINTR && cc <= 0)
-                       continue;       /* try it again */
-               if (cc <= 0)
-                       break;
-               if (tflg && timingfd) {
-                       newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
-                       fprintf(timingfd, "%f %zd\n", newtime - oldtime, cc);
-                       oldtime = newtime;
+       DBG(SIGNAL, ul_debug("signal FD %d active", fd));
+
+       bytes = read(fd, &info, sizeof(info));
+       if (bytes != sizeof(info)) {
+               if (bytes < 0 && (errno == EAGAIN || errno == EINTR))
+                       return;
+               fail(ctl);
+       }
+
+       switch (info.ssi_signo) {
+       case SIGCHLD:
+               DBG(SIGNAL, ul_debug(" get signal SIGCHLD [ssi_code=%d, ssi_status=%d]",
+                                                       info.ssi_code, info.ssi_status));
+               if (info.ssi_code == CLD_EXITED
+                   || info.ssi_code == CLD_KILLED
+                   || info.ssi_code == CLD_DUMPED) {
+                       wait_for_child(ctl, 0);
+                       ctl->poll_timeout = 10;
+
+               /* In case of ssi_code is CLD_TRAPPED, CLD_STOPPED, or CLD_CONTINUED */
+               } else if (info.ssi_status == SIGSTOP && ctl->child) {
+                       DBG(SIGNAL, ul_debug(" child stop by SIGSTOP -- stop parent too"));
+                       kill(getpid(), SIGSTOP);
+                       DBG(SIGNAL, ul_debug(" resume"));
+                       kill(ctl->child, SIGCONT);
+               }
+               return;
+       case SIGWINCH:
+               DBG(SIGNAL, ul_debug(" get signal SIGWINCH"));
+               if (ctl->isterm) {
+                       ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ctl->win);
+                       ioctl(ctl->slave, TIOCSWINSZ, (char *)&ctl->win);
                }
-               if (fwrite_all(obuf, 1, cc, fscript)) {
-                       warn (_("cannot write script file"));
-                       fail();
+               break;
+       case SIGTERM:
+               /* fallthrough */
+       case SIGINT:
+               /* fallthrough */
+       case SIGQUIT:
+               DBG(SIGNAL, ul_debug(" get signal SIG{TERM,INT,QUIT}"));
+               fprintf(stderr, _("\nSession terminated.\n"));
+               /* Child termination is going to generate SIGCHILD (see above) */
+               kill(ctl->child, SIGTERM);
+               return;
+       default:
+               abort();
+       }
+       DBG(SIGNAL, ul_debug("signal handle on FD %d done", fd));
+}
+
+static void do_io(struct script_control *ctl)
+{
+       int ret, eof = 0;
+       size_t i;
+       enum {
+               POLLFD_SIGNAL = 0,
+               POLLFD_MASTER,
+               POLLFD_STDIN
+
+       };
+       struct pollfd pfd[] = {
+               [POLLFD_SIGNAL] = { .fd = ctl->sigfd,   .events = POLLIN | POLLERR | POLLHUP },
+               [POLLFD_MASTER] = { .fd = ctl->master,  .events = POLLIN | POLLERR | POLLHUP },
+               [POLLFD_STDIN]  = { .fd = STDIN_FILENO, .events = POLLIN | POLLERR | POLLHUP }
+       };
+
+
+       /* start all output logs */
+       for (i = 0; i < ctl->out.nlogs; i++)
+               log_start(ctl, ctl->out.logs[i]);
+
+       /* start all input logs */
+       for (i = 0; i < ctl->in.nlogs; i++)
+               log_start(ctl, ctl->in.logs[i]);
+
+       while (!ctl->die) {
+               size_t i;
+               int errsv;
+
+               DBG(POLL, ul_debug("calling poll()"));
+
+               /* wait for input or signal */
+               ret = poll(pfd, ARRAY_SIZE(pfd), ctl->poll_timeout);
+               errsv = errno;
+               DBG(POLL, ul_debug("poll() rc=%d", ret));
+
+               if (ret < 0) {
+                       if (errsv == EAGAIN)
+                               continue;
+                       warn(_("poll failed"));
+                       fail(ctl);
                }
-               if (fflg) {
-                       fflush(fscript);
-                       if (tflg && timingfd)
-                               fflush(timingfd);
+               if (ret == 0) {
+                       DBG(POLL, ul_debug("setting die=1"));
+                       ctl->die = 1;
+                       break;
                }
-               if (write_all(STDOUT_FILENO, obuf, cc)) {
-                       warn (_("write failed"));
-                       fail();
+
+               for (i = 0; i < ARRAY_SIZE(pfd); i++) {
+                       if (pfd[i].revents == 0)
+                               continue;
+
+                       DBG(POLL, ul_debug(" active pfd[%s].fd=%d %s %s %s",
+                                               i == POLLFD_STDIN  ? "stdin" :
+                                               i == POLLFD_MASTER ? "master" :
+                                               i == POLLFD_SIGNAL ? "signal" : "???",
+                                               pfd[i].fd,
+                                               pfd[i].revents & POLLIN  ? "POLLIN" : "",
+                                               pfd[i].revents & POLLHUP ? "POLLHUP" : "",
+                                               pfd[i].revents & POLLERR ? "POLLERR" : ""));
+                       switch (i) {
+                       case POLLFD_STDIN:
+                       case POLLFD_MASTER:
+                               /* data */
+                               if (pfd[i].revents & POLLIN)
+                                       handle_io(ctl, pfd[i].fd, &eof);
+                               /* EOF maybe detected by two ways:
+                                *      A) poll() return POLLHUP event after close()
+                                *      B) read() returns 0 (no data) */
+                               if ((pfd[i].revents & POLLHUP) || eof) {
+                                       DBG(POLL, ul_debug(" ignore FD"));
+                                       pfd[i].fd = -1;
+                                       if (i == POLLFD_STDIN) {
+                                               write_eof_to_shell(ctl);
+                                               DBG(POLL, ul_debug("  ignore STDIN"));
+                                       }
+                               }
+                               continue;
+                       case POLLFD_SIGNAL:
+                               handle_signal(ctl, pfd[i].fd);
+                               break;
+                       }
                }
-       } while(1);
+       }
+
+       DBG(POLL, ul_debug("poll() done"));
 
-       done();
+       if (!ctl->die)
+               wait_for_child(ctl, 1);
+
+       done(ctl);
 }
 
-static void getslave(void)
+static void getslave(struct script_control *ctl)
 {
 #ifndef HAVE_LIBUTIL
-       line[strlen("/dev/")] = 't';
-       slave = open(line, O_RDWR);
-       if (slave < 0) {
-               warn(_("cannot open %s"), line);
-               fail();
+       ctl->line[strlen("/dev/")] = 't';
+       ctl->slave = open(ctl->line, O_RDWR | O_CLOEXEC);
+       if (ctl->slave < 0) {
+               warn(_("cannot open %s"), ctl->line);
+               fail(ctl);
        }
-       if (isterm) {
-               tcsetattr(slave, TCSANOW, &tt);
-               ioctl(slave, TIOCSWINSZ, (char *)&win);
+       if (ctl->isterm) {
+               tcsetattr(ctl->slave, TCSANOW, &ctl->attrs);
+               ioctl(ctl->slave, TIOCSWINSZ, (char *)&ctl->win);
        }
 #endif
        setsid();
-       ioctl(slave, TIOCSCTTY, 0);
+       ioctl(ctl->slave, TIOCSCTTY, 0);
 }
 
-static void doshell(void)
+/* don't use DBG() stuff here otherwise it will be in  the typescript file */
+static void __attribute__((__noreturn__)) do_shell(struct script_control *ctl)
 {
        char *shname;
 
-       getslave();
+       getslave(ctl);
 
        /* close things irrelevant for this process */
-       close(master);
-       if (fscript)
-               fclose(fscript);
-       if (timingfd)
-               fclose(timingfd);
-       fscript = timingfd = NULL;
+       close(ctl->master);
+       close(ctl->sigfd);
 
-       dup2(slave, STDIN_FILENO);
-       dup2(slave, STDOUT_FILENO);
-       dup2(slave, STDERR_FILENO);
-       close(slave);
+       dup2(ctl->slave, STDIN_FILENO);
+       dup2(ctl->slave, STDOUT_FILENO);
+       dup2(ctl->slave, STDERR_FILENO);
+       close(ctl->slave);
 
-       master = -1;
+       ctl->master = -1;
 
-       shname = strrchr(shell, '/');
+       shname = strrchr(ctl->shell, '/');
        if (shname)
                shname++;
        else
-               shname = shell;
+               shname = ctl->shell;
+
+       sigprocmask(SIG_SETMASK, &ctl->sigorg, NULL);
 
        /*
         * When invoked from within /etc/csh.login, script spawns a csh shell
@@ -479,230 +799,260 @@ static void doshell(void)
         */
        signal(SIGTERM, SIG_DFL);
 
-       if (access(shell, X_OK) == 0) {
-               if (cflg)
-                       execl(shell, shname, "-c", cflg, NULL);
+       if (access(ctl->shell, X_OK) == 0) {
+               if (ctl->command)
+                       execl(ctl->shell, shname, "-c", ctl->command, NULL);
                else
-                       execl(shell, shname, "-i", NULL);
+                       execl(ctl->shell, shname, "-i", NULL);
        } else {
-               if (cflg)
-                       execlp(shname, "-c", cflg, NULL);
+               if (ctl->command)
+                       execlp(shname, "-c", ctl->command, NULL);
                else
                        execlp(shname, "-i", NULL);
        }
-       warn(_("failed to execute %s"), shell);
-       fail();
+       warn(_("failed to execute %s"), ctl->shell);
+       fail(ctl);
 }
 
-static void fixtty(void)
-{
-       struct termios rtt;
 
-       if (!isterm)
-               return;
-
-       rtt = tt;
-       cfmakeraw(&rtt);
-       rtt.c_lflag &= ~ECHO;
-       tcsetattr(STDIN_FILENO, TCSANOW, &rtt);
-}
-
-static void getmaster(void)
+static void getmaster(struct script_control *ctl)
 {
 #if defined(HAVE_LIBUTIL) && defined(HAVE_PTY_H)
        int rc;
 
-       isterm = isatty(STDIN_FILENO);
+       ctl->isterm = isatty(STDIN_FILENO);
 
-       if (isterm) {
-               if (tcgetattr(STDIN_FILENO, &tt) != 0)
+       if (ctl->isterm) {
+               if (tcgetattr(STDIN_FILENO, &ctl->attrs) != 0)
                        err(EXIT_FAILURE, _("failed to get terminal attributes"));
-               ioctl(STDIN_FILENO, TIOCGWINSZ, (char *) &win);
-               rc = openpty(&master, &slave, NULL, &tt, &win);
+               ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ctl->win);
+               rc = openpty(&ctl->master, &ctl->slave, NULL, &ctl->attrs, &ctl->win);
        } else
-               rc = openpty(&master, &slave, NULL, NULL, NULL);
+               rc = openpty(&ctl->master, &ctl->slave, NULL, NULL, NULL);
 
        if (rc < 0) {
                warn(_("openpty failed"));
-               fail();
+               fail(ctl);
        }
 #else
        char *pty, *bank, *cp;
-       struct stat stb;
 
-       isterm = isatty(STDIN_FILENO);
+       ctl->isterm = isatty(STDIN_FILENO);
 
-       pty = &line[strlen("/dev/ptyp")];
+       pty = &ctl->line[strlen("/dev/ptyp")];
        for (bank = "pqrs"; *bank; bank++) {
-               line[strlen("/dev/pty")] = *bank;
+               ctl->line[strlen("/dev/pty")] = *bank;
                *pty = '0';
-               if (stat(line, &stb) < 0)
+               if (access(ctl->line, F_OK) != 0)
                        break;
                for (cp = "0123456789abcdef"; *cp; cp++) {
                        *pty = *cp;
-                       master = open(line, O_RDWR);
-                       if (master >= 0) {
-                               char *tp = &line[strlen("/dev/")];
+                       ctl->master = open(ctl->line, O_RDWR | O_CLOEXEC);
+                       if (ctl->master >= 0) {
+                               char *tp = &ctl->line[strlen("/dev/")];
                                int ok;
 
                                /* verify slave side is usable */
                                *tp = 't';
-                               ok = access(line, R_OK|W_OK) == 0;
+                               ok = access(ctl->line, R_OK | W_OK) == 0;
                                *tp = 'p';
                                if (ok) {
-                                       if (isterm) {
-                                               tcgetattr(STDIN_FILENO, &tt);
-                                               ioctl(STDIN_FILENO, TIOCGWINSZ,
-                                                               (char *)&win);
+                                       if (ctl->isterm) {
+                                               tcgetattr(STDIN_FILENO, &ctl->attrs);
+                                               ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ctl->win);
                                        }
                                        return;
                                }
-                               close(master);
-                               master = -1;
+                               close(ctl->master);
+                               ctl->master = -1;
                        }
                }
        }
-       master = -1;
+       ctl->master = -1;
        warn(_("out of pty's"));
-       fail();
-#endif /* not HAVE_LIBUTIL */
+       fail(ctl);
+#endif                         /* not HAVE_LIBUTIL */
+
+       DBG(IO, ul_debug("master fd: %d", ctl->master));
 }
 
-/*
- * script -t prints time delays as floating point numbers
- * The example program (scriptreplay) that we provide to handle this
- * timing output is a perl script, and does not handle numbers in
- * locale format (not even when "use locale;" is added).
- * So, since these numbers are not for human consumption, it seems
- * easiest to set LC_NUMERIC here.
- */
 int main(int argc, char **argv)
 {
-       struct sigaction sa;
+       struct script_control ctl = {
+#if !HAVE_LIBUTIL || !HAVE_PTY_H
+               .line = "/dev/ptyXX",
+#endif
+               .master = -1,
+               .slave  = -1,
+
+               .poll_timeout = -1
+       };
        int ch;
+       const char *outfile = NULL, *infile = NULL;
+       const char *timingfile = NULL;
 
        enum { FORCE_OPTION = CHAR_MAX + 1 };
 
        static const struct option longopts[] = {
-               { "append",     no_argument,       NULL, 'a' },
-               { "command",    required_argument, NULL, 'c' },
-               { "return",     no_argument,       NULL, 'e' },
-               { "flush",      no_argument,       NULL, 'f' },
-               { "force",      no_argument,       NULL, FORCE_OPTION, },
-               { "quiet",      no_argument,       NULL, 'q' },
-               { "timing",     optional_argument, NULL, 't' },
-               { "version",    no_argument,       NULL, 'V' },
-               { "help",       no_argument,       NULL, 'h' },
-               { NULL,         0, NULL, 0 }
+               {"append", no_argument, NULL, 'a'},
+               {"command", required_argument, NULL, 'c'},
+               {"return", no_argument, NULL, 'e'},
+               {"flush", no_argument, NULL, 'f'},
+               {"force", no_argument, NULL, FORCE_OPTION,},
+               {"log-in", required_argument, NULL, 'I'},
+               {"log-out", required_argument, NULL, 'O'},
+               {"log-timing", required_argument, NULL, 'T'},
+               {"output-limit", required_argument, NULL, 'o'},
+               {"quiet", no_argument, NULL, 'q'},
+               {"timing", optional_argument, NULL, 't'},
+               {"version", no_argument, NULL, 'V'},
+               {"help", no_argument, NULL, 'h'},
+               {NULL, 0, NULL, 0}
        };
-
+       static const ul_excl_t excl[] = {       /* rows and cols in ASCII order */
+               { 'T', 't' },
+               { 0 }
+       };
+       int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
        setlocale(LC_ALL, "");
-       setlocale(LC_NUMERIC, "C");     /* see comment above */
+       /*
+        * script -t prints time delays as floating point numbers.  The example
+        * program (scriptreplay) that we provide to handle this timing output
+        * is a perl script, and does not handle numbers in locale format (not
+        * even when "use locale;" is added).  So, since these numbers are not
+        * for human consumption, it seems easiest to set LC_NUMERIC here.
+        */
+       setlocale(LC_NUMERIC, "C");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
+
+       script_init_debug();
+
+       while ((ch = getopt_long(argc, argv, "ac:efI:O:o:qT:t::Vh", longopts, NULL)) != -1) {
+
+               err_exclusive_options(ch, longopts, excl, excl_st);
 
-       while ((ch = getopt_long(argc, argv, "ac:efqt::Vh", longopts, NULL)) != -1)
-               switch(ch) {
+               switch (ch) {
                case 'a':
-                       aflg = 1;
+                       ctl.append = 1;
                        break;
                case 'c':
-                       cflg = optarg;
+                       ctl.command = optarg;
                        break;
                case 'e':
-                       eflg = 1;
+                       ctl.rc_wanted = 1;
                        break;
                case 'f':
-                       fflg = 1;
+                       ctl.flush = 1;
                        break;
                case FORCE_OPTION:
-                       forceflg = 1;
+                       ctl.force = 1;
+                       break;
+               case 'I':
+                       log_associate(&ctl, &ctl.in, optarg, SCRIPT_FMT_RAW);
+                       infile = optarg;
+                       break;
+               case 'O':
+                       log_associate(&ctl, &ctl.out, optarg, SCRIPT_FMT_RAW);
+                       outfile = optarg;
+                       break;
+               case 'o':
+                       ctl.maxsz = strtosize_or_err(optarg, _("failed to parse output limit size"));
                        break;
                case 'q':
-                       qflg = 1;
+                       ctl.quiet = 1;
                        break;
                case 't':
-                       if (optarg && !(timingfd = fopen(optarg, "w")))
-                               err(EXIT_FAILURE, _("cannot open %s"), optarg);
-                       tflg = 1;
+                       if (optarg && *optarg == '=')
+                               optarg++;
+                       log_associate(&ctl, &ctl.out,
+                               optarg ? optarg : "/dev/stderr",
+                               SCRIPT_FMT_TIMING_SIMPLE);
+                       /* used for message only */
+                       timingfile = optarg ? optarg : "stderr";
                        break;
-               case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       exit(EXIT_SUCCESS);
+               case 'T' :
+                       log_associate(&ctl, &ctl.out, optarg, SCRIPT_FMT_TIMING_SIMPLE);
+                       timingfile = optarg;
                        break;
+               case 'V':
+                       print_version(EXIT_SUCCESS);
                case 'h':
-                       usage(stdout);
-                       break;
-               case '?':
+                       usage();
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
+       }
        argc -= optind;
        argv += optind;
 
-       if (argc > 0)
-               fname = argv[0];
-       else {
-               fname = DEFAULT_OUTPUT;
-               die_if_link(fname);
-       }
+       /* default if no --log-* specified */
+       if (!outfile && !infile) {
+               if (argc > 0)
+                       outfile = argv[0];
+               else {
+                       die_if_link(&ctl, DEFAULT_TYPESCRIPT_FILENAME);
+                       outfile = DEFAULT_TYPESCRIPT_FILENAME;
+               }
 
-       if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
-               warn(_("cannot open %s"), fname);
-               fail();
+               /* associate stdout with typescript file */
+               log_associate(&ctl, &ctl.out, outfile, SCRIPT_FMT_RAW);
        }
 
-       shell = getenv("SHELL");
-       if (shell == NULL)
-               shell = _PATH_BSHELL;
-
-       getmaster();
-       if (!qflg)
-               printf(_("Script started, file is %s\n"), fname);
-       fixtty();
+       ctl.shell = getenv("SHELL");
+       if (ctl.shell == NULL)
+               ctl.shell = _PATH_BSHELL;
+
+       getmaster(&ctl);
+       if (!ctl.quiet) {
+               printf(_("Script started"));
+               if (outfile)
+                       printf(_(", output log file is '%s'"), outfile);
+               if (infile)
+                       printf(_(", input log file is '%s'"), infile);
+               if (timingfile)
+                       printf(_(", timing file is '%s'"), timingfile);
+               printf(_(".\n"));
+       }
+       enable_rawmode_tty(&ctl);
 
 #ifdef HAVE_LIBUTEMPTER
-       utempter_add_record(master, NULL);
+       utempter_add_record(ctl.master, NULL);
 #endif
-       /* setup SIGCHLD handler */
-       sigemptyset(&sa.sa_mask);
-       sa.sa_flags = 0;
-       sa.sa_handler = sig_finish;
-       sigaction(SIGCHLD, &sa, NULL);
+       /* setup signal handler */
+       sigemptyset(&ctl.sigset);
+       sigaddset(&ctl.sigset, SIGCHLD);
+       sigaddset(&ctl.sigset, SIGWINCH);
+       sigaddset(&ctl.sigset, SIGTERM);
+       sigaddset(&ctl.sigset, SIGINT);
+       sigaddset(&ctl.sigset, SIGQUIT);
 
-       /* init mask for SIGCHLD */
-       sigprocmask(SIG_SETMASK, NULL, &block_mask);
-       sigaddset(&block_mask, SIGCHLD);
+       /* block signals used for signalfd() to prevent the signals being
+        * handled according to their default dispositions */
+       sigprocmask(SIG_BLOCK, &ctl.sigset, &ctl.sigorg);
 
-       fflush(stdout);
-       sigprocmask(SIG_SETMASK, &block_mask, &unblock_mask);
-       child = fork();
-       sigprocmask(SIG_SETMASK, &unblock_mask, NULL);
+       if ((ctl.sigfd = signalfd(-1, &ctl.sigset, SFD_CLOEXEC)) < 0)
+               err(EXIT_FAILURE, _("cannot set signal handler"));
 
-       if (child < 0) {
-               warn(_("fork failed"));
-               fail();
-       }
-       if (child == 0) {
+       DBG(SIGNAL, ul_debug("signal fd=%d", ctl.sigfd));
 
-               sigprocmask(SIG_SETMASK, &block_mask, NULL);
-               subchild = child = fork();
-               sigprocmask(SIG_SETMASK, &unblock_mask, NULL);
+       fflush(stdout);
+       ctl.child = fork();
 
-               if (child < 0) {
-                       warn(_("fork failed"));
-                       fail();
-               }
-               if (child)
-                       dooutput();
-               else
-                       doshell();
-       } else {
-               sa.sa_handler = resize;
-               sigaction(SIGWINCH, &sa, NULL);
+       switch (ctl.child) {
+       case -1: /* error */
+               warn(_("fork failed"));
+               fail(&ctl);
+               break;
+       case 0: /* child */
+               do_shell(&ctl);
+               break;
+       default: /* parent */
+               do_io(&ctl);
+               break;
        }
-       doinput();
 
-       return EXIT_SUCCESS;
+       /* should not happen, all used functions are non-return */
+       return EXIT_FAILURE;
 }