cdown / rpms / util-linux

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