From 126b2adbd0b66ac4553654b1291752bad0bd610c Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Mon, 18 Jul 2011 18:38:56 -0400 Subject: [PATCH] New script for port reservation. Use with the vsys_portreservation attribute --- exec/claimport | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 exec/claimport diff --git a/exec/claimport b/exec/claimport new file mode 100644 index 0000000..ac1fdd3 --- /dev/null +++ b/exec/claimport @@ -0,0 +1,45 @@ +#!/usr/bin/python + +import sys +import os +import string + +vsys_config_dir = "/etc/planetlab/vsys-attributes" +slicename=sys.argv[1] +port_config=os.path.join(vsys_config_dir,slicename,"vsys_portreservation") + +ports = open(port_config).read().rstrip() +(low,high) = map(lambda n:int(n),ports.split('-')) + +arglines = map(string.strip, sys.stdin.readlines()) +if len(arglines)<1: + print >>sys.stderr, "Insufficient argument lines." + sys.exit(1) + +port = int(arglines[0]) # interface name + +def do_kill(p): + if (p): + slice_info = os.popen('chcontext --ctx 1 cat /proc/%s/vinfo'%p).readlines() + xid_info = slice_info[0] + slice_id = xid_info.split()[1] + os.system("vkill %s"%p) + slice = os.popen('getent passwd %s'%slice_id).read().split(':')[0] + mail_cmd = 'mail -s "PlanetLab notification" %s@slices.planet-lab.org -c %s@slices.planet-lab.org'%(slice,slicename) + f = os.popen(mail_cmd,'w') + f.write('Hi,\nOne of your processes on PlanetLab was terminated because you were using port %s, which is assigned to %s.'%(port,slicename)) + f.close() + + +if (low<=port<=high): + fuser_cmd = "ncontext --nid 1 --migrate -- chcontext --ctx 1 fuser -n tcp %d 2> /dev/null"%port + process_udp = os.popen(fuser_cmd).read().rstrip() + + fuser_cmd = "ncontext --nid 1 --migrate -- chcontext --ctx 1 fuser -n udp %d 2> /dev/null"%port + process_tcp = os.popen(fuser_cmd).read().rstrip() + + do_kill(process_tcp.lstrip()) + do_kill(process_udp.lstrip()) + +else: + print "%d is not in your range"%port -- 2.43.0