3c467d97ca1a7fc24c405976a1430fe1654bb438
[vsys-scripts.git] / root-context / exec / claimport
1 #!/usr/bin/python
2 # Lets a user kill processes bound to
3 # ports in his assigned port range. To assign a port
4 # range, set the vsys_portreservation attribute for that user.
5 # e.g. vsys_portreservation => 61000-61005
6
7 import sys
8 import os
9 import string
10
11 vsys_config_dir = "/etc/planetlab/vsys-attributes"
12 slicename=sys.argv[1]
13 port_config=os.path.join(vsys_config_dir,slicename,"vsys_portreservation")
14
15 ports = open(port_config).read().rstrip()
16 (low,high) = map(lambda n:int(n),ports.split('-'))
17
18 arglines = map(string.strip, sys.stdin.readlines())
19 if len(arglines)<1:
20     print >>sys.stderr, "Insufficient argument lines."
21     sys.exit(1)
22
23 port = int(arglines[0]) # interface name
24
25 def do_kill(p):
26    if (p):
27        slice_info = os.popen('chcontext --ctx 1 cat /proc/%s/vinfo'%p).readlines()
28        xid_info = slice_info[0]
29        slice_id = xid_info.split()[1]
30        os.system("vkill %s"%p)
31        slice = os.popen('getent passwd %s'%slice_id).read().split(':')[0]
32        mail_cmd = 'mail -s "PlanetLab notification" %s@slices.planet-lab.org -c %s@slices.planet-lab.org'%(slice,slicename)
33        f = os.popen(mail_cmd,'w')
34        f.write('Hi,\nOne of your processes on PlanetLab was terminated because you were using port %s, which is assigned to %s.'%(port,slicename))
35        f.close()
36
37
38 if (low<=port<=high):
39    fuser_cmd = "ncontext --nid 1 --migrate -- chcontext --ctx 1 fuser -n tcp %d 2> /dev/null"%port
40    process_udp = os.popen(fuser_cmd).read().rstrip()
41
42    fuser_cmd = "ncontext --nid 1 --migrate -- chcontext --ctx 1 fuser -n udp %d 2> /dev/null"%port
43    process_tcp = os.popen(fuser_cmd).read().rstrip()
44
45    do_kill(process_tcp.lstrip())
46    do_kill(process_udp.lstrip())
47    
48 else:
49     print "%d is not in your range"%port