531551
diff -up util-linux-2.23.2/term-utils/agetty.c.kzak util-linux-2.23.2/term-utils/agetty.c
531551
--- util-linux-2.23.2/term-utils/agetty.c.kzak	2013-07-30 11:14:18.124912322 +0200
531551
+++ util-linux-2.23.2/term-utils/agetty.c	2013-09-09 09:07:46.406689270 +0200
531551
@@ -132,13 +132,20 @@ struct options {
531551
 	int delay;			/* Sleep seconds before prompt */
531551
 	int nice;			/* Run login with this priority */
531551
 	int numspeed;			/* number of baud rates to try */
531551
+	int clocal;			/* CLOCAL_MODE_* */
531551
 	speed_t speeds[MAX_SPEED];	/* baud rates to be tried */
531551
 };
531551
 
531551
+enum {
531551
+	CLOCAL_MODE_AUTO = 0,
531551
+	CLOCAL_MODE_ALWAYS,
531551
+	CLOCAL_MODE_NEVER
531551
+};
531551
+
531551
 #define	F_PARSE		(1<<0)	/* process modem status messages */
531551
 #define	F_ISSUE		(1<<1)	/* display /etc/issue */
531551
 #define	F_RTSCTS	(1<<2)	/* enable RTS/CTS flow control */
531551
-#define F_LOCAL		(1<<3)	/* force local */
531551
+
531551
 #define F_INITSTRING    (1<<4)	/* initstring is set */
531551
 #define F_WAITCRLF	(1<<5)	/* wait for CR or LF */
531551
 #define F_CUSTISSUE	(1<<6)	/* give alternative issue file */
531551
@@ -235,10 +242,13 @@ static void login_options_to_argv(char *
531551
 static char *fakehost;
531551
 
531551
 #ifdef DEBUGGING
531551
-#define debug(s) do { fprintf(dbf,s); fflush(dbf); } while (0)
531551
+# ifndef DEBUG_OUTPUT
531551
+#  define DEBUG_OUTPUT "/dev/ttyp0"
531551
+# endif
531551
+# define debug(s) do { fprintf(dbf,s); fflush(dbf); } while (0)
531551
 FILE *dbf;
531551
 #else
531551
-#define debug(s) do { ; } while (0)
531551
+# define debug(s) do { ; } while (0)
531551
 #endif
531551
 
531551
 int main(int argc, char **argv)
531551
@@ -270,7 +280,7 @@ int main(int argc, char **argv)
531551
 	sigaction(SIGINT, &sa, &sa_int);
531551
 
531551
 #ifdef DEBUGGING
531551
-	dbf = fopen("/dev/ttyp0", "w");
531551
+	dbf = fopen(DEBUG_OUTPUT, "w");
531551
 	for (int i = 1; i < argc; i++)
531551
 		debug(argv[i]);
531551
 #endif				/* DEBUGGING */
531551
@@ -311,8 +321,10 @@ int main(int argc, char **argv)
531551
 			   strlen(options.initstring));
531551
 	}
531551
 
531551
-	if (!serial_tty_option(&options, F_LOCAL))
531551
-		/* Go to blocking write mode unless -L is specified. */
531551
+	if (options.flags & F_VCONSOLE || options.clocal != CLOCAL_MODE_ALWAYS)
531551
+		/* Go to blocking mode unless -L is specified, this change
531551
+		 * affects stdout, stdin and stderr as all the file descriptors
531551
+		 * are created by dup().   */
531551
 		fcntl(STDOUT_FILENO, F_SETFL,
531551
 		      fcntl(STDOUT_FILENO, F_GETFL, 0) & ~O_NONBLOCK);
531551
 
531551
@@ -420,6 +432,12 @@ int main(int argc, char **argv)
531551
 				options.tty);
531551
 	}
531551
 
531551
+#ifdef DEBUGGING
531551
+	fprintf(dbf, "read %c\n", ch);
531551
+	if (close_stream(dbf) != 0)
531551
+		log_err("write failed: %s", DEBUG_OUTPUT);
531551
+#endif
531551
+
531551
 	/* Let the login program take care of password validation. */
531551
 	execv(options.login, login_argv);
531551
 	log_err(_("%s: can't exec %s: %m"), options.tty, login_argv[0]);
531551
@@ -534,7 +552,7 @@ static void parse_args(int argc, char **
531551
 		{  "init-string",    required_argument,  0,  'I'  },
531551
 		{  "noclear",	     no_argument,	 0,  'J'  },
531551
 		{  "login-program",  required_argument,  0,  'l'  },
531551
-		{  "local-line",     no_argument,	 0,  'L'  },
531551
+		{  "local-line",     optional_argument,	 0,  'L'  },
531551
 		{  "extract-baud",   no_argument,	 0,  'm'  },
531551
 		{  "skip-login",     no_argument,	 0,  'n'  },
531551
 		{  "nonewline",	     no_argument,	 0,  'N'  },
531551
@@ -603,7 +621,18 @@ static void parse_args(int argc, char **
531551
 			op->login = optarg;
531551
 			break;
531551
 		case 'L':
531551
-			op->flags |= F_LOCAL;
531551
+			/* -L and -L=always have the same meaning */
531551
+			op->clocal = CLOCAL_MODE_ALWAYS;
531551
+			if (optarg) {
531551
+				if (strcmp(optarg, "=always") == 0)
531551
+					op->clocal = CLOCAL_MODE_ALWAYS;
531551
+				else if (strcmp(optarg, "=never") == 0)
531551
+					op->clocal = CLOCAL_MODE_NEVER;
531551
+				else if (strcmp(optarg, "=auto") == 0)
531551
+					op->clocal = CLOCAL_MODE_AUTO;
531551
+				else
531551
+					log_err(_("unssuported --local-line mode argument"));
531551
+			}
531551
 			break;
531551
 		case 'm':
531551
 			op->flags |= F_PARSE;
531551
@@ -1090,8 +1119,19 @@ static void termio_init(struct options *
531551
 	cfsetispeed(tp, ispeed);
531551
 	cfsetospeed(tp, ospeed);
531551
 
531551
-	if (op->flags & F_LOCAL)
531551
-		tp->c_cflag |= CLOCAL;
531551
+	/* The default is to follow setting from kernel, but it's possible
531551
+	 * to explicitly remove/add CLOCAL flag by -L[=<mode>]*/
531551
+	switch (op->clocal) {
531551
+	case CLOCAL_MODE_ALWAYS:
531551
+		tp->c_cflag |= CLOCAL;		/* -L or -L=always */
531551
+		break;
531551
+	case CLOCAL_MODE_NEVER:
531551
+		tp->c_cflag &= ~CLOCAL;		/* -L=never */
531551
+		break;
531551
+	case CLOCAL_MODE_AUTO:			/* -L=auto */
531551
+		break;
531551
+	}
531551
+
531551
 #ifdef HAVE_STRUCT_TERMIOS_C_LINE
531551
 	tp->c_line = 0;
531551
 #endif
531551
@@ -1412,9 +1452,10 @@ static char *get_logname(struct options
531551
 
531551
 			if (read(STDIN_FILENO, &c, 1) < 1) {
531551
 
531551
-				/* Do not report trivial like EINTR/EIO errors. */
531551
+				/* The terminal could be open with O_NONBLOCK when
531551
+				 * -L (force CLOCAL) is specified...  */
531551
 				if (errno == EINTR || errno == EAGAIN) {
531551
-					usleep(1000);
531551
+					usleep(250000);
531551
 					continue;
531551
 				}
531551
 				switch (errno) {
531551
@@ -1648,7 +1689,7 @@ static void __attribute__ ((__noreturn__
531551
 	fputs(_(" -i, --noissue              do not display issue file\n"), out);
531551
 	fputs(_(" -I, --init-string <string> set init string\n"), out);
531551
 	fputs(_(" -l, --login-program <file> specify login program\n"), out);
531551
-	fputs(_(" -L, --local-line           force local line\n"), out);
531551
+	fputs(_(" -L, --local-line[=<mode>]  cotrol local line flag\n"), out);
531551
 	fputs(_(" -m, --extract-baud         extract baud rate during connect\n"), out);
531551
 	fputs(_(" -n, --skip-login           do not prompt for login\n"), out);
531551
 	fputs(_(" -o, --login-options <opts> options that are passed to login\n"), out);