Make file descriptor handling more resilient
Daniel Hokka Zakrisson [Tue, 31 Jul 2007 16:31:04 +0000 (16:31 +0000)]
python/vserver.py

index d358a11..e27f6bb 100644 (file)
@@ -1,6 +1,6 @@
 # Copyright 2005 Princeton University
 
-#$Id: vserver.py,v 1.64 2007/07/27 18:02:08 dhozac Exp $
+#$Id: vserver.py,v 1.65 2007/07/31 14:36:19 dhozac Exp $
 
 import errno
 import fcntl
@@ -405,11 +405,19 @@ class VServer:
                 state_file = open("/var/run/vservers/%s" % self.name, "w")
 
                 # use /dev/null for stdin, /var/log/boot.log for stdout/err
-                os.close(0)
-                os.close(1)
-                os.open("/dev/null", os.O_RDONLY)
+                try:
+                    os.close(0)
+                    os.close(1)
+                except:
+                    pass
+                fd = os.open("/dev/null", os.O_RDONLY)
+                if fd != 0:
+                    os.dup2(fd, 0)
+                    os.close(fd)
                 self.__do_chroot()
                 log = open("/var/log/boot.log", "w", 0)
+                if log.fileno() != 1:
+                    os.dup2(log.fileno(), 1)
                 os.dup2(1, 2)
 
                 print >>log, ("%s: starting the virtual server %s" %