def watch_domain_up(path, xs): #read the domid, see if it's valid th = xs.transaction_start() value = xs.read(th, path) xs.transaction_end(th) if (int(value) > 0): print "Domain "+str(value)+" detected" #setup registration directory for this domid in /malpage/register th = xs.transaction_start() register_path = MONITOR_XS_REGISTER_PATH + "/"+ str(value) print "Creating "+register_path xs.rm(th,register_path) xs.mkdir(th,register_path) #setup watch on new directory print "Setting watch at "+register_path result = xswatch(register_path, watch_domain_register, xs) #<xen.xend.xenstore.xswatch.xswatch instance at 0x7f14e98d9518> Good #set perms of new directory: set_permissions takes a list of three tuples perm_tuple = { "dom":int(value), "read":True , "write":True } xs.set_permissions(th,register_path, [perm_tuple,perm_tuple,perm_tuple]) xs.transaction_end(th) #remove boot-up watch return False return True
def watch_domain_down(path, xs): #read the value, see if it's valid th = xs.transaction_start() value = xs.read(th, path) xs.transaction_end(th) if value == None: print "Domain down, deregistering." ops = Monitor(MONITOR_DEVICE) ops.doMonitorOp(MONITOR_DEREGISTER, domid) ops.close() xswatch(path, watch_domain_up, xs) return False return True
def main(): xs = xshandle() if len(sys.argv) > 1 and sys.argv[1]=='clean': clean(xs) return if len(sys.argv) > 1 and sys.argv[1]=='dump': dump() return th = xs.transaction_start() xs.mkdir(th,MONITOR_XS_REGISTER_PATH) xs.transaction_end(th) for i in range(MONITOR_MIN_DOMID, MONITOR_MAX_DOMID): path = xs.get_domain_path(i) + "/domid" #print "watching "+path xswatch(path, watch_domain_up, xs) while(True): time.sleep(10000)
def watch_domain_register(path, xs): #read the value, see if it's valid global_sem.acquire() th = xs.transaction_start() value = xs.read(th, path) xs.transaction_end(th) global_sem.release() if (len(value) > 0): #delete the node global_sem.acquire() th = xs.transaction_start() print "Removing "+path xs.rm(th,path) xs.transaction_end(th) #notify the Kmod print "Sending registration to Kmod: "+value values = value.split(":") ops = Monitor(MONITOR_DEVICE) procStruct = struct.pack("IIIs",int(values[0]),int(values[1]),int(values[2]),str(values[3])) ops.doMonitorOp(MONITOR_REGISTER, procStruct) ops.close() watch_path = xs.get_domain_path(int(values[0])) + "/domid" print "Watching path for shutdown: "+watch_path xswatch(watch_path, watch_domain_down, xs) #set perms of new directory: set_permissions takes a list of three tuples watch_path = MONITOR_XS_REPORT_PATH+"/"+str(values[0]) watchreport_path = MONITOR_XS_WATCHREPORT_PATH+"/"+str(values[0]) th = xs.transaction_start() perm_tuple = { "dom":int(values[0]), "read":True , "write":True } xs.mkdir(th,watch_path) xs.set_permissions(th,watch_path, [perm_tuple,perm_tuple,perm_tuple]) xs.mkdir(th,watchreport_path) xs.set_permissions(th,watchreport_path, [perm_tuple,perm_tuple,perm_tuple]) xs.transaction_end(th) print "Watching path for report: "+watch_path+str(MONITOR_XS_REPORT_READY_PATH) xswatch(watch_path+MONITOR_XS_REPORT_READY_PATH, watch_domain_report, xs) print "Watching path for watch_report: "+watchreport_path xswatch(watchreport_path+MONITOR_XS_REPORT_READY_PATH, watch_domain_watchreport, xs) print "Domain "+str(value)+" registered" global_sem.release() #remove the watch return False return True