From nobody Fri Apr 26 16:39:17 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VQz2Y66xTz5HnBc; Fri, 26 Apr 2024 16:39:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VQz2Y3yjDz4p6s; Fri, 26 Apr 2024 16:39:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714149557; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8fCEgSdIckIbn+RC1Mzvb+Mt8eM5Clg4YTbpvrX/t5g=; b=nKSp/7O7qE9wk26CgqYSzi6CP440aCRYfUuHYF6WYXitY9MLjafdXlm/4i2gPigicX+gYI ipUZej5U0S+NQ4z/h5LY/f/8NS0a9cWzUBqPDQ8+EMfa39NS9UJzyzhYb3H/TiI2BQijHv Ig/OyQwuBGsmhTPtBqlKK9gEanNXuwnXl2JS4XIH+cC57exSFE81n1p5gfya4DBAjgb4w9 WykbWFzG43s6L1C0SX3yZx5gPhfklsqOLlBBiL4zRkm6P46+ArDEXBsDLWGfHn5DwrZX47 nB6c2kBgLrUZFZI9yn/w4jH/ISSCO3fD2VWPq/sK5Sf5uJPuZC57OvLCfwqNMQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1714149557; a=rsa-sha256; cv=none; b=E4tTI++83oaSBpNW8x7I4Py3p4seWHK0xVqzwEi1OB0CGeaHak1Q63MOjBoi+BtFyf38zQ owj8DrU7h3xxpcxEHXNL7d8DRlXBN7Tm5cEYwKnYbfjOy0ipJbEB5rCO/gG5X5/us/Z/KY nsxq6f6N/HCCsERkOELG/6eH2cUerl7HxkiU+2/sMYjmL+VyE5uxhq8GyYsAOCX6kKzeJa xr2+eMaD8mwv9HxY9MxuagK5oRFHesfth7PchAmTh44Wjwr2VhX5CiLkryxqG8MO7dNw2o WQuV06PSnO89jdbwipUIxvlDAZX9RUGpi96yWenHlNfFL5P/ltcrDk5UYhIBCA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714149557; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8fCEgSdIckIbn+RC1Mzvb+Mt8eM5Clg4YTbpvrX/t5g=; b=JwmdzGbIvhGgRboxHMTgqlNhuAXvoL1Em/nQvBKB/1rB0+5znA6STJsJWOS5TR1BdlXXQp lN4kBW0Khl+LKjROiSgBE/jUMveNg/eVNpZs9JBl4rw+ium4FV142XwmPoESezYLBm41+d riPRO2x6xsSdpS1NMQzoF10sPkoLESyienO4Y0myo7OHC37raFhNpKTM1Kt+CmNgUvNmiL /UHtoDcIX3U1f3ep49BhyYH72tPARC8VwSSBuqbCHwC9wChSWVkgacSW43fGr9EXx58VrQ 38NIcmm7QAA84H9IGr3Rf2BnX9+16qpxxwl5VLlQ2txTp4BiOKS7dhogyKPfow== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VQz2Y3ZgpzqYb; Fri, 26 Apr 2024 16:39:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43QGdHU1064934; Fri, 26 Apr 2024 16:39:17 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43QGdHOZ064931; Fri, 26 Apr 2024 16:39:17 GMT (envelope-from git) Date: Fri, 26 Apr 2024 16:39:17 GMT Message-Id: <202404261639.43QGdHOZ064931@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 8ceac8e13dcc - main - script: handle terminal resize on SIGWINCH List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8ceac8e13dccbe4e177c8f2f443b87b7d2e3edb3 Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=8ceac8e13dccbe4e177c8f2f443b87b7d2e3edb3 commit 8ceac8e13dccbe4e177c8f2f443b87b7d2e3edb3 Author: Kyle Evans AuthorDate: 2024-04-26 16:12:00 +0000 Commit: Kyle Evans CommitDate: 2024-04-26 16:39:00 +0000 script: handle terminal resize on SIGWINCH Add a -w flag to forward terminal resize events on to the child, which can be useful in some circumstances to avoid terminal corruption. Reviewed by: des Co-authored-by: Xavier Beaudouin Sponsored by: Modirum MDPay Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D44167 --- usr.bin/script/script.1 | 5 +++- usr.bin/script/script.c | 62 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/usr.bin/script/script.1 b/usr.bin/script/script.1 index 78e2feef3712..5f40e5af28ff 100644 --- a/usr.bin/script/script.1 +++ b/usr.bin/script/script.1 @@ -33,7 +33,7 @@ .Nd make typescript of terminal session .Sh SYNOPSIS .Nm -.Op Fl aeFfkqr +.Op Fl aeFfkqrw .Op Fl t Ar time .Op Ar file Op Ar command ... .Nm @@ -131,6 +131,9 @@ characters, it indicates the default format: which is useful for both tools and humans to read, should be used. Note that time-stamps will only be output when different from the previous one. +.It Fl w +Forward terminal size changes on +.Dv SIGWINCH . .El .Pp The script ends when the forked shell (or command) exits (a diff --git a/usr.bin/script/script.c b/usr.bin/script/script.c index 0373a6667784..cca265b8941b 100644 --- a/usr.bin/script/script.c +++ b/usr.bin/script/script.c @@ -77,6 +77,7 @@ static char *fmfname; static int fflg, qflg, ttyflg; static int usesleep, rawout, showexit; static TAILQ_HEAD(, buf_elm) obuf_list = TAILQ_HEAD_INITIALIZER(obuf_list); +static volatile sig_atomic_t doresize; static struct termios tt; @@ -94,31 +95,43 @@ static void record(FILE *, char *, size_t, int); static void consume(FILE *, off_t, char *, int); static void playback(FILE *) __dead2; static void usage(void) __dead2; +static void resizeit(int); int main(int argc, char *argv[]) { struct termios rtt, stt; struct winsize win; - struct timeval tv, *tvp; + struct timespec tv, *tvp; time_t tvec, start; char obuf[BUFSIZ]; char ibuf[BUFSIZ]; + sigset_t *pselmask, selmask; fd_set rfd, wfd; struct buf_elm *be; ssize_t cc; - int aflg, Fflg, kflg, pflg, ch, k, n, fcm; + int aflg, Fflg, kflg, pflg, wflg, ch, k, n, fcm; int flushtime, readstdin; int fm_fd, fm_log; - aflg = Fflg = kflg = pflg = 0; + aflg = Fflg = kflg = pflg = wflg = 0; + doresize = 0; usesleep = 1; rawout = 0; flushtime = 30; fm_fd = -1; showexit = 0; - while ((ch = getopt(argc, argv, "adeFfkpqrT:t:")) != -1) + /* + * For normal operation, we'll leave pselmask == NULL so that pselect(2) + * leaves the signal mask alone. If -w is specified, we'll restore the + * process signal mask upon entry with SIGWINCH unblocked so that we can + * forward resize events properly. + */ + sigemptyset(&selmask); + pselmask = NULL; + + while ((ch = getopt(argc, argv, "adeFfkpqrT:t:w")) != -1) switch (ch) { case 'a': aflg = 1; @@ -157,6 +170,9 @@ main(int argc, char *argv[]) if (strchr(optarg, '%')) tstamp_fmt = optarg; break; + case 'w': + wflg = 1; + break; case '?': default: usage(); @@ -260,6 +276,23 @@ main(int argc, char *argv[]) } close(slave); + if (wflg) { + struct sigaction sa = { .sa_handler = resizeit }; + sigset_t smask; + + sigaction(SIGWINCH, &sa, NULL); + + sigemptyset(&smask); + sigaddset(&smask, SIGWINCH); + + if (sigprocmask(SIG_BLOCK, &smask, &selmask) != 0) + err(1, "Failed to block SIGWINCH"); + + /* Just in case SIGWINCH was blocked before we came in. */ + sigdelset(&selmask, SIGWINCH); + pselmask = &selmask; + } + start = tvec = time(0); readstdin = 1; for (;;) { @@ -272,19 +305,26 @@ main(int argc, char *argv[]) FD_SET(master, &wfd); if (!readstdin && ttyflg) { tv.tv_sec = 1; - tv.tv_usec = 0; + tv.tv_nsec = 0; tvp = &tv; readstdin = 1; } else if (flushtime > 0) { tv.tv_sec = flushtime - (tvec - start); - tv.tv_usec = 0; + tv.tv_nsec = 0; tvp = &tv; } else { tvp = NULL; } - n = select(master + 1, &rfd, &wfd, NULL, tvp); + n = pselect(master + 1, &rfd, &wfd, NULL, tvp, pselmask); if (n < 0 && errno != EINTR) break; + + if (doresize) { + if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) != -1) + ioctl(master, TIOCSWINSZ, &win); + doresize = 0; + } + if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { cc = read(STDIN_FILENO, ibuf, BUFSIZ); if (cc < 0) @@ -359,7 +399,7 @@ static void usage(void) { (void)fprintf(stderr, - "usage: script [-aeFfkpqr] [-t time] [file [command ...]]\n"); + "usage: script [-aeFfkpqrw] [-t time] [file [command ...]]\n"); (void)fprintf(stderr, " script -p [-deq] [-T fmt] [file]\n"); exit(1); @@ -605,3 +645,9 @@ playback(FILE *fp) (void)fclose(fp); exit(0); } + +static void +resizeit(int signo __unused) +{ + doresize = 1; +}