[Python-Dev] posixmodule.c diffs for working forkpty() and openpty() under Solaris 2.8

Lance Ellinghaus lellinghaus@yahoo.com
Wed, 26 Jun 2002 01:21:35 -0700 (PDT)


--0-68967167-1025079695=:16014
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hello everyone!
I had to get forkpty() and openpty() working under Solaris 2.8 for a
project I am working on.
Here are the diffs to the 2.2.1 source file.

Please let me know if anyone has any problems with this!

Lance Ellinghaus


=====
--
Lance Ellinghaus

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com
--0-68967167-1025079695=:16014
Content-Type: text/plain; name="posixmodule.c.diff"
Content-Description: posixmodule.c.diff
Content-Disposition: inline; filename="posixmodule.c.diff"

*** Python-2.2.1/Modules/posixmodule.c	Tue Mar 12 16:38:31 2002
--- Python-2.2.1.new/Modules/posixmodule.c	Tue May 21 01:16:29 2002
***************
*** 1904,1910 ****
  }
  #endif
  
! #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
  #ifdef HAVE_PTY_H
  #include <pty.h>
  #else
--- 1904,1913 ----
  }
  #endif
  
! #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(sun)
! #ifdef sun
! #include <sys/stropts.h>
! #endif
  #ifdef HAVE_PTY_H
  #include <pty.h>
  #else
***************
*** 1914,1920 ****
  #endif /* HAVE_PTY_H */
  #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
  
! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
  static char posix_openpty__doc__[] =
  "openpty() -> (master_fd, slave_fd)\n\
  Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
--- 1917,1923 ----
  #endif /* HAVE_PTY_H */
  #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
  
! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(sun)
  static char posix_openpty__doc__[] =
  "openpty() -> (master_fd, slave_fd)\n\
  Open a pseudo-terminal, returning open fd's for both master and slave end.\n";
***************
*** 1925,1932 ****
  	int master_fd, slave_fd;
  #ifndef HAVE_OPENPTY
  	char * slave_name;
  #endif
! 
  	if (!PyArg_ParseTuple(args, ":openpty"))
  		return NULL;
  
--- 1928,1941 ----
  	int master_fd, slave_fd;
  #ifndef HAVE_OPENPTY
  	char * slave_name;
+ #ifdef sun
+         void *sig_saved;
  #endif
! #endif
! #if !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) && defined(sun)
!         extern char *ptsname();
! #endif
!         
  	if (!PyArg_ParseTuple(args, ":openpty"))
  		return NULL;
  
***************
*** 1933,1939 ****
  #ifdef HAVE_OPENPTY
  	if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
  		return posix_error();
! #else
  	slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
  	if (slave_name == NULL)
  		return posix_error();
--- 1942,1948 ----
  #ifdef HAVE_OPENPTY
  	if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
  		return posix_error();
! #elif HAVE__GETPTY
  	slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
  	if (slave_name == NULL)
  		return posix_error();
***************
*** 1941,1946 ****
--- 1950,1966 ----
  	slave_fd = open(slave_name, O_RDWR);
  	if (slave_fd < 0)
  		return posix_error();
+ #else
+         master_fd = open("/dev/ptmx", O_RDWR|O_NOCTTY);  /* open master */
+         sig_saved = signal(SIGCHLD, SIG_DFL);
+         grantpt(master_fd);                     /* change permission of   slave */
+         unlockpt(master_fd);                    /* unlock slave */
+         signal(SIGCHLD,sig_saved);
+         slave_name = ptsname(master_fd);         /* get name of slave */
+         slave_fd = open(slave_name, O_RDWR);    /* open slave */
+         ioctl(slave_fd, I_PUSH, "ptem");       /* push ptem */
+         ioctl(slave_fd, I_PUSH, "ldterm");     /* push ldterm*/
+         ioctl(slave_fd, I_PUSH, "ttcompat");     /* push ttcompat*/
  #endif /* HAVE_OPENPTY */
  
  	return Py_BuildValue("(ii)", master_fd, slave_fd);
***************
*** 1948,1954 ****
  }
  #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */
  
! #ifdef HAVE_FORKPTY
  static char posix_forkpty__doc__[] =
  "forkpty() -> (pid, master_fd)\n\
  Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
--- 1968,1974 ----
  }
  #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) */
  
! #if defined(HAVE_FORKPTY) || defined(sun)
  static char posix_forkpty__doc__[] =
  "forkpty() -> (pid, master_fd)\n\
  Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
***************
*** 1959,1968 ****
--- 1979,2067 ----
  posix_forkpty(PyObject *self, PyObject *args)
  {
  	int master_fd, pid;
+ #if defined(sun)
+         int slave;
+ 	char * slave_name;
+         void *sig_saved;
+         int fd;
+ #endif
  	
  	if (!PyArg_ParseTuple(args, ":forkpty"))
  		return NULL;
+ #if defined(sun)
+         master_fd = open("/dev/ptmx", O_RDWR|O_NOCTTY);  /* open master */
+         sig_saved = signal(SIGCHLD, SIG_DFL);
+         grantpt(master_fd);                     /* change permission of   slave */
+         unlockpt(master_fd);                    /* unlock slave */
+         signal(SIGCHLD,sig_saved);
+         slave_name = ptsname(master_fd);         /* get name of slave */
+         slave = open(slave_name, O_RDWR);    /* open slave */
+         ioctl(slave, I_PUSH, "ptem");       /* push ptem */
+         ioctl(slave, I_PUSH, "ldterm");     /* push ldterm*/
+         ioctl(slave, I_PUSH, "ttcompat");     /* push ttcompat*/
+         if (master_fd < 0 || slave < 0)
+         {
+             return posix_error();
+         }
+ 	switch (pid = fork()) {
+ 	case -1:	
+             return posix_error();
+ 	case 0:
+             /* First disconnect from the old controlling tty. */
+ #ifdef TIOCNOTTY
+             fd = open("/dev/tty", O_RDWR | O_NOCTTY);
+             if (fd >= 0) {
+ 		(void) ioctl(fd, TIOCNOTTY, NULL);
+ 		close(fd);
+             }
+ #endif /* TIOCNOTTY */
+             if (setsid() < 0)
+ 		return posix_error();
+             
+             /*
+              * Verify that we are successfully disconnected from the controlling
+              * tty.
+              */
+             fd = open("/dev/tty", O_RDWR | O_NOCTTY);
+             if (fd >= 0) {
+ 		return posix_error();
+ 		close(fd);
+             }
+             /* Make it our controlling tty. */
+ #ifdef TIOCSCTTY
+             if (ioctl(slave, TIOCSCTTY, NULL) < 0)
+ 		return posix_error();
+ #endif /* TIOCSCTTY */
+             fd = open(slave_name, O_RDWR);
+             if (fd < 0) {
+ 		return posix_error();
+             } else {
+ 		close(fd);
+             }
+             /* Verify that we now have a controlling tty. */
+             fd = open("/dev/tty", O_WRONLY);
+             if (fd < 0)
+ 		return posix_error();
+             else {
+ 		close(fd);
+             }
+             (void) close(master_fd);
+             (void) dup2(slave, 0);
+             (void) dup2(slave, 1);
+             (void) dup2(slave, 2);
+             if (slave > 2)
+                 (void) close(slave);
+             pid = 0;
+             break;
+           defautlt:
+             /*
+              * parent
+              */
+             (void) close(slave);
+ 	}
+ #else
  	pid = forkpty(&master_fd, NULL, NULL, NULL);
+ #endif
  	if (pid == -1)
  		return posix_error();
  	if (pid == 0)
***************
*** 5607,5616 ****
  #ifdef HAVE_FORK
  	{"fork",	posix_fork, METH_VARARGS, posix_fork__doc__},
  #endif /* HAVE_FORK */
! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY)
  	{"openpty",	posix_openpty, METH_VARARGS, posix_openpty__doc__},
  #endif /* HAVE_OPENPTY || HAVE__GETPTY */
! #ifdef HAVE_FORKPTY
  	{"forkpty",	posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
  #endif /* HAVE_FORKPTY */
  #ifdef HAVE_GETEGID
--- 5706,5715 ----
  #ifdef HAVE_FORK
  	{"fork",	posix_fork, METH_VARARGS, posix_fork__doc__},
  #endif /* HAVE_FORK */
! #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(sun)
  	{"openpty",	posix_openpty, METH_VARARGS, posix_openpty__doc__},
  #endif /* HAVE_OPENPTY || HAVE__GETPTY */
! #if defined(HAVE_FORKPTY) || defined(sun)
  	{"forkpty",	posix_forkpty, METH_VARARGS, posix_forkpty__doc__},
  #endif /* HAVE_FORKPTY */
  #ifdef HAVE_GETEGID

--0-68967167-1025079695=:16014--