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