1 # Copyright 2005 Princeton University
11 from vserver_vars import *
13 CAP_SAFE = (linuxcaps.CAP_CHOWN |
14 linuxcaps.CAP_DAC_OVERRIDE |
15 linuxcaps.CAP_DAC_READ_SEARCH |
16 linuxcaps.CAP_FOWNER |
17 linuxcaps.CAP_FSETID |
19 linuxcaps.CAP_SETGID |
20 linuxcaps.CAP_SETUID |
21 linuxcaps.CAP_SETPCAP |
22 linuxcaps.CAP_SYS_TTY_CONFIG |
24 linuxcaps.CAP_SYS_CHROOT |
25 linuxcaps.CAP_SYS_PTRACE)
28 # XXX - these are the flags taken from chcontext.c, but they don't match
29 # up with those apparently used by the kernel
43 def __init__(self, name):
46 self.config = self.__read_config_file("/etc/vservers.conf")
47 self.config.update(self.__read_config_file("/etc/vservers/%s.conf" %
50 flags = self.config["S_FLAGS"].split(" ")
52 self.flags |= FLAGS_LOCK
54 self.flags |= FLAGS_NPROC
55 self.remove_caps = ~CAP_SAFE
56 print "%x %x" % (self.flags, ~self.remove_caps)
58 config_var_re = re.compile(r"^ *([A-Z_]+)=(.*)\n?$", re.MULTILINE)
60 def __read_config_file(self, filename):
62 f = open(filename, "r")
66 for m in self.config_var_re.finditer(data):
67 (key, val) = m.groups()
68 config[key] = val.strip('"')
71 def open(self, filename, mode = "r"):
73 (sendsock, recvsock) = passfdimpl.socketpair()
78 os.chroot("%s/%s" % (VROOTDIR, self.name))
79 f = open(filename, mode)
80 passfdimpl.sendmsg(f.fileno(), sendsock)
82 except EnvironmentError, ex:
83 (result, errmsg) = (ex.errno, ex.strerror)
85 (result, errmsg) = (255, str(ex))
86 os.write(sendsock, errmsg)
91 # XXX - need this since a lambda can't raise an exception
96 throw = lambda : __throw(Exception(errmsg))
99 (pid, status) = os.waitpid(child_pid, 0)
100 if os.WIFEXITED(status):
101 result = os.WEXITSTATUS(status)
103 errmsg = os.strerror(result)
104 throw = lambda : __throw(IOError(result, errmsg))
106 errmsg = "unexpected exception in child"
109 errmsg = "child killed"
112 if ex.errno != errno.EINTR:
115 fcntl.fcntl(recvsock, fcntl.F_SETFL, os.O_NONBLOCK)
117 (fd, errmsg) = passfdimpl.recvmsg(recvsock)
119 if ex.errno != errno.EAGAIN:
120 throw = lambda : __throw(ex)