From nobody Mon Apr 29 05:29:40 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 4VSX2Y2Vljz5JHsD; Mon, 29 Apr 2024 05:29:41 +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 4VSX2Y0tMZz4FLY; Mon, 29 Apr 2024 05:29:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714368581; 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=+Psr1swo5M1J7GLLPB21i0KLo7e45ebVoMY6d3rXnEc=; b=ljxDTReRM6eqTo/+nPBx+/5AvOUhcgUuAsBz9V54DZztZewbVft+UPZd9ekNmfnGL3zmzU 8vZJaskFA8D/OuYu7FS/W+lS1kfmMiSD3zQGDrSARSiAvSHMm53FTwMshRnAhJl6PqETWf z1sU77NhFXGtaDa4GKNiDdc6JcQfxTKLDqlQ91fhwJ70nclF7fuWTdx2Tmr1DVWYPikQmd wUwsxiLDFW6Vz+zRLOerHYhvnjTBJfE1D5eeHF73oph25N1VXz6YPL0j6x5R7WUHkqLpYH K7YPYFXVoQBESNyq+6r/iiLFJlCZ/IP8xV87US8EI2BUPhr807xY+FC1EqJ7rQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1714368581; a=rsa-sha256; cv=none; b=otMgTZkNkN9bojVi9iK3AKszuFrIBWt8uF300BepmDmRo/Vlwlw6y6dmgvSN5PYfsAAn7w xxqrqVjgdhRMRC76nqXAKPp/zElTNSg7pqdP2bAayOAlULa5ErCazCONZuQ7HFF9jWXNCP s0X8+CQYMvTiDIuBk1S2J4mUUWtFyW7GePpyKYgkjjcTnr65i69oVViApPEGxKC73dI+6m nzMDaMGDheEmtnB5OqTpmq53QeaU6nKue4EN7usxOQh2lfs72Q0aW6alXXlT5FZaZ6kB3z E7ReFCW2kJDSUqazDvURkWotuyIXMs+9cVZmSjmF9M2Ik7EjJqVt8w+iXh6bzQ== 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=1714368581; 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=+Psr1swo5M1J7GLLPB21i0KLo7e45ebVoMY6d3rXnEc=; b=wXUxGi2sK1dDBdoDXYKSBRwoefxqYlWB2FHl1tTHIe9u5adtj0mHIdVXWK8X/oUVzGjTjF zSROxnHgxE1B/p0bjbOnVP1rbXAcROp7ra5FBlU78+zMm6aVAGBTCa0HKE32psAPxJI0BK U9mnKTVxCbLgcC7T7mQ6m0CXc+xV0u7NSeazVKeMDNMqBTbakzf5VJns8gM3REewY+Teou ecbH0ReadTytLV5mlGaWUP8FxeOMZP+0l5V67qxxPZEzBoE14tIpfBSfVQ15x6rnSLM0b6 bCZs2k9r9s3MgasxqC4goEqLv47vo7Osr/+UaYgW94wCp3uePeBK1z/he1L/0Q== 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 4VSX2Y0P2MzwC3; Mon, 29 Apr 2024 05:29:41 +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 43T5TeUU047397; Mon, 29 Apr 2024 05:29:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43T5TeFr047394; Mon, 29 Apr 2024 05:29:40 GMT (envelope-from git) Date: Mon, 29 Apr 2024 05:29:40 GMT Message-Id: <202404290529.43T5TeFr047394@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: f224a2ca04ad - stable/13 - 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/stable/13 X-Git-Reftype: branch X-Git-Commit: f224a2ca04ad8c9322738517a9092bc488256bc8 Auto-Submitted: auto-generated The branch stable/13 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=f224a2ca04ad8c9322738517a9092bc488256bc8 commit f224a2ca04ad8c9322738517a9092bc488256bc8 Author: Kyle Evans AuthorDate: 2024-04-26 16:12:00 +0000 Commit: Kyle Evans CommitDate: 2024-04-29 05:28:45 +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. (cherry picked from commit 8ceac8e13dccbe4e177c8f2f443b87b7d2e3edb3) --- 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 318ae4d743d9..bc096c0895c5 100644 --- a/usr.bin/script/script.1 +++ b/usr.bin/script/script.1 @@ -35,7 +35,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 @@ -133,6 +133,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 3d82bad9ae2b..62f1399b6dc2 100644 --- a/usr.bin/script/script.c +++ b/usr.bin/script/script.c @@ -86,6 +86,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; @@ -103,31 +104,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; @@ -166,6 +179,9 @@ main(int argc, char *argv[]) if (strchr(optarg, '%')) tstamp_fmt = optarg; break; + case 'w': + wflg = 1; + break; case '?': default: usage(); @@ -269,6 +285,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 (;;) { @@ -281,19 +314,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) @@ -368,7 +408,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); @@ -614,3 +654,9 @@ playback(FILE *fp) (void)fclose(fp); exit(0); } + +static void +resizeit(int signo __unused) +{ + doresize = 1; +}