S
Stelios Xanthakis
Hi.
I'm trying to open an xterm in slave mode connected
to a pseudo tty with an interactive python shell.
This can be useful when working in a GUI to pop up
xterm'd shells. The program below is a first attempt.
It opens an xterm with an interactive python interpreter
in which we can even run "os.system ('bash')" and
call vi. However there are problems...
------------------------------------------------------
import os
import code
import thread
import time
import sys
STDIN_FILENO = 0
STDOUT_FILENO = 1
STDERR_FILENO = 2
CHILD = 0
def openpty ():
for x in 'pqrstuvwxyzPQRST':
for y in '0123456789abcdef':
pty_name = '/dev/pty' + x + y
try:
fd = os.open(pty_name, os.O_RDWR)
except os.error:
continue
return (fd, '/dev/tty' + x + y)
def slave_open(tty_name):
result = os.open(tty_name, os.O_RDWR)
try:
from fcntl import ioctl, I_PUSH
except ImportError:
return result
try:
ioctl(result, I_PUSH, "ptem")
ioctl(result, I_PUSH, "ldterm")
except IOError:
pass
return result
def fork():
master_fd, slave_fdn = openpty()
slave_fd = slave_open (slave_fdn)
pid = os.fork()
if pid == CHILD:
# Establish a new session.
os.setsid()
os.close(master_fd)
# Slave becomes stdin/stdout/stderr of child.
os.dup2(slave_fd, STDIN_FILENO)
os.dup2(slave_fd, STDOUT_FILENO)
os.dup2(slave_fd, STDERR_FILENO)
if (slave_fd > STDERR_FILENO):
os.close (slave_fd)
# Parent and child process.
return pid, master_fd, slave_fdn
def thrr (cmd):
thread.start_new_thread (os.system, (cmd,))
pid, fd, n = fork ()
if pid > 0:
print 'parent, reading', n[8:]
cmd = 'xterm -S'+n[8:]+str (fd)
thrr (cmd)
time.sleep (3)
print "Send a command to the xterm!"
os.write (fd, 'print "Hi!"\n')
time.sleep (10)
print "leave..."
else:
code.interact ()
print "terminated"
sys.exit (1)
---------------------------------------
This is wrong. The fact that the interactive interpreter is executed in
a different process, means that it has its own data and cannot modify
the main program's state.
If we use threads, because of CLONE_FILES, we can't connect the
thread's stdin/stdout to the pty.
There are other problems, like xterm complaining that it cannot
'stat /dev/pts/p0', how do we kill the xterm once the
code.interact() terminates, zombies, etc.
I'm confused. Ideas?
Stelios
I'm trying to open an xterm in slave mode connected
to a pseudo tty with an interactive python shell.
This can be useful when working in a GUI to pop up
xterm'd shells. The program below is a first attempt.
It opens an xterm with an interactive python interpreter
in which we can even run "os.system ('bash')" and
call vi. However there are problems...
------------------------------------------------------
import os
import code
import thread
import time
import sys
STDIN_FILENO = 0
STDOUT_FILENO = 1
STDERR_FILENO = 2
CHILD = 0
def openpty ():
for x in 'pqrstuvwxyzPQRST':
for y in '0123456789abcdef':
pty_name = '/dev/pty' + x + y
try:
fd = os.open(pty_name, os.O_RDWR)
except os.error:
continue
return (fd, '/dev/tty' + x + y)
def slave_open(tty_name):
result = os.open(tty_name, os.O_RDWR)
try:
from fcntl import ioctl, I_PUSH
except ImportError:
return result
try:
ioctl(result, I_PUSH, "ptem")
ioctl(result, I_PUSH, "ldterm")
except IOError:
pass
return result
def fork():
master_fd, slave_fdn = openpty()
slave_fd = slave_open (slave_fdn)
pid = os.fork()
if pid == CHILD:
# Establish a new session.
os.setsid()
os.close(master_fd)
# Slave becomes stdin/stdout/stderr of child.
os.dup2(slave_fd, STDIN_FILENO)
os.dup2(slave_fd, STDOUT_FILENO)
os.dup2(slave_fd, STDERR_FILENO)
if (slave_fd > STDERR_FILENO):
os.close (slave_fd)
# Parent and child process.
return pid, master_fd, slave_fdn
def thrr (cmd):
thread.start_new_thread (os.system, (cmd,))
pid, fd, n = fork ()
if pid > 0:
print 'parent, reading', n[8:]
cmd = 'xterm -S'+n[8:]+str (fd)
thrr (cmd)
time.sleep (3)
print "Send a command to the xterm!"
os.write (fd, 'print "Hi!"\n')
time.sleep (10)
print "leave..."
else:
code.interact ()
print "terminated"
sys.exit (1)
---------------------------------------
This is wrong. The fact that the interactive interpreter is executed in
a different process, means that it has its own data and cannot modify
the main program's state.
If we use threads, because of CLONE_FILES, we can't connect the
thread's stdin/stdout to the pty.
There are other problems, like xterm complaining that it cannot
'stat /dev/pts/p0', how do we kill the xterm once the
code.interact() terminates, zombies, etc.
I'm confused. Ideas?
Stelios