1 # Copyright 2005 Princeton University
12 from util_vserver_vars import *
14 CAP_SAFE = (linuxcaps.CAP_CHOWN |
15 linuxcaps.CAP_DAC_OVERRIDE |
16 linuxcaps.CAP_DAC_READ_SEARCH |
17 linuxcaps.CAP_FOWNER |
18 linuxcaps.CAP_FSETID |
20 linuxcaps.CAP_SETGID |
21 linuxcaps.CAP_SETUID |
22 linuxcaps.CAP_SETPCAP |
23 linuxcaps.CAP_SYS_TTY_CONFIG |
25 linuxcaps.CAP_SYS_CHROOT |
26 linuxcaps.CAP_SYS_PTRACE)
29 # these are the flags taken from the kernel linux/vserver/legacy.h
32 FLAGS_SCHED = 2 # XXX - defined in util-vserver/src/chcontext.c
44 def __init__(self, name):
47 self.config = self.__read_config_file("/etc/vservers.conf")
48 self.config.update(self.__read_config_file("/etc/vservers/%s.conf" %
51 flags = self.config["S_FLAGS"].split(" ")
53 self.flags |= FLAGS_LOCK
55 self.flags |= FLAGS_NPROC
56 self.remove_caps = ~CAP_SAFE
57 self.ctx = int(self.config["S_CONTEXT"])
59 config_var_re = re.compile(r"^ *([A-Z_]+)=(.*)\n?$", re.MULTILINE)
61 def __read_config_file(self, filename):
63 f = open(filename, "r")
67 for m in self.config_var_re.finditer(data):
68 (key, val) = m.groups()
69 config[key] = val.strip('"')
72 def __do_chroot(self):
74 return os.chroot("%s/%s" % (VROOTDIR, self.name))
76 def open(self, filename, mode = "r"):
78 (sendsock, recvsock) = passfdimpl.socketpair()
84 f = open(filename, mode)
85 passfdimpl.sendmsg(f.fileno(), sendsock)
87 except EnvironmentError, ex:
88 (result, errmsg) = (ex.errno, ex.strerror)
90 (result, errmsg) = (255, str(ex))
91 os.write(sendsock, errmsg)
96 # XXX - need this since a lambda can't raise an exception
101 throw = lambda : __throw(Exception(errmsg))
104 (pid, status) = os.waitpid(child_pid, 0)
105 if os.WIFEXITED(status):
106 result = os.WEXITSTATUS(status)
108 errmsg = os.strerror(result)
109 throw = lambda : __throw(IOError(result, errmsg))
111 errmsg = "unexpected exception in child"
114 errmsg = "child killed"
117 if ex.errno != errno.EINTR:
120 fcntl.fcntl(recvsock, fcntl.F_SETFL, os.O_NONBLOCK)
122 (fd, errmsg) = passfdimpl.recvmsg(recvsock)
124 if ex.errno != errno.EAGAIN:
125 throw = lambda : __throw(ex)
135 state_file = open("/var/run/vservers/%s.ctx" % self.name, "w")
137 vserverimpl.chcontext(self.ctx, self.remove_caps)
138 print >>state_file, "S_CONTEXT=%d" % self.ctx
139 print >>state_file, "S_PROFILE=%s" % self.config.get("S_PROFILE", "")