blob: 487382725e21a32e6c3d99adc10bdf19b13bfd24 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Pseudo terminal utilities."""
Guido van Rossum23cb2a81994-09-12 10:36:35 +00002
3# Bugs: No signal handling. Doesn't set slave termios and window size.
Tim Peters2344fae2001-01-15 00:50:52 +00004# Only tested on Linux.
5# See: W. Richard Stevens. 1992. Advanced Programming in the
6# UNIX Environment. Chapter 19.
Guido van Rossum23cb2a81994-09-12 10:36:35 +00007# Author: Steen Lumholt -- with additions by Guido.
8
9from select import select
Fred Drake66aaaae2001-05-10 05:17:02 +000010import os
Guido van Rossum23cb2a81994-09-12 10:36:35 +000011import tty
12
Skip Montanaroc62c81e2001-02-12 02:00:42 +000013__all__ = ["openpty","fork","spawn"]
14
Guido van Rossum23cb2a81994-09-12 10:36:35 +000015STDIN_FILENO = 0
16STDOUT_FILENO = 1
17STDERR_FILENO = 2
18
19CHILD = 0
20
Fred Drake8cef4cf2000-06-28 16:40:38 +000021def openpty():
Tim Peters2344fae2001-01-15 00:50:52 +000022 """openpty() -> (master_fd, slave_fd)
23 Open a pty master/slave pair, using os.openpty() if possible."""
Fred Drake8cef4cf2000-06-28 16:40:38 +000024
Tim Peters2344fae2001-01-15 00:50:52 +000025 try:
26 return os.openpty()
27 except (AttributeError, OSError):
28 pass
29 master_fd, slave_name = _open_terminal()
30 slave_fd = slave_open(slave_name)
31 return master_fd, slave_fd
Fred Drake8cef4cf2000-06-28 16:40:38 +000032
Guido van Rossum23cb2a81994-09-12 10:36:35 +000033def master_open():
Tim Peters2344fae2001-01-15 00:50:52 +000034 """master_open() -> (master_fd, slave_name)
35 Open a pty master and return the fd, and the filename of the slave end.
36 Deprecated, use openpty() instead."""
Fred Drake8cef4cf2000-06-28 16:40:38 +000037
Tim Peters2344fae2001-01-15 00:50:52 +000038 try:
39 master_fd, slave_fd = os.openpty()
40 except (AttributeError, OSError):
41 pass
42 else:
43 slave_name = os.ttyname(slave_fd)
44 os.close(slave_fd)
45 return master_fd, slave_name
Fred Drake8cef4cf2000-06-28 16:40:38 +000046
Tim Peters2344fae2001-01-15 00:50:52 +000047 return _open_terminal()
Fred Drake8cef4cf2000-06-28 16:40:38 +000048
49def _open_terminal():
Tim Peters2344fae2001-01-15 00:50:52 +000050 """Open pty master and return (master_fd, tty_name).
51 SGI and generic BSD version, for when openpty() fails."""
52 try:
53 import sgi
54 except ImportError:
55 pass
56 else:
57 try:
Guido van Rossumcd16bf62007-06-13 18:07:49 +000058 tty_name, master_fd = sgi._getpty(os.O_RDWR, 0o666, 0)
Guido van Rossumb940e112007-01-10 16:19:56 +000059 except IOError as msg:
Collin Winterce36ad82007-08-30 01:19:48 +000060 raise os.error(msg)
Tim Peters2344fae2001-01-15 00:50:52 +000061 return master_fd, tty_name
62 for x in 'pqrstuvwxyzPQRST':
63 for y in '0123456789abcdef':
64 pty_name = '/dev/pty' + x + y
65 try:
Fred Drake66aaaae2001-05-10 05:17:02 +000066 fd = os.open(pty_name, os.O_RDWR)
Tim Peters2344fae2001-01-15 00:50:52 +000067 except os.error:
68 continue
69 return (fd, '/dev/tty' + x + y)
Collin Winterce36ad82007-08-30 01:19:48 +000070 raise os.error('out of pty devices')
Guido van Rossum23cb2a81994-09-12 10:36:35 +000071
Guido van Rossum23cb2a81994-09-12 10:36:35 +000072def slave_open(tty_name):
Tim Peters2344fae2001-01-15 00:50:52 +000073 """slave_open(tty_name) -> slave_fd
74 Open the pty slave and acquire the controlling terminal, returning
75 opened filedescriptor.
76 Deprecated, use openpty() instead."""
Fred Drake8cef4cf2000-06-28 16:40:38 +000077
Martin v. Löwis14e73b12003-01-01 09:51:12 +000078 result = os.open(tty_name, os.O_RDWR)
79 try:
80 from fcntl import ioctl, I_PUSH
81 except ImportError:
82 return result
83 try:
Tim Peters2c60f7a2003-01-29 03:49:43 +000084 ioctl(result, I_PUSH, "ptem")
85 ioctl(result, I_PUSH, "ldterm")
Martin v. Löwis14e73b12003-01-01 09:51:12 +000086 except IOError:
87 pass
88 return result
Guido van Rossum23cb2a81994-09-12 10:36:35 +000089
Guido van Rossum23cb2a81994-09-12 10:36:35 +000090def fork():
Tim Peters2344fae2001-01-15 00:50:52 +000091 """fork() -> (pid, master_fd)
92 Fork and make the child a session leader with a controlling terminal."""
Fred Drake8cef4cf2000-06-28 16:40:38 +000093
Tim Peters2344fae2001-01-15 00:50:52 +000094 try:
95 pid, fd = os.forkpty()
96 except (AttributeError, OSError):
97 pass
98 else:
99 if pid == CHILD:
100 try:
101 os.setsid()
102 except OSError:
103 # os.forkpty() already set us session leader
104 pass
105 return pid, fd
Fred Drake8cef4cf2000-06-28 16:40:38 +0000106
Tim Peters2344fae2001-01-15 00:50:52 +0000107 master_fd, slave_fd = openpty()
108 pid = os.fork()
109 if pid == CHILD:
110 # Establish a new session.
111 os.setsid()
112 os.close(master_fd)
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000113
Tim Peters2344fae2001-01-15 00:50:52 +0000114 # Slave becomes stdin/stdout/stderr of child.
115 os.dup2(slave_fd, STDIN_FILENO)
116 os.dup2(slave_fd, STDOUT_FILENO)
117 os.dup2(slave_fd, STDERR_FILENO)
118 if (slave_fd > STDERR_FILENO):
119 os.close (slave_fd)
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000120
Thomas Wouters89f507f2006-12-13 04:49:30 +0000121 # Explicitly open the tty to make it become a controlling tty.
122 tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR)
123 os.close(tmp_fd)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000124 else:
125 os.close(slave_fd)
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000126
Tim Peters2344fae2001-01-15 00:50:52 +0000127 # Parent and child process.
128 return pid, master_fd
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000129
Fred Drake8cef4cf2000-06-28 16:40:38 +0000130def _writen(fd, data):
Tim Peters2344fae2001-01-15 00:50:52 +0000131 """Write all the data to a descriptor."""
132 while data != '':
133 n = os.write(fd, data)
134 data = data[n:]
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000135
Fred Drake8cef4cf2000-06-28 16:40:38 +0000136def _read(fd):
Tim Peters2344fae2001-01-15 00:50:52 +0000137 """Default read function."""
138 return os.read(fd, 1024)
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000139
Fred Drake8cef4cf2000-06-28 16:40:38 +0000140def _copy(master_fd, master_read=_read, stdin_read=_read):
Tim Peters2344fae2001-01-15 00:50:52 +0000141 """Parent copy loop.
142 Copies
143 pty master -> standard output (master_read)
144 standard input -> pty master (stdin_read)"""
145 while 1:
146 rfds, wfds, xfds = select(
147 [master_fd, STDIN_FILENO], [], [])
148 if master_fd in rfds:
149 data = master_read(master_fd)
150 os.write(STDOUT_FILENO, data)
151 if STDIN_FILENO in rfds:
152 data = stdin_read(STDIN_FILENO)
153 _writen(master_fd, data)
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000154
Fred Drake8cef4cf2000-06-28 16:40:38 +0000155def spawn(argv, master_read=_read, stdin_read=_read):
Tim Peters2344fae2001-01-15 00:50:52 +0000156 """Create a spawned process."""
157 if type(argv) == type(''):
158 argv = (argv,)
159 pid, master_fd = fork()
160 if pid == CHILD:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000161 os.execlp(argv[0], *argv)
Martin v. Löwis6c611fa2002-07-28 09:42:57 +0000162 try:
163 mode = tty.tcgetattr(STDIN_FILENO)
164 tty.setraw(STDIN_FILENO)
165 restore = 1
166 except tty.error: # This is the same as termios.error
167 restore = 0
Tim Peters2344fae2001-01-15 00:50:52 +0000168 try:
169 _copy(master_fd, master_read, stdin_read)
Martin v. Löwis6c611fa2002-07-28 09:42:57 +0000170 except (IOError, OSError):
171 if restore:
172 tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)
Andrew M. Kuchlinga2c9a982004-06-05 16:27:16 +0000173
174 os.close(master_fd)