Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Pseudo terminal utilities.""" |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 2 | |
| 3 | # Bugs: No signal handling. Doesn't set slave termios and window size. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 4 | # Only tested on Linux. |
| 5 | # See: W. Richard Stevens. 1992. Advanced Programming in the |
| 6 | # UNIX Environment. Chapter 19. |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 7 | # Author: Steen Lumholt -- with additions by Guido. |
| 8 | |
| 9 | from select import select |
Fred Drake | 66aaaae | 2001-05-10 05:17:02 +0000 | [diff] [blame] | 10 | import os |
Tim Peters | a059957 | 2001-05-13 09:01:06 +0000 | [diff] [blame] | 11 | |
| 12 | # Absurd: import termios and then delete it. This is to force an attempt |
| 13 | # to import pty to raise an ImportError on platforms that lack termios. |
| 14 | # Without this explicit import of termios here, some other module may |
| 15 | # import tty first, which in turn imports termios and dies with an |
| 16 | # ImportError then. But since tty *does* exist across platforms, that |
| 17 | # leaves a damaged module object for tty in sys.modules, and the import |
| 18 | # of tty here then appears to work despite that the tty imported is junk. |
Tim Peters | 3a2ab1a | 2001-05-29 06:06:54 +0000 | [diff] [blame] | 19 | import termios |
Tim Peters | a059957 | 2001-05-13 09:01:06 +0000 | [diff] [blame] | 20 | del termios |
| 21 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 22 | import tty |
| 23 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 24 | __all__ = ["openpty","fork","spawn"] |
| 25 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 26 | STDIN_FILENO = 0 |
| 27 | STDOUT_FILENO = 1 |
| 28 | STDERR_FILENO = 2 |
| 29 | |
| 30 | CHILD = 0 |
| 31 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 32 | def openpty(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 33 | """openpty() -> (master_fd, slave_fd) |
| 34 | Open a pty master/slave pair, using os.openpty() if possible.""" |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 35 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 36 | try: |
| 37 | return os.openpty() |
| 38 | except (AttributeError, OSError): |
| 39 | pass |
| 40 | master_fd, slave_name = _open_terminal() |
| 41 | slave_fd = slave_open(slave_name) |
| 42 | return master_fd, slave_fd |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 44 | def master_open(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 45 | """master_open() -> (master_fd, slave_name) |
| 46 | Open a pty master and return the fd, and the filename of the slave end. |
| 47 | Deprecated, use openpty() instead.""" |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 48 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 49 | try: |
| 50 | master_fd, slave_fd = os.openpty() |
| 51 | except (AttributeError, OSError): |
| 52 | pass |
| 53 | else: |
| 54 | slave_name = os.ttyname(slave_fd) |
| 55 | os.close(slave_fd) |
| 56 | return master_fd, slave_name |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 57 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 58 | return _open_terminal() |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 59 | |
| 60 | def _open_terminal(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 61 | """Open pty master and return (master_fd, tty_name). |
| 62 | SGI and generic BSD version, for when openpty() fails.""" |
| 63 | try: |
| 64 | import sgi |
| 65 | except ImportError: |
| 66 | pass |
| 67 | else: |
| 68 | try: |
Fred Drake | 66aaaae | 2001-05-10 05:17:02 +0000 | [diff] [blame] | 69 | tty_name, master_fd = sgi._getpty(os.O_RDWR, 0666, 0) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 70 | except IOError, msg: |
| 71 | raise os.error, msg |
| 72 | return master_fd, tty_name |
| 73 | for x in 'pqrstuvwxyzPQRST': |
| 74 | for y in '0123456789abcdef': |
| 75 | pty_name = '/dev/pty' + x + y |
| 76 | try: |
Fred Drake | 66aaaae | 2001-05-10 05:17:02 +0000 | [diff] [blame] | 77 | fd = os.open(pty_name, os.O_RDWR) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 78 | except os.error: |
| 79 | continue |
| 80 | return (fd, '/dev/tty' + x + y) |
| 81 | raise os.error, 'out of pty devices' |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 83 | def slave_open(tty_name): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 84 | """slave_open(tty_name) -> slave_fd |
| 85 | Open the pty slave and acquire the controlling terminal, returning |
| 86 | opened filedescriptor. |
| 87 | Deprecated, use openpty() instead.""" |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 88 | |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 89 | result = os.open(tty_name, os.O_RDWR) |
| 90 | try: |
| 91 | from fcntl import ioctl, I_PUSH |
| 92 | except ImportError: |
| 93 | return result |
| 94 | try: |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 95 | ioctl(result, I_PUSH, "ptem") |
| 96 | ioctl(result, I_PUSH, "ldterm") |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 97 | except IOError: |
| 98 | pass |
| 99 | return result |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 101 | def fork(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 102 | """fork() -> (pid, master_fd) |
| 103 | Fork and make the child a session leader with a controlling terminal.""" |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 104 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 105 | try: |
| 106 | pid, fd = os.forkpty() |
| 107 | except (AttributeError, OSError): |
| 108 | pass |
| 109 | else: |
| 110 | if pid == CHILD: |
| 111 | try: |
| 112 | os.setsid() |
| 113 | except OSError: |
| 114 | # os.forkpty() already set us session leader |
| 115 | pass |
| 116 | return pid, fd |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 117 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 118 | master_fd, slave_fd = openpty() |
| 119 | pid = os.fork() |
| 120 | if pid == CHILD: |
| 121 | # Establish a new session. |
| 122 | os.setsid() |
| 123 | os.close(master_fd) |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 124 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 125 | # Slave becomes stdin/stdout/stderr of child. |
| 126 | os.dup2(slave_fd, STDIN_FILENO) |
| 127 | os.dup2(slave_fd, STDOUT_FILENO) |
| 128 | os.dup2(slave_fd, STDERR_FILENO) |
| 129 | if (slave_fd > STDERR_FILENO): |
| 130 | os.close (slave_fd) |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 131 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 132 | # Parent and child process. |
| 133 | return pid, master_fd |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 134 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 135 | def _writen(fd, data): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 136 | """Write all the data to a descriptor.""" |
| 137 | while data != '': |
| 138 | n = os.write(fd, data) |
| 139 | data = data[n:] |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 140 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 141 | def _read(fd): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 142 | """Default read function.""" |
| 143 | return os.read(fd, 1024) |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 144 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 145 | def _copy(master_fd, master_read=_read, stdin_read=_read): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 146 | """Parent copy loop. |
| 147 | Copies |
| 148 | pty master -> standard output (master_read) |
| 149 | standard input -> pty master (stdin_read)""" |
| 150 | while 1: |
| 151 | rfds, wfds, xfds = select( |
| 152 | [master_fd, STDIN_FILENO], [], []) |
| 153 | if master_fd in rfds: |
| 154 | data = master_read(master_fd) |
| 155 | os.write(STDOUT_FILENO, data) |
| 156 | if STDIN_FILENO in rfds: |
| 157 | data = stdin_read(STDIN_FILENO) |
| 158 | _writen(master_fd, data) |
Guido van Rossum | 23cb2a8 | 1994-09-12 10:36:35 +0000 | [diff] [blame] | 159 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 160 | def spawn(argv, master_read=_read, stdin_read=_read): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 161 | """Create a spawned process.""" |
| 162 | if type(argv) == type(''): |
| 163 | argv = (argv,) |
| 164 | pid, master_fd = fork() |
| 165 | if pid == CHILD: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 166 | os.execlp(argv[0], *argv) |
Martin v. Löwis | 6c611fa | 2002-07-28 09:42:57 +0000 | [diff] [blame] | 167 | try: |
| 168 | mode = tty.tcgetattr(STDIN_FILENO) |
| 169 | tty.setraw(STDIN_FILENO) |
| 170 | restore = 1 |
| 171 | except tty.error: # This is the same as termios.error |
| 172 | restore = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 173 | try: |
| 174 | _copy(master_fd, master_read, stdin_read) |
Martin v. Löwis | 6c611fa | 2002-07-28 09:42:57 +0000 | [diff] [blame] | 175 | except (IOError, OSError): |
| 176 | if restore: |
| 177 | tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) |
Andrew M. Kuchling | a2c9a98 | 2004-06-05 16:27:16 +0000 | [diff] [blame] | 178 | |
| 179 | os.close(master_fd) |