From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: Sami Kerola <kerolasa@iki.fi>, Wolfgang Richter <wolf@cs.cmu.edu>,
Ruediger Meier <ruediger.meier@ga-group.nl>
Subject: [PATCH 18/22] script: remove io vs signal race
Date: Sat, 27 Dec 2014 17:17:31 +0000 [thread overview]
Message-ID: <1419700655-19107-5-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1419700655-19107-1-git-send-email-kerolasa@iki.fi>
Make do_io() to run poll() until all streams are empty. This should
remove the signal from child versus io handling race for good.
Addresses: https://github.com/karelzak/util-linux/pull/62
CC: Wolfgang Richter <wolf@cs.cmu.edu>
CC: Ruediger Meier <ruediger.meier@ga-group.nl>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
term-utils/script.c | 20 +++++++++++++-------
tests/ts/script/race | 4 ----
2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/term-utils/script.c b/term-utils/script.c
index 4a761c6..2def44a 100644
--- a/term-utils/script.c
+++ b/term-utils/script.c
@@ -92,6 +92,7 @@ struct script_control {
FILE *timingfp; /* timing file pointer */
int master; /* pseudoterminal master file descriptor */
int slave; /* pseudoterminal slave file descriptor */
+ int poll_timeout; /* poll() timeout, used in end of execution */
pid_t child; /* child pid */
int childstatus; /* child process exit value */
struct termios tt; /* slave terminal runtime attributes */
@@ -185,16 +186,11 @@ static void finish(struct script_control *ctl, int wait)
{
int status;
pid_t pid;
- int errsv = errno;
int options = wait ? 0 : WNOHANG;
while ((pid = wait3(&status, options, 0)) > 0)
- if (pid == ctl->child) {
+ if (pid == ctl->child)
ctl->childstatus = status;
- ctl->die = 1;
- }
-
- errno = errsv;
}
static void write_output(struct script_control *ctl, char *obuf,
@@ -250,13 +246,15 @@ static void do_io(struct script_control *ctl)
while (!ctl->die) {
/* wait for input or signal */
- ret = poll(pfd, POLLFDS, -1);
+ ret = poll(pfd, POLLFDS, ctl->poll_timeout);
if (ret < 0) {
if (errno == EAGAIN)
continue;
warn(_("poll failed"));
fail(ctl);
}
+ if (ret == 0)
+ ctl->die = 1;
for (i = 0; i < POLLFDS; i++) {
if (pfd[i].revents == 0)
continue;
@@ -295,6 +293,13 @@ static void do_io(struct script_control *ctl)
switch (info.ssi_signo) {
case SIGCHLD:
finish(ctl, 0);
+ ctl->poll_timeout = 10;
+ if (!ctl->isterm)
+ /* In situation such as 'date' in
+ * $ echo date | ./script
+ * ignore input when shell has
+ * exited. */
+ pfd[0].fd = -1;
break;
case SIGWINCH:
if (ctl->isterm) {
@@ -471,6 +476,7 @@ int main(int argc, char **argv)
.line = "/dev/ptyXX",
#endif
.master = -1,
+ .poll_timeout = -1,
0
};
int ch;
diff --git a/tests/ts/script/race b/tests/ts/script/race
index c947402..103a14a 100755
--- a/tests/ts/script/race
+++ b/tests/ts/script/race
@@ -24,10 +24,6 @@ ts_init "$*"
ts_check_test_command "$TS_CMD_SCRIPT"
-# TODO see comments about script design
-# https://github.com/karelzak/util-linux/pull/62
-TS_KNOWN_FAIL="yes"
-
bingofile="$TS_OUTDIR/${TS_TESTNAME}-bingo"
set -o pipefail
--
2.2.1
next prev parent reply other threads:[~2014-12-27 17:17 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-12-27 17:17 [PATCH 00/22] pull: follow-up to chsh, newgrp, and script changes Sami Kerola
2014-12-27 17:17 ` [PATCH 15/22] script: use signalfd() to catch signals Sami Kerola
2014-12-27 17:17 ` [PATCH 16/22] script: use poll() rather than select() Sami Kerola
2014-12-27 17:17 ` [PATCH 17/22] script: merge doinput() and output() functions to do_io() Sami Kerola
2014-12-27 17:17 ` Sami Kerola [this message]
2014-12-27 17:17 ` [PATCH 19/22] script: add 'Script started' line always to capture file Sami Kerola
2014-12-27 17:17 ` [PATCH 20/22] script: move do_io() content to small functions Sami Kerola
2014-12-27 17:17 ` [PATCH 21/22] script: replace strftime() workaround with CFLAGS = -Wno-format-y2k Sami Kerola
2014-12-27 17:17 ` [PATCH 22/22] script: use correct input type, move comment, and so on Sami Kerola
2015-01-06 11:11 ` Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1419700655-19107-5-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=ruediger.meier@ga-group.nl \
--cc=util-linux@vger.kernel.org \
--cc=wolf@cs.cmu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.