Module Name:    src
Committed By:   gson
Date:           Fri Jun 12 17:28:53 UTC 2015

Modified Files:
        src/sys/kern: tty.c

Log Message:
When closing a tty, limit the amount of time spent waiting for the
output to drain to five seconds so that exiting processes with
buffered output for a serial port blocked by flow control do not
hang indefinitely.  Should fix PR kern/12534.  OK christos.


To generate a diff of this commit:
cvs rdiff -u -r1.262 -r1.263 src/sys/kern/tty.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/tty.c
diff -u src/sys/kern/tty.c:1.262 src/sys/kern/tty.c:1.263
--- src/sys/kern/tty.c:1.262	Fri Sep  5 05:33:39 2014
+++ src/sys/kern/tty.c	Fri Jun 12 17:28:53 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: tty.c,v 1.262 2014/09/05 05:33:39 matt Exp $	*/
+/*	$NetBSD: tty.c,v 1.263 2015/06/12 17:28:53 gson Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tty.c,v 1.262 2014/09/05 05:33:39 matt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tty.c,v 1.263 2015/06/12 17:28:53 gson Exp $");
 
 #include "opt_compat_netbsd.h"
 
@@ -102,6 +102,7 @@ __KERNEL_RCSID(0, "$NetBSD: tty.c,v 1.26
 #endif /* COMPAT_60 */
 
 static int	ttnread(struct tty *);
+static int	ttywait_timo(struct tty *, int timo);
 static void	ttyblock(struct tty *);
 static void	ttyecho(int, struct tty *);
 static void	ttyrubo(struct tty *, int);
@@ -1536,10 +1537,10 @@ ttnread(struct tty *tp)
 }
 
 /*
- * Wait for output to drain.
+ * Wait for output to drain, or if this times out, flush it.
  */
-int
-ttywait(struct tty *tp)
+static int
+ttywait_timo(struct tty *tp, int timo)
 {
 	int	error;
 
@@ -1549,9 +1550,11 @@ ttywait(struct tty *tp)
 	while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
 	    CONNECTED(tp) && tp->t_oproc) {
 		(*tp->t_oproc)(tp);
-		error = ttysleep(tp, &tp->t_outcv, true, 0);
-		if (error)
+		error = ttysleep(tp, &tp->t_outcv, true, timo);
+		if (error) {
+			ttyflush(tp, FWRITE);
 			break;
+		}
 	}
 	mutex_spin_exit(&tty_lock);
 
@@ -1559,6 +1562,15 @@ ttywait(struct tty *tp)
 }
 
 /*
+ * Wait for output to drain.
+ */
+int
+ttywait(struct tty *tp)
+{
+	return ttywait_timo(tp, 0);
+}
+
+/*
  * Flush if successfully wait.
  */
 int
@@ -1566,7 +1578,8 @@ ttywflush(struct tty *tp)
 {
 	int	error;
 
-	if ((error = ttywait(tp)) == 0) {
+	error = ttywait_timo(tp, 5 * hz);
+	if (error == 0 || error == EWOULDBLOCK) {
 		mutex_spin_enter(&tty_lock);
 		ttyflush(tp, FREAD);
 		mutex_spin_exit(&tty_lock);

Reply via email to