1 # Copyright 2005 Princeton University
15 from util_vserver_vars import *
17 CAP_SAFE = (linuxcaps.CAP_CHOWN |
18 linuxcaps.CAP_DAC_OVERRIDE |
19 linuxcaps.CAP_DAC_READ_SEARCH |
20 linuxcaps.CAP_FOWNER |
21 linuxcaps.CAP_FSETID |
23 linuxcaps.CAP_SETGID |
24 linuxcaps.CAP_SETUID |
25 linuxcaps.CAP_SETPCAP |
26 linuxcaps.CAP_SYS_TTY_CONFIG |
28 linuxcaps.CAP_SYS_CHROOT |
29 linuxcaps.CAP_SYS_PTRACE)
32 # these are the flags taken from the kernel linux/vserver/legacy.h
35 FLAGS_SCHED = 2 # XXX - defined in util-vserver/src/chcontext.c
47 INITSCRIPTS = [('/etc/rc.vinit', 'start'), ('/etc/rc.d/rc', '3')]
49 def __init__(self, name):
52 self.config = self.__read_config_file("/etc/vservers.conf")
53 self.config.update(self.__read_config_file("/etc/vservers/%s.conf" %
56 flags = self.config["S_FLAGS"].split(" ")
58 self.flags |= FLAGS_LOCK
60 self.flags |= FLAGS_NPROC
61 self.remove_caps = ~CAP_SAFE
62 self.ctx = int(self.config["S_CONTEXT"])
64 config_var_re = re.compile(r"^ *([A-Z_]+)=(.*)\n?$", re.MULTILINE)
66 def __read_config_file(self, filename):
68 f = open(filename, "r")
72 for m in self.config_var_re.finditer(data):
73 (key, val) = m.groups()
74 config[key] = val.strip('"')
77 def __do_chroot(self):
79 return os.chroot("%s/%s" % (VROOTDIR, self.name))
81 def open(self, filename, mode = "r", bufsize = -1):
83 (sendsock, recvsock) = passfdimpl.socketpair()
89 f = open(filename, mode)
90 passfdimpl.sendmsg(f.fileno(), sendsock)
92 except EnvironmentError, ex:
93 (result, errmsg) = (ex.errno, ex.strerror)
95 (result, errmsg) = (255, str(ex))
96 os.write(sendsock, errmsg)
101 # XXX - need this since a lambda can't raise an exception
106 throw = lambda : __throw(Exception(errmsg))
109 (pid, status) = os.waitpid(child_pid, 0)
110 if os.WIFEXITED(status):
111 result = os.WEXITSTATUS(status)
113 errmsg = os.strerror(result)
114 throw = lambda : __throw(IOError(result, errmsg))
116 errmsg = "unexpected exception in child"
119 errmsg = "child killed"
122 if ex.errno != errno.EINTR:
125 fcntl.fcntl(recvsock, fcntl.F_SETFL, os.O_NONBLOCK)
127 (fd, errmsg) = passfdimpl.recvmsg(recvsock)
129 if ex.errno != errno.EAGAIN:
130 throw = lambda : __throw(ex)
136 return os.fdopen(fd, mode, bufsize)
140 state_file = open("/var/run/vservers/%s.ctx" % self.name, "w")
142 vserverimpl.chcontext(self.ctx, self.remove_caps)
143 print >>state_file, "S_CONTEXT=%d" % self.ctx
144 print >>state_file, "S_PROFILE=%s" % self.config.get("S_PROFILE", "")
149 child_pid = os.fork()
156 # use /dev/null for stdin, /var/log/boot.log for stdout/err
159 os.open("/dev/null", os.O_RDONLY)
160 log = self.open("/var/log/boot.log", "w", 0)
163 # write same output that vserver script does
164 print >>log, ("%s: starting the virtual server %s" %
165 (time.asctime(time.gmtime()), self.name))
167 # execute each init script in turn
168 # XXX - we don't support all scripts that vserver script does
170 for cmd in self.INITSCRIPTS + [None]:
171 # don't bother waiting for last command to terminate
175 # wait for previous command to terminate
178 os.waitpid(cmd_pid, 0)
180 print >>log, "error waiting for %s:" % cmd_pid
181 traceback.print_exc()
183 # fork and exec next command
187 # enter vserver context
189 print >>log, "executing '%s'" % " ".join(cmd)
190 os.execl(cmd[0], *cmd)
192 traceback.print_exc()
195 # we get here due to an exception in the top-level child process
196 except Exception, ex:
197 traceback.print_exc()