ad9577
From f720781671c9f5421fb8101dd3bcf7b56efca131 Mon Sep 17 00:00:00 2001
ad9577
From: Karel Zak <kzak@redhat.com>
ad9577
Date: Mon, 20 Aug 2018 14:56:08 +0200
ad9577
Subject: [PATCH 178/178] sulogin: backport RHEL-8 version
ad9577
ad9577
The new version is more robust and it does not use mmap()-ed shared
ad9577
memory between sulogins instances. It also provides some fallbacks for
ad9577
s390 machines.
ad9577
ad9577
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1616264
ad9577
Signed-off-by: Karel Zak <kzak@redhat.com>
ad9577
---
ad9577
 configure.ac                   |   1 +
ad9577
 include/c.h                    |   4 +
ad9577
 login-utils/sulogin-consoles.c | 116 +++++++++++++------
ad9577
 login-utils/sulogin.c          | 253 +++++++++++++++++++++++++++--------------
ad9577
 4 files changed, 252 insertions(+), 122 deletions(-)
ad9577
ad9577
diff --git a/configure.ac b/configure.ac
ad9577
index d561e01d0..3f07df495 100644
ad9577
--- a/configure.ac
ad9577
+++ b/configure.ac
ad9577
@@ -218,6 +218,7 @@ AC_CHECK_HEADERS([ \
ad9577
 	sys/prctl.h \
ad9577
 	sys/queue.h \
ad9577
 	sys/resource.h \
ad9577
+	sys/sysmacros.h \
ad9577
 	sys/socket.h \
ad9577
 	sys/sockio.h \
ad9577
 	sys/stat.h \
ad9577
diff --git a/include/c.h b/include/c.h
ad9577
index 124035ea5..51d439297 100644
ad9577
--- a/include/c.h
ad9577
+++ b/include/c.h
ad9577
@@ -19,6 +19,10 @@
ad9577
 # include <err.h>
ad9577
 #endif
ad9577
 
ad9577
+#ifdef HAVE_SYS_SYSMACROS_H
ad9577
+# include <sys/sysmacros.h>     /* for major, minor */
ad9577
+#endif
ad9577
+
ad9577
 #ifndef HAVE_USLEEP
ad9577
 # include <time.h>
ad9577
 #endif
ad9577
diff --git a/login-utils/sulogin-consoles.c b/login-utils/sulogin-consoles.c
ad9577
index 07af33a6d..2c0eed3a4 100644
ad9577
--- a/login-utils/sulogin-consoles.c
ad9577
+++ b/login-utils/sulogin-consoles.c
ad9577
@@ -36,8 +36,9 @@
ad9577
 # include <linux/serial.h>
ad9577
 # include <linux/major.h>
ad9577
 #endif
ad9577
-#include <fcntl.h>
ad9577
 #include <dirent.h>
ad9577
+#include <errno.h>
ad9577
+#include <fcntl.h>
ad9577
 #include <unistd.h>
ad9577
 
ad9577
 #ifdef USE_SULOGIN_EMERGENCY_MOUNT
ad9577
@@ -74,7 +75,7 @@ static int consoles_debug;
ad9577
 		} while (0)
ad9577
 
ad9577
 static inline void __attribute__ ((__format__ (__printf__, 1, 2)))
ad9577
-dbgprint(const char *mesg, ...)
ad9577
+dbgprint(const char * const mesg, ...)
ad9577
 {
ad9577
 	va_list ap;
ad9577
 	va_start(ap, mesg);
ad9577
@@ -112,7 +113,7 @@ void emergency_do_mounts(void)
ad9577
 	}
ad9577
 
ad9577
 	if (stat("/", &rt) != 0) {
ad9577
-		warn("can not get file status of root file system\n");
ad9577
+		warn("cannot get file status of root file system\n");
ad9577
 		return;
ad9577
 	}
ad9577
 
ad9577
@@ -150,17 +151,19 @@ void emergency_do_mounts(void) { }
ad9577
  * the caller has to free the result
ad9577
  */
ad9577
 static __attribute__((__nonnull__))
ad9577
-char *oneline(const char *file)
ad9577
+char *oneline(const char * const file)
ad9577
 {
ad9577
 	FILE *fp;
ad9577
 	char *ret = NULL;
ad9577
-	size_t len = 0;
ad9577
+	size_t dummy = 0;
ad9577
+	ssize_t len;
ad9577
 
ad9577
 	DBG(dbgprint("reading %s", file));
ad9577
 
ad9577
-	if (!(fp = fopen(file, "re")))
ad9577
+	if (!(fp = fopen(file, "r" UL_CLOEXECSTR)))
ad9577
 		return NULL;
ad9577
-	if (getline(&ret, &len, fp) >= 0) {
ad9577
+	len = getline(&ret, &dummy, fp);
ad9577
+	if (len >= 0) {
ad9577
 		char *nl;
ad9577
 
ad9577
 		if (len)
ad9577
@@ -179,7 +182,7 @@ char *oneline(const char *file)
ad9577
  *  /sys/class/tty, the caller has to free the result.
ad9577
  */
ad9577
 static __attribute__((__malloc__))
ad9577
-char *actattr(const char *tty)
ad9577
+char *actattr(const char * const tty)
ad9577
 {
ad9577
 	char *ret, *path;
ad9577
 
ad9577
@@ -198,7 +201,7 @@ char *actattr(const char *tty)
ad9577
  * /sys/class/tty.
ad9577
  */
ad9577
 static
ad9577
-dev_t devattr(const char *tty)
ad9577
+dev_t devattr(const char * const tty)
ad9577
 {
ad9577
 	dev_t dev = 0;
ad9577
 	char *path, *value;
ad9577
@@ -223,42 +226,77 @@ dev_t devattr(const char *tty)
ad9577
 #endif /* __linux__ */
ad9577
 
ad9577
 /*
ad9577
- * Search below /dev for the characer device in `dev_t comparedev' variable.
ad9577
+ * Search below /dev for the character device in `dev_t comparedev' variable.
ad9577
+ * Note that realpath(3) is used here to avoid not existent devices due the
ad9577
+ * strdup(3) used in our canonicalize_path()!
ad9577
  */
ad9577
 static
ad9577
 #ifdef __GNUC__
ad9577
 __attribute__((__nonnull__,__malloc__,__hot__))
ad9577
 #endif
ad9577
-char* scandev(DIR *dir, dev_t comparedev)
ad9577
+char* scandev(DIR *dir, const dev_t comparedev)
ad9577
 {
ad9577
+	char path[PATH_MAX];
ad9577
 	char *name = NULL;
ad9577
-	struct dirent *dent;
ad9577
-	int fd;
ad9577
+	const struct dirent *dent;
ad9577
+	int len, fd;
ad9577
 
ad9577
 	DBG(dbgprint("scanning /dev for %u:%u", major(comparedev), minor(comparedev)));
ad9577
 
ad9577
+	/*
ad9577
+	 * Try udev links on character devices first.
ad9577
+	 */
ad9577
+	if ((len = snprintf(path, sizeof(path),
ad9577
+			    "/dev/char/%u:%u", major(comparedev), minor(comparedev))) > 0 &&
ad9577
+	    (size_t)len < sizeof(path)) {
ad9577
+
ad9577
+	    name = realpath(path, NULL);
ad9577
+	    if (name)
ad9577
+		    goto out;
ad9577
+	}
ad9577
+
ad9577
 	fd = dirfd(dir);
ad9577
 	rewinddir(dir);
ad9577
 	while ((dent = readdir(dir))) {
ad9577
-		char path[PATH_MAX];
ad9577
 		struct stat st;
ad9577
+
ad9577
+#ifdef _DIRENT_HAVE_D_TYPE
ad9577
+		if (dent->d_type != DT_UNKNOWN && dent->d_type != DT_CHR)
ad9577
+			continue;
ad9577
+#endif
ad9577
 		if (fstatat(fd, dent->d_name, &st, 0) < 0)
ad9577
 			continue;
ad9577
 		if (!S_ISCHR(st.st_mode))
ad9577
 			continue;
ad9577
 		if (comparedev != st.st_rdev)
ad9577
 			continue;
ad9577
-		if ((size_t)snprintf(path, sizeof(path), "/dev/%s", dent->d_name) >= sizeof(path))
ad9577
+		if ((len = snprintf(path, sizeof(path), "/dev/%s", dent->d_name)) < 0 ||
ad9577
+		    (size_t)len >= sizeof(path))
ad9577
 			continue;
ad9577
-#ifdef USE_SULOGIN_EMERGENCY_MOUNT
ad9577
-		if (emergency_flags & MNT_DEVTMPFS)
ad9577
-			mknod(path, S_IFCHR|S_IRUSR|S_IWUSR, comparedev);
ad9577
-#endif
ad9577
 
ad9577
-		name = canonicalize_path(path);
ad9577
-		break;
ad9577
+		name = realpath(path, NULL);
ad9577
+		if (name)
ad9577
+			goto out;
ad9577
 	}
ad9577
 
ad9577
+#ifdef USE_SULOGIN_EMERGENCY_MOUNT
ad9577
+	/*
ad9577
+	 * There was no /dev mounted hence and no device was found hence we create our own.
ad9577
+	 */
ad9577
+	if (!name && (emergency_flags & MNT_DEVTMPFS)) {
ad9577
+
ad9577
+		if ((len = snprintf(path, sizeof(path),
ad9577
+				    "/dev/tmp-%u:%u", major(comparedev), minor(comparedev))) < 0 ||
ad9577
+		    (size_t)len >= sizeof(path))
ad9577
+			goto out;
ad9577
+
ad9577
+		if (mknod(path, S_IFCHR|S_IRUSR|S_IWUSR, comparedev) < 0 && errno != EEXIST)
ad9577
+			goto out;
ad9577
+
ad9577
+		name = realpath(path, NULL);
ad9577
+	}
ad9577
+#endif
ad9577
+out:
ad9577
 	return name;
ad9577
 }
ad9577
 
ad9577
@@ -273,12 +311,12 @@ char* scandev(DIR *dir, dev_t comparedev)
ad9577
  */
ad9577
 static
ad9577
 #ifdef __GNUC__
ad9577
-__attribute__((__nonnull__,__hot__))
ad9577
+__attribute__((__hot__))
ad9577
 #endif
ad9577
-int append_console(struct list_head *consoles, const char *name)
ad9577
+int append_console(struct list_head *consoles, const char * const name)
ad9577
 {
ad9577
 	struct console *restrict tail;
ad9577
-	struct console *last = NULL;
ad9577
+	const struct console *last = NULL;
ad9577
 
ad9577
 	DBG(dbgprint("appenging %s", name));
ad9577
 
ad9577
@@ -300,7 +338,7 @@ int append_console(struct list_head *consoles, const char *name)
ad9577
 	tail->flags = 0;
ad9577
 	tail->fd = -1;
ad9577
 	tail->id = last ? last->id + 1 : 0;
ad9577
-	tail->pid = 0;
ad9577
+	tail->pid = -1;
ad9577
 	memset(&tail->tio, 0, sizeof(tail->tio));
ad9577
 
ad9577
 	return 0;
ad9577
@@ -319,11 +357,11 @@ static int detect_consoles_from_proc(struct list_head *consoles)
ad9577
 	char fbuf[16 + 1];
ad9577
 	DIR *dir = NULL;
ad9577
 	FILE *fc = NULL;
ad9577
-	int maj, min, rc = 1;
ad9577
+	int maj, min, rc = 1, matches;
ad9577
 
ad9577
 	DBG(dbgprint("trying /proc"));
ad9577
 
ad9577
-	fc = fopen("/proc/consoles", "re");
ad9577
+	fc = fopen("/proc/consoles", "r" UL_CLOEXECSTR);
ad9577
 	if (!fc) {
ad9577
 		rc = 2;
ad9577
 		goto done;
ad9577
@@ -332,10 +370,12 @@ static int detect_consoles_from_proc(struct list_head *consoles)
ad9577
 	if (!dir)
ad9577
 		goto done;
ad9577
 
ad9577
-	while (fscanf(fc, "%*s %*s (%16[^)]) %d:%d", fbuf, &maj, &min) == 3) {
ad9577
+	while ((matches = fscanf(fc, "%*s %*s (%16[^)]) %d:%d", fbuf, &maj, &min)) >= 1) {
ad9577
 		char *name;
ad9577
 		dev_t comparedev;
ad9577
 
ad9577
+		if (matches != 3)
ad9577
+			continue;
ad9577
 		if (!strchr(fbuf, 'E'))
ad9577
 			continue;
ad9577
 		comparedev = makedev(maj, min);
ad9577
@@ -509,7 +549,7 @@ done:
ad9577
 
ad9577
 #ifdef TIOCGDEV
ad9577
 static int detect_consoles_from_tiocgdev(struct list_head *consoles,
ad9577
-					int fallback,
ad9577
+					const int fallback,
ad9577
 					const char *device)
ad9577
 {
ad9577
 	unsigned int devnum;
ad9577
@@ -579,7 +619,7 @@ done:
ad9577
  * Returns 1 if stdout and stderr should be reconnected and 0
ad9577
  * otherwise or less than zero on error.
ad9577
  */
ad9577
-int detect_consoles(const char *device, int fallback, struct list_head *consoles)
ad9577
+int detect_consoles(const char *device, const int fallback, struct list_head *consoles)
ad9577
 {
ad9577
 	int fd, reconnect = 0, rc;
ad9577
 	dev_t comparedev = 0;
ad9577
@@ -587,7 +627,7 @@ int detect_consoles(const char *device, int fallback, struct list_head *consoles
ad9577
 	consoles_debug = getenv("CONSOLES_DEBUG") ? 1 : 0;
ad9577
 
ad9577
 	if (!device || !*device)
ad9577
-		fd = dup(fallback);
ad9577
+		fd = fallback >= 0 ? dup(fallback) : - 1;
ad9577
 	else {
ad9577
 		fd = open(device, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
ad9577
 		reconnect = 1;
ad9577
@@ -602,6 +642,14 @@ int detect_consoles(const char *device, int fallback, struct list_head *consoles
ad9577
 		struct stat st;
ad9577
 #ifdef TIOCGDEV
ad9577
 		unsigned int devnum;
ad9577
+#endif
ad9577
+#ifdef __GNU__
ad9577
+		/*
ad9577
+		 * The Hurd always gives st_rdev as 0, which causes this
ad9577
+		 * method to select the first terminal it finds.
ad9577
+		 */
ad9577
+		close(fd);
ad9577
+		goto fallback;
ad9577
 #endif
ad9577
 		DBG(dbgprint("trying device/fallback file descriptor"));
ad9577
 
ad9577
@@ -670,7 +718,7 @@ int detect_consoles(const char *device, int fallback, struct list_head *consoles
ad9577
 #ifdef __linux__
ad9577
 console:
ad9577
 	/*
ad9577
-	 * Detection of devices used for Linux system consolei using
ad9577
+	 * Detection of devices used for Linux system console using
ad9577
 	 * the /proc/consoles API with kernel 2.6.38 and higher.
ad9577
 	 */
ad9577
 	rc = detect_consoles_from_proc(consoles);
ad9577
@@ -754,8 +802,7 @@ int main(int argc, char *argv[])
ad9577
 {
ad9577
 	char *name = NULL;
ad9577
 	int fd, re;
ad9577
-	LIST_HEAD(consoles);
ad9577
-	struct list_head *p;
ad9577
+	struct list_head *p, consoles;
ad9577
 
ad9577
 	if (argc == 2) {
ad9577
 		name = argv[1];
ad9577
@@ -768,6 +815,7 @@ int main(int argc, char *argv[])
ad9577
 	if (!name)
ad9577
 		errx(EXIT_FAILURE, "usage: %s [<tty>]\n", program_invocation_short_name);
ad9577
 
ad9577
+	INIT_LIST_HEAD(&consoles);
ad9577
 	re = detect_consoles(name, fd, &consoles);
ad9577
 
ad9577
 	list_for_each(p, &consoles) {
ad9577
diff --git a/login-utils/sulogin.c b/login-utils/sulogin.c
ad9577
index dd73a1c50..4620ed2da 100644
ad9577
--- a/login-utils/sulogin.c
ad9577
+++ b/login-utils/sulogin.c
ad9577
@@ -56,8 +56,12 @@
ad9577
 
ad9577
 #include "c.h"
ad9577
 #include "closestream.h"
ad9577
+#include "env.h"
ad9577
 #include "nls.h"
ad9577
 #include "pathnames.h"
ad9577
+#ifdef USE_PLYMOUTH_SUPPORT
ad9577
+# include "plymouth-ctrl.h"
ad9577
+#endif
ad9577
 #include "strutils.h"
ad9577
 #include "ttyutils.h"
ad9577
 #include "sulogin-consoles.h"
ad9577
@@ -66,13 +70,12 @@
ad9577
 static unsigned int timeout;
ad9577
 static int profile;
ad9577
 static volatile uint32_t openfd;		/* Remember higher file descriptors */
ad9577
-static volatile uint32_t *usemask;
ad9577
 
ad9577
-struct sigaction saved_sigint;
ad9577
-struct sigaction saved_sigtstp;
ad9577
-struct sigaction saved_sigquit;
ad9577
-struct sigaction saved_sighup;
ad9577
-struct sigaction saved_sigchld;
ad9577
+static struct sigaction saved_sigint;
ad9577
+static struct sigaction saved_sigtstp;
ad9577
+static struct sigaction saved_sigquit;
ad9577
+static struct sigaction saved_sighup;
ad9577
+static struct sigaction saved_sigchld;
ad9577
 
ad9577
 static volatile sig_atomic_t alarm_rised;
ad9577
 static volatile sig_atomic_t sigchild;
ad9577
@@ -81,7 +84,12 @@ static volatile sig_atomic_t sigchild;
ad9577
 # define IUCLC		0
ad9577
 #endif
ad9577
 
ad9577
-static int locked_account_password(const char *passwd)
ad9577
+#ifndef WEXITED
ad9577
+# warning "WEXITED is missing, sulogin may not work as expected"
ad9577
+# define WEXITED 0
ad9577
+#endif
ad9577
+
ad9577
+static int locked_account_password(const char * const passwd)
ad9577
 {
ad9577
 	if (passwd && (*passwd == '*' || *passwd == '!'))
ad9577
 		return 1;
ad9577
@@ -93,10 +101,29 @@ static int locked_account_password(const char *passwd)
ad9577
  */
ad9577
 static void tcinit(struct console *con)
ad9577
 {
ad9577
-	int mode = 0, flags = 0;
ad9577
+	int flags = 0, mode = 0;
ad9577
 	struct termios *tio = &con->tio;
ad9577
-	int fd = con->fd;
ad9577
-
ad9577
+	const int fd = con->fd;
ad9577
+#ifdef USE_PLYMOUTH_SUPPORT
ad9577
+	struct termios lock;
ad9577
+	int i = (plymouth_command(MAGIC_PING)) ? PLYMOUTH_TERMIOS_FLAGS_DELAY : 0;
ad9577
+	if (i)
ad9577
+		plymouth_command(MAGIC_QUIT);
ad9577
+	while (i-- > 0) {
ad9577
+		/*
ad9577
+		 * With plymouth the termios flags become changed after this
ad9577
+		 * function had changed the termios.
ad9577
+		 */
ad9577
+		memset(&lock, 0, sizeof(struct termios));
ad9577
+		if (ioctl(fd, TIOCGLCKTRMIOS, &lock) < 0)
ad9577
+			break;
ad9577
+		if (!lock.c_iflag && !lock.c_oflag && !lock.c_cflag && !lock.c_lflag)
ad9577
+			break;
ad9577
+		sleep(1);
ad9577
+	}
ad9577
+	memset(&lock, 0, sizeof(struct termios));
ad9577
+	ioctl(fd, TIOCSLCKTRMIOS, &lock);
ad9577
+#endif
ad9577
 	errno = 0;
ad9577
 
ad9577
 	if (tcgetattr(fd, tio) < 0) {
ad9577
@@ -189,20 +216,23 @@ setattr:
ad9577
  */
ad9577
 static void tcfinal(struct console *con)
ad9577
 {
ad9577
-	struct termios *tio;
ad9577
-	int fd;
ad9577
+	struct termios *tio = &con->tio;
ad9577
+	const int fd = con->fd;
ad9577
 
ad9577
 	if ((con->flags & CON_SERIAL) == 0) {
ad9577
-		setenv("TERM", "linux", 1);
ad9577
+		xsetenv("TERM", "linux", 1);
ad9577
 		return;
ad9577
 	}
ad9577
-	if (con->flags & CON_NOTTY)
ad9577
+	if (con->flags & CON_NOTTY) {
ad9577
+		xsetenv("TERM", "dumb", 1);
ad9577
 		return;
ad9577
+	}
ad9577
 
ad9577
-	setenv("TERM", "vt102", 1);
ad9577
-	tio = &con->tio;
ad9577
-	fd = con->fd;
ad9577
-
ad9577
+#if defined (__s390__) || defined (__s390x__)
ad9577
+	xsetenv("TERM", "dumb", 1);
ad9577
+#else
ad9577
+	xsetenv("TERM", "vt102", 1);
ad9577
+#endif
ad9577
 	tio->c_iflag |= (IXON | IXOFF);
ad9577
 	tio->c_lflag |= (ICANON | ISIG | ECHO|ECHOE|ECHOK|ECHOKE);
ad9577
 	tio->c_oflag |= OPOST;
ad9577
@@ -237,11 +267,11 @@ static void tcfinal(struct console *con)
ad9577
 		break;
ad9577
 	case 1:				/* odd parity */
ad9577
 		tio->c_cflag |= PARODD;
ad9577
-		/* fall through */
ad9577
+		/* fallthrough */
ad9577
 	case 2:				/* even parity */
ad9577
 		tio->c_cflag |= PARENB;
ad9577
 		tio->c_iflag |= (INPCK | ISTRIP);
ad9577
-		/* fall through */
ad9577
+		/* fallthrough */
ad9577
 	case (1 | 2):			/* no parity bit */
ad9577
 		tio->c_cflag &= ~CSIZE;
ad9577
 		tio->c_cflag |= CS7;
ad9577
@@ -466,7 +496,7 @@ static struct passwd *getrootpwent(int try_manually)
ad9577
 		warnx(_("%s: no entry for root"), _PATH_SHADOW_PASSWD);
ad9577
 		*pwd.pw_passwd = '\0';
ad9577
 	}
ad9577
-	/* locked accont passwords are valid too */
ad9577
+	/* locked account passwords are valid too */
ad9577
 	if (!locked_account_password(pwd.pw_passwd) && !valid(pwd.pw_passwd)) {
ad9577
 		warnx(_("%s: root password garbled"), _PATH_SHADOW_PASSWD);
ad9577
 		*pwd.pw_passwd = '\0';
ad9577
@@ -524,22 +554,17 @@ err:
ad9577
  */
ad9577
 static void setup(struct console *con)
ad9577
 {
ad9577
-	pid_t pid, pgrp, ppgrp, ttypgrp;
ad9577
-	int fd;
ad9577
+	int fd = con->fd;
ad9577
+	const pid_t pid = getpid(), pgrp = getpgid(0), ppgrp =
ad9577
+	    getpgid(getppid()), ttypgrp = tcgetpgrp(fd);
ad9577
 
ad9577
 	if (con->flags & CON_NOTTY)
ad9577
 		return;
ad9577
-	fd = con->fd;
ad9577
 
ad9577
 	/*
ad9577
 	 * Only go through this trouble if the new
ad9577
 	 * tty doesn't fall in this process group.
ad9577
 	 */
ad9577
-	pid = getpid();
ad9577
-	pgrp = getpgid(0);
ad9577
-	ppgrp = getpgid(getppid());
ad9577
-	ttypgrp = tcgetpgrp(fd);
ad9577
-
ad9577
 	if (pgrp != ttypgrp && ppgrp != ttypgrp) {
ad9577
 		if (pid != getsid(0)) {
ad9577
 			if (pid == getpgid(0))
ad9577
@@ -575,21 +600,20 @@ static void setup(struct console *con)
ad9577
  * Ask for the password. Note that there is no default timeout as we normally
ad9577
  * skip this during boot.
ad9577
  */
ad9577
-static char *getpasswd(struct console *con)
ad9577
+static const char *getpasswd(struct console *con)
ad9577
 {
ad9577
 	struct sigaction sa;
ad9577
 	struct termios tty;
ad9577
 	static char pass[128], *ptr;
ad9577
 	struct chardata *cp;
ad9577
-	char *ret = pass;
ad9577
+	const char *ret = pass;
ad9577
 	unsigned char tc;
ad9577
 	char c, ascval;
ad9577
 	int eightbit;
ad9577
-	int fd;
ad9577
+	const int fd = con->fd;
ad9577
 
ad9577
 	if (con->flags & CON_NOTTY)
ad9577
 		goto out;
ad9577
-	fd = con->fd;
ad9577
 	cp = &con->cp;
ad9577
 	tty = con->tio;
ad9577
 
ad9577
@@ -597,6 +621,7 @@ static char *getpasswd(struct console *con)
ad9577
 	tty.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP|ISIG);
ad9577
 	tc = (tcsetattr(fd, TCSAFLUSH, &tty) == 0);
ad9577
 
ad9577
+	sigemptyset(&sa.sa_mask);
ad9577
 	sa.sa_handler = alrm_handler;
ad9577
 	sa.sa_flags = 0;
ad9577
 	sigaction(SIGALRM, &sa, NULL);
ad9577
@@ -615,7 +640,7 @@ static char *getpasswd(struct console *con)
ad9577
 					ret = NULL;
ad9577
 					goto quit;
ad9577
 				}
ad9577
-				usleep(1000);
ad9577
+				usleep(250000);
ad9577
 				continue;
ad9577
 			}
ad9577
 			ret = (char*)0;
ad9577
@@ -627,7 +652,7 @@ static char *getpasswd(struct console *con)
ad9577
 			case ENOENT:
ad9577
 				break;
ad9577
 			default:
ad9577
-				warn(_("%s: read failed"), con->tty);
ad9577
+				warn(_("cannot read %s"), con->tty);
ad9577
 				break;
ad9577
 			}
ad9577
 			goto quit;
ad9577
@@ -694,8 +719,8 @@ static void sushell(struct passwd *pwd)
ad9577
 {
ad9577
 	char shell[PATH_MAX];
ad9577
 	char home[PATH_MAX];
ad9577
-	char *p;
ad9577
-	char *su_shell;
ad9577
+	char const *p;
ad9577
+	char const *su_shell;
ad9577
 
ad9577
 	/*
ad9577
 	 * Set directory and shell.
ad9577
@@ -731,16 +756,16 @@ static void sushell(struct passwd *pwd)
ad9577
 	if (getcwd(home, sizeof(home)) == NULL)
ad9577
 		strcpy(home, "/");
ad9577
 
ad9577
-	setenv("HOME", home, 1);
ad9577
-	setenv("LOGNAME", "root", 1);
ad9577
-	setenv("USER", "root", 1);
ad9577
+	xsetenv("HOME", home, 1);
ad9577
+	xsetenv("LOGNAME", "root", 1);
ad9577
+	xsetenv("USER", "root", 1);
ad9577
 	if (!profile)
ad9577
-		setenv("SHLVL","0",1);
ad9577
+		xsetenv("SHLVL","0",1);
ad9577
 
ad9577
 	/*
ad9577
 	 * Try to execute a shell.
ad9577
 	 */
ad9577
-	setenv("SHELL", su_shell, 1);
ad9577
+	xsetenv("SHELL", su_shell, 1);
ad9577
 	unmask_signal(SIGINT, &saved_sigint);
ad9577
 	unmask_signal(SIGTSTP, &saved_sigtstp);
ad9577
 	unmask_signal(SIGQUIT, &saved_sigquit);
ad9577
@@ -765,17 +790,21 @@ static void sushell(struct passwd *pwd)
ad9577
 	execl(su_shell, shell, NULL);
ad9577
 	warn(_("failed to execute %s"), su_shell);
ad9577
 
ad9577
-	setenv("SHELL", "/bin/sh", 1);
ad9577
+	xsetenv("SHELL", "/bin/sh", 1);
ad9577
 	execl("/bin/sh", profile ? "-sh" : "sh", NULL);
ad9577
 	warn(_("failed to execute %s"), "/bin/sh");
ad9577
 }
ad9577
 
ad9577
-static void usage(FILE *out)
ad9577
+static void usage(void)
ad9577
 {
ad9577
+	FILE *out = stdout;
ad9577
 	fputs(USAGE_HEADER, out);
ad9577
 	fprintf(out, _(
ad9577
 		" %s [options] [tty device]\n"), program_invocation_short_name);
ad9577
 
ad9577
+	fputs(USAGE_SEPARATOR, out);
ad9577
+	fputs(_("Single-user login.\n"), out);
ad9577
+
ad9577
 	fputs(USAGE_OPTIONS, out);
ad9577
 	fputs(_(" -p, --login-shell        start a login shell\n"
ad9577
 		" -t, --timeout <seconds>  max time to wait for a password (default: no limit)\n"
ad9577
@@ -783,32 +812,35 @@ static void usage(FILE *out)
ad9577
 		out);
ad9577
 
ad9577
 	fputs(USAGE_SEPARATOR, out);
ad9577
-	fputs(USAGE_HELP, out);
ad9577
-	fputs(USAGE_VERSION, out);
ad9577
-	fprintf(out, USAGE_MAN_TAIL("sulogin(8)"));
ad9577
+	printf(USAGE_HELP_OPTIONS(26));
ad9577
+	printf(USAGE_MAN_TAIL("sulogin(8)"));
ad9577
 }
ad9577
 
ad9577
 int main(int argc, char **argv)
ad9577
 {
ad9577
-	LIST_HEAD(consoles);
ad9577
-	struct list_head *ptr;
ad9577
+	struct list_head *ptr, consoles;
ad9577
 	struct console *con;
ad9577
 	char *tty = NULL;
ad9577
 	struct passwd *pwd;
ad9577
-	int c, status = 0;
ad9577
-	int reconnect = 0;
ad9577
+	const struct timespec sigwait = { .tv_sec = 0, .tv_nsec = 50000000 };
ad9577
+	siginfo_t status = { 0 };
ad9577
+	sigset_t set;
ad9577
+	int c, reconnect = 0;
ad9577
 	int opt_e = 0;
ad9577
+	int wait = 0;
ad9577
 	pid_t pid;
ad9577
 
ad9577
 	static const struct option longopts[] = {
ad9577
-		{ "login-shell",  0, 0, 'p' },
ad9577
-		{ "timeout",      1, 0, 't' },
ad9577
-		{ "force",        0, 0, 'e' },
ad9577
-		{ "help",         0, 0, 'h' },
ad9577
-		{ "version",      0, 0, 'V' },
ad9577
-		{ NULL,           0, 0, 0 }
ad9577
+		{ "login-shell",  no_argument,       NULL, 'p' },
ad9577
+		{ "timeout",      required_argument, NULL, 't' },
ad9577
+		{ "force",        no_argument,       NULL, 'e' },
ad9577
+		{ "help",         no_argument,       NULL, 'h' },
ad9577
+		{ "version",      no_argument,       NULL, 'V' },
ad9577
+		{ NULL, 0, NULL, 0 }
ad9577
 	};
ad9577
 
ad9577
+	INIT_LIST_HEAD(&consoles);
ad9577
+
ad9577
 	/*
ad9577
 	 * If we are init we need to set up a own session.
ad9577
 	 */
ad9577
@@ -840,17 +872,16 @@ int main(int argc, char **argv)
ad9577
 			printf(UTIL_LINUX_VERSION);
ad9577
 			return EXIT_SUCCESS;
ad9577
 		case 'h':
ad9577
-			usage(stdout);
ad9577
+			usage();
ad9577
 			return EXIT_SUCCESS;
ad9577
 		default:
ad9577
-			usage(stderr);
ad9577
-			/* Do not exit! */
ad9577
+			/* Do not exit! getopt prints a warning. */
ad9577
 			break;
ad9577
 		}
ad9577
 	}
ad9577
 
ad9577
 	if (geteuid() != 0)
ad9577
-		errx(EXIT_FAILURE, _("only root can run this program."));
ad9577
+		errx(EXIT_FAILURE, _("only superuser can run this program"));
ad9577
 
ad9577
 	mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
ad9577
 	mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
ad9577
@@ -877,7 +908,7 @@ int main(int argc, char **argv)
ad9577
 	reconnect = detect_consoles(tty, STDIN_FILENO, &consoles);
ad9577
 
ad9577
 	/*
ad9577
-	 * If previous stdin was not the speified tty and therefore reconnected
ad9577
+	 * If previous stdin was not the specified tty and therefore reconnected
ad9577
 	 * to the specified tty also reconnect stdout and stderr.
ad9577
 	 */
ad9577
 	if (reconnect) {
ad9577
@@ -900,7 +931,7 @@ int main(int argc, char **argv)
ad9577
 	 * Get the root password.
ad9577
 	 */
ad9577
 	if ((pwd = getrootpwent(opt_e)) == NULL) {
ad9577
-		warnx(_("cannot open password database."));
ad9577
+		warnx(_("cannot open password database"));
ad9577
 		sleep(2);
ad9577
 		return EXIT_FAILURE;
ad9577
 	}
ad9577
@@ -923,9 +954,6 @@ int main(int argc, char **argv)
ad9577
 		tcinit(con);
ad9577
 	}
ad9577
 	ptr = (&consoles)->next;
ad9577
-	usemask = (uint32_t*) mmap(NULL, sizeof(uint32_t),
ad9577
-					PROT_READ|PROT_WRITE,
ad9577
-					MAP_ANONYMOUS|MAP_SHARED, -1, 0);
ad9577
 
ad9577
 	if (ptr->next == &consoles) {
ad9577
 		con = list_entry(ptr, struct console, entry);
ad9577
@@ -942,7 +970,6 @@ int main(int argc, char **argv)
ad9577
 		switch ((con->pid = fork())) {
ad9577
 		case 0:
ad9577
 			mask_signal(SIGCHLD, SIG_DFL, NULL);
ad9577
-			/* fall through */
ad9577
 		nofork:
ad9577
 			setup(con);
ad9577
 			while (1) {
ad9577
@@ -971,9 +998,7 @@ int main(int argc, char **argv)
ad9577
 				}
ad9577
 
ad9577
 				if (doshell) {
ad9577
-					*usemask |= (1<<con->id);
ad9577
 					sushell(pwd);
ad9577
-					*usemask &= ~(1<<con->id);
ad9577
 					failed++;
ad9577
 				}
ad9577
 
ad9577
@@ -982,7 +1007,7 @@ int main(int argc, char **argv)
ad9577
 				mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
ad9577
 
ad9577
 				if (failed) {
ad9577
-					fprintf(stderr, _("Can not execute su shell\n\n"));
ad9577
+					fprintf(stderr, _("cannot execute su shell\n\n"));
ad9577
 					break;
ad9577
 				}
ad9577
 				fprintf(stderr, _("Login incorrect\n\n"));
ad9577
@@ -997,7 +1022,7 @@ int main(int argc, char **argv)
ad9577
 			exit(0);
ad9577
 		case -1:
ad9577
 			warn(_("fork failed"));
ad9577
-			/* fall through */
ad9577
+			/* fallthrough */
ad9577
 		default:
ad9577
 			break;
ad9577
 		}
ad9577
@@ -1006,28 +1031,80 @@ int main(int argc, char **argv)
ad9577
 
ad9577
 	} while (ptr != &consoles);
ad9577
 
ad9577
-	while ((pid = wait(&status))) {
ad9577
-		if (errno == ECHILD)
ad9577
+	do {
ad9577
+		int ret;
ad9577
+
ad9577
+		status.si_pid = 0;
ad9577
+		ret = waitid(P_ALL, 0, &status, WEXITED);
ad9577
+
ad9577
+		if (ret == 0)
ad9577
 			break;
ad9577
-		if (pid < 0)
ad9577
-			continue;
ad9577
-		list_for_each(ptr, &consoles) {
ad9577
-			con = list_entry(ptr, struct console, entry);
ad9577
-			if (con->pid == pid) {
ad9577
-				*usemask &= ~(1<<con->id);
ad9577
+		if (ret < 0) {
ad9577
+			if (errno == ECHILD)
ad9577
+				break;
ad9577
+			if (errno == EINTR)
ad9577
 				continue;
ad9577
-			}
ad9577
-			if (kill(con->pid, 0) < 0) {
ad9577
-				*usemask &= ~(1<<con->id);
ad9577
+		}
ad9577
+
ad9577
+		errx(EXIT_FAILURE, _("cannot wait on su shell\n\n"));
ad9577
+
ad9577
+	} while (1);
ad9577
+
ad9577
+	list_for_each(ptr, &consoles) {
ad9577
+		con = list_entry(ptr, struct console, entry);
ad9577
+
ad9577
+		if (con->fd < 0)
ad9577
+			continue;
ad9577
+		if (con->pid < 0)
ad9577
+			continue;
ad9577
+		if (con->pid == status.si_pid)
ad9577
+			con->pid = -1;
ad9577
+		else {
ad9577
+			kill(con->pid, SIGTERM);
ad9577
+			wait++;
ad9577
+		}
ad9577
+	}
ad9577
+
ad9577
+	sigemptyset(&set);
ad9577
+	sigaddset(&set, SIGCHLD);
ad9577
+
ad9577
+	do {
ad9577
+		int signum, ret;
ad9577
+
ad9577
+		if (!wait)
ad9577
+			break;
ad9577
+
ad9577
+		status.si_pid = 0;
ad9577
+		ret = waitid(P_ALL, 0, &status, WEXITED|WNOHANG);
ad9577
+
ad9577
+		if (ret < 0) {
ad9577
+			if (errno == ECHILD)
ad9577
+				break;
ad9577
+			if (errno == EINTR)
ad9577
 				continue;
ad9577
+		}
ad9577
+
ad9577
+		if (!ret && status.si_pid > 0) {
ad9577
+			list_for_each(ptr, &consoles) {
ad9577
+				con = list_entry(ptr, struct console, entry);
ad9577
+
ad9577
+				if (con->fd < 0)
ad9577
+					continue;
ad9577
+				if (con->pid < 0)
ad9577
+					continue;
ad9577
+				if (con->pid == status.si_pid) {
ad9577
+					con->pid = -1;
ad9577
+					wait--;
ad9577
+				}
ad9577
 			}
ad9577
-			if (*usemask & (1<<con->id))
ad9577
-				continue;
ad9577
-			kill(con->pid, SIGHUP);
ad9577
-			usleep(5000);
ad9577
-			kill(con->pid, SIGKILL);
ad9577
+			continue;
ad9577
 		}
ad9577
-	}
ad9577
+
ad9577
+		signum = sigtimedwait(&set, NULL, &sigwait);
ad9577
+		if (signum != SIGCHLD && signum < 0 && errno == EAGAIN)
ad9577
+			break;
ad9577
+
ad9577
+	} while (1);
ad9577
 
ad9577
 	mask_signal(SIGCHLD, SIG_DFL, NULL);
ad9577
 	return EXIT_SUCCESS;
ad9577
-- 
ad9577
2.14.4
ad9577