blob: e841f12f3edb9b85cc34891e2a441943f85f6759 [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():
Andrew Kuchlingedf33c02013-06-01 13:52:30 -040050 """Open pty master and return (master_fd, tty_name)."""
Tim Peters2344fae2001-01-15 00:50:52 +000051 for x in 'pqrstuvwxyzPQRST':
52 for y in '0123456789abcdef':
53 pty_name = '/dev/pty' + x + y
54 try:
Fred Drake66aaaae2001-05-10 05:17:02 +000055 fd = os.open(pty_name, os.O_RDWR)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +020056 except OSError:
Tim Peters2344fae2001-01-15 00:50:52 +000057 continue
58 return (fd, '/dev/tty' + x + y)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +020059 raise OSError('out of pty devices')
Guido van Rossum23cb2a81994-09-12 10:36:35 +000060
Guido van Rossum23cb2a81994-09-12 10:36:35 +000061def slave_open(tty_name):
Tim Peters2344fae2001-01-15 00:50:52 +000062 """slave_open(tty_name) -> slave_fd
63 Open the pty slave and acquire the controlling terminal, returning
64 opened filedescriptor.
65 Deprecated, use openpty() instead."""
Fred Drake8cef4cf2000-06-28 16:40:38 +000066
Martin v. Löwis14e73b12003-01-01 09:51:12 +000067 result = os.open(tty_name, os.O_RDWR)
68 try:
69 from fcntl import ioctl, I_PUSH
Brett Cannoncd171c82013-07-04 17:43:24 -040070 except ImportError:
Martin v. Löwis14e73b12003-01-01 09:51:12 +000071 return result
72 try:
Tim Peters2c60f7a2003-01-29 03:49:43 +000073 ioctl(result, I_PUSH, "ptem")
74 ioctl(result, I_PUSH, "ldterm")
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020075 except OSError:
Martin v. Löwis14e73b12003-01-01 09:51:12 +000076 pass
77 return result
Guido van Rossum23cb2a81994-09-12 10:36:35 +000078
Guido van Rossum23cb2a81994-09-12 10:36:35 +000079def fork():
Tim Peters2344fae2001-01-15 00:50:52 +000080 """fork() -> (pid, master_fd)
81 Fork and make the child a session leader with a controlling terminal."""
Fred Drake8cef4cf2000-06-28 16:40:38 +000082
Tim Peters2344fae2001-01-15 00:50:52 +000083 try:
84 pid, fd = os.forkpty()
85 except (AttributeError, OSError):
86 pass
87 else:
88 if pid == CHILD:
89 try:
90 os.setsid()
91 except OSError:
92 # os.forkpty() already set us session leader
93 pass
94 return pid, fd
Fred Drake8cef4cf2000-06-28 16:40:38 +000095
Tim Peters2344fae2001-01-15 00:50:52 +000096 master_fd, slave_fd = openpty()
97 pid = os.fork()
98 if pid == CHILD:
99 # Establish a new session.
100 os.setsid()
101 os.close(master_fd)
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000102
Tim Peters2344fae2001-01-15 00:50:52 +0000103 # Slave becomes stdin/stdout/stderr of child.
104 os.dup2(slave_fd, STDIN_FILENO)
105 os.dup2(slave_fd, STDOUT_FILENO)
106 os.dup2(slave_fd, STDERR_FILENO)
107 if (slave_fd > STDERR_FILENO):
108 os.close (slave_fd)
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000109
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110 # Explicitly open the tty to make it become a controlling tty.
111 tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR)
112 os.close(tmp_fd)
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000113 else:
114 os.close(slave_fd)
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000115
Tim Peters2344fae2001-01-15 00:50:52 +0000116 # Parent and child process.
117 return pid, master_fd
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000118
Fred Drake8cef4cf2000-06-28 16:40:38 +0000119def _writen(fd, data):
Tim Peters2344fae2001-01-15 00:50:52 +0000120 """Write all the data to a descriptor."""
Antoine Pitrou9cadb1b2008-09-15 23:02:56 +0000121 while data:
Tim Peters2344fae2001-01-15 00:50:52 +0000122 n = os.write(fd, data)
123 data = data[n:]
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000124
Fred Drake8cef4cf2000-06-28 16:40:38 +0000125def _read(fd):
Tim Peters2344fae2001-01-15 00:50:52 +0000126 """Default read function."""
127 return os.read(fd, 1024)
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000128
Fred Drake8cef4cf2000-06-28 16:40:38 +0000129def _copy(master_fd, master_read=_read, stdin_read=_read):
Tim Peters2344fae2001-01-15 00:50:52 +0000130 """Parent copy loop.
131 Copies
132 pty master -> standard output (master_read)
133 standard input -> pty master (stdin_read)"""
Gregory P. Smith05f59532012-02-16 00:29:12 -0800134 fds = [master_fd, STDIN_FILENO]
135 while True:
136 rfds, wfds, xfds = select(fds, [], [])
Tim Peters2344fae2001-01-15 00:50:52 +0000137 if master_fd in rfds:
138 data = master_read(master_fd)
Gregory P. Smith05f59532012-02-16 00:29:12 -0800139 if not data: # Reached EOF.
140 fds.remove(master_fd)
141 else:
142 os.write(STDOUT_FILENO, data)
Tim Peters2344fae2001-01-15 00:50:52 +0000143 if STDIN_FILENO in rfds:
144 data = stdin_read(STDIN_FILENO)
Gregory P. Smith05f59532012-02-16 00:29:12 -0800145 if not data:
146 fds.remove(STDIN_FILENO)
147 else:
148 _writen(master_fd, data)
Guido van Rossum23cb2a81994-09-12 10:36:35 +0000149
Fred Drake8cef4cf2000-06-28 16:40:38 +0000150def spawn(argv, master_read=_read, stdin_read=_read):
Tim Peters2344fae2001-01-15 00:50:52 +0000151 """Create a spawned process."""
152 if type(argv) == type(''):
153 argv = (argv,)
154 pid, master_fd = fork()
155 if pid == CHILD:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000156 os.execlp(argv[0], *argv)
Martin v. Löwis6c611fa2002-07-28 09:42:57 +0000157 try:
158 mode = tty.tcgetattr(STDIN_FILENO)
159 tty.setraw(STDIN_FILENO)
160 restore = 1
161 except tty.error: # This is the same as termios.error
162 restore = 0
Tim Peters2344fae2001-01-15 00:50:52 +0000163 try:
164 _copy(master_fd, master_read, stdin_read)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200165 except OSError:
Martin v. Löwis6c611fa2002-07-28 09:42:57 +0000166 if restore:
167 tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)
Andrew M. Kuchlinga2c9a982004-06-05 16:27:16 +0000168
169 os.close(master_fd)
Gregory P. Smith0f21adf72012-09-29 12:41:03 -0700170 return os.waitpid(pid, 0)[1]