[git commit master] cttyhack: don't do anything if ctty is already available

Denys Vlasenko vda.linux at googlemail.com
Fri May 14 02:08:20 UTC 2010


commit: http://git.busybox.net/busybox/commit/?id=5055a9f98999d3a6c2f4d043a85f6c2d6fb7eaf2
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
cttyhack_main                                        244     269     +25

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/cttyhack.c |   43 ++++++++++++++++++++++++++-----------------
 1 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/shell/cttyhack.c b/shell/cttyhack.c
index 572a3af..bde2acd 100644
--- a/shell/cttyhack.c
+++ b/shell/cttyhack.c
@@ -53,23 +53,32 @@ int cttyhack_main(int argc UNUSED_PARAM, char **argv)
 	}
 
 	strcpy(console, "/dev/tty");
-	if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
-		/* this is a serial console */
-		sprintf(console + 8, "S%d", u.sr.line);
-	} else if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
-		/* this is linux virtual tty */
-		sprintf(console + 8, "S%d" + 1, u.vt.v_active);
-	}
-
-	if (console[8]) {
-		fd = xopen(console, O_RDWR);
-		//bb_error_msg("switching to '%s'", console);
-		dup2(fd, 0);
-		dup2(fd, 1);
-		dup2(fd, 2);
-		while (fd > 2) close(fd--);
-		/* Some other session may have it as ctty. Steal it from them */
-		ioctl(0, TIOCSCTTY, 1);
+	fd = open(console, O_RDWR);
+	if (fd >= 0) {
+		/* We already have ctty, nothing to do */
+		close(fd);
+	} else {
+		/* We don't have ctty (or don't have "/dev/tty" node...) */
+		if (ioctl(0, TIOCGSERIAL, &u.sr) == 0) {
+			/* this is a serial console */
+			sprintf(console + 8, "S%d", u.sr.line);
+		} else if (ioctl(0, VT_GETSTATE, &u.vt) == 0) {
+			/* this is linux virtual tty */
+			sprintf(console + 8, "S%d" + 1, u.vt.v_active);
+		}
+		if (console[8]) {
+			fd = xopen(console, O_RDWR);
+			//bb_error_msg("switching to '%s'", console);
+			dup2(fd, 0);
+			dup2(fd, 1);
+			dup2(fd, 2);
+			while (fd > 2)
+				close(fd--);
+			/* Some other session may have it as ctty,
+			 * steal it from them:
+			 */
+			ioctl(0, TIOCSCTTY, 1);
+		}
 	}
 
 	BB_EXECVP(argv[0], argv);
-- 
1.6.3.3



More information about the busybox-cvs mailing list