Skip to contentSkip to conversation

sulogin: use the linked lists from list.h for consoles list

PATCH Applied Werner Fink 3 participants 20 messages Dec 7, 2012 – Dec 23, 2012 Util-Linux package

lore
Share & tools
lore
#1 lore raw

PATCH 2/5 sulogin: make usleep() workaround work

In reply to Werner Fink

Simply fix a compile problem found during debugging console.c

---
 include/c.h |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
c.h +7 -3
index 1107287..ec1020e 100644
@@ -19,6 +19,10 @@
 # include <err.h>
 #endif
 
+#ifndef HAVE_USLEEP
+# include <time.h>
+#endif
+
 /*
  * Compiler specific stuff
  */
@@ -246,13 +250,13 @@ static inline size_t get_hostname_max(void)
  * This function is marked obsolete in POSIX.1-2001 and removed in
  * POSIX.1-2008. It is replaced with nanosleep().
  */
-static inline usleep(useconds_t usec)
+static inline int usleep(useconds_t usec)
 {
 	struct timespec waittime = {
 		.tv_sec   =  usec / 1000000L,
 		.tv_nsec  = (usec % 1000000L) * 1000
-	}
-	nanosleep(&waittime, NULL);
+	};
+	return nanosleep(&waittime, NULL);
 }
 #endif
 
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 3/5 sulogin: use alarm function to indicate if a timeout occurs

In reply to Werner Fink

A small change to use the alarm handler to indicate if a timeout occurs.

---
 login-utils/sulogin.c |   20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
sulogin.c +17 -3
index db783a2..dbcc855 100644
@@ -8,6 +8,7 @@
  *
  * Copyright (C) 1998-2003 Miquel van Smoorenburg.
  * Copyright (C) 2012 Karel Zak <[email protected]>
+ * Copyright (C) 2012 Werner Fink <[email protected]>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -60,12 +61,15 @@ struct sigaction saved_sigint;
 struct sigaction saved_sigtstp;
 struct sigaction saved_sigquit;
 
+static volatile sig_atomic_t alarm_rised;
+
 /*
  * Called at timeout.
  */
 static void alrm_handler(int sig __attribute__((unused)))
 {
-	return;
+	/* Timeout expired */
+	alarm_rised++;
 }
 
 static void mask_signal(int signal, void (*handler)(int),
@@ -594,17 +598,27 @@ int main(int argc, char **argv)
 	 * Ask for the password.
 	 */
 	while (pwd) {
+		int failed = 0;
 		if ((p = getpasswd(pwd->pw_passwd)) == NULL)
 			break;
 		if (pwd->pw_passwd[0] == 0 ||
-		    strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd) == 0)
+		    strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd) == 0) {
 			sushell(pwd);
+			failed++;
+		}
 		mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
 		mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
 		mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
-		fprintf(stderr, _("Login incorrect\n\n"));
+		if (failed) {
+		    fprintf(stderr, _("Can not execute su shell\n\n"));
+		    break;
+		} else
+		    fprintf(stderr, _("Login incorrect\n\n"));
 	}
 
+	if (alarm_rised)
+		fprintf(stderr, _("Timed out\n\n"));
+
 	/*
 	 * User pressed Control-D.
 	 */
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 4/5 sulogin: mount temporary /dev and /proc if not found

In reply to Werner Fink

This is very usefull if initrd can not loaded that is no /dev and no /proc is found. Also if the /etc/shadow and /etc/passwd is copied into the initrd the sulogin can be used in initrd even before /dev and/or /proc are mounted.

---
 configure.ac          |   10 ++++++++
 login-utils/sulogin.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
configure.ac +10 -0
index 3be3715..631002f 100644
@@ -1296,6 +1296,16 @@ if test "x$enable_use_tty_group" = xyes; then
   AC_DEFINE(USE_TTY_GROUP, 1, [Should wall and write be installed setgid tty?])
 fi
 
+AC_ARG_ENABLE([use-emergency-dev],
+  AS_HELP_STRING([--disable-use-emergency-dev], [do not use emergency mount of /dev and /proc for sulogin]),
+  [], enable_use_emergency_dev=yes
+)
+AM_CONDITIONAL(USE_EMERGENCY_DEV, test "x$enable_use_emergency_dev" = xyes)
+
+if test "x$enable_use_emergency_dev" = xyes; then
+  AC_DEFINE(USE_EMERGENCY_DEV, 1, [Should sulogin use a emergency mount of /dev and /proc?])
+fi
+
 AC_ARG_ENABLE([makeinstall-chown],
   AS_HELP_STRING([--disable-makeinstall-chown], [do not do chown-like operations during "make install"]),
   [], enable_makeinstall_chown=yes
sulogin.c +57 -0

69 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#0 lore raw

PATCH 1/5 sulogin: use the linked lists from list.h for consoles list

with this the double linked list feature from util-linux is used instead of the single linked list.

---
 include/consoles.h |    6 ++--
 lib/consoles.c     |   77 ++++++++++++++++++++++++++++++++--------------------
 2 files changed, 51 insertions(+), 32 deletions(-)
consoles.h +4 -2
index 2283d02..2544263 100644
@@ -2,6 +2,7 @@
  * consoles.h	    Header file for routines to detect the system consoles
  *
  * Copyright (c) 2011 SuSE LINUX Products GmbH, All rights reserved.
+ * Copyright (c) 2012 Werner Fink <[email protected]>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -25,6 +26,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <termios.h>
+#include <list.h>
 
 struct chardata {
 	uint8_t	erase;
@@ -33,6 +35,7 @@ struct chardata {
 	uint8_t parity;
 };
 struct console {
+	struct list_head entry;
 	char *tty;
 	FILE *file;
 	uint32_t flags;
@@ -42,8 +45,7 @@ struct console {
 	pid_t pid;
 	struct chardata cp;
 	struct termios tio;
-	struct console *next;
 };
 
 extern int detect_consoles(const char *device, int fallback,
-			   struct console **consoles);
+			   struct list_head *consoles);
consoles.c +47 -30

225 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#17 lore raw

PATCH 5/5 sulogin: add multi console feature from SysVinit sulogin

Now after adding Conflicts=rescue.service to [email protected] and [email protected] and Conflicts=getty.target to rescue.target all works with systemctl rescue'. Even adding init=/sbin/sulogin to the kernels command line by using the e' key in grub2 boot menu works flawless.

---
 login-utils/sulogin.c |  665 ++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 516 insertions(+), 149 deletions(-)
sulogin.c +516 -149
index 0b0d21c..c4e44a9 100644
@@ -24,8 +24,10 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -67,15 +69,188 @@
 #include "pathnames.h"
 #include "strutils.h"
 #include "ttyutils.h"
+#include "consoles.h"
+#define CONMAX		16
+
+#define BS		CTRL('h')
+#define NL		CTRL('j')
+#define CR		CTRL('m')
 
 static unsigned int timeout;
 static int profile;
+static volatile uint32_t openfd;		/* Remember higher file descriptors */
+static volatile uint32_t *usemask;
 
 struct sigaction saved_sigint;
 struct sigaction saved_sigtstp;
 struct sigaction saved_sigquit;
+struct sigaction saved_sighup;
+struct sigaction saved_sigchld;
 
 static volatile sig_atomic_t alarm_rised;
+static volatile sig_atomic_t sigchild;
+
+#ifndef IUCLC
+# define IUCLC		0
+#endif
+
+/*
+ * Fix the tty modes and set reasonable defaults.
+ */
+static void tcinit(struct console *con)
+{
+	int mode = 0, flags = 0;
+	struct termios *tio = &con->tio;
+	int fd = con->fd;
+
+	errno = 0;
+
+	if (tcgetattr(fd, tio) < 0) {
+		warn(_("tcgetattr failed"));
+		con->flags |= CON_NOTTY;
+		return;
+	}
+
+	/* Handle serial lines here */
+	if (ioctl(fd, TIOCMGET, (char *) &mode) == 0) {
+		speed_t ispeed, ospeed;
+		struct winsize ws;
+
+		/* this is a modem line */
+		con->flags |= CON_SERIAL;
+
+		/* Flush input and output queues on modem lines */
+		(void) tcflush(fd, TCIOFLUSH);
+
+		ispeed = cfgetispeed(tio);
+		ospeed = cfgetospeed(tio);
+
+		if (!ispeed) ispeed = TTYDEF_SPEED;
+		if (!ospeed) ospeed = TTYDEF_SPEED;
+
+		tio->c_iflag = tio->c_lflag = tio->c_oflag = 0;
+		tio->c_cflag = CREAD | CS8 | HUPCL | (tio->c_cflag & CLOCAL);
+
+		cfsetispeed(tio, ispeed);
+		cfsetospeed(tio, ospeed);
+
+		tio->c_line         = 0;
+		tio->c_cc[VTIME]    = 0;
+		tio->c_cc[VMIN]     = 1;
+
+		if (ioctl(fd, TIOCGWINSZ, &ws) == 0) {
+			int set = 0;
+			if (ws.ws_row == 0) {
+				ws.ws_row = 24;
+				set++;
+			}
+			if (ws.ws_col == 0) {
+				ws.ws_col = 80;
+				set++;
+			}
+			if (set)
+				(void)ioctl(fd, TIOCSWINSZ, &ws);
+		}
+
+		setlocale(LC_CTYPE, "POSIX");
+		goto setattr;
+	}
+#if defined(IUTF8) && defined(KDGKBMODE)
+	/* Detect mode of current keyboard setup, e.g. for UTF-8 */
+	if (ioctl(fd, KDGKBMODE, &mode) < 0)
+		mode = K_RAW;
+	switch(mode) {
+	case K_UNICODE:
+		setlocale(LC_CTYPE, "C.UTF-8");
+		flags |= UL_TTY_UTF8;
+		break;
+	case K_RAW:
+	case K_MEDIUMRAW:
+	case K_XLATE:
+	default:
+		setlocale(LC_CTYPE, "POSIX");
+		break;
+	}
+#else
+	setlocale(LC_CTYPE, "POSIX");
+#endif
+	reset_virtual_console(tio, flags);
+setattr:
+	if (tcsetattr(fd, TCSANOW, tio))
+		warn(_("tcsetattr failed"));
+
+	/* Enable blocking mode for read and write */
+	if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
+		(void)fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
+}
+
+/*
+ * Finalize the tty modes on modem lines.
+ */
+static void tcfinal(struct console *con)
+{
+	struct termios *tio;
+	int fd;
+
+	if ((con->flags & CON_SERIAL) == 0) {
+		setenv("TERM", "linux", 1);
+		return;
+	}
+	if (con->flags & CON_NOTTY)
+		return;
+
+	setenv("TERM", "vt102", 1);
+	tio = &con->tio;
+	fd = con->fd;
+
+	tio->c_iflag |= (IXON | IXOFF);
+	tio->c_lflag |= (ICANON | ISIG | ECHO|ECHOE|ECHOK|ECHOKE);
+	tio->c_oflag |= OPOST;
+
+	tio->c_cc[VINTR]    = CINTR;
+	tio->c_cc[VQUIT]    = CQUIT;
+	tio->c_cc[VERASE]   = con->cp.erase;
+	tio->c_cc[VKILL]    = con->cp.kill;
+	tio->c_cc[VEOF]     = CEOF;
+#ifdef VSWTC
+	tio->c_cc[VSWTC]    = _POSIX_VDISABLE;
+#else
+	tio->c_cc[VSWTCH]   = _POSIX_VDISABLE;
+#endif
+	tio->c_cc[VSTART]   = CSTART;
+	tio->c_cc[VSTOP]    = CSTOP;
+	tio->c_cc[VSUSP]    = CSUSP;
+	tio->c_cc[VEOL]     = _POSIX_VDISABLE;
+
+	if (con->cp.eol == CR) {
+		tio->c_iflag |= ICRNL;
+		tio->c_iflag &= ~(INLCR|IGNCR);
+		tio->c_oflag |= ONLCR;
+		tio->c_oflag &= ~(OCRNL|ONLRET);
+	}
+
+	switch (con->cp.parity) {
+	default:
+	case 0:
+		tio->c_cflag &= ~(PARODD | PARENB);
+		tio->c_iflag &= ~(INPCK | ISTRIP);
+		break;
+	case 1:				/* odd parity */
+		tio->c_cflag |= PARODD;
+		/* fall through */
+	case 2:				/* even parity */
+		tio->c_cflag |= PARENB;
+		tio->c_iflag |= (INPCK | ISTRIP);
+		/* fall through */
+	case (1 | 2):			/* no parity bit */
+		tio->c_cflag &= ~CSIZE;
+		tio->c_cflag |= CS7;
+		break;
+	}
+
+	/* Set line attributes */
+	(void)tcsetattr(fd, TCSANOW, tio);
+}
 
 /*
  * Called at timeout.
@@ -86,6 +261,11 @@ static void alrm_handler(int sig __attribute__((unused)))
 	alarm_rised++;
 }
 
+static void chld_handler(int sig __attribute__((unused)))
+{
+	sigchild++;
+}
+
 static void mask_signal(int signal, void (*handler)(int),
 		struct sigaction *origaction)
 {
@@ -95,8 +275,7 @@ static void mask_signal(int signal, void (*handler)(int),
 	sigemptyset(&newaction.sa_mask);
 	newaction.sa_flags = 0;
 
-	sigaction(signal, NULL, origaction);
-	sigaction(signal, &newaction, NULL);
+	sigaction(signal, &newaction, origaction);
 }
 
 static void unmask_signal(int signal, struct sigaction *sa)
@@ -296,51 +475,202 @@ static struct passwd *getrootpwent(int try_manually)
 }
 
 /*
+ * Ask by prompt for the password.
+ */
+static void doprompt(const char *crypted, struct console *con)
+{
+	struct termios tty;
+
+	if (con->flags & CON_SERIAL) {
+		tty = con->tio;
+		/*
+		 * For prompting: map NL in output to CR-NL
+		 * otherwise we may see stairs in the output.
+		 */
+		tty.c_oflag |= (ONLCR | OPOST);
+		(void) tcsetattr(con->fd, TCSADRAIN, &tty);
+	}
+	if (con->file == (FILE*)0) {
+		if  ((con->file = fdopen(con->fd, "r+")) == (FILE*)0)
+			goto err;
+	}
+#if defined(USE_ONELINE)
+	if (crypted[0])
+		fprintf(con->file, _("Give root password for login: "));
+	else
+		fprintf(con->file, _("Press enter for login: "));
+#else
+	if (crypted[0])
+		fprintf(con->file, _("Give root password for maintenance\n"));
+	else
+		fprintf(con->file, _("Press enter for maintenance"));
+	fprintf(con->file, _("(or type Control-D to continue): "));
+#endif
+	fflush(con->file);
+err:
+	if (con->flags & CON_SERIAL)
+		(void) tcsetattr(con->fd, TCSADRAIN, &con->tio);
+}
+
+/*
+ * Make sure to have an own session and controlling terminal
+ */
+static void setup(struct console *con)
+{
+	pid_t pid, pgrp, ppgrp, ttypgrp;
+	int fd;
+
+	if (con->flags & CON_NOTTY)
+		return;
+	fd = con->fd;
+
+	/*
+	 * Only go through this trouble if the new
+	 * tty doesn't fall in this process group.
+	 */
+	pid = getpid();
+	pgrp = getpgid(0);
+	ppgrp = getpgid(getppid());
+	ttypgrp = tcgetpgrp(fd);
+
+	if (pgrp != ttypgrp && ppgrp != ttypgrp) {
+		if (pid != getsid(0)) {
+			if (pid == getpgid(0))
+				setpgid(0, getpgid(getppid()));
+			setsid();
+		}
+
+		mask_signal(SIGHUP, SIG_IGN, &saved_sighup);
+		if (ttypgrp > 0)
+			ioctl(STDIN_FILENO, TIOCNOTTY, (char *)1);
+		unmask_signal(SIGHUP, &saved_sighup);
+		if (fd > STDIN_FILENO)  close(STDIN_FILENO);
+		if (fd > STDOUT_FILENO) close(STDOUT_FILENO);
+		if (fd > STDERR_FILENO) close(STDERR_FILENO);
+
+		ioctl(fd, TIOCSCTTY, (char *)1);
+		tcsetpgrp(fd, ppgrp);
+	}
+	dup2(fd, STDIN_FILENO);
+	dup2(fd, STDOUT_FILENO);
+	dup2(fd, STDERR_FILENO);
+	con->fd = STDIN_FILENO;
+
+	for (fd = STDERR_FILENO+1; fd < 32; fd++) {
+		if (openfd & (1<<fd)) {
+			close(fd);
+			openfd &= ~(1<<fd);
+		}
+	}
+}
+
+/*
  * Ask for the password. Note that there is no default timeout as we normally
  * skip this during boot.
  */
-static char *getpasswd(char *crypted)
+static char *getpasswd(struct console *con)
 {
 	struct sigaction sa;
-	struct termios old, tty;
-	static char pass[128];
+	struct termios tty;
+	static char pass[128], *ptr;
+	struct chardata *cp;
 	char *ret = pass;
-	size_t i;
+	unsigned char tc;
+	char c, ascval;
+	int eightbit;
+	int fd;
 
-	if (crypted[0])
-		printf(_("Give root password for maintenance\n"));
-	else
-		printf(_("Press enter for maintenance"));
-	printf(_("(or type Control-D to continue): "));
-	fflush(stdout);
+	if (con->flags & CON_NOTTY)
+		goto out;
+	fd = con->fd;
+	cp = &con->cp;
+	tty = con->tio;
 
-	tcgetattr(0, &old);
-	tcgetattr(0, &tty);
 	tty.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
-	tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
-	tcsetattr(0, TCSANOW, &tty);
-
-	pass[sizeof(pass) - 1] = 0;
+	tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP|ISIG);
+	tc = (tcsetattr(fd, TCSAFLUSH, &tty) == 0);
 
 	sa.sa_handler = alrm_handler;
 	sa.sa_flags = 0;
 	sigaction(SIGALRM, &sa, NULL);
-	if (timeout)
-		alarm(timeout);
+	if (timeout) alarm(timeout);
 
-	if (read(0, pass, sizeof(pass) - 1) <= 0)
-		ret = NULL;
-	else {
-		for (i = 0; i < sizeof(pass) && pass[i]; i++)
-			if (pass[i] == '\r' || pass[i] == '\n') {
-				pass[i] = 0;
+	ptr = &pass[0];
+	cp->eol = *ptr = '\0';
+
+	eightbit = ((con->flags & CON_SERIAL) == 0 || (tty.c_cflag & (PARODD|PARENB)) == 0);
+	while (cp->eol == '\0') {
+		if (read(fd, &c, 1) < 1) {
+			if (errno == EINTR || errno == EAGAIN) {
+				usleep(1000);
+				continue;
+			}
+			ret = (char*)0;
+			switch (errno) {
+			case 0:
+			case EIO:
+			case ESRCH:
+			case EINVAL:
+			case ENOENT:
 				break;
+			default:
+				fprintf(stderr, "sulogin: read(%s): %m\n\r", con->tty);
+				break;
+			}
+			goto quit;
+		}
+
+		if (eightbit)
+			ascval = c;
+		else if (c != (ascval = (c & 0177))) {
+			uint32_t bits, mask;
+			for (bits = 1, mask = 1; mask & 0177; mask <<= 1) {
+				if (mask & ascval)
+					bits++;
+			}
+			cp->parity |= ((bits & 1) ? 1 : 2);
+		}
+
+		switch (ascval) {
+		case 0:
+			*ptr = '\0';
+			goto quit; 
+		case CR:
+		case NL:
+			*ptr = '\0';
+			cp->eol = ascval;
+			break;
+		case BS:
+		case CERASE:
+			cp->erase = ascval;
+			if (ptr > &pass[0])
+				ptr--;
+			break;
+		case CKILL:
+			cp->kill = ascval;
+			while (ptr > &pass[0])
+				ptr--;
+			break;
+		case CEOF:
+			goto quit;		
+		default:
+			if ((size_t)(ptr - &pass[0]) >= (sizeof(pass) -1 )) {
+				 fprintf(stderr, "sulogin: input overrun at %s\n\r", con->tty);
+				 ret = (char*)0;
+				 goto quit;
 			}
+			*ptr++ = ascval;
+			break;
+		}
 	}
+quit:
 	alarm(0);
-	tcsetattr(0, TCSANOW, &old);
-	printf("\n");
-
+	if (tc)
+		(void)tcsetattr(fd, TCSAFLUSH, &con->tio);
+	if (ret && *ret != '\0')
+		tcfinal(con);
+	printf("\r\n");
+out:
 	return ret;
 }
 
@@ -385,9 +715,10 @@ static void sushell(struct passwd *pwd)
 	/*
 	 * Set some important environment variables.
 	 */
-	if (getcwd(home, sizeof(home)) != NULL)
-		setenv("HOME", home, 1);
+	if (getcwd(home, sizeof(home)) == NULL)
+		strcpy(home, "/");
 
+	setenv("HOME", home, 1);
 	setenv("LOGNAME", "root", 1);
 	setenv("USER", "root", 1);
 	if (!profile)
@@ -400,6 +731,7 @@ static void sushell(struct passwd *pwd)
 	unmask_signal(SIGINT, &saved_sigint);
 	unmask_signal(SIGTSTP, &saved_sigtstp);
 	unmask_signal(SIGQUIT, &saved_sigquit);
+	mask_signal(SIGHUP, SIG_DFL, NULL);
 
 #ifdef HAVE_LIBSELINUX
 	if (is_selinux_enabled() > 0) {
@@ -425,36 +757,6 @@ static void sushell(struct passwd *pwd)
 	warn(_("%s: exec failed"), "/bin/sh");
 }
 
-static void fixtty(void)
-{
-	struct termios tp;
-	int x = 0, fl = 0;
-
-	/* Skip serial console */
-	if (ioctl(STDIN_FILENO, TIOCMGET, (char *) &x) == 0)
-		return;
-
-#if defined(IUTF8) && defined(KDGKBMODE)
-	/* Detect mode of current keyboard setup, e.g. for UTF-8 */
-	if (ioctl(STDIN_FILENO, KDGKBMODE, &x) == 0 && x == K_UNICODE) {
-		setlocale(LC_CTYPE, "C.UTF-8");
-		fl |= UL_TTY_UTF8;
-	}
-#else
-	setlocale(LC_CTYPE, "POSIX");
-#endif
-	memset(&tp, 0, sizeof(struct termios));
-	if (tcgetattr(STDIN_FILENO, &tp) < 0) {
-		warn(_("tcgetattr failed"));
-		return;
-	}
-
-	reset_virtual_console(&tp, fl);
-
-	if (tcsetattr(STDIN_FILENO, TCSADRAIN, &tp))
-		warn(_("tcsetattr failed"));
-}
-
 #ifdef USE_EMERGENCY_DEV
 /*
  * Make C library standard calls such like ttyname(3) work
@@ -518,13 +820,14 @@ static void usage(FILE *out)
 
 int main(int argc, char **argv)
 {
+	struct list_head consoles = {&consoles, &consoles}, *ptr;
+	struct console *con;
 	char *tty = NULL;
-	char *p;
 	struct passwd *pwd;
-	int c, fd = -1;
+	int c, status = 0;
+	int reconnect = 0;
 	int opt_e = 0;
-	pid_t pid, pgrp, ppgrp, ttypgrp;
-	struct sigaction saved_sighup;
+	pid_t pid;
 
 	static const struct option longopts[] = {
 		{ "login-shell",  0, 0, 'p' },
@@ -535,10 +838,18 @@ int main(int argc, char **argv)
 		{ NULL,           0, 0, 0 }
 	};
 
+	/*
+	 * If we are init we need to set up a own session.
+	 */
+	if ((pid = getpid()) == 1) {
+		setsid();
+		(void)ioctl(STDIN_FILENO, TIOCSCTTY, (char *)1);
+	}
+
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
-	atexit(close_stdout);
+	atexit(close_stdout); /* XXX */
 
 	/*
 	 * See if we have a timeout flag.
@@ -570,78 +881,45 @@ int main(int argc, char **argv)
 	if (geteuid() != 0)
 		errx(EXIT_FAILURE, _("only root can run this program."));
 
-	/*
-	 * See if we need to open an other tty device.
-	 */
 	mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
 	mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
 	mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
+	mask_signal(SIGHUP,  SIG_IGN, &saved_sighup);
+
+	/*
+	 * See if we need to open an other tty device.
+	 */
 	if (optind < argc)
 		tty = argv[optind];
 
-	if (tty || (tty = getenv("CONSOLE"))) {
-
-		if ((fd = open(tty, O_RDWR)) < 0) {
-			warn(_("cannot open %s"), tty);
-			fd = dup(STDIN_FILENO);
-		}
-
-		if (fd < 0) {
-			warn(_("cannot duplicate stdin file descriptor"));
-		} else if (!isatty(fd)) {
-			warn(_("%s: not a tty"), tty);
-			close(fd);
-		} else {
+	if (!tty || *tty == '\0')
+		tty = getenv("CONSOLE");
 
-			/*
-			 * Only go through this trouble if the new tty doesn't
-			 * fall in this process group.
-			 */
-			pid = getpid();
-			pgrp = getpgid(0);
-			ppgrp = getpgid(getppid());
-			ttypgrp = tcgetpgrp(fd);
-
-			if (pgrp != ttypgrp && ppgrp != ttypgrp) {
-				if (pid != getsid(0)) {
-					if (pid == getpgid(0))
-						setpgid(0, getpgid(getppid()));
-					setsid();
-				}
+	/*
+	 * Detect possible consoles, use stdin as fallback.
+	 * If an optional tty is given, reconnect it to stdin.
+	 */
+	reconnect = detect_consoles(tty, STDIN_FILENO, &consoles);
 
-				sigaction(SIGHUP, NULL, &saved_sighup);
-				if (ttypgrp > 0)
-					ioctl(0, TIOCNOTTY, (char *)1);
-				sigaction(SIGHUP, &saved_sighup, NULL);
-				close(STDIN_FILENO);
-				close(STDOUT_FILENO);
-				close(STDERR_FILENO);
-				if (fd > 2)
-					close(fd);
-				if ((fd = open(tty, O_RDWR|O_NOCTTY)) < 0)
-					warn(_("cannot open %s"), tty);
-				else {
-					ioctl(STDIN_FILENO, TIOCSCTTY, (char *)1);
-					tcsetpgrp(fd, ppgrp);
-					dup2(fd, STDIN_FILENO);
-					dup2(fd, STDOUT_FILENO);
-					dup2(fd, STDERR_FILENO);
-
-					if (fd > STDERR_FILENO)
-						close(fd);
-				}
-			} else
-				if (fd > STDERR_FILENO)
-					close(fd);
-		}
-	} else if (getpid() == 1) {
-		/* We are init. We hence need to set a session anyway */
-		setsid();
-		if (ioctl(STDIN_FILENO, TIOCSCTTY, (char *)1))
-			warn(_("TIOCSCTTY: ioctl failed"));
+	/*
+	 * If previous stdin was not the speified tty and therefore reconnected
+	 * to the specified tty also reconnect stdout and stderr.
+	 */
+	if (reconnect) {
+		if (isatty(STDOUT_FILENO) == 0)
+			dup2(STDOUT_FILENO, STDIN_FILENO);
+		if (isatty(STDERR_FILENO) == 0)
+			dup2(STDOUT_FILENO, STDERR_FILENO);
 	}
 
-	fixtty();
+	/*
+	 * Should not happen
+	 */
+	if (list_empty(&consoles)) {
+		if (!errno)
+			errno = ENOENT;
+		errx(EXIT_FAILURE, _("cannot open console: %m\n"));
+	}
 
 	/*
 	 * Get the root password.
@@ -652,32 +930,121 @@ int main(int argc, char **argv)
 	}
 
 	/*
-	 * Ask for the password.
+	 * Ask for the password on the consoles.
 	 */
-	while (pwd) {
-		int failed = 0;
-		if ((p = getpasswd(pwd->pw_passwd)) == NULL)
+	list_for_each(ptr, &consoles) {
+		con = list_entry(ptr, struct console, entry);
+		if (con->id >= CONMAX)
 			break;
-		if (pwd->pw_passwd[0] == 0 ||
-		    strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd) == 0) {
-			sushell(pwd);
-			failed++;
+		if (con->fd >= 0) {
+			openfd |= (1<<con->fd);
+			tcinit(con);
+			continue;
 		}
-		mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
-		mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
-		mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
-		if (failed) {
-		    fprintf(stderr, _("Can not execute su shell\n\n"));
-		    break;
-		} else
-		    fprintf(stderr, _("Login incorrect\n\n"));
+		if ((con->fd = open(con->tty, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
+			continue;
+		openfd |= (1<<con->fd);
+		tcinit(con);
 	}
+	ptr = (&consoles)->next;
+	usemask = (uint32_t*)mmap(NULL, sizeof(uint32_t), PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
 
-	if (alarm_rised)
-		fprintf(stderr, _("Timed out\n\n"));
+	if (ptr->next == &consoles) {
+		con = list_entry(ptr, struct console, entry);
+		goto nofork;
+	}
 
-	/*
-	 * User pressed Control-D.
-	 */
+	mask_signal(SIGCHLD, chld_handler, &saved_sigchld);
+	do {
+		con = list_entry(ptr, struct console, entry);
+		if (con->id >= CONMAX)
+			break;
+
+		switch ((con->pid = fork())) {
+		case 0:
+			mask_signal(SIGCHLD, SIG_DFL, NULL);
+			/* fall through */
+		nofork:
+			setup(con);
+			while (1) {
+				const char *passwd = pwd->pw_passwd;
+				const char *answer;
+				int failed = 0, doshell = 0;
+
+				doprompt(passwd, con);
+				if ((answer = getpasswd(con)) == NULL)
+					break;
+
+				if (passwd[0] == '\0')
+					doshell++;
+				else {
+					const char *cryptbuf;
+					cryptbuf = crypt(answer, passwd);
+					if (cryptbuf == NULL)
+						warnx(_("crypt failed: %m\n"));
+					else if (strcmp(cryptbuf, pwd->pw_passwd) == 0)
+						doshell++;
+				}
+
+				if (doshell) {
+					*usemask |= (1<<con->id);
+					sushell(pwd);
+					*usemask &= ~(1<<con->id);
+					failed++;
+				}
+
+				mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
+				mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
+				mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
+
+				if (failed) {
+					fprintf(stderr, _("Can not execute su shell\n\n"));
+					break;
+				}
+				fprintf(stderr, _("Login incorrect\n\n"));
+			}
+			if (alarm_rised) {
+				tcfinal(con);
+				warnx(_("Timed out\n\n"));
+			}
+			/*
+			 * User pressed Control-D.
+			 */
+			exit(0);
+		case -1:
+			warnx(_("Can not fork: %m\n"));
+			/* fall through */
+		default:
+			break;
+		}
+
+		ptr = ptr->next;
+
+	} while (ptr != &consoles);
+
+	while ((pid = wait(&status))) {
+		if (errno == ECHILD)
+			break;
+		if (pid < 0)
+			continue;
+		list_for_each(ptr, &consoles) {
+			con = list_entry(ptr, struct console, entry);
+			if (con->pid == pid) {
+				*usemask &= ~(1<<con->id);
+				continue;
+			}
+			if (kill(con->pid, 0) < 0) {
+				*usemask &= ~(1<<con->id);
+				continue;
+			}
+			if (*usemask & (1<<con->id))
+				continue;
+			kill(con->pid, SIGHUP);
+			usleep(5000);
+			kill(con->pid, SIGKILL);
+		}
+	}
+
+	mask_signal(SIGCHLD, SIG_DFL, NULL);
 	return EXIT_SUCCESS;
 }
Signature · 1 line
1.7.10.4
#18 lore raw

Re: [PATCH 5/5] sulogin: add multi console feature from SysVinit sulogin

Quoted 7 lines · Werner Fink

Now after adding Conflicts=rescue.service to [email protected] and [email protected] and Conflicts=getty.target to rescue.target all works with systemctl rescue'. Even adding init=/sbin/sulogin to the kernels command line by using the e' key in grub2 boot menu works flawless.

Hi, just a picture from the virtual system, the terminal above is the serial line within an xterm running socat and screen, the window below is the VirtualBox its self. I've done a login first on the serial line as this is had shown the grub2 menue. After this a login on tty1 haf followed.

Werner

Signature · 2 lines
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr
#6 lore raw

Re: [PATCH 4/5] sulogin: mount temporary /dev and /proc if not found

In reply to Werner Fink
Quoted 75 lines · Werner Fink

This is very usefull if initrd can not loaded that is no /dev and no /proc is found. Also if the /etc/shadow and /etc/passwd is copied into the initrd the sulogin can be used in initrd even before /dev and/or /proc are mounted.

---
 configure.ac          |   10 ++++++++
 login-utils/sulogin.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
configure.ac +10 -0
index 3be3715..631002f 100644
@@ -1296,6 +1296,16 @@ if test "x$enable_use_tty_group" = xyes; then
   AC_DEFINE(USE_TTY_GROUP, 1, [Should wall and write be installed setgid tty?])
 fi
 
+AC_ARG_ENABLE([use-emergency-dev],
+  AS_HELP_STRING([--disable-use-emergency-dev], [do not use emergency mount of /dev and /proc for sulogin]),
+  [], enable_use_emergency_dev=yes
+)
+AM_CONDITIONAL(USE_EMERGENCY_DEV, test "x$enable_use_emergency_dev" = xyes)
+
+if test "x$enable_use_emergency_dev" = xyes; then
+  AC_DEFINE(USE_EMERGENCY_DEV, 1, [Should sulogin use a emergency mount of /dev and /proc?])
+fi
+
 AC_ARG_ENABLE([makeinstall-chown],
   AS_HELP_STRING([--disable-makeinstall-chown], [do not do chown-like operations during "make install"]),
   [], enable_makeinstall_chown=yes
sulogin.c +28 -0
index dbcc855..0b0d21c 100644
@@ -47,6 +47,20 @@
 # include <selinux/get_context_list.h>
 #endif
 
+#ifdef USE_EMERGENCY_DEV
+# include <sys/statfs.h>
+# include <sys/mount.h>
+# include <linux/fs.h>
+# include <linux/magic.h>
+# include <linux/major.h>
+# ifndef TMPFS_MAGIC
+#  define TMPFS_MAGIC	0x01021994
+# endif
+# ifndef MNT_DETACH
+#  define MNT_DETACH	2
+# endif
+#endif
+
 #include "c.h"
 #include "closestream.h"
 #include "nls.h"
@@ -441,6 +455,49 @@ static void fixtty(void)
 		warn(_("tcsetattr failed"));
 }
 
+#ifdef USE_EMERGENCY_DEV
+/*
+ * Make C library standard calls such like ttyname(3) work
+ * even if the system does not show any of the standard
+ * directories.
+ */
+
+static uint32_t mounts;
+# define MNT_PROCFS	0x0001
+# define MNT_DEVTMPFS	0x0002
+
+static __attribute__((__noinline__)) void putmounts(void)
+{
+	if (mounts & MNT_DEVTMPFS)

sulogin.c static void fixtty(void) · line 481

Quoted 10 lines
sulogin.c +10 -0
+		umount2("/dev", MNT_DETACH);
+	if (mounts & MNT_PROCFS)
+		umount2("/proc", MNT_DETACH);
+}
+
+# define dovoid(f)	if ((f)){}
+static __attribute__((__constructor__)) void getmounts(void)
+{
+	struct statfs st;
+	if (statfs("/proc", &st) == 0 && st.f_type != PROC_SUPER_MAGIC) {

Why not use the standard mountpoint check -- compare the devno with / ?

sulogin.c static void fixtty(void) · line 487

Quoted 6 lines
sulogin.c +6 -0
+		if (mount("proc", "/proc", "proc", MS_RELATIME, NULL) == 0)
+			mounts |= MNT_PROCFS;
+	}
+	if (statfs("/dev", &st) == 0 && st.f_type != TMPFS_MAGIC) {
+		if (mount("devtmpfs", "/dev", "devtmpfs", MS_RELATIME, "mode=0755,nr_inodes=0") == 0) {
+			mounts |= MNT_DEVTMPFS;

Isn't all the work below here redundant? devtmpfs provides these things.

Quoted 23 lines
sulogin.c +13 -0
+			(void)mknod("/dev/console", S_IFCHR|S_IRUSR|S_IWUSR, makedev(TTYAUX_MAJOR, 1));
+			if (symlink("/proc/self/fd", "/dev/fd") == 0) {
+				dovoid(symlink("fd/0", "/dev/stdin"));
+				dovoid(symlink("fd/1", "/dev/stdout"));
+				dovoid(symlink("fd/2", "/dev/stderr"));
+			}
+		}
+	}
+	if (mounts) atexit(putmounts);
+}
+# undef dovoid
+#endif
+
 static void usage(FILE *out)
 {
 	fputs(USAGE_HEADER, out);
Signature · 6 lines
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#7 lore raw

Re: [util-linux] [PATCH 4/5] sulogin: mount temporary /dev and /proc if not found

Quoted 19 lines · Dave Reisner

sulogin.c static void fixtty(void) · line 481

Quoted 5 lines · Werner Fink
sulogin.c +5 -0
+# define dovoid(f)	if ((f)){}
+static __attribute__((__constructor__)) void getmounts(void)
+{
+	struct statfs st;
+	if (statfs("/proc", &st) == 0 && st.f_type != PROC_SUPER_MAGIC) {

Why not use the standard mountpoint check -- compare the devno with / ?

sulogin.c static void fixtty(void) · line 487

Quoted 6 lines
sulogin.c +6 -0
+		if (mount("proc", "/proc", "proc", MS_RELATIME, NULL) == 0)
+			mounts |= MNT_PROCFS;
+	}
+	if (statfs("/dev", &st) == 0 && st.f_type != TMPFS_MAGIC) {
+		if (mount("devtmpfs", "/dev", "devtmpfs", MS_RELATIME, "mode=0755,nr_inodes=0") == 0) {
+			mounts |= MNT_DEVTMPFS;

Isn't all the work below here redundant? devtmpfs provides these things.

Quoted 16 lines
Quoted 6 lines
sulogin.c +6 -0
+			(void)mknod("/dev/console", S_IFCHR|S_IRUSR|S_IWUSR, makedev(TTYAUX_MAJOR, 1));
+			if (symlink("/proc/self/fd", "/dev/fd") == 0) {
+				dovoid(symlink("fd/0", "/dev/stdin"));
+				dovoid(symlink("fd/1", "/dev/stdout"));
+				dovoid(symlink("fd/2", "/dev/stderr"));
+			}

sulogin.c static void fixtty(void) · line 503

Quoted 10 lines
sulogin.c +7 -0
+		}
+	}
+	if (mounts) atexit(putmounts);
+}
+# undef dovoid
+#endif
+
 static void usage(FILE *out)
 {
 	fputs(USAGE_HEADER, out);

We're talking about the very early boot stage, there is no udev nor anything else and the I've choosen this as this had worked very well in initrd where it had crashed before. I do not say that there is no other way to do this but this way it works and the links I've done had been missed.

If in initrd the sulogin will be called after mounting /proc and /dev and running udev rules or enabling the links in initrd then the code does only check for existence of /proc and /dev.

Werner

Signature · 2 lines
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr
#8 lore raw

Re: [util-linux] [PATCH 4/5] sulogin: mount temporary /dev and /proc if not found

Quoted 33 lines · Dr. Werner Fink
Quoted 32 lines · Dave Reisner

sulogin.c static void fixtty(void) · line 481

Quoted 5 lines · Werner Fink
sulogin.c +5 -0
+# define dovoid(f)	if ((f)){}
+static __attribute__((__constructor__)) void getmounts(void)
+{
+	struct statfs st;
+	if (statfs("/proc", &st) == 0 && st.f_type != PROC_SUPER_MAGIC) {

Why not use the standard mountpoint check -- compare the devno with / ?

sulogin.c static void fixtty(void) · line 487

Quoted 6 lines
sulogin.c +6 -0
+		if (mount("proc", "/proc", "proc", MS_RELATIME, NULL) == 0)
+			mounts |= MNT_PROCFS;
+	}
+	if (statfs("/dev", &st) == 0 && st.f_type != TMPFS_MAGIC) {
+		if (mount("devtmpfs", "/dev", "devtmpfs", MS_RELATIME, "mode=0755,nr_inodes=0") == 0) {
+			mounts |= MNT_DEVTMPFS;

Isn't all the work below here redundant? devtmpfs provides these things.

Quoted 13 lines
sulogin.c +13 -0
+			(void)mknod("/dev/console", S_IFCHR|S_IRUSR|S_IWUSR, makedev(TTYAUX_MAJOR, 1));
+			if (symlink("/proc/self/fd", "/dev/fd") == 0) {
+				dovoid(symlink("fd/0", "/dev/stdin"));
+				dovoid(symlink("fd/1", "/dev/stdout"));
+				dovoid(symlink("fd/2", "/dev/stderr"));
+			}
+		}
+	}
+	if (mounts) atexit(putmounts);
+}
+# undef dovoid
+#endif
+
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

static void usage(FILE *out)
{
fputs(USAGE_HEADER, out);

We're talking about the very early boot stage, there is no udev nor anything else and the I've choosen this as this had worked very well in initrd where it had crashed before. I do not say that there is no other way to do this but this way it works and the links I've done had been missed.

Ok, I didn't realize udev still was responsible for these links these days, but perusing the code shows otherwise. Could you explain why sulogin is wanted in the initramfs? Seems like an odd use case to me.

Quoted 3 lines

If in initrd the sulogin will be called after mounting /proc and /dev and running udev rules or enabling the links in initrd then the code does only check for existence of /proc and /dev.

If you're in the initramfs, rootfs is tmpfs. Won't your statfs check return a buffer which has f_type == TMPFS_MAGIC even if /dev is not mounted?

Quoted 9 lines

Werner

Signature · 6 lines
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#9 lore raw

Re: [util-linux] [PATCH 4/5] sulogin: mount temporary /dev and /proc if not found

Quoted 5 lines · Dave Reisner

Ok, I didn't realize udev still was responsible for these links these days, but perusing the code shows otherwise. Could you explain why sulogin is wanted in the initramfs? Seems like an odd use case to me.

Quoted 7 lines
Quoted 3 lines

If in initrd the sulogin will be called after mounting /proc and /dev and running udev rules or enabling the links in initrd then the code does only check for existence of /proc and /dev.

If you're in the initramfs, rootfs is tmpfs. Won't your statfs check return a buffer which has f_type == TMPFS_MAGIC even if /dev is not mounted?

Hmmm ... maybe a check for RAMFS_MAGIC should be done before to distinguish between TMPFS_MAGIC or RAMFS_MAGIC for the initrd. Or the mounted /proc could be used to check for /dev

Werner

Signature · 2 lines
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr
#10 lore raw

PATCH 4/5 sulogin: mount temporary /dev and /proc if not found

In reply to Dave Reisner
Quoted 4 lines · Dave Reisner

If you're in the initramfs, rootfs is tmpfs. Won't your statfs check return a buffer which has f_type == TMPFS_MAGIC even if /dev is not mounted?

Indeed it could be a poroblem, therefore I've moved the stuff to lib/conosle.c and switch over to device comparission as you suggested. Also if /dev has been mounted by this code I'll also create missing nodes during scan of e.g. /proc/consoles

Quoted 5 lines · Werner Fink

This is very usefull if initrd can not loaded that is no /dev and no /proc is found. Also if the /etc/shadow and /etc/passwd is copied into the initrd the sulogin can be used in initrd even before /dev and/or /proc are mounted.

---
 configure.ac   |   10 +++++++++
 lib/consoles.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
configure.ac +10 -0
index 3be3715..631002f 100644
@@ -1296,6 +1296,16 @@ if test "x$enable_use_tty_group" = xyes; then
   AC_DEFINE(USE_TTY_GROUP, 1, [Should wall and write be installed setgid tty?])
 fi
 
+AC_ARG_ENABLE([use-emergency-dev],
+  AS_HELP_STRING([--disable-use-emergency-dev], [do not use emergency mount of /dev and /proc for sulogin]),
+  [], enable_use_emergency_dev=yes
+)
+AM_CONDITIONAL(USE_EMERGENCY_DEV, test "x$enable_use_emergency_dev" = xyes)
+
+if test "x$enable_use_emergency_dev" = xyes; then
+  AC_DEFINE(USE_EMERGENCY_DEV, 1, [Should sulogin use a emergency mount of /dev and /proc?])
+fi
+
 AC_ARG_ENABLE([makeinstall-chown],
   AS_HELP_STRING([--disable-makeinstall-chown], [do not do chown-like operations during "make install"]),
   [], enable_makeinstall_chown=yes
consoles.c +66 -0

84 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#11 lore raw

PATCH 5/5 sulogin: add multi console feature from SysVinit sulogin

Now after adding Conflicts=rescue.service to [email protected] and [email protected] and Conflicts=getty.target to rescue.target all works with systemctl rescue'. Even adding init=/sbin/sulogin to the kernels command line by using the e' key in grub2 boot menu works flawless.

This version is without the emergency code as I've this moved to lib/console.c where it belongs to.

---
 login-utils/sulogin.c |  665 ++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 516 insertions(+), 149 deletions(-)
sulogin.c +516 -149
index 9fdbb69..b489535 100644
@@ -24,8 +24,10 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -53,15 +55,188 @@
 #include "pathnames.h"
 #include "strutils.h"
 #include "ttyutils.h"
+#include "consoles.h"
+#define CONMAX		16
+
+#define BS		CTRL('h')
+#define NL		CTRL('j')
+#define CR		CTRL('m')
 
 static unsigned int timeout;
 static int profile;
+static volatile uint32_t openfd;		/* Remember higher file descriptors */
+static volatile uint32_t *usemask;
 
 struct sigaction saved_sigint;
 struct sigaction saved_sigtstp;
 struct sigaction saved_sigquit;
+struct sigaction saved_sighup;
+struct sigaction saved_sigchld;
 
 static volatile sig_atomic_t alarm_rised;
+static volatile sig_atomic_t sigchild;
+
+#ifndef IUCLC
+# define IUCLC		0
+#endif
+
+/*
+ * Fix the tty modes and set reasonable defaults.
+ */
+static void tcinit(struct console *con)
+{
+	int mode = 0, flags = 0;
+	struct termios *tio = &con->tio;
+	int fd = con->fd;
+
+	errno = 0;
+
+	if (tcgetattr(fd, tio) < 0) {
+		warn(_("tcgetattr failed"));
+		con->flags |= CON_NOTTY;
+		return;
+	}
+
+	/* Handle serial lines here */
+	if (ioctl(fd, TIOCMGET, (char *) &mode) == 0) {
+		speed_t ispeed, ospeed;
+		struct winsize ws;
+
+		/* this is a modem line */
+		con->flags |= CON_SERIAL;
+
+		/* Flush input and output queues on modem lines */
+		(void) tcflush(fd, TCIOFLUSH);
+
+		ispeed = cfgetispeed(tio);
+		ospeed = cfgetospeed(tio);
+
+		if (!ispeed) ispeed = TTYDEF_SPEED;
+		if (!ospeed) ospeed = TTYDEF_SPEED;
+
+		tio->c_iflag = tio->c_lflag = tio->c_oflag = 0;
+		tio->c_cflag = CREAD | CS8 | HUPCL | (tio->c_cflag & CLOCAL);
+
+		cfsetispeed(tio, ispeed);
+		cfsetospeed(tio, ospeed);
+
+		tio->c_line         = 0;
+		tio->c_cc[VTIME]    = 0;
+		tio->c_cc[VMIN]     = 1;
+
+		if (ioctl(fd, TIOCGWINSZ, &ws) == 0) {
+			int set = 0;
+			if (ws.ws_row == 0) {
+				ws.ws_row = 24;
+				set++;
+			}
+			if (ws.ws_col == 0) {
+				ws.ws_col = 80;
+				set++;
+			}
+			if (set)
+				(void)ioctl(fd, TIOCSWINSZ, &ws);
+		}
+
+		setlocale(LC_CTYPE, "POSIX");
+		goto setattr;
+	}
+#if defined(IUTF8) && defined(KDGKBMODE)
+	/* Detect mode of current keyboard setup, e.g. for UTF-8 */
+	if (ioctl(fd, KDGKBMODE, &mode) < 0)
+		mode = K_RAW;
+	switch(mode) {
+	case K_UNICODE:
+		setlocale(LC_CTYPE, "C.UTF-8");
+		flags |= UL_TTY_UTF8;
+		break;
+	case K_RAW:
+	case K_MEDIUMRAW:
+	case K_XLATE:
+	default:
+		setlocale(LC_CTYPE, "POSIX");
+		break;
+	}
+#else
+	setlocale(LC_CTYPE, "POSIX");
+#endif
+	reset_virtual_console(tio, flags);
+setattr:
+	if (tcsetattr(fd, TCSANOW, tio))
+		warn(_("tcsetattr failed"));
+
+	/* Enable blocking mode for read and write */
+	if ((flags = fcntl(fd, F_GETFL, 0)) != -1)
+		(void)fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
+}
+
+/*
+ * Finalize the tty modes on modem lines.
+ */
+static void tcfinal(struct console *con)
+{
+	struct termios *tio;
+	int fd;
+
+	if ((con->flags & CON_SERIAL) == 0) {
+		setenv("TERM", "linux", 1);
+		return;
+	}
+	if (con->flags & CON_NOTTY)
+		return;
+
+	setenv("TERM", "vt102", 1);
+	tio = &con->tio;
+	fd = con->fd;
+
+	tio->c_iflag |= (IXON | IXOFF);
+	tio->c_lflag |= (ICANON | ISIG | ECHO|ECHOE|ECHOK|ECHOKE);
+	tio->c_oflag |= OPOST;
+
+	tio->c_cc[VINTR]    = CINTR;
+	tio->c_cc[VQUIT]    = CQUIT;
+	tio->c_cc[VERASE]   = con->cp.erase;
+	tio->c_cc[VKILL]    = con->cp.kill;
+	tio->c_cc[VEOF]     = CEOF;
+#ifdef VSWTC
+	tio->c_cc[VSWTC]    = _POSIX_VDISABLE;
+#else
+	tio->c_cc[VSWTCH]   = _POSIX_VDISABLE;
+#endif
+	tio->c_cc[VSTART]   = CSTART;
+	tio->c_cc[VSTOP]    = CSTOP;
+	tio->c_cc[VSUSP]    = CSUSP;
+	tio->c_cc[VEOL]     = _POSIX_VDISABLE;
+
+	if (con->cp.eol == CR) {
+		tio->c_iflag |= ICRNL;
+		tio->c_iflag &= ~(INLCR|IGNCR);
+		tio->c_oflag |= ONLCR;
+		tio->c_oflag &= ~(OCRNL|ONLRET);
+	}
+
+	switch (con->cp.parity) {
+	default:
+	case 0:
+		tio->c_cflag &= ~(PARODD | PARENB);
+		tio->c_iflag &= ~(INPCK | ISTRIP);
+		break;
+	case 1:				/* odd parity */
+		tio->c_cflag |= PARODD;
+		/* fall through */
+	case 2:				/* even parity */
+		tio->c_cflag |= PARENB;
+		tio->c_iflag |= (INPCK | ISTRIP);
+		/* fall through */
+	case (1 | 2):			/* no parity bit */
+		tio->c_cflag &= ~CSIZE;
+		tio->c_cflag |= CS7;
+		break;
+	}
+
+	/* Set line attributes */
+	(void)tcsetattr(fd, TCSANOW, tio);
+}
 
 /*
  * Called at timeout.
@@ -72,6 +247,11 @@ static void alrm_handler(int sig __attribute__((unused)))
 	alarm_rised++;
 }
 
+static void chld_handler(int sig __attribute__((unused)))
+{
+	sigchild++;
+}
+
 static void mask_signal(int signal, void (*handler)(int),
 		struct sigaction *origaction)
 {
@@ -81,8 +261,7 @@ static void mask_signal(int signal, void (*handler)(int),
 	sigemptyset(&newaction.sa_mask);
 	newaction.sa_flags = 0;
 
-	sigaction(signal, NULL, origaction);
-	sigaction(signal, &newaction, NULL);
+	sigaction(signal, &newaction, origaction);
 }
 
 static void unmask_signal(int signal, struct sigaction *sa)
@@ -282,51 +461,202 @@ static struct passwd *getrootpwent(int try_manually)
 }
 
 /*
+ * Ask by prompt for the password.
+ */
+static void doprompt(const char *crypted, struct console *con)
+{
+	struct termios tty;
+
+	if (con->flags & CON_SERIAL) {
+		tty = con->tio;
+		/*
+		 * For prompting: map NL in output to CR-NL
+		 * otherwise we may see stairs in the output.
+		 */
+		tty.c_oflag |= (ONLCR | OPOST);
+		(void) tcsetattr(con->fd, TCSADRAIN, &tty);
+	}
+	if (con->file == (FILE*)0) {
+		if  ((con->file = fdopen(con->fd, "r+")) == (FILE*)0)
+			goto err;
+	}
+#if defined(USE_ONELINE)
+	if (crypted[0])
+		fprintf(con->file, _("Give root password for login: "));
+	else
+		fprintf(con->file, _("Press enter for login: "));
+#else
+	if (crypted[0])
+		fprintf(con->file, _("Give root password for maintenance\n"));
+	else
+		fprintf(con->file, _("Press enter for maintenance"));
+	fprintf(con->file, _("(or type Control-D to continue): "));
+#endif
+	fflush(con->file);
+err:
+	if (con->flags & CON_SERIAL)
+		(void) tcsetattr(con->fd, TCSADRAIN, &con->tio);
+}
+
+/*
+ * Make sure to have an own session and controlling terminal
+ */
+static void setup(struct console *con)
+{
+	pid_t pid, pgrp, ppgrp, ttypgrp;
+	int fd;
+
+	if (con->flags & CON_NOTTY)
+		return;
+	fd = con->fd;
+
+	/*
+	 * Only go through this trouble if the new
+	 * tty doesn't fall in this process group.
+	 */
+	pid = getpid();
+	pgrp = getpgid(0);
+	ppgrp = getpgid(getppid());
+	ttypgrp = tcgetpgrp(fd);
+
+	if (pgrp != ttypgrp && ppgrp != ttypgrp) {
+		if (pid != getsid(0)) {
+			if (pid == getpgid(0))
+				setpgid(0, getpgid(getppid()));
+			setsid();
+		}
+
+		mask_signal(SIGHUP, SIG_IGN, &saved_sighup);
+		if (ttypgrp > 0)
+			ioctl(STDIN_FILENO, TIOCNOTTY, (char *)1);
+		unmask_signal(SIGHUP, &saved_sighup);
+		if (fd > STDIN_FILENO)  close(STDIN_FILENO);
+		if (fd > STDOUT_FILENO) close(STDOUT_FILENO);
+		if (fd > STDERR_FILENO) close(STDERR_FILENO);
+
+		ioctl(fd, TIOCSCTTY, (char *)1);
+		tcsetpgrp(fd, ppgrp);
+	}
+	dup2(fd, STDIN_FILENO);
+	dup2(fd, STDOUT_FILENO);
+	dup2(fd, STDERR_FILENO);
+	con->fd = STDIN_FILENO;
+
+	for (fd = STDERR_FILENO+1; fd < 32; fd++) {
+		if (openfd & (1<<fd)) {
+			close(fd);
+			openfd &= ~(1<<fd);
+		}
+	}
+}
+
+/*
  * Ask for the password. Note that there is no default timeout as we normally
  * skip this during boot.
  */
-static char *getpasswd(char *crypted)
+static char *getpasswd(struct console *con)
 {
 	struct sigaction sa;
-	struct termios old, tty;
-	static char pass[128];
+	struct termios tty;
+	static char pass[128], *ptr;
+	struct chardata *cp;
 	char *ret = pass;
-	size_t i;
+	unsigned char tc;
+	char c, ascval;
+	int eightbit;
+	int fd;
 
-	if (crypted[0])
-		printf(_("Give root password for maintenance\n"));
-	else
-		printf(_("Press enter for maintenance"));
-	printf(_("(or type Control-D to continue): "));
-	fflush(stdout);
+	if (con->flags & CON_NOTTY)
+		goto out;
+	fd = con->fd;
+	cp = &con->cp;
+	tty = con->tio;
 
-	tcgetattr(0, &old);
-	tcgetattr(0, &tty);
 	tty.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
-	tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
-	tcsetattr(0, TCSANOW, &tty);
-
-	pass[sizeof(pass) - 1] = 0;
+	tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP|ISIG);
+	tc = (tcsetattr(fd, TCSAFLUSH, &tty) == 0);
 
 	sa.sa_handler = alrm_handler;
 	sa.sa_flags = 0;
 	sigaction(SIGALRM, &sa, NULL);
-	if (timeout)
-		alarm(timeout);
+	if (timeout) alarm(timeout);
 
-	if (read(0, pass, sizeof(pass) - 1) <= 0)
-		ret = NULL;
-	else {
-		for (i = 0; i < sizeof(pass) && pass[i]; i++)
-			if (pass[i] == '\r' || pass[i] == '\n') {
-				pass[i] = 0;
+	ptr = &pass[0];
+	cp->eol = *ptr = '\0';
+
+	eightbit = ((con->flags & CON_SERIAL) == 0 || (tty.c_cflag & (PARODD|PARENB)) == 0);
+	while (cp->eol == '\0') {
+		if (read(fd, &c, 1) < 1) {
+			if (errno == EINTR || errno == EAGAIN) {
+				usleep(1000);
+				continue;
+			}
+			ret = (char*)0;
+			switch (errno) {
+			case 0:
+			case EIO:
+			case ESRCH:
+			case EINVAL:
+			case ENOENT:
 				break;
+			default:
+				fprintf(stderr, "sulogin: read(%s): %m\n\r", con->tty);
+				break;
+			}
+			goto quit;
+		}
+
+		if (eightbit)
+			ascval = c;
+		else if (c != (ascval = (c & 0177))) {
+			uint32_t bits, mask;
+			for (bits = 1, mask = 1; mask & 0177; mask <<= 1) {
+				if (mask & ascval)
+					bits++;
+			}
+			cp->parity |= ((bits & 1) ? 1 : 2);
+		}
+
+		switch (ascval) {
+		case 0:
+			*ptr = '\0';
+			goto quit; 
+		case CR:
+		case NL:
+			*ptr = '\0';
+			cp->eol = ascval;
+			break;
+		case BS:
+		case CERASE:
+			cp->erase = ascval;
+			if (ptr > &pass[0])
+				ptr--;
+			break;
+		case CKILL:
+			cp->kill = ascval;
+			while (ptr > &pass[0])
+				ptr--;
+			break;
+		case CEOF:
+			goto quit;		
+		default:
+			if ((size_t)(ptr - &pass[0]) >= (sizeof(pass) -1 )) {
+				 fprintf(stderr, "sulogin: input overrun at %s\n\r", con->tty);
+				 ret = (char*)0;
+				 goto quit;
 			}
+			*ptr++ = ascval;
+			break;
+		}
 	}
+quit:
 	alarm(0);
-	tcsetattr(0, TCSANOW, &old);
-	printf("\n");
-
+	if (tc)
+		(void)tcsetattr(fd, TCSAFLUSH, &con->tio);
+	if (ret && *ret != '\0')
+		tcfinal(con);
+	printf("\r\n");
+out:
 	return ret;
 }
 
@@ -371,9 +701,10 @@ static void sushell(struct passwd *pwd)
 	/*
 	 * Set some important environment variables.
 	 */
-	if (getcwd(home, sizeof(home)) != NULL)
-		setenv("HOME", home, 1);
+	if (getcwd(home, sizeof(home)) == NULL)
+		strcpy(home, "/");
 
+	setenv("HOME", home, 1);
 	setenv("LOGNAME", "root", 1);
 	setenv("USER", "root", 1);
 	if (!profile)
@@ -386,6 +717,7 @@ static void sushell(struct passwd *pwd)
 	unmask_signal(SIGINT, &saved_sigint);
 	unmask_signal(SIGTSTP, &saved_sigtstp);
 	unmask_signal(SIGQUIT, &saved_sigquit);
+	mask_signal(SIGHUP, SIG_DFL, NULL);
 
 #ifdef HAVE_LIBSELINUX
 	if (is_selinux_enabled() > 0) {
@@ -411,36 +743,6 @@ static void sushell(struct passwd *pwd)
 	warn(_("%s: exec failed"), "/bin/sh");
 }
 
-static void fixtty(void)
-{
-	struct termios tp;
-	int x = 0, fl = 0;
-
-	/* Skip serial console */
-	if (ioctl(STDIN_FILENO, TIOCMGET, (char *) &x) == 0)
-		return;
-
-#if defined(IUTF8) && defined(KDGKBMODE)
-	/* Detect mode of current keyboard setup, e.g. for UTF-8 */
-	if (ioctl(STDIN_FILENO, KDGKBMODE, &x) == 0 && x == K_UNICODE) {
-		setlocale(LC_CTYPE, "C.UTF-8");
-		fl |= UL_TTY_UTF8;
-	}
-#else
-	setlocale(LC_CTYPE, "POSIX");
-#endif
-	memset(&tp, 0, sizeof(struct termios));
-	if (tcgetattr(STDIN_FILENO, &tp) < 0) {
-		warn(_("tcgetattr failed"));
-		return;
-	}
-
-	reset_virtual_console(&tp, fl);
-
-	if (tcsetattr(STDIN_FILENO, TCSADRAIN, &tp))
-		warn(_("tcsetattr failed"));
-}
-
 static void usage(FILE *out)
 {
 	fputs(USAGE_HEADER, out);
@@ -461,13 +763,14 @@ static void usage(FILE *out)
 
 int main(int argc, char **argv)
 {
+	struct list_head consoles = {&consoles, &consoles}, *ptr;
+	struct console *con;
 	char *tty = NULL;
-	char *p;
 	struct passwd *pwd;
-	int c, fd = -1;
+	int c, status = 0;
+	int reconnect = 0;
 	int opt_e = 0;
-	pid_t pid, pgrp, ppgrp, ttypgrp;
-	struct sigaction saved_sighup;
+	pid_t pid;
 
 	static const struct option longopts[] = {
 		{ "login-shell",  0, 0, 'p' },
@@ -478,10 +781,18 @@ int main(int argc, char **argv)
 		{ NULL,           0, 0, 0 }
 	};
 
+	/*
+	 * If we are init we need to set up a own session.
+	 */
+	if ((pid = getpid()) == 1) {
+		setsid();
+		(void)ioctl(STDIN_FILENO, TIOCSCTTY, (char *)1);
+	}
+
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
-	atexit(close_stdout);
+	atexit(close_stdout); /* XXX */
 
 	/*
 	 * See if we have a timeout flag.
@@ -513,78 +824,45 @@ int main(int argc, char **argv)
 	if (geteuid() != 0)
 		errx(EXIT_FAILURE, _("only root can run this program."));
 
-	/*
-	 * See if we need to open an other tty device.
-	 */
 	mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
 	mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
 	mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
+	mask_signal(SIGHUP,  SIG_IGN, &saved_sighup);
+
+	/*
+	 * See if we need to open an other tty device.
+	 */
 	if (optind < argc)
 		tty = argv[optind];
 
-	if (tty || (tty = getenv("CONSOLE"))) {
-
-		if ((fd = open(tty, O_RDWR)) < 0) {
-			warn(_("cannot open %s"), tty);
-			fd = dup(STDIN_FILENO);
-		}
-
-		if (fd < 0) {
-			warn(_("cannot duplicate stdin file descriptor"));
-		} else if (!isatty(fd)) {
-			warn(_("%s: not a tty"), tty);
-			close(fd);
-		} else {
+	if (!tty || *tty == '\0')
+		tty = getenv("CONSOLE");
 
-			/*
-			 * Only go through this trouble if the new tty doesn't
-			 * fall in this process group.
-			 */
-			pid = getpid();
-			pgrp = getpgid(0);
-			ppgrp = getpgid(getppid());
-			ttypgrp = tcgetpgrp(fd);
-
-			if (pgrp != ttypgrp && ppgrp != ttypgrp) {
-				if (pid != getsid(0)) {
-					if (pid == getpgid(0))
-						setpgid(0, getpgid(getppid()));
-					setsid();
-				}
+	/*
+	 * Detect possible consoles, use stdin as fallback.
+	 * If an optional tty is given, reconnect it to stdin.
+	 */
+	reconnect = detect_consoles(tty, STDIN_FILENO, &consoles);
 
-				sigaction(SIGHUP, NULL, &saved_sighup);
-				if (ttypgrp > 0)
-					ioctl(0, TIOCNOTTY, (char *)1);
-				sigaction(SIGHUP, &saved_sighup, NULL);
-				close(STDIN_FILENO);
-				close(STDOUT_FILENO);
-				close(STDERR_FILENO);
-				if (fd > 2)
-					close(fd);
-				if ((fd = open(tty, O_RDWR|O_NOCTTY)) < 0)
-					warn(_("cannot open %s"), tty);
-				else {
-					ioctl(STDIN_FILENO, TIOCSCTTY, (char *)1);
-					tcsetpgrp(fd, ppgrp);
-					dup2(fd, STDIN_FILENO);
-					dup2(fd, STDOUT_FILENO);
-					dup2(fd, STDERR_FILENO);
-
-					if (fd > STDERR_FILENO)
-						close(fd);
-				}
-			} else
-				if (fd > STDERR_FILENO)
-					close(fd);
-		}
-	} else if (getpid() == 1) {
-		/* We are init. We hence need to set a session anyway */
-		setsid();
-		if (ioctl(STDIN_FILENO, TIOCSCTTY, (char *)1))
-			warn(_("TIOCSCTTY: ioctl failed"));
+	/*
+	 * If previous stdin was not the speified tty and therefore reconnected
+	 * to the specified tty also reconnect stdout and stderr.
+	 */
+	if (reconnect) {
+		if (isatty(STDOUT_FILENO) == 0)
+			dup2(STDOUT_FILENO, STDIN_FILENO);
+		if (isatty(STDERR_FILENO) == 0)
+			dup2(STDOUT_FILENO, STDERR_FILENO);
 	}
 
-	fixtty();
+	/*
+	 * Should not happen
+	 */
+	if (list_empty(&consoles)) {
+		if (!errno)
+			errno = ENOENT;
+		errx(EXIT_FAILURE, _("cannot open console: %m\n"));
+	}
 
 	/*
 	 * Get the root password.
@@ -595,32 +873,121 @@ int main(int argc, char **argv)
 	}
 
 	/*
-	 * Ask for the password.
+	 * Ask for the password on the consoles.
 	 */
-	while (pwd) {
-		int failed = 0;
-		if ((p = getpasswd(pwd->pw_passwd)) == NULL)
+	list_for_each(ptr, &consoles) {
+		con = list_entry(ptr, struct console, entry);
+		if (con->id >= CONMAX)
 			break;
-		if (pwd->pw_passwd[0] == 0 ||
-		    strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd) == 0) {
-			sushell(pwd);
-			failed++;
+		if (con->fd >= 0) {
+			openfd |= (1<<con->fd);
+			tcinit(con);
+			continue;
 		}
-		mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
-		mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
-		mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
-		if (failed) {
-		    fprintf(stderr, _("Can not execute su shell\n\n"));
-		    break;
-		} else
-		    fprintf(stderr, _("Login incorrect\n\n"));
+		if ((con->fd = open(con->tty, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)
+			continue;
+		openfd |= (1<<con->fd);
+		tcinit(con);
 	}
+	ptr = (&consoles)->next;
+	usemask = (uint32_t*)mmap(NULL, sizeof(uint32_t), PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
 
-	if (alarm_rised)
-		fprintf(stderr, _("Timed out\n\n"));
+	if (ptr->next == &consoles) {
+		con = list_entry(ptr, struct console, entry);
+		goto nofork;
+	}
 
-	/*
-	 * User pressed Control-D.
-	 */
+	mask_signal(SIGCHLD, chld_handler, &saved_sigchld);
+	do {
+		con = list_entry(ptr, struct console, entry);
+		if (con->id >= CONMAX)
+			break;
+
+		switch ((con->pid = fork())) {
+		case 0:
+			mask_signal(SIGCHLD, SIG_DFL, NULL);
+			/* fall through */
+		nofork:
+			setup(con);
+			while (1) {
+				const char *passwd = pwd->pw_passwd;
+				const char *answer;
+				int failed = 0, doshell = 0;
+
+				doprompt(passwd, con);
+				if ((answer = getpasswd(con)) == NULL)
+					break;
+
+				if (passwd[0] == '\0')
+					doshell++;
+				else {
+					const char *cryptbuf;
+					cryptbuf = crypt(answer, passwd);
+					if (cryptbuf == NULL)
+						warnx(_("crypt failed: %m\n"));
+					else if (strcmp(cryptbuf, pwd->pw_passwd) == 0)
+						doshell++;
+				}
+
+				if (doshell) {
+					*usemask |= (1<<con->id);
+					sushell(pwd);
+					*usemask &= ~(1<<con->id);
+					failed++;
+				}
+
+				mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
+				mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
+				mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
+
+				if (failed) {
+					fprintf(stderr, _("Can not execute su shell\n\n"));
+					break;
+				}
+				fprintf(stderr, _("Login incorrect\n\n"));
+			}
+			if (alarm_rised) {
+				tcfinal(con);
+				warnx(_("Timed out\n\n"));
+			}
+			/*
+			 * User pressed Control-D.
+			 */
+			exit(0);
+		case -1:
+			warnx(_("Can not fork: %m\n"));
+			/* fall through */
+		default:
+			break;
+		}
+
+		ptr = ptr->next;
+
+	} while (ptr != &consoles);
+
+	while ((pid = wait(&status))) {
+		if (errno == ECHILD)
+			break;
+		if (pid < 0)
+			continue;
+		list_for_each(ptr, &consoles) {
+			con = list_entry(ptr, struct console, entry);
+			if (con->pid == pid) {
+				*usemask &= ~(1<<con->id);
+				continue;
+			}
+			if (kill(con->pid, 0) < 0) {
+				*usemask &= ~(1<<con->id);
+				continue;
+			}
+			if (*usemask & (1<<con->id))
+				continue;
+			kill(con->pid, SIGHUP);
+			usleep(5000);
+			kill(con->pid, SIGKILL);
+		}
+	}
+
+	mask_signal(SIGCHLD, SIG_DFL, NULL);
 	return EXIT_SUCCESS;
 }
Signature · 1 line
1.7.10.4
maintainer Applied
#19 lore raw
In reply to Werner Fink
Quoted 3 lines · Werner Fink
 include/consoles.h |    6 ++--
 lib/consoles.c     |   77 ++++++++++++++++++++++++++++++++--------------------
 2 files changed, 51 insertions(+), 32 deletions(-)

Applied, thanks. I have added list_last_entry() to avoid consoles->prev usage in the code.o

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer Applied
#2 lore raw

Re: [PATCH 2/5] sulogin: make usleep() workaround work

In reply to Werner Fink
Quoted 2 lines · Werner Fink
 include/c.h |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer Applied
#4 lore raw

Re: [PATCH 3/5] sulogin: use alarm function to indicate if a timeout occurs

In reply to Werner Fink
Quoted 2 lines · Werner Fink
 login-utils/sulogin.c |   20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#16 lore raw

Re: [PATCH 4/5] sulogin: mount temporary /dev and /proc if not found

In reply to Werner Fink
Quoted 3 lines · Werner Fink
 configure.ac          |   10 ++++++++
 login-utils/sulogin.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

Applied with some minor changes, thanks.

configure.ac if test "x$enable_use_tty_group" = xyes; then · line 1302

Quoted 4 lines
configure.ac +4 -0
+AC_ARG_ENABLE([use-emergency-dev],
+  AS_HELP_STRING([--disable-use-emergency-dev], [do not use emergency mount of /dev and /proc for sulogin]),
+  [], enable_use_emergency_dev=yes
+)

renamed to --disable-sulogin-emergency-mount

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer Applied
#12 lore raw

Re: [PATCH 5/5] sulogin: add multi console feature from SysVinit sulogin

In reply to Werner Fink
Quoted 2 lines · Werner Fink
 login-utils/sulogin.c |  665 ++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 516 insertions(+), 149 deletions(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#13 lore raw

Re: [PATCH 4/5] sulogin: mount temporary /dev and /proc if not found

In reply to Werner Fink
Quoted 3 lines · Werner Fink
 configure.ac   |   10 +++++++++
 lib/consoles.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)

Note that I have applied this (fixed) version of the patch.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#14 lore raw

Re: [PATCH 4/5] sulogin: mount temporary /dev and /proc if not found

Quoted 9 lines · Karel Zak
Quoted 3 lines · Werner Fink
 configure.ac   |   10 +++++++++
 lib/consoles.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)

Note that I have applied this (fixed) version of the patch.

Karel

This is pretty broken. Using the constructor and destructor attributes means that ANY programs linking in the code will invoke these functions. As it's not a direct inclusion, but part of a libtool convenience lib, the effect is pretty severe:

$ git grep -F libcommon.la | wc -l
78

This includes things such as libblkid and libmount, among others.

It wouldn't generally matter (though I'm still in favor of finding another solution for this), but this breaks early userspace pretty hard. Invoking mount (or even findmnt) will preemptively run this code. I'm not clear on why, but the filesystems were NOT umounted by the destructor, and my busybox umount as well as util-linux's switch_root simply failed with EINVAL.

I suggest that this code be relegated to sulogin.

d

maintainer
#15 lore raw

Re: [PATCH 4/5] sulogin: mount temporary /dev and /proc if not found

Quoted 1 line · Dave Reisner

I suggest that this code be relegated to sulogin.

Fixed:
- removed from libcommon (it's sulogin specific)
- removed the attributes

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com