util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] sulogin: ignore not existing console devices
@ 2021-05-06 10:54 Werner Fink
  2021-05-06 10:58 ` John Paul Adrian Glaubitz
  2021-05-10 14:51 ` Karel Zak
  0 siblings, 2 replies; 6+ messages in thread
From: Werner Fink @ 2021-05-06 10:54 UTC (permalink / raw)
  To: util-linux; +Cc: werner

[-- Attachment #1: Type: text/plain, Size: 6188 bytes --]

and also not functional console devices. Redirect the error
messages to the appropiate console device.

Signed-off-by: Werner Fink <werner@suse.de>

---
 login-utils/sulogin-consoles.h |  1 +
 login-utils/sulogin.c          | 97 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 82 insertions(+), 16 deletions(-)

diff --git a/login-utils/sulogin-consoles.h b/login-utils/sulogin-consoles.h
index 0bfbc3871..12032c997 100644
--- a/login-utils/sulogin-consoles.h
+++ b/login-utils/sulogin-consoles.h
@@ -40,6 +40,7 @@ struct console {
 	int fd, id;
 #define	CON_SERIAL	0x0001
 #define	CON_NOTTY	0x0002
+#define	CON_EIO		0x0004
 	pid_t pid;
 	struct chardata cp;
 	struct termios tio;
diff --git a/login-utils/sulogin.c b/login-utils/sulogin.c
index 9091caf14..c833796e7 100644
--- a/login-utils/sulogin.c
+++ b/login-utils/sulogin.c
@@ -52,6 +52,7 @@
 #ifdef __linux__
 # include <sys/kd.h>
 # include <sys/param.h>
+# include <linux/serial.h>
 #endif
 
 #include "c.h"
@@ -104,6 +105,9 @@ static void tcinit(struct console *con)
 	int flags = 0, mode = 0;
 	struct termios *tio = &con->tio;
 	const int fd = con->fd;
+#if defined(TIOCGSERIAL)
+	struct serial_struct serinfo;
+#endif
 #ifdef USE_PLYMOUTH_SUPPORT
 	struct termios lock;
 	int i = (plymouth_command(MAGIC_PING)) ? PLYMOUTH_TERMIOS_FLAGS_DELAY : 0;
@@ -123,27 +127,72 @@ static void tcinit(struct console *con)
 	}
 	memset(&lock, 0, sizeof(struct termios));
 	ioctl(fd, TIOCSLCKTRMIOS, &lock);
+	errno = 0;
 #endif
+
+#if defined(TIOCGSERIAL)
+	if (ioctl(fd, TIOCGSERIAL,  &serinfo) >= 0)
+	    	con->flags |= CON_SERIAL;
+	errno = 0;
+#else
+# if defined(KDGKBMODE)
+	if (ioctl(fd, KDGKBMODE, &mode) < 0)
+		con->flags |= CON_SERIAL;
 	errno = 0;
+# endif
+#endif
 
 	if (tcgetattr(fd, tio) < 0) {
-		warn(_("tcgetattr failed"));
-		con->flags |= CON_NOTTY;
-		return;
+		int saveno = errno;
+#if defined(KDGKBMODE) || defined(TIOCGSERIAL)
+		if (con->flags & CON_SERIAL) {			/* Try to recover this */
+
+# if defined(TIOCGSERIAL)
+			serinfo.flags |= ASYNC_SKIP_TEST;	/* Skip test of UART */
+
+			if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
+				goto tcgeterr;
+			if (ioctl(fd, TIOCSERCONFIG) < 0)	/* Try to autoconfigure */
+				goto tcgeterr;
+			if (ioctl(fd, TIOCGSERIAL,  &serinfo) < 0)
+				goto tcgeterr;			/* Ouch */
+# endif
+			if (tcgetattr(fd, tio) < 0)		/* Retry to get tty attributes */
+				saveno = errno;
+		}
+# if defined(TIOCGSERIAL)
+	tcgeterr:
+# endif
+		if (saveno)
+#endif
+		{
+			FILE *fcerr = fdopen(fd, "w");
+			if (fcerr) {
+				fprintf(fcerr, _("tcgetattr failed"));
+				fclose(fcerr);
+			}
+			warn(_("tcgetattr failed"));
+
+			con->flags &= ~CON_SERIAL;
+			if (saveno != EIO)
+				con->flags |= CON_NOTTY;
+			else
+				con->flags |= CON_EIO;
+
+			errno = 0;
+			return;
+		}
 	}
 
 	/* Handle lines other than virtual consoles here */
-#if defined(KDGKBMODE)
-	if (ioctl(fd, KDGKBMODE, &mode) < 0)
+#if defined(KDGKBMODE) || defined(TIOCGSERIAL)
+	if (con->flags & CON_SERIAL)
 #endif
 	{
 		speed_t ispeed, ospeed;
 		struct winsize ws;
 		errno = 0;
 
-		/* this is a modem line */
-		con->flags |= CON_SERIAL;
-
 		/* Flush input and output queues on modem lines */
 		tcflush(fd, TCIOFLUSH);
 
@@ -220,6 +269,8 @@ static void tcfinal(struct console *con)
 	struct termios *tio = &con->tio;
 	const int fd = con->fd;
 
+	if (con->flags & CON_EIO)
+		return;
 	if ((con->flags & CON_SERIAL) == 0) {
 		xsetenv("TERM", "linux", 1);
 		return;
@@ -557,12 +608,16 @@ err:
 static void setup(struct console *con)
 {
 	int fd = con->fd;
-	const pid_t pid = getpid(), pgrp = getpgid(0), ppgrp =
-	    getpgid(getppid()), ttypgrp = tcgetpgrp(fd);
+	const pid_t pid = getpid(), pgrp = getpgid(0), ppgrp = getpgid(getppid());
+	pid_t ttypgrp;
 
 	if (con->flags & CON_NOTTY)
+		goto notty;
+	if (con->flags & CON_EIO)
 		return;
 
+	ttypgrp = tcgetpgrp(fd);
+
 	/*
 	 * Only go through this trouble if the new
 	 * tty doesn't fall in this process group.
@@ -585,6 +640,7 @@ static void setup(struct console *con)
 		ioctl(fd, TIOCSCTTY, (char *)1);
 		tcsetpgrp(fd, ppgrp);
 	}
+notty:
 	dup2(fd, STDIN_FILENO);
 	dup2(fd, STDOUT_FILENO);
 	dup2(fd, STDERR_FILENO);
@@ -608,20 +664,25 @@ static const char *getpasswd(struct console *con)
 	struct termios tty;
 	static char pass[128], *ptr;
 	struct chardata *cp;
-	const char *ret = pass;
+	const char *ret = NULL;
 	unsigned char tc;
 	char c, ascval;
 	int eightbit;
 	const int fd = con->fd;
 
-	if (con->flags & CON_NOTTY)
+	if (con->flags & CON_EIO)
 		goto out;
+
 	cp = &con->cp;
 	tty = con->tio;
+	tc = 0;
+	ret = pass;
 
 	tty.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
 	tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP|ISIG);
-	tc = (tcsetattr(fd, TCSAFLUSH, &tty) == 0);
+
+	if ((con->flags & CON_NOTTY) == 0)
+		tc = (tcsetattr(fd, TCSAFLUSH, &tty) == 0);
 
 	sigemptyset(&sa.sa_mask);
 	sa.sa_handler = alrm_handler;
@@ -647,11 +708,12 @@ static const char *getpasswd(struct console *con)
 			}
 			ret = NULL;
 			switch (errno) {
-			case 0:
 			case EIO:
+				con->flags |= CON_EIO;
 			case ESRCH:
 			case EINVAL:
 			case ENOENT:
+			case 0:
 				break;
 			default:
 				warn(_("cannot read %s"), con->tty);
@@ -775,7 +837,7 @@ static void sushell(struct passwd *pwd)
 
 #ifdef HAVE_LIBSELINUX
 	if (is_selinux_enabled() > 0) {
-		security_context_t scon=NULL;
+		char *scon=NULL;
 		char *seuser=NULL;
 		char *level=NULL;
 		if (getseuserbyname("root", &seuser, &level) == 0) {
@@ -968,10 +1030,13 @@ int main(int argc, char **argv)
 		con = list_entry(ptr, struct console, entry);
 		if (con->id >= CONMAX)
 			break;
+		if (con->flags & CON_EIO)
+			goto next;
 
 		switch ((con->pid = fork())) {
 		case 0:
 			mask_signal(SIGCHLD, SIG_DFL, NULL);
+			dup2(con->fd, STDERR_FILENO);
 		nofork:
 			setup(con);
 			while (1) {
@@ -1026,7 +1091,7 @@ int main(int argc, char **argv)
 		default:
 			break;
 		}
-
+	next:
 		ptr = ptr->next;
 
 	} while (ptr != &consoles);

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 894 bytes --]

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] sulogin: ignore not existing console devices
  2021-05-06 10:54 [PATCH] sulogin: ignore not existing console devices Werner Fink
@ 2021-05-06 10:58 ` John Paul Adrian Glaubitz
  2021-05-10 14:51 ` Karel Zak
  1 sibling, 0 replies; 6+ messages in thread
From: John Paul Adrian Glaubitz @ 2021-05-06 10:58 UTC (permalink / raw)
  To: util-linux

On 5/6/21 12:54 PM, Werner Fink wrote:
> and also not functional console devices. Redirect the error
> messages to the appropiate console device.

I guess that should be:

	s/not existing/non-existing/

and

	s/not functional/non-functional/

Adrian

> Signed-off-by: Werner Fink <werner@suse.de>
> 
> ---
>  login-utils/sulogin-consoles.h |  1 +
>  login-utils/sulogin.c          | 97 +++++++++++++++++++++++++++++++++++-------
>  2 files changed, 82 insertions(+), 16 deletions(-)
> 
> diff --git a/login-utils/sulogin-consoles.h b/login-utils/sulogin-consoles.h
> index 0bfbc3871..12032c997 100644
> --- a/login-utils/sulogin-consoles.h
> +++ b/login-utils/sulogin-consoles.h
> @@ -40,6 +40,7 @@ struct console {
>  	int fd, id;
>  #define	CON_SERIAL	0x0001
>  #define	CON_NOTTY	0x0002
> +#define	CON_EIO		0x0004
>  	pid_t pid;
>  	struct chardata cp;
>  	struct termios tio;
> diff --git a/login-utils/sulogin.c b/login-utils/sulogin.c
> index 9091caf14..c833796e7 100644
> --- a/login-utils/sulogin.c
> +++ b/login-utils/sulogin.c
> @@ -52,6 +52,7 @@
>  #ifdef __linux__
>  # include <sys/kd.h>
>  # include <sys/param.h>
> +# include <linux/serial.h>
>  #endif
>  
>  #include "c.h"
> @@ -104,6 +105,9 @@ static void tcinit(struct console *con)
>  	int flags = 0, mode = 0;
>  	struct termios *tio = &con->tio;
>  	const int fd = con->fd;
> +#if defined(TIOCGSERIAL)
> +	struct serial_struct serinfo;
> +#endif
>  #ifdef USE_PLYMOUTH_SUPPORT
>  	struct termios lock;
>  	int i = (plymouth_command(MAGIC_PING)) ? PLYMOUTH_TERMIOS_FLAGS_DELAY : 0;
> @@ -123,27 +127,72 @@ static void tcinit(struct console *con)
>  	}
>  	memset(&lock, 0, sizeof(struct termios));
>  	ioctl(fd, TIOCSLCKTRMIOS, &lock);
> +	errno = 0;
>  #endif
> +
> +#if defined(TIOCGSERIAL)
> +	if (ioctl(fd, TIOCGSERIAL,  &serinfo) >= 0)
> +	    	con->flags |= CON_SERIAL;
> +	errno = 0;
> +#else
> +# if defined(KDGKBMODE)
> +	if (ioctl(fd, KDGKBMODE, &mode) < 0)
> +		con->flags |= CON_SERIAL;
>  	errno = 0;
> +# endif
> +#endif
>  
>  	if (tcgetattr(fd, tio) < 0) {
> -		warn(_("tcgetattr failed"));
> -		con->flags |= CON_NOTTY;
> -		return;
> +		int saveno = errno;
> +#if defined(KDGKBMODE) || defined(TIOCGSERIAL)
> +		if (con->flags & CON_SERIAL) {			/* Try to recover this */
> +
> +# if defined(TIOCGSERIAL)
> +			serinfo.flags |= ASYNC_SKIP_TEST;	/* Skip test of UART */
> +
> +			if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
> +				goto tcgeterr;
> +			if (ioctl(fd, TIOCSERCONFIG) < 0)	/* Try to autoconfigure */
> +				goto tcgeterr;
> +			if (ioctl(fd, TIOCGSERIAL,  &serinfo) < 0)
> +				goto tcgeterr;			/* Ouch */
> +# endif
> +			if (tcgetattr(fd, tio) < 0)		/* Retry to get tty attributes */
> +				saveno = errno;
> +		}
> +# if defined(TIOCGSERIAL)
> +	tcgeterr:
> +# endif
> +		if (saveno)
> +#endif
> +		{
> +			FILE *fcerr = fdopen(fd, "w");
> +			if (fcerr) {
> +				fprintf(fcerr, _("tcgetattr failed"));
> +				fclose(fcerr);
> +			}
> +			warn(_("tcgetattr failed"));
> +
> +			con->flags &= ~CON_SERIAL;
> +			if (saveno != EIO)
> +				con->flags |= CON_NOTTY;
> +			else
> +				con->flags |= CON_EIO;
> +
> +			errno = 0;
> +			return;
> +		}
>  	}
>  
>  	/* Handle lines other than virtual consoles here */
> -#if defined(KDGKBMODE)
> -	if (ioctl(fd, KDGKBMODE, &mode) < 0)
> +#if defined(KDGKBMODE) || defined(TIOCGSERIAL)
> +	if (con->flags & CON_SERIAL)
>  #endif
>  	{
>  		speed_t ispeed, ospeed;
>  		struct winsize ws;
>  		errno = 0;
>  
> -		/* this is a modem line */
> -		con->flags |= CON_SERIAL;
> -
>  		/* Flush input and output queues on modem lines */
>  		tcflush(fd, TCIOFLUSH);
>  
> @@ -220,6 +269,8 @@ static void tcfinal(struct console *con)
>  	struct termios *tio = &con->tio;
>  	const int fd = con->fd;
>  
> +	if (con->flags & CON_EIO)
> +		return;
>  	if ((con->flags & CON_SERIAL) == 0) {
>  		xsetenv("TERM", "linux", 1);
>  		return;
> @@ -557,12 +608,16 @@ err:
>  static void setup(struct console *con)
>  {
>  	int fd = con->fd;
> -	const pid_t pid = getpid(), pgrp = getpgid(0), ppgrp =
> -	    getpgid(getppid()), ttypgrp = tcgetpgrp(fd);
> +	const pid_t pid = getpid(), pgrp = getpgid(0), ppgrp = getpgid(getppid());
> +	pid_t ttypgrp;
>  
>  	if (con->flags & CON_NOTTY)
> +		goto notty;
> +	if (con->flags & CON_EIO)
>  		return;
>  
> +	ttypgrp = tcgetpgrp(fd);
> +
>  	/*
>  	 * Only go through this trouble if the new
>  	 * tty doesn't fall in this process group.
> @@ -585,6 +640,7 @@ static void setup(struct console *con)
>  		ioctl(fd, TIOCSCTTY, (char *)1);
>  		tcsetpgrp(fd, ppgrp);
>  	}
> +notty:
>  	dup2(fd, STDIN_FILENO);
>  	dup2(fd, STDOUT_FILENO);
>  	dup2(fd, STDERR_FILENO);
> @@ -608,20 +664,25 @@ static const char *getpasswd(struct console *con)
>  	struct termios tty;
>  	static char pass[128], *ptr;
>  	struct chardata *cp;
> -	const char *ret = pass;
> +	const char *ret = NULL;
>  	unsigned char tc;
>  	char c, ascval;
>  	int eightbit;
>  	const int fd = con->fd;
>  
> -	if (con->flags & CON_NOTTY)
> +	if (con->flags & CON_EIO)
>  		goto out;
> +
>  	cp = &con->cp;
>  	tty = con->tio;
> +	tc = 0;
> +	ret = pass;
>  
>  	tty.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
>  	tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP|ISIG);
> -	tc = (tcsetattr(fd, TCSAFLUSH, &tty) == 0);
> +
> +	if ((con->flags & CON_NOTTY) == 0)
> +		tc = (tcsetattr(fd, TCSAFLUSH, &tty) == 0);
>  
>  	sigemptyset(&sa.sa_mask);
>  	sa.sa_handler = alrm_handler;
> @@ -647,11 +708,12 @@ static const char *getpasswd(struct console *con)
>  			}
>  			ret = NULL;
>  			switch (errno) {
> -			case 0:
>  			case EIO:
> +				con->flags |= CON_EIO;
>  			case ESRCH:
>  			case EINVAL:
>  			case ENOENT:
> +			case 0:
>  				break;
>  			default:
>  				warn(_("cannot read %s"), con->tty);
> @@ -775,7 +837,7 @@ static void sushell(struct passwd *pwd)
>  
>  #ifdef HAVE_LIBSELINUX
>  	if (is_selinux_enabled() > 0) {
> -		security_context_t scon=NULL;
> +		char *scon=NULL;
>  		char *seuser=NULL;
>  		char *level=NULL;
>  		if (getseuserbyname("root", &seuser, &level) == 0) {
> @@ -968,10 +1030,13 @@ int main(int argc, char **argv)
>  		con = list_entry(ptr, struct console, entry);
>  		if (con->id >= CONMAX)
>  			break;
> +		if (con->flags & CON_EIO)
> +			goto next;
>  
>  		switch ((con->pid = fork())) {
>  		case 0:
>  			mask_signal(SIGCHLD, SIG_DFL, NULL);
> +			dup2(con->fd, STDERR_FILENO);
>  		nofork:
>  			setup(con);
>  			while (1) {
> @@ -1026,7 +1091,7 @@ int main(int argc, char **argv)
>  		default:
>  			break;
>  		}
> -
> +	next:
>  		ptr = ptr->next;
>  
>  	} while (ptr != &consoles);
> 

-- 
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - glaubitz@debian.org
`. `'   Freie Universitaet Berlin - glaubitz@physik.fu-berlin.de
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] sulogin: ignore not existing console devices
  2021-05-06 10:54 [PATCH] sulogin: ignore not existing console devices Werner Fink
  2021-05-06 10:58 ` John Paul Adrian Glaubitz
@ 2021-05-10 14:51 ` Karel Zak
  2021-05-17 14:58   ` Dr. Werner Fink
  1 sibling, 1 reply; 6+ messages in thread
From: Karel Zak @ 2021-05-10 14:51 UTC (permalink / raw)
  To: util-linux; +Cc: Werner Fink

On Thu, May 06, 2021 at 12:54:12PM +0200, Werner Fink wrote:
>  login-utils/sulogin-consoles.h |  1 +
>  login-utils/sulogin.c          | 97 +++++++++++++++++++++++++++++++++++-------
>  2 files changed, 82 insertions(+), 16 deletions(-)

Seems good, but git does not like the patch from your e-mail:

        Applying: sulogin: ignore not existing console devices
        .git/rebase-apply/patch:49: space before tab in indent.
                    con->flags |= CON_SERIAL;
        error: patch failed: login-utils/sulogin.c:775
        error: login-utils/sulogin.c: patch does not apply
        Patch failed at 0001 sulogin: ignore not existing console devices


    Karel



-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] sulogin: ignore not existing console devices
  2021-05-10 14:51 ` Karel Zak
@ 2021-05-17 14:58   ` Dr. Werner Fink
  2021-05-17 15:25     ` Dr. Werner Fink
  0 siblings, 1 reply; 6+ messages in thread
From: Dr. Werner Fink @ 2021-05-17 14:58 UTC (permalink / raw)
  To: util-linux

[-- Attachment #1: Type: text/plain, Size: 959 bytes --]

On 2021/05/10 16:51:11 +0200, Karel Zak wrote:
> On Thu, May 06, 2021 at 12:54:12PM +0200, Werner Fink wrote:
> >  login-utils/sulogin-consoles.h |  1 +
> >  login-utils/sulogin.c          | 97 +++++++++++++++++++++++++++++++++++-------
> >  2 files changed, 82 insertions(+), 16 deletions(-)
> 
> Seems good, but git does not like the patch from your e-mail:
> 
>         Applying: sulogin: ignore not existing console devices
>         .git/rebase-apply/patch:49: space before tab in indent.
>                     con->flags |= CON_SERIAL;
>         error: patch failed: login-utils/sulogin.c:775
>         error: login-utils/sulogin.c: patch does not apply
>         Patch failed at 0001 sulogin: ignore not existing console devices

Thanks for spotting! ... looks like I've missed the a/b prefixes

Werner

-- 
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 894 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] sulogin: ignore not existing console devices
  2021-05-17 14:58   ` Dr. Werner Fink
@ 2021-05-17 15:25     ` Dr. Werner Fink
  2021-05-18 11:00       ` Karel Zak
  0 siblings, 1 reply; 6+ messages in thread
From: Dr. Werner Fink @ 2021-05-17 15:25 UTC (permalink / raw)
  To: util-linux


[-- Attachment #1.1: Type: text/plain, Size: 1110 bytes --]

On 2021/05/17 16:58:25 +0200, Dr. Werner Fink wrote:
> On 2021/05/10 16:51:11 +0200, Karel Zak wrote:
> > On Thu, May 06, 2021 at 12:54:12PM +0200, Werner Fink wrote:
> > >  login-utils/sulogin-consoles.h |  1 +
> > >  login-utils/sulogin.c          | 97 +++++++++++++++++++++++++++++++++++-------
> > >  2 files changed, 82 insertions(+), 16 deletions(-)
> > 
> > Seems good, but git does not like the patch from your e-mail:
> > 
> >         Applying: sulogin: ignore not existing console devices
> >         .git/rebase-apply/patch:49: space before tab in indent.
> >                     con->flags |= CON_SERIAL;
> >         error: patch failed: login-utils/sulogin.c:775
> >         error: login-utils/sulogin.c: patch does not apply
> >         Patch failed at 0001 sulogin: ignore not existing console devices
> 
> Thanks for spotting! ... looks like I've missed the a/b prefixes
> 
> Werner

The attached patch is for latest git repository

Werner

-- 
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr

[-- Attachment #1.2: 38a9f563577e0ff73bfeed2d011d211d1486180f.patch --]
[-- Type: text/x-patch, Size: 6118 bytes --]

From 38a9f563577e0ff73bfeed2d011d211d1486180f Mon Sep 17 00:00:00 2001
From: Werner Fink <werner@suse.de>
Date: Mon, 17 May 2021 17:20:32 +0200
Subject: [PATCH] sulogin: ignore none-existing console devices

and also none-functional console devices. Redirect the error
messages to the appropiate console device.

Signed-off-by: Werner Fink <werner@suse.de>
---
 login-utils/sulogin-consoles.h |  1 +
 login-utils/sulogin.c          | 95 ++++++++++++++++++++++++++++------
 2 files changed, 81 insertions(+), 15 deletions(-)

diff --git login-utils/sulogin-consoles.h login-utils/sulogin-consoles.h
index 0bfbc3871..12032c997 100644
--- a/login-utils/sulogin-consoles.h
+++ b/login-utils/sulogin-consoles.h
@@ -40,6 +40,7 @@ struct console {
 	int fd, id;
 #define	CON_SERIAL	0x0001
 #define	CON_NOTTY	0x0002
+#define	CON_EIO		0x0004
 	pid_t pid;
 	struct chardata cp;
 	struct termios tio;
diff --git login-utils/sulogin.c login-utils/sulogin.c
index 6ed63f1a0..8b4e2d108 100644
--- a/login-utils/sulogin.c
+++ b/login-utils/sulogin.c
@@ -52,6 +52,7 @@
 #ifdef __linux__
 # include <sys/kd.h>
 # include <sys/param.h>
+# include <linux/serial.h>
 #endif
 
 #include "c.h"
@@ -104,6 +105,9 @@ static void tcinit(struct console *con)
 	int flags = 0, mode = 0;
 	struct termios *tio = &con->tio;
 	const int fd = con->fd;
+#if defined(TIOCGSERIAL)
+	struct serial_struct serinfo;
+#endif
 #ifdef USE_PLYMOUTH_SUPPORT
 	struct termios lock;
 	int i = (plymouth_command(MAGIC_PING)) ? PLYMOUTH_TERMIOS_FLAGS_DELAY : 0;
@@ -123,27 +127,72 @@ static void tcinit(struct console *con)
 	}
 	memset(&lock, 0, sizeof(struct termios));
 	ioctl(fd, TIOCSLCKTRMIOS, &lock);
+	errno = 0;
 #endif
+
+#if defined(TIOCGSERIAL)
+	if (ioctl(fd, TIOCGSERIAL,  &serinfo) >= 0)
+	    	con->flags |= CON_SERIAL;
+	errno = 0;
+#else
+# if defined(KDGKBMODE)
+	if (ioctl(fd, KDGKBMODE, &mode) < 0)
+		con->flags |= CON_SERIAL;
 	errno = 0;
+# endif
+#endif
 
 	if (tcgetattr(fd, tio) < 0) {
-		warn(_("tcgetattr failed"));
-		con->flags |= CON_NOTTY;
-		return;
+		int saveno = errno;
+#if defined(KDGKBMODE) || defined(TIOCGSERIAL)
+		if (con->flags & CON_SERIAL) {			/* Try to recover this */
+
+# if defined(TIOCGSERIAL)
+			serinfo.flags |= ASYNC_SKIP_TEST;	/* Skip test of UART */
+
+			if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
+				goto tcgeterr;
+			if (ioctl(fd, TIOCSERCONFIG) < 0)	/* Try to autoconfigure */
+				goto tcgeterr;
+			if (ioctl(fd, TIOCGSERIAL,  &serinfo) < 0)
+				goto tcgeterr;			/* Ouch */
+# endif
+			if (tcgetattr(fd, tio) < 0)		/* Retry to get tty attributes */
+				saveno = errno;
+		}
+# if defined(TIOCGSERIAL)
+	tcgeterr:
+# endif
+		if (saveno)
+#endif
+		{
+			FILE *fcerr = fdopen(fd, "w");
+			if (fcerr) {
+				fprintf(fcerr, _("tcgetattr failed"));
+				fclose(fcerr);
+			}
+			warn(_("tcgetattr failed"));
+
+			con->flags &= ~CON_SERIAL;
+			if (saveno != EIO)
+				con->flags |= CON_NOTTY;
+			else
+				con->flags |= CON_EIO;
+
+			errno = 0;
+			return;
+		}
 	}
 
 	/* Handle lines other than virtual consoles here */
-#if defined(KDGKBMODE)
-	if (ioctl(fd, KDGKBMODE, &mode) < 0)
+#if defined(KDGKBMODE) || defined(TIOCGSERIAL)
+	if (con->flags & CON_SERIAL)
 #endif
 	{
 		speed_t ispeed, ospeed;
 		struct winsize ws;
 		errno = 0;
 
-		/* this is a modem line */
-		con->flags |= CON_SERIAL;
-
 		/* Flush input and output queues on modem lines */
 		tcflush(fd, TCIOFLUSH);
 
@@ -220,6 +269,8 @@ static void tcfinal(struct console *con)
 	struct termios *tio = &con->tio;
 	const int fd = con->fd;
 
+	if (con->flags & CON_EIO)
+		return;
 	if ((con->flags & CON_SERIAL) == 0) {
 		xsetenv("TERM", "linux", 1);
 		return;
@@ -557,12 +608,16 @@ err:
 static void setup(struct console *con)
 {
 	int fd = con->fd;
-	const pid_t pid = getpid(), pgrp = getpgid(0), ppgrp =
-	    getpgid(getppid()), ttypgrp = tcgetpgrp(fd);
+	const pid_t pid = getpid(), pgrp = getpgid(0), ppgrp = getpgid(getppid());
+	pid_t ttypgrp;
 
 	if (con->flags & CON_NOTTY)
+		goto notty;
+	if (con->flags & CON_EIO)
 		return;
 
+	ttypgrp = tcgetpgrp(fd);
+
 	/*
 	 * Only go through this trouble if the new
 	 * tty doesn't fall in this process group.
@@ -585,6 +640,7 @@ static void setup(struct console *con)
 		ioctl(fd, TIOCSCTTY, (char *)1);
 		tcsetpgrp(fd, ppgrp);
 	}
+notty:
 	dup2(fd, STDIN_FILENO);
 	dup2(fd, STDOUT_FILENO);
 	dup2(fd, STDERR_FILENO);
@@ -608,20 +664,25 @@ static const char *getpasswd(struct console *con)
 	struct termios tty;
 	static char pass[128], *ptr;
 	struct chardata *cp;
-	const char *ret = pass;
+	const char *ret = NULL;
 	unsigned char tc;
 	char c, ascval;
 	int eightbit;
 	const int fd = con->fd;
 
-	if (con->flags & CON_NOTTY)
+	if (con->flags & CON_EIO)
 		goto out;
+
 	cp = &con->cp;
 	tty = con->tio;
+	tc = 0;
+	ret = pass;
 
 	tty.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
 	tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP|ISIG);
-	tc = (tcsetattr(fd, TCSAFLUSH, &tty) == 0);
+
+	if ((con->flags & CON_NOTTY) == 0)
+		tc = (tcsetattr(fd, TCSAFLUSH, &tty) == 0);
 
 	sigemptyset(&sa.sa_mask);
 	sa.sa_handler = alrm_handler;
@@ -647,11 +708,12 @@ static const char *getpasswd(struct console *con)
 			}
 			ret = NULL;
 			switch (errno) {
-			case 0:
 			case EIO:
+				con->flags |= CON_EIO;
 			case ESRCH:
 			case EINVAL:
 			case ENOENT:
+			case 0:
 				break;
 			default:
 				warn(_("cannot read %s"), con->tty);
@@ -969,10 +1031,13 @@ int main(int argc, char **argv)
 		con = list_entry(ptr, struct console, entry);
 		if (con->id >= CONMAX)
 			break;
+		if (con->flags & CON_EIO)
+			goto next;
 
 		switch ((con->pid = fork())) {
 		case 0:
 			mask_signal(SIGCHLD, SIG_DFL, NULL);
+			dup2(con->fd, STDERR_FILENO);
 		nofork:
 			setup(con);
 			while (1) {
@@ -1027,7 +1092,7 @@ int main(int argc, char **argv)
 		default:
 			break;
 		}
-
+	next:
 		ptr = ptr->next;
 
 	} while (ptr != &consoles);
-- 
2.28.0


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 894 bytes --]

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] sulogin: ignore not existing console devices
  2021-05-17 15:25     ` Dr. Werner Fink
@ 2021-05-18 11:00       ` Karel Zak
  0 siblings, 0 replies; 6+ messages in thread
From: Karel Zak @ 2021-05-18 11:00 UTC (permalink / raw)
  To: util-linux

On Mon, May 17, 2021 at 05:25:28PM +0200, Dr. Werner Fink wrote:
> The attached patch is for latest git repository

Thanks, applied to the "next" branch and it will be merged to the
"master" after v2.37 release.

>   "Having a smoking section in a restaurant is like having
>           a peeing section in a swimming pool." -- Edward Burr

 +1 ;-)

    Karel




-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-05-18 11:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-06 10:54 [PATCH] sulogin: ignore not existing console devices Werner Fink
2021-05-06 10:58 ` John Paul Adrian Glaubitz
2021-05-10 14:51 ` Karel Zak
2021-05-17 14:58   ` Dr. Werner Fink
2021-05-17 15:25     ` Dr. Werner Fink
2021-05-18 11:00       ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).