This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: /dev/ptmx support for ptys (cygwin)
Type: Stage:
Components: Extension Modules Versions:
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: nnorwitz Nosy List: loewis, nnorwitz, z3p
Priority: normal Keywords: patch

Created on 2002-12-20 01:38 by z3p, last changed 2022-04-10 16:06 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
cygwin_pty.patch z3p, 2002-12-20 01:42 patch against Python CVS
cygwin_pty_2.patch z3p, 2002-12-21 18:23 patch, version 2
cygwin_pty_3.diff z3p, 2002-12-27 19:13 patch, version 3
Messages (19)
msg42033 - (view) Author: Paul Swartz (z3p) Date: 2002-12-20 01:38
On Cygwin (and I believe Solaris, but I'm not sure), ptys 
are allocated using /dev/ptmx.  This patch checks 
for /dev/ptmx, and if it exists and openpty and _getpty 
don't, it will open ptys using /dev/ptmx.
msg42034 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-12-21 11:41
Logged In: YES 
user_id=21627

How does this compare to #579433, in your opinion?

I like that this patch uses autoconf for ptmx usage, but I 
wonder whether the various pushes are necessary in the 
other patch.

Please use #elif instead of nested #ifs where possible.
msg42035 - (view) Author: Paul Swartz (z3p) Date: 2002-12-21 16:49
Logged In: YES 
user_id=123843

I took a look at the other patch, and back at the OpenSSH 
pty code (where I got the idea).  The pushes are in an '#ifndef 
CYGWIN' in OpenSSH, and the ioctl for ttcompant is in 
an '#ifndef _hpux'.  Not changing the SIGCHLD handler didn't 
break the cygwin code, but I doubt adding that would break 
things.  I'll work on combining the two, and upload another 
patch.  I'll also fix the nested #ifs.
msg42036 - (view) Author: Paul Swartz (z3p) Date: 2002-12-21 18:23
Logged In: YES 
user_id=123843

I reconciled the openpty parts of the two patches, and have 
attached a new patch.  The only thing I wonder about is if 
there is a better way to check for Cygwin in posixmodule.  
Adding an extra define seems like the wrong way.
msg42037 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-12-27 10:25
Logged In: YES 
user_id=21627

It is common in Python to check for cygwin by checking
whether __CYGWIN__ is defined, so there is no need to invent
another mechanism.

Also, could you work out patches for forkpty as well?
msg42038 - (view) Author: Paul Swartz (z3p) Date: 2002-12-27 19:13
Logged In: YES 
user_id=123843

Fixed to use __CYGWIN__.  I don't think doing forkpty in the 
same way is the right idea, given that pty.fork() does the 
same thing and is the "right" way to fork with ptys.  The 
openpty change is necessary because there is no way 
duplicate it from pure-python, whereas forkpty can be 
duplicated with openpty/fork.
msg42039 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-12-31 12:57
Logged In: YES 
user_id=21627

Thanks for the patch. Committed as

configure 1.371
configure.in 1.382
pyconfig.h.in 1.67
ACKS 1.221
NEWS 1.581
posixmodule.c 2.277
msg42040 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-12-31 17:18
Logged In: YES 
user_id=33168

Paul, I had to modify the patch a bit to get posixmodule.c
to compile on HPUX11.  HPUX also needs sys/stropts.h for
I_PUSH and the macro is __hpux, not _hpux.

However, after making these changes, test_openpty fails: 
'Master-end of pty is not a terminal' and test_pty hangs
when doing:  if not os.isatty(slave_fd)

Any ideas?
msg42041 - (view) Author: Paul Swartz (z3p) Date: 2002-12-31 17:48
Logged In: YES 
user_id=123843

The only thing that I can think of to try is on the slave end:

fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
if fd:
    fnctl.ioctl(fd, tty.TIOCNOTTY, '')
    close(fd)
fcntl.ioctl(slavefd, tty.TIOCSCTTY, '')

That should disconnect the slave from it's old controlling TTY 
and reconnect it to the new TTY.  (I'm not sure if it will come 
through, lines 3 and 4 are indented.)  Past this, I have no 
idea, as it works for me.

As for HPUX also needing sys/stropts.h, maybe adding a 
check for that file is a better idea then checking for sun/hpux.
msg42042 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-12-31 17:52
Logged In: YES 
user_id=21627

I get the same on Solaris. Could it be that the test is
broken? Why should the master end be itself a terminal?
msg42043 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-12-31 17:57
Logged In: YES 
user_id=33168

The code seems to work, I can believe it's the test.  I
don't understand why isatty(slave_fd) hangs though.  Paul's
code is the same as the HPUX man page, with one exception
the man page doesn't use O_NOCTTY.  However, removing this
flag makes no difference.

Should we use O_NONBLOCK?  I haven't tried it.
msg42044 - (view) Author: Paul Swartz (z3p) Date: 2002-12-31 18:10
Logged In: YES 
user_id=123843

The code comes from the OpenSSH pty handling code.  I 
can't see why isatty would be hanging, but I don't think 
O_NONBLOCK can hurt.
msg42045 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-12-31 18:14
Logged In: YES 
user_id=21627

Ok, I have removed the test for the master from test_openpty 
1.8. As for the test hanging: can you perform strace/truss on 
that machine to see what it is doing? On Solaris, isatty 
translates to ioctl(fd, TCGETA, struct termio); a terminal is a 
file that supports this ioctl.

If we need a quick work-around for 2.3a1, I suggest to skip 
that test temporarily for hpux. I would not like to backout this 
change, as the code appears to be basically correct.
msg42046 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-12-31 18:21
Logged In: YES 
user_id=33168

Martin, I agree with everything you said.  I'm commenting
out the test for now.
msg42047 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-12-31 20:26
Logged In: YES 
user_id=33168

Here's what I've found so far.

The test does: pty.master_open() which does an openpty(),
but closes the slave_fd.  The test then reopens the slave by
doing pty.slave_open().  Calling isatty() on this fd causes
HPUX to hang.  Note:  master_open() and slave_open have been
deprecated since 2.0, if I'm reading CVS logs properly (1.4
of pty.py).  Neither is documented.

I have no idea specifically why the hang occurs.  There's no
strace/truss installed for tracing syscalls.  From gdb,
here's the stack trace:

#0  0x77f9e548 in _ioctl_sys () from /usr/lib/libc.2
#1  0x77f282f4 in isatty () from /usr/lib/libc.2
#2  0x14d484 in posix_isatty (self=0x0, args=0x4004fbd0)
    at ./Modules/posixmodule.c:5414


Using openpty() works just fine.  I'm not sure how to
proceed.  One option is to remove master_open() and
slave_open().  I don't know if this is good or not.  We can
certainly skip the isatty() part of the test on HPUX.
msg42048 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-12-31 21:07
Logged In: YES 
user_id=21627

What is the ttyname returned from master_open?

On Solaris, I noticed that the isatty call also fails, and
the device name is /dev/pts/<n>, where n in a number.
pts(7D) reveals that merely opening that device is not
sufficient: you also need to push "ptem" and "ldterm". In
fact, pushing ptem alone is sufficient to make isatty succeed.

Could you try  whether pushing those drivers in slave_open
changes the behaviour? Exposing I_PUSH isn't strictly
necessary for this; I_PUSH = ((ord('S')<<8)|2) worked fine
for me (after reading stropts.h).

If that works, I think we should expose the entire family of
I_ constants in fcntl, and try pushing the drivers in
slave_open; if I_PUSH is not present or pusing a driver
fails, we return the "raw" file descriptor.
msg42049 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-12-31 22:36
Logged In: YES 
user_id=33168

ttyname is similar: /dev/pts/6

I verified that I_PUSH is also (ord('S')<<8)|2

I tried doing this:

  import pty
  master_fd, slave_name = pty.master_open()
  slave_fd = pty.slave_open(slave_name)
  fcntl.ioctl.(slave_fd, ((ord('S')<<8)|2), "ptem")


But after the ioctl, I got:
  IOError: [Errno 22] Invalid argument


Is this what you were doing?  Does this work for you?  If
it's easier, we can do this through email too.
msg42050 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2003-01-01 09:53
Logged In: YES 
user_id=21627

I have now changed slave_open to push ptem and ldterm, and
autodetect stropts.h (which is the POSIX name of that
header), in

configure 1.372
configure.in 1.383
pyconfig.h.in 1.68
pty.py 1.13
NEWS 1.587
fcntlmodule.c 2.37
posixmodule.c 2.280
msg42051 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-01-01 14:54
Logged In: YES 
user_id=33168

Excellent Martin!  With your last set of changes, the hang
is gone on HPUX.  I'm reverting the change to test_pty which
commented out the call to isatty().

test_pty.py 1.17
History
Date User Action Args
2022-04-10 16:06:02adminsetgithub: 37631
2002-12-20 01:38:47z3pcreate