From nobody Sun Jan 16 02:54:08 2022 X-Original-To: dev-commits-src-branches@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 7A7AF19582E8; Sun, 16 Jan 2022 02:54:08 +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 4Jc0402wVCz4fnc; Sun, 16 Jan 2022 02:54:08 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642301648; 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=shnScEuFR3Ghz/t3bZFtYgAZVWJeBjwXoqlwwb2qH7E=; b=Mug6/swiSFaD25ArArApshUAy8rgikNOZzSO1ve62B4uqm3SdaXEFKr7Uxi0NFVZtjE3L5 4EdMIBjV+2lxzQ3WmEdpuP4mUd5U174Gi+6g4yLq0UJ58L2yhyykW0NfvAkiK4B30QJIuC f0+9+i8wFELqZbD7L4DkzEP/OIOy6uBOyp54QByb81zbNRbdFj2BpocMXQfdsXk/tjR/k0 Y/v6Ow+2qfIZTkxkVRw4j8edZRdTaLh+TjlBXkKKreRb2/R80+ui5hIRC6cP/fwuGnL4FC dHowF+Pl4s0FVYIlNBs3Obu8fhnr+C/NIWQ9tyTbdkvJ1GoNNddfkYNiBhp3hQ== 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 450D412954; Sun, 16 Jan 2022 02:54:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 20G2s8ZL014194; Sun, 16 Jan 2022 02:54:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 20G2s8fn014193; Sun, 16 Jan 2022 02:54:08 GMT (envelope-from git) Date: Sun, 16 Jan 2022 02:54:08 GMT Message-Id: <202201160254.20G2s8fn014193@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 6c12b392280c - stable/13 - script(1): work around slow reading child List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 6c12b392280cf99380ad2897d37d46659e8a1d9a Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642301648; 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=shnScEuFR3Ghz/t3bZFtYgAZVWJeBjwXoqlwwb2qH7E=; b=ZCmucrn7WwUedu148Tqr4Zq8v/qxDhytg1ik3wq/OtcVxNJ3J5qWsbjw4PY6Y60peSIyww oWz68KDTKgwXYio3pNGfDlbWZDxT/YzPAwbVZBPyNp2sKECBYEwcsxiE6EecAyGb2CKqpo CIChkkqVmdGU8wVyZJLSF6HbU9HIV/cJO8dJbhM+/eOqX8oQ3AfKFl04tYGR0cQbhgSHs2 RnQ2VE9Q8gc1uRALL/lsZaRFBXtFbvA5U3w4IXDwbnGBEd5ny6oXaY8y076CG9LLcyr3Im Ezkq3+P7XeTsLggO9Ijgzht7EfvVVgEERwRQ28y2G+/9DzguyBOx29AYK2z1xA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1642301648; a=rsa-sha256; cv=none; b=p1Rh9p2jkOuD+KT+W/x9zYa0zxzeYsoBjK18JhMkRMuEuROMk9od1Jae5MtOW9HCFpShIg bFW4Ppax3/iVmTXR39EtQN/wero3QLFLbRlvT0k2r2FtrxfHgpmtJt4k/hvbkeeb0ZP4k6 CjoJ/jnvIiXTpEwqjIkZG0mlOPdQbwIRQnlOx6hcj3O4FlHnf93hKPqqgqT974ro1OtBmj RWR5uU2+A7/pqsvcX4XTAj8Ns13nX+/FVjd24nzKzSc4K8wxPU3zf5LESgi88N0BD/A3qK fXuOieKOXvq6mW2xnW7qc5nGwCMFxQlvm8DlM4ysZSMulfVp+B6BJ2VmzHjujQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=6c12b392280cf99380ad2897d37d46659e8a1d9a commit 6c12b392280cf99380ad2897d37d46659e8a1d9a Author: Konstantin Belousov AuthorDate: 2022-01-08 13:19:14 +0000 Commit: Konstantin Belousov CommitDate: 2022-01-15 12:55:32 +0000 script(1): work around slow reading child PR: 260938 (cherry picked from commit c0ba4c2ee2c48ec9892d10c0aca797f3685c53ee) --- usr.bin/script/script.c | 56 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/usr.bin/script/script.c b/usr.bin/script/script.c index 4ecc2099926f..9c18dc73390f 100644 --- a/usr.bin/script/script.c +++ b/usr.bin/script/script.c @@ -45,6 +45,7 @@ static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; #include #include #include +#include #include #include #include @@ -70,6 +71,13 @@ struct stamp { uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */ }; +struct buf_elm { + TAILQ_ENTRY(buf_elm) link; + int rpos; + int len; + char ibuf[]; +}; + static FILE *fscript; static int master, slave; static int child; @@ -77,6 +85,7 @@ static const char *fname; 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 struct termios tt; @@ -98,8 +107,9 @@ main(int argc, char *argv[]) time_t tvec, start; char obuf[BUFSIZ]; char ibuf[BUFSIZ]; - fd_set rfd; - int aflg, Fflg, kflg, pflg, ch, k, n; + fd_set rfd, wfd; + struct buf_elm *be; + int aflg, Fflg, kflg, pflg, ch, k, n, fcm; int flushtime, readstdin; int fm_fd, fm_log; @@ -189,6 +199,12 @@ main(int argc, char *argv[]) err(1, "openpty"); ttyflg = 1; } + fcm = fcntl(master, F_GETFL); + if (fcm == -1) + err(1, "master F_GETFL"); + fcm |= O_NONBLOCK; + if (fcntl(master, F_SETFL, fcm) == -1) + err(1, "master F_SETFL"); if (rawout) record(fscript, NULL, 0, 's'); @@ -243,9 +259,12 @@ main(int argc, char *argv[]) readstdin = 1; for (;;) { FD_ZERO(&rfd); + FD_ZERO(&wfd); FD_SET(master, &rfd); if (readstdin) FD_SET(STDIN_FILENO, &rfd); + if (!TAILQ_EMPTY(&obuf_list)) + FD_SET(master, &wfd); if (!readstdin && ttyflg) { tv.tv_sec = 1; tv.tv_usec = 0; @@ -258,7 +277,7 @@ main(int argc, char *argv[]) } else { tvp = NULL; } - n = select(master + 1, &rfd, 0, 0, tvp); + n = select(master + 1, &rfd, &wfd, NULL, tvp); if (n < 0 && errno != EINTR) break; if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { @@ -275,10 +294,37 @@ main(int argc, char *argv[]) if (cc > 0) { if (rawout) record(fscript, ibuf, cc, 'i'); - (void)write(master, ibuf, cc); + be = malloc(sizeof(*be) + cc); + be->rpos = 0; + be->len = cc; + memcpy(be->ibuf, ibuf, cc); + TAILQ_INSERT_TAIL(&obuf_list, be, link); + } + } + if (n > 0 && FD_ISSET(master, &wfd)) { + while ((be = TAILQ_FIRST(&obuf_list)) != NULL) { + cc = write(master, be->ibuf + be->rpos, + be->len); + if (cc == -1) { + if (errno == EWOULDBLOCK || + errno == EINTR) + break; + warn("write master"); + done(1); + } + if (cc == 0) + break; /* retry later ? */ if (kflg && tcgetattr(master, &stt) >= 0 && ((stt.c_lflag & ECHO) == 0)) { - (void)fwrite(ibuf, 1, cc, fscript); + (void)fwrite(be->ibuf + be->rpos, + 1, cc, fscript); + } + be->len -= cc; + if (be->len == 0) { + TAILQ_REMOVE(&obuf_list, be, link); + free(be); + } else { + be->rpos += cc; } } }