File mingetty-1.0.7s-ctty.dif of Package mingetty

--- mingetty.c
+++ mingetty.c	2008-12-04 12:48:40.618608756 +0100
@@ -301,6 +301,7 @@ static void open_tty (void)
 	sigset_t set;
 	char buf[PATH_MAX+1];
 	int fd, len;
+	pid_t tid;
 	gid_t gid = 0;
 	struct group *gr = NULL;
 
@@ -328,32 +329,40 @@ static void open_tty (void)
 	sa.sa_handler = sigquit_handler;
 	sigaction (SIGQUIT, &sa, NULL);
 
-	/* vhangup() will replace all open file descriptors in the kernel
-	   that point to our controlling tty by a dummy that will deny
-	   further reading/writing to our device. It will also reset the
-	   tty to sane defaults, so we don't have to modify the tty device
-	   for sane settings. We also get a SIGHUP/SIGCONT.
-	 */
-	if ((fd = open (buf, O_RDWR, 0)) < 0)
+	if ((fd = open (buf, O_RDWR|O_NOCTTY, 0)) < 0)
 		error ("%s: cannot open tty: %s", buf, strerror(errno));
 	if (!isatty (fd))
 		error ("%s: not a tty", buf);
+	if (((tid = tcgetsid(fd)) < 0) || (pid != tid)) {
+		if (ioctl (fd, TIOCSCTTY, 1) == -1)
+			error ("%s: cannot get controlling tty: %s", buf, strerror(errno));
+	}
 
 	if (nohangup == 0) {
+		/* vhangup() will replace all open file descriptors in the kernel
+		   that point to our controlling tty by a dummy that will deny
+		   further reading/writing to our device. It will also reset the
+		   tty to sane defaults, so we don't have to modify the tty device
+		   for sane settings. We also get a SIGHUP/SIGCONT.
+		 */
 		if (vhangup ())
 			error ("%s: vhangup() failed", tty);
-		/* Get rid of the present stdout/stderr. */
-		close (2);
-		close (1);
-		close (0);
-		if (fd > 2)
-			close (fd);
+		(void)ioctl (fd, TIOCNOTTY);
 	}
 
-	if ((fd = open (buf, O_RDWR, 0)) < 0)
+	/* Get rid of the present stdout/stderr. */
+	close (2);
+	close (1);
+	close (0);
+	if (fd > 2)
+		close (fd);
+
+	if ((fd = open (buf, O_RDWR|O_NOCTTY, 0)) < 0)
 		error ("%s: cannot open tty: %s", buf, strerror(errno));
-	if (ioctl (fd, TIOCSCTTY, (void *)1) == -1)
-		error ("%s: cannot get controlling tty: %s", buf, strerror(errno));
+	if (((tid = tcgetsid(fd)) < 0) || (pid != tid)) {
+		if (ioctl (fd, TIOCSCTTY, 1) == -1)
+			error ("%s: cannot get controlling tty: %s", buf, strerror(errno));
+	}
 	if (tcsetpgrp(fd, pid))
 		error ("%s: cannot set process group: %s", buf, strerror(errno));
 
openSUSE Build Service is sponsored by