ia64/xen-unstable

changeset 12725:36fe7ca48e54

Tidy up the creation of directories that Xend needs. This avoids potential
races in this creation.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Fri Dec 01 11:32:32 2006 +0000 (2006-12-01)
parents aedeaa926893
children bec95280b565
files tools/python/xen/util/mkdir.py tools/python/xen/util/xmlrpclib2.py tools/python/xen/web/unix.py tools/python/xen/xend/XendDomain.py tools/python/xen/xend/XendLogging.py tools/python/xen/xend/XendStorageRepository.py tools/python/xen/xend/server/SrvDaemon.py
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/python/xen/util/mkdir.py	Fri Dec 01 11:32:32 2006 +0000
     1.3 @@ -0,0 +1,44 @@
     1.4 +#============================================================================
     1.5 +# This library is free software; you can redistribute it and/or modify
     1.6 +# it under the terms of the GNU Lesser General Public License as published by
     1.7 +# the Free Software Foundation; either version 2.1 of the License, or
     1.8 +# (at your option) any later version.
     1.9 +#
    1.10 +# This library is distributed in the hope that it will be useful,
    1.11 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.12 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.13 +# GNU Lesser General Public License for more details.
    1.14 +#
    1.15 +# You should have received a copy of the GNU Lesser General Public License
    1.16 +# along with this library; if not, write to the Free Software
    1.17 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.18 +#============================================================================
    1.19 +# Copyright (c) 2006 XenSource Inc.
    1.20 +#============================================================================
    1.21 +
    1.22 +import errno
    1.23 +import os
    1.24 +import os.path
    1.25 +import stat
    1.26 +
    1.27 +
    1.28 +def parents(dir, perms, enforcePermissions = False):
    1.29 +    """
    1.30 +    Ensure that the given directory exists, creating it if necessary, but not
    1.31 +    complaining if it's already there.
    1.32 +    
    1.33 +    @param dir The directory name.
    1.34 +    @param perms One of the stat.S_ constants.
    1.35 +    @param enforcePermissions Enforce our ownership and the given permissions,
    1.36 +    even if the directory pre-existed with different ones.
    1.37 +    """
    1.38 +    # Catch the exception here, rather than checking for the directory's
    1.39 +    # existence first, to avoid races.
    1.40 +    try:
    1.41 +        os.makedirs(dir, perms)
    1.42 +    except OSError, exn:
    1.43 +        if exn.args[0] != errno.EEXIST or not os.path.isdir(dir):
    1.44 +            raise
    1.45 +    if enforcePermissions:
    1.46 +        os.chown(dir, os.geteuid(), os.getegid())
    1.47 +        os.chmod(dir, stat.S_IRWXU)
     2.1 --- a/tools/python/xen/util/xmlrpclib2.py	Fri Dec 01 11:08:34 2006 +0000
     2.2 +++ b/tools/python/xen/util/xmlrpclib2.py	Fri Dec 01 11:32:32 2006 +0000
     2.3 @@ -30,6 +30,8 @@ from SimpleXMLRPCServer import SimpleXML
     2.4  import SocketServer
     2.5  import xmlrpclib, socket, os, stat
     2.6  
     2.7 +import mkdir
     2.8 +
     2.9  from xen.web import connection
    2.10  from xen.xend.XendLogging import log
    2.11  
    2.12 @@ -234,14 +236,9 @@ class UnixXMLRPCServer(TCPXMLRPCServer):
    2.13      address_family = socket.AF_UNIX
    2.14  
    2.15      def __init__(self, addr, allowed, logRequests = 1):
    2.16 -        parent = os.path.dirname(addr)
    2.17 -        if os.path.exists(parent):
    2.18 -            os.chown(parent, os.geteuid(), os.getegid())
    2.19 -            os.chmod(parent, stat.S_IRWXU)
    2.20 -            if self.allow_reuse_address and os.path.exists(addr):
    2.21 -                os.unlink(addr)
    2.22 -        else:
    2.23 -            os.makedirs(parent, stat.S_IRWXU)
    2.24 +        mkdir.parents(os.path.dirname(addr), stat.S_IRWXU, True)
    2.25 +        if self.allow_reuse_address and os.path.exists(addr):
    2.26 +            os.unlink(addr)
    2.27  
    2.28          TCPXMLRPCServer.__init__(self, addr, allowed,
    2.29                                   UnixXMLRPCRequestHandler, logRequests)
     3.1 --- a/tools/python/xen/web/unix.py	Fri Dec 01 11:08:34 2006 +0000
     3.2 +++ b/tools/python/xen/web/unix.py	Fri Dec 01 11:32:32 2006 +0000
     3.3 @@ -22,6 +22,8 @@ import os.path
     3.4  import socket
     3.5  import stat
     3.6  
     3.7 +from xen.util import mkdir
     3.8 +
     3.9  import connection
    3.10  
    3.11  
    3.12 @@ -30,13 +32,9 @@ def bind(path):
    3.13  created such that only the current user may access it."""
    3.14  
    3.15      parent = os.path.dirname(path)
    3.16 -    if os.path.exists(parent):
    3.17 -        os.chown(parent, os.geteuid(), os.getegid())
    3.18 -        os.chmod(parent, stat.S_IRWXU)
    3.19 -        if os.path.exists(path):
    3.20 -            os.unlink(path)
    3.21 -    else:
    3.22 -        os.makedirs(parent, stat.S_IRWXU)
    3.23 +    mkdir.parents(parent, stat.S_IRWXU, True)
    3.24 +    if os.path.exists(path):
    3.25 +        os.unlink(path)
    3.26  
    3.27      sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    3.28      sock.bind(path)
     4.1 --- a/tools/python/xen/xend/XendDomain.py	Fri Dec 01 11:08:34 2006 +0000
     4.2 +++ b/tools/python/xen/xend/XendDomain.py	Fri Dec 01 11:32:32 2006 +0000
     4.3 @@ -23,6 +23,7 @@
     4.4  """
     4.5  
     4.6  import os
     4.7 +import stat
     4.8  import shutil
     4.9  import socket
    4.10  import threading
    4.11 @@ -44,7 +45,7 @@ from xen.xend.XendDevices import XendDev
    4.12  
    4.13  from xen.xend.xenstore.xstransact import xstransact
    4.14  from xen.xend.xenstore.xswatch import xswatch
    4.15 -from xen.util import security
    4.16 +from xen.util import mkdir, security
    4.17  from xen.xend import uuid
    4.18  
    4.19  xc = xen.lowlevel.xc.xc()
    4.20 @@ -99,11 +100,7 @@ class XendDomain:
    4.21          """Singleton initialisation function."""
    4.22  
    4.23          dom_path = self._managed_path()
    4.24 -        try:
    4.25 -            os.stat(dom_path)
    4.26 -        except OSError:
    4.27 -            log.info("Making %s", dom_path)
    4.28 -            os.makedirs(dom_path, 0755)
    4.29 +        mkdir.parents(dom_path, stat.S_IRWXU)
    4.30  
    4.31          xstransact.Mkdir(XS_VMROOT)
    4.32          xstransact.SetPermissions(XS_VMROOT, {'dom': DOM0_ID})
    4.33 @@ -271,25 +268,17 @@ class XendDomain:
    4.34              domains_dir = self._managed_path()
    4.35              dom_uuid = dominfo.get_uuid()            
    4.36              domain_config_dir = self._managed_path(dom_uuid)
    4.37 -        
    4.38 -            # make sure the domain dir exists
    4.39 -            if not os.path.exists(domains_dir):
    4.40 -                os.makedirs(domains_dir, 0755)
    4.41 -            elif not os.path.isdir(domains_dir):
    4.42 -                log.error("xend_domain_dir is not a directory.")
    4.43 -                raise XendError("Unable to save managed configuration "
    4.44 -                                "because %s is not a directory." %
    4.45 -                                domains_dir)
    4.46 -            
    4.47 -            if not os.path.exists(domain_config_dir):
    4.48 +
    4.49 +            def make_or_raise(path):
    4.50                  try:
    4.51 -                    os.makedirs(domain_config_dir, 0755)
    4.52 -                except IOError:
    4.53 -                    log.exception("Failed to create directory: %s" %
    4.54 -                                  domain_config_dir)
    4.55 -                    raise XendError("Failed to create directory: %s" %
    4.56 -                                    domain_config_dir)
    4.57 -                
    4.58 +                    mkdir.parents(path, stat.S_IRWXU)
    4.59 +                except:
    4.60 +                    log.exception("%s could not be created." % path)
    4.61 +                    raise XendError("%s could not be created." % path)
    4.62 +
    4.63 +            make_or_raise(domains_dir)
    4.64 +            make_or_raise(domain_config_dir)
    4.65 +
    4.66              try:
    4.67                  sxp_cache_file = open(self._managed_config_path(dom_uuid),'w')
    4.68                  prettyprint(dominfo.sxpr(), sxp_cache_file, width = 78)
     5.1 --- a/tools/python/xen/xend/XendLogging.py	Fri Dec 01 11:08:34 2006 +0000
     5.2 +++ b/tools/python/xen/xend/XendLogging.py	Fri Dec 01 11:32:32 2006 +0000
     5.3 @@ -16,13 +16,15 @@
     5.4  # Copyright (C) 2005, 2006 XenSource Ltd.
     5.5  #============================================================================
     5.6  
     5.7 -
     5.8 +import os
     5.9 +import stat
    5.10  import tempfile
    5.11  import types
    5.12  import logging
    5.13  import logging.handlers
    5.14  import fcntl
    5.15  
    5.16 +from xen.util import mkdir
    5.17  from xen.xend.server import params
    5.18  
    5.19  
    5.20 @@ -80,6 +82,7 @@ def init(filename, level):
    5.21      global logfilename
    5.22  
    5.23      def openFileHandler(fname):
    5.24 +        mkdir.parents(os.path.dirname(fname), stat.S_IRWXU)
    5.25          return XendRotatingFileHandler(fname, mode = 'a',
    5.26                                         maxBytes = MAX_BYTES,
    5.27                                         backupCount = BACKUP_COUNT)
     6.1 --- a/tools/python/xen/xend/XendStorageRepository.py	Fri Dec 01 11:08:34 2006 +0000
     6.2 +++ b/tools/python/xen/xend/XendStorageRepository.py	Fri Dec 01 11:32:32 2006 +0000
     6.3 @@ -19,10 +19,12 @@
     6.4  # The default QCOW Xen API Storage Repository
     6.5  #
     6.6  
     6.7 +import commands
     6.8  import os
     6.9 -import commands
    6.10 +import stat
    6.11  import threading
    6.12  
    6.13 +from xen.util import mkdir
    6.14  from xen.xend import uuid
    6.15  from xen.xend.XendError import XendError
    6.16  from xen.xend.XendVDI import *
    6.17 @@ -98,10 +100,7 @@ class XendStorageRepository:
    6.18          """
    6.19          self.lock.acquire()
    6.20          try:
    6.21 -            # create directory if /var/lib/xend/storage does not exist
    6.22 -            if not os.path.exists(XEND_STORAGE_DIR):
    6.23 -                os.makedirs(XEND_STORAGE_DIR)
    6.24 -                os.chmod(XEND_STORAGE_DIR, 0700)
    6.25 +            mkdir.parents(XEND_STORAGE_DIR, stat.S_IRWXU)
    6.26  
    6.27              # scan the directory and populate self.images
    6.28              total_used = 0
     7.1 --- a/tools/python/xen/xend/server/SrvDaemon.py	Fri Dec 01 11:08:34 2006 +0000
     7.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py	Fri Dec 01 11:32:32 2006 +0000
     7.3 @@ -21,6 +21,7 @@ import xen.lowlevel.xc
     7.4  
     7.5  from xen.xend.XendLogging import log
     7.6  from xen.xend import osdep
     7.7 +from xen.util import mkdir
     7.8  
     7.9  import relocate
    7.10  import SrvServer
    7.11 @@ -108,8 +109,7 @@ class Daemon:
    7.12          # so _before_ we close stderr.
    7.13          try:
    7.14              parent = os.path.dirname(XEND_DEBUG_LOG)
    7.15 -            if not os.path.exists(parent):
    7.16 -                os.makedirs(parent, stat.S_IRWXU)
    7.17 +            mkdir.parents(parent, stat.S_IRWXU)
    7.18              fd = os.open(XEND_DEBUG_LOG, os.O_WRONLY|os.O_CREAT|os.O_APPEND)
    7.19          except Exception, exn:
    7.20              print >>sys.stderr, exn