debuggers.hg

view tools/python/xen/util/console_client.py @ 3320:7a1b61b77a0b

bitkeeper revision 1.1159.187.67 (41bf7064JLmIpzjd7PLO18qfijLBrA)

Avoid backtraces from Python console client.
author kaf24@scramble.cl.cam.ac.uk
date Tue Dec 14 22:59:48 2004 +0000 (2004-12-14)
parents 236a9f2698a3
children 8724fc0ba838 57a1ea255d3c
line source
1 #!/usr/bin/env python
3 ##############################################
4 # Console client for Xen guest OSes
5 # Copyright (c) 2004, K A Fraser
6 ##############################################
8 import errno, os, signal, socket, struct, sys
10 from termios import *
11 # Indexes into termios.tcgetattr() list.
12 IFLAG = 0
13 OFLAG = 1
14 CFLAG = 2
15 LFLAG = 3
16 ISPEED = 4
17 OSPEED = 5
18 CC = 6
20 def __child_death(signum, frame):
21 global stop
22 stop = True
24 def __recv_from_sock(sock):
25 global stop
26 stop = False
27 while not stop:
28 try:
29 data = sock.recv(1024)
30 except socket.error, error:
31 if error[0] != errno.EINTR:
32 raise
33 try:
34 os.write(1, data)
35 except os.error, error:
36 if error[0] != errno.EINTR:
37 raise
38 os.wait()
40 def __send_to_sock(sock):
41 while 1:
42 try:
43 data = os.read(0,1024)
44 except os.error, error:
45 if error[0] != errno.EINTR:
46 raise
47 else:
48 if ord(data[0]) == ord(']')-64:
49 break
50 try:
51 sock.send(data)
52 except socket.error, error:
53 if error[0] == errno.EPIPE:
54 sys.exit(0)
55 if error[0] != errno.EINTR:
56 raise
57 sys.exit(0)
59 def connect(host,port):
60 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
61 sock.connect((host,port))
63 oattrs = tcgetattr(0)
64 nattrs = tcgetattr(0)
65 nattrs[IFLAG] = nattrs[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON)
66 nattrs[OFLAG] = nattrs[OFLAG] & ~(OPOST)
67 nattrs[CFLAG] = nattrs[CFLAG] & ~(CSIZE | PARENB)
68 nattrs[CFLAG] = nattrs[CFLAG] | CS8
69 nattrs[LFLAG] = nattrs[LFLAG] & ~(ECHO | ICANON | IEXTEN | ISIG)
70 nattrs[CC][VMIN] = 1
71 nattrs[CC][VTIME] = 0
73 if os.fork():
74 signal.signal(signal.SIGCHLD, __child_death)
75 print "************ REMOTE CONSOLE: CTRL-] TO QUIT ********"
76 tcsetattr(0, TCSAFLUSH, nattrs)
77 try:
78 __recv_from_sock(sock)
79 finally:
80 tcsetattr(0, TCSAFLUSH, oattrs)
81 print
82 print "************ REMOTE CONSOLE EXITED *****************"
83 else:
84 signal.signal(signal.SIGPIPE, signal.SIG_IGN)
85 __send_to_sock(sock)
87 if __name__ == '__main__':
88 if len(sys.argv) != 3:
89 print sys.argv[0] + " <host> <port>"
90 sys.exit(1)
91 connect(str(sys.argv[1]),int(sys.argv[2]))