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