Loading...
util/pty.c Libc-498 Libc-262
--- Libc/Libc-498/util/pty.c
+++ Libc/Libc-262/util/pty.c
@@ -1,22 +1,21 @@
 /*
- * Copyright (c) 1999, 2007 Apple Inc. All rights reserved.
+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
+ * The contents of this file constitute Original Code as defined in and
+ * are subject to the Apple Public Source License Version 1.1 (the
+ * "License").  You may not use this file except in compliance with the
+ * License.  Please obtain a copy of the License at
+ * http://www.apple.com/publicsource and read it before using this file.
  * 
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * This Original Code and all software distributed under the License are
+ * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
- * limitations under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
+ * License for the specific language governing rights and limitations
+ * under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
@@ -66,10 +65,6 @@
 #include <string.h>
 #include <termios.h>
 #include <unistd.h>
-#include <util.h>
-#include <syslog.h>
-
-static char ptytemplate[] = "/dev/ptyXX";
 
 int openpty(amaster, aslave, name, termp, winp)
 	int *amaster, *aslave;
@@ -77,29 +72,50 @@
 	struct termios *termp;
 	struct winsize *winp;
 {
-	int master, slave;
-	char *sname;
+	static char line[] = "/dev/ptyXX";
+	register const char *cp1, *cp2;
+	register int master, slave, ttygid;
+	struct group *gr;
 
-	if ((master = posix_openpt(O_RDWR|O_NOCTTY)) < 0)
-		return -1;
-	if (grantpt(master) < 0 || unlockpt(master) < 0
-	    || (sname = ptsname(master)) == NULL
-	    || (slave = open(sname, O_RDWR|O_NOCTTY, 0)) < 0) {
-		(void) close(master);
-		return -1;
+	if ((gr = getgrnam("tty")) != NULL)
+		ttygid = gr->gr_gid;
+	else
+		ttygid = -1;
+
+	for (cp1 = "pqrstuvwxy"; *cp1; cp1++) {
+		line[8] = *cp1;
+		for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
+			line[5] = 'p';
+			line[9] = *cp2;
+			if ((master = open(line, O_RDWR, 0)) == -1) {
+				if (errno == ENOENT)
+					return (-1);	/* out of ptys */
+			} else {
+				line[5] = 't';
+				(void) chown(line, getuid(), ttygid);
+				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
+				(void) revoke(line);
+				if ((slave = open(line, O_RDWR, 0)) != -1) {
+					*amaster = master;
+					*aslave = slave;
+					if (name)
+						strcpy(name, line);
+					if (termp)
+						(void) tcsetattr(slave, 
+							TCSAFLUSH, termp);
+					if (winp)
+						(void) ioctl(slave, TIOCSWINSZ, 
+							(char *)winp);
+					return (0);
+				}
+				(void) close(master);
+			}
+		}
 	}
-	*amaster = master;
-	*aslave = slave;
-	if (name)
-		strcpy(name, sname);
-	if (termp)
-		(void) tcsetattr(slave, TCSAFLUSH, termp);
-	if (winp)
-		(void) ioctl(slave, TIOCSWINSZ, (char *)winp);
-	return (0);
+	errno = ENOENT;	/* out of ptys */
+	return (-1);
 }
 
-int
 forkpty(amaster, name, termp, winp)
 	int *amaster;
 	char *name;
@@ -118,19 +134,7 @@
 		 * child
 		 */
 		(void) close(master);
-		/*
-		 * 4300297: login_tty() may fail to set the controlling tty.
-		 * Since we have already forked, the best we can do is to 
-		 * dup the slave as if login_tty() succeeded.
-		 */
-		if (login_tty(slave) < 0) {
-			syslog(LOG_ERR, "forkpty: login_tty could't make controlling tty");
-			(void) dup2(slave, 0);
-			(void) dup2(slave, 1);
-			(void) dup2(slave, 2);
-			if (slave > 2)
-				(void) close(slave);
-		}
+		login_tty(slave);
 		return (0);
 	}
 	/*