libroot/posix/stdio: Remove unused portions.
[haiku.git] / build / scripts / bootstrap_daemon.py
blobf36df6708a1d582fef86c0bd02cbcef4db4ed3c2
1 #!/usr/bin/env python
3 import socket
4 import subprocess
5 import sys
8 address = '0.0.0.0'
9 port = 4242
12 def receiveExactly(connection, size):
13 data = '';
14 while size > 0:
15 dataReceived = connection.recv(size)
16 if not dataReceived:
17 raise EOFError()
18 data += dataReceived
19 size -= len(dataReceived)
20 return data
23 def handleConnection(listenerSocket):
24 (controlConnection, controlAddress) = listenerSocket.accept()
25 (stdioConnection, stdioAddress) = listenerSocket.accept()
26 (stderrConnection, stderrAddress) = listenerSocket.accept()
28 print 'accepted client connections'
30 try:
31 commandLength = receiveExactly(controlConnection, 8)
32 commandToRun = receiveExactly(controlConnection, int(commandLength))
34 print 'received command: ' + commandToRun
36 exitCode = subprocess.call(commandToRun, stdin=stdioConnection,
37 stdout=stdioConnection, stderr=stderrConnection, shell=True)
39 controlConnection.send(str(exitCode))
40 finally:
41 controlConnection.close()
42 stdioConnection.close()
43 stderrConnection.close()
45 print 'client connections closed'
48 listenerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
49 listenerSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
51 try:
52 listenerSocket.bind((address, port))
53 except socket.error, msg:
54 sys.exit('Failed to bind to %s port %d: %s' % (address, port, msg[1]))
56 listenerSocket.listen(3)
58 print 'started listening on adddress %s port %s' % (address, port)
60 while True:
61 handleConnection(listenerSocket)