X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=python%2Fvserver.py;h=7652b784c5b1d99c06db7d7c3839958a73345c32;hb=5d32782d7fd7141309464b18678ee16b92af6686;hp=e3748f4accfd4a845115680505058dbb52242d25;hpb=c691247edc297fa51265d333950b2004d8b357fc;p=util-vserver-pl.git diff --git a/python/vserver.py b/python/vserver.py index e3748f4..7652b78 100644 --- a/python/vserver.py +++ b/python/vserver.py @@ -267,7 +267,6 @@ class VServer: block_usage = self.disk_blocks inode_usage = self.disk_inodes - try: vserverimpl.setdlimit(self.dir, self.ctx, @@ -396,13 +395,18 @@ class VServer: self.__do_chroot() self.__do_chcontext(None) - def start(self, wait, runlevel = 3): - self.vm_running = True + def start(self, runlevel = 3): - child_pid = os.fork() - if child_pid == 0: + if (os.fork() != 0): + # Parent should just return. + self.vm_running = True + return + else: # child process try: + # so we don't chcontext with priv'ed fds + close_nonstandard_fds() + # get a new session os.setsid() @@ -431,25 +435,22 @@ class VServer: # XXX - we don't support all scripts that vserver script does self.__do_chcontext(state_file) for cmd in self.INITSCRIPTS: - try: - # enter vserver context - arg_subst = { 'runlevel': runlevel } - cmd_args = [cmd[0]] + map(lambda x: x % arg_subst, + try: + # enter vserver context + arg_subst = { 'runlevel': runlevel } + cmd_args = [cmd[0]] + map(lambda x: x % arg_subst, cmd[1:]) - print >>log, "executing '%s'" % " ".join(cmd_args) - os.spawnvp(os.P_NOWAIT,cmd[0],cmd_args) - except: - print >>log, traceback.format_exc() - os._exit(1) + if os.path.isfile(cmd[0]): + print >>log, "executing '%s'" % " ".join(cmd_args) + os.spawnvp(os.P_NOWAIT,cmd[0],cmd_args) + except: + print >>log, traceback.format_exc() # we get here due to an exception in the top-level child process except Exception, ex: self.log(traceback.format_exc()) os._exit(0) - # parent process - return child_pid - def set_resources(self): """ Called when vserver context is entered for first time, @@ -458,6 +459,13 @@ class VServer: pass def init_disk_info(self): + try: + dlimit = vserver.getdlimit(self.dir, self.ctx) + self.disk_blocks = dlimit[0] + self.disk_inodes = dlimit[2] + return self.disk_blocks * 1024 + except Exception, e: + pass cmd = "/usr/sbin/vdu --script --space --inodes --blocksize 1024 --xid %d %s" % (self.ctx, self.dir) p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, @@ -506,3 +514,12 @@ def create(vm_name, static = False, ctor = VServer): vm_id = pwd.getpwnam(vm_name)[2] return ctor(vm_name, vm_id) + + +def close_nonstandard_fds(): + """Close all open file descriptors other than 0, 1, and 2.""" + _SC_OPEN_MAX = 4 + for fd in range(3, os.sysconf(_SC_OPEN_MAX)): + try: os.close(fd) + except OSError: pass # most likely an fd that isn't open +