| 1 | n/a | """Pseudo terminal utilities.""" | 
|---|
| 2 | n/a |  | 
|---|
| 3 | n/a | # Bugs: No signal handling.  Doesn't set slave termios and window size. | 
|---|
| 4 | n/a | #       Only tested on Linux. | 
|---|
| 5 | n/a | # See:  W. Richard Stevens. 1992.  Advanced Programming in the | 
|---|
| 6 | n/a | #       UNIX Environment.  Chapter 19. | 
|---|
| 7 | n/a | # Author: Steen Lumholt -- with additions by Guido. | 
|---|
| 8 | n/a |  | 
|---|
| 9 | n/a | from select import select | 
|---|
| 10 | n/a | import os | 
|---|
| 11 | n/a | import tty | 
|---|
| 12 | n/a |  | 
|---|
| 13 | n/a | __all__ = ["openpty","fork","spawn"] | 
|---|
| 14 | n/a |  | 
|---|
| 15 | n/a | STDIN_FILENO = 0 | 
|---|
| 16 | n/a | STDOUT_FILENO = 1 | 
|---|
| 17 | n/a | STDERR_FILENO = 2 | 
|---|
| 18 | n/a |  | 
|---|
| 19 | n/a | CHILD = 0 | 
|---|
| 20 | n/a |  | 
|---|
| 21 | n/a | def openpty(): | 
|---|
| 22 | n/a | """openpty() -> (master_fd, slave_fd) | 
|---|
| 23 | n/a | Open a pty master/slave pair, using os.openpty() if possible.""" | 
|---|
| 24 | n/a |  | 
|---|
| 25 | n/a | try: | 
|---|
| 26 | n/a | return os.openpty() | 
|---|
| 27 | n/a | except (AttributeError, OSError): | 
|---|
| 28 | n/a | pass | 
|---|
| 29 | n/a | master_fd, slave_name = _open_terminal() | 
|---|
| 30 | n/a | slave_fd = slave_open(slave_name) | 
|---|
| 31 | n/a | return master_fd, slave_fd | 
|---|
| 32 | n/a |  | 
|---|
| 33 | n/a | def master_open(): | 
|---|
| 34 | n/a | """master_open() -> (master_fd, slave_name) | 
|---|
| 35 | n/a | Open a pty master and return the fd, and the filename of the slave end. | 
|---|
| 36 | n/a | Deprecated, use openpty() instead.""" | 
|---|
| 37 | n/a |  | 
|---|
| 38 | n/a | try: | 
|---|
| 39 | n/a | master_fd, slave_fd = os.openpty() | 
|---|
| 40 | n/a | except (AttributeError, OSError): | 
|---|
| 41 | n/a | pass | 
|---|
| 42 | n/a | else: | 
|---|
| 43 | n/a | slave_name = os.ttyname(slave_fd) | 
|---|
| 44 | n/a | os.close(slave_fd) | 
|---|
| 45 | n/a | return master_fd, slave_name | 
|---|
| 46 | n/a |  | 
|---|
| 47 | n/a | return _open_terminal() | 
|---|
| 48 | n/a |  | 
|---|
| 49 | n/a | def _open_terminal(): | 
|---|
| 50 | n/a | """Open pty master and return (master_fd, tty_name).""" | 
|---|
| 51 | n/a | for x in 'pqrstuvwxyzPQRST': | 
|---|
| 52 | n/a | for y in '0123456789abcdef': | 
|---|
| 53 | n/a | pty_name = '/dev/pty' + x + y | 
|---|
| 54 | n/a | try: | 
|---|
| 55 | n/a | fd = os.open(pty_name, os.O_RDWR) | 
|---|
| 56 | n/a | except OSError: | 
|---|
| 57 | n/a | continue | 
|---|
| 58 | n/a | return (fd, '/dev/tty' + x + y) | 
|---|
| 59 | n/a | raise OSError('out of pty devices') | 
|---|
| 60 | n/a |  | 
|---|
| 61 | n/a | def slave_open(tty_name): | 
|---|
| 62 | n/a | """slave_open(tty_name) -> slave_fd | 
|---|
| 63 | n/a | Open the pty slave and acquire the controlling terminal, returning | 
|---|
| 64 | n/a | opened filedescriptor. | 
|---|
| 65 | n/a | Deprecated, use openpty() instead.""" | 
|---|
| 66 | n/a |  | 
|---|
| 67 | n/a | result = os.open(tty_name, os.O_RDWR) | 
|---|
| 68 | n/a | try: | 
|---|
| 69 | n/a | from fcntl import ioctl, I_PUSH | 
|---|
| 70 | n/a | except ImportError: | 
|---|
| 71 | n/a | return result | 
|---|
| 72 | n/a | try: | 
|---|
| 73 | n/a | ioctl(result, I_PUSH, "ptem") | 
|---|
| 74 | n/a | ioctl(result, I_PUSH, "ldterm") | 
|---|
| 75 | n/a | except OSError: | 
|---|
| 76 | n/a | pass | 
|---|
| 77 | n/a | return result | 
|---|
| 78 | n/a |  | 
|---|
| 79 | n/a | def fork(): | 
|---|
| 80 | n/a | """fork() -> (pid, master_fd) | 
|---|
| 81 | n/a | Fork and make the child a session leader with a controlling terminal.""" | 
|---|
| 82 | n/a |  | 
|---|
| 83 | n/a | try: | 
|---|
| 84 | n/a | pid, fd = os.forkpty() | 
|---|
| 85 | n/a | except (AttributeError, OSError): | 
|---|
| 86 | n/a | pass | 
|---|
| 87 | n/a | else: | 
|---|
| 88 | n/a | if pid == CHILD: | 
|---|
| 89 | n/a | try: | 
|---|
| 90 | n/a | os.setsid() | 
|---|
| 91 | n/a | except OSError: | 
|---|
| 92 | n/a | # os.forkpty() already set us session leader | 
|---|
| 93 | n/a | pass | 
|---|
| 94 | n/a | return pid, fd | 
|---|
| 95 | n/a |  | 
|---|
| 96 | n/a | master_fd, slave_fd = openpty() | 
|---|
| 97 | n/a | pid = os.fork() | 
|---|
| 98 | n/a | if pid == CHILD: | 
|---|
| 99 | n/a | # Establish a new session. | 
|---|
| 100 | n/a | os.setsid() | 
|---|
| 101 | n/a | os.close(master_fd) | 
|---|
| 102 | n/a |  | 
|---|
| 103 | n/a | # Slave becomes stdin/stdout/stderr of child. | 
|---|
| 104 | n/a | os.dup2(slave_fd, STDIN_FILENO) | 
|---|
| 105 | n/a | os.dup2(slave_fd, STDOUT_FILENO) | 
|---|
| 106 | n/a | os.dup2(slave_fd, STDERR_FILENO) | 
|---|
| 107 | n/a | if (slave_fd > STDERR_FILENO): | 
|---|
| 108 | n/a | os.close (slave_fd) | 
|---|
| 109 | n/a |  | 
|---|
| 110 | n/a | # Explicitly open the tty to make it become a controlling tty. | 
|---|
| 111 | n/a | tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR) | 
|---|
| 112 | n/a | os.close(tmp_fd) | 
|---|
| 113 | n/a | else: | 
|---|
| 114 | n/a | os.close(slave_fd) | 
|---|
| 115 | n/a |  | 
|---|
| 116 | n/a | # Parent and child process. | 
|---|
| 117 | n/a | return pid, master_fd | 
|---|
| 118 | n/a |  | 
|---|
| 119 | n/a | def _writen(fd, data): | 
|---|
| 120 | n/a | """Write all the data to a descriptor.""" | 
|---|
| 121 | n/a | while data: | 
|---|
| 122 | n/a | n = os.write(fd, data) | 
|---|
| 123 | n/a | data = data[n:] | 
|---|
| 124 | n/a |  | 
|---|
| 125 | n/a | def _read(fd): | 
|---|
| 126 | n/a | """Default read function.""" | 
|---|
| 127 | n/a | return os.read(fd, 1024) | 
|---|
| 128 | n/a |  | 
|---|
| 129 | n/a | def _copy(master_fd, master_read=_read, stdin_read=_read): | 
|---|
| 130 | n/a | """Parent copy loop. | 
|---|
| 131 | n/a | Copies | 
|---|
| 132 | n/a | pty master -> standard output   (master_read) | 
|---|
| 133 | n/a | standard input -> pty master    (stdin_read)""" | 
|---|
| 134 | n/a | fds = [master_fd, STDIN_FILENO] | 
|---|
| 135 | n/a | while True: | 
|---|
| 136 | n/a | rfds, wfds, xfds = select(fds, [], []) | 
|---|
| 137 | n/a | if master_fd in rfds: | 
|---|
| 138 | n/a | data = master_read(master_fd) | 
|---|
| 139 | n/a | if not data:  # Reached EOF. | 
|---|
| 140 | n/a | fds.remove(master_fd) | 
|---|
| 141 | n/a | else: | 
|---|
| 142 | n/a | os.write(STDOUT_FILENO, data) | 
|---|
| 143 | n/a | if STDIN_FILENO in rfds: | 
|---|
| 144 | n/a | data = stdin_read(STDIN_FILENO) | 
|---|
| 145 | n/a | if not data: | 
|---|
| 146 | n/a | fds.remove(STDIN_FILENO) | 
|---|
| 147 | n/a | else: | 
|---|
| 148 | n/a | _writen(master_fd, data) | 
|---|
| 149 | n/a |  | 
|---|
| 150 | n/a | def spawn(argv, master_read=_read, stdin_read=_read): | 
|---|
| 151 | n/a | """Create a spawned process.""" | 
|---|
| 152 | n/a | if type(argv) == type(''): | 
|---|
| 153 | n/a | argv = (argv,) | 
|---|
| 154 | n/a | pid, master_fd = fork() | 
|---|
| 155 | n/a | if pid == CHILD: | 
|---|
| 156 | n/a | os.execlp(argv[0], *argv) | 
|---|
| 157 | n/a | try: | 
|---|
| 158 | n/a | mode = tty.tcgetattr(STDIN_FILENO) | 
|---|
| 159 | n/a | tty.setraw(STDIN_FILENO) | 
|---|
| 160 | n/a | restore = 1 | 
|---|
| 161 | n/a | except tty.error:    # This is the same as termios.error | 
|---|
| 162 | n/a | restore = 0 | 
|---|
| 163 | n/a | try: | 
|---|
| 164 | n/a | _copy(master_fd, master_read, stdin_read) | 
|---|
| 165 | n/a | except OSError: | 
|---|
| 166 | n/a | if restore: | 
|---|
| 167 | n/a | tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) | 
|---|
| 168 | n/a |  | 
|---|
| 169 | n/a | os.close(master_fd) | 
|---|
| 170 | n/a | return os.waitpid(pid, 0)[1] | 
|---|