2014-07-22 17:37:06 -07:00
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import signal
|
2014-08-05 23:28:39 +00:00
|
|
|
import qira_config
|
2014-07-22 17:37:06 -07:00
|
|
|
|
|
|
|
def get_next_run_id():
|
|
|
|
ret = -1
|
2014-08-05 23:28:39 +00:00
|
|
|
for i in os.listdir(qira_config.TRACE_FILE_BASE):
|
2014-07-22 17:37:06 -07:00
|
|
|
if "_" in i:
|
|
|
|
continue
|
|
|
|
ret = max(ret, int(i))
|
|
|
|
return ret+1
|
|
|
|
|
2014-07-25 04:26:15 +00:00
|
|
|
bound_ports = {}
|
2014-07-22 17:37:06 -07:00
|
|
|
|
2014-07-25 04:26:15 +00:00
|
|
|
def start_bindserver(program, port, parent_id, start_cl, loop = False):
|
|
|
|
if port not in bound_ports:
|
|
|
|
myss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
myss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2014-09-07 18:31:56 -04:00
|
|
|
myss.bind((qira_config.HOST, port))
|
2014-07-25 04:26:15 +00:00
|
|
|
myss.listen(5)
|
|
|
|
bound_ports[port] = myss
|
|
|
|
else:
|
|
|
|
myss = bound_ports[port]
|
|
|
|
|
2014-07-22 17:37:06 -07:00
|
|
|
if os.fork() != 0:
|
|
|
|
return
|
|
|
|
# bindserver runs in a fork
|
|
|
|
while 1:
|
|
|
|
print "**** listening on",myss
|
|
|
|
(cs, address) = myss.accept()
|
|
|
|
|
|
|
|
# fork off the child if we are looping
|
|
|
|
if loop:
|
|
|
|
if os.fork() != 0:
|
|
|
|
cs.close()
|
|
|
|
continue
|
|
|
|
run_id = get_next_run_id()
|
|
|
|
print "**** ID",run_id,"CLIENT",cs, address, cs.fileno()
|
|
|
|
|
|
|
|
fd = cs.fileno()
|
|
|
|
# python nonblocking is a lie...
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
2014-08-05 22:12:01 +00:00
|
|
|
try:
|
|
|
|
import fcntl
|
|
|
|
fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL, 0) & ~os.O_NONBLOCK)
|
|
|
|
except:
|
|
|
|
pass
|
2014-07-22 17:37:06 -07:00
|
|
|
os.dup2(fd, 0)
|
|
|
|
os.dup2(fd, 1)
|
|
|
|
os.dup2(fd, 2)
|
|
|
|
for i in range(3, fd+1):
|
|
|
|
try:
|
|
|
|
os.close(i)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
# fingerprint here
|
2014-08-06 12:46:33 +00:00
|
|
|
program.execqira(["-qirachild", "%d %d %d" % (parent_id, start_cl, run_id)], shouldfork=False)
|
2014-07-22 17:37:06 -07:00
|
|
|
|