cdown / rpms / util-linux

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