1 # Copyright 2005 Princeton University
13 from util_vserver_vars import *
15 CAP_SAFE = (linuxcaps.CAP_CHOWN |
16 linuxcaps.CAP_DAC_OVERRIDE |
17 linuxcaps.CAP_DAC_READ_SEARCH |
18 linuxcaps.CAP_FOWNER |
19 linuxcaps.CAP_FSETID |
21 linuxcaps.CAP_SETGID |
22 linuxcaps.CAP_SETUID |
23 linuxcaps.CAP_SETPCAP |
24 linuxcaps.CAP_SYS_TTY_CONFIG |
26 linuxcaps.CAP_SYS_CHROOT |
27 linuxcaps.CAP_SYS_PTRACE)
30 # these are the flags taken from the kernel linux/vserver/legacy.h
33 FLAGS_SCHED = 2 # XXX - defined in util-vserver/src/chcontext.c
45 INITSCRIPTS = [('/etc/rc.vinit', 'start'), ('/etc/rc.d/rc', '3')]
47 def __init__(self, name):
50 self.config = self.__read_config_file("/etc/vservers.conf")
51 self.config.update(self.__read_config_file("/etc/vservers/%s.conf" %
54 flags = self.config["S_FLAGS"].split(" ")
56 self.flags |= FLAGS_LOCK
58 self.flags |= FLAGS_NPROC
59 self.remove_caps = ~CAP_SAFE
60 self.ctx = int(self.config["S_CONTEXT"])
62 config_var_re = re.compile(r"^ *([A-Z_]+)=(.*)\n?$", re.MULTILINE)
64 def __read_config_file(self, filename):
66 f = open(filename, "r")
70 for m in self.config_var_re.finditer(data):
71 (key, val) = m.groups()
72 config[key] = val.strip('"')
75 def __do_chroot(self):
77 return os.chroot("%s/%s" % (VROOTDIR, self.name))
79 def open(self, filename, mode = "r"):
81 (sendsock, recvsock) = passfdimpl.socketpair()
87 f = open(filename, mode)
88 passfdimpl.sendmsg(f.fileno(), sendsock)
90 except EnvironmentError, ex:
91 (result, errmsg) = (ex.errno, ex.strerror)
93 (result, errmsg) = (255, str(ex))
94 os.write(sendsock, errmsg)
99 # XXX - need this since a lambda can't raise an exception
104 throw = lambda : __throw(Exception(errmsg))
107 (pid, status) = os.waitpid(child_pid, 0)
108 if os.WIFEXITED(status):
109 result = os.WEXITSTATUS(status)
111 errmsg = os.strerror(result)
112 throw = lambda : __throw(IOError(result, errmsg))
114 errmsg = "unexpected exception in child"
117 errmsg = "child killed"
120 if ex.errno != errno.EINTR:
123 fcntl.fcntl(recvsock, fcntl.F_SETFL, os.O_NONBLOCK)
125 (fd, errmsg) = passfdimpl.recvmsg(recvsock)
127 if ex.errno != errno.EAGAIN:
128 throw = lambda : __throw(ex)
138 state_file = open("/var/run/vservers/%s.ctx" % self.name, "w")
140 vserverimpl.chcontext(self.ctx, self.remove_caps)
141 print >>state_file, "S_CONTEXT=%d" % self.ctx
142 print >>state_file, "S_PROFILE=%s" % self.config.get("S_PROFILE", "")
147 child_pid = os.fork()
154 # enter vserver context
157 # use /dev/null for stdin, /var/log/boot.log for stdout/err
160 os.open("/dev/null", os.O_RDONLY)
161 os.open("/var/log/boot.log",
162 os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
165 # write same output that vserver script does
166 os.write(1, "Starting the virtual server %s\n" % self.name)
167 os.write(1, "Server %s is not running\n" % self.name)
169 # execute each init script in turn
170 # XXX - we don't support all the possible scripts
171 for cmd in self.INITSCRIPTS:
172 os.spawnl(os.P_WAIT, *cmd)
173 except Exception, ex:
174 traceback.print_exc()