Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 2038

Show
Ignore:
Timestamp:
09/27/08 13:51:56
Author:
fumanchu
Message:

Fix for #856 (Prevent open sockets from being inherited by child processes). Thanks to Nicolas Grilly for the ticket and patch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/wsgiserver/__init__.py

    r2007 r2038  
    12041204 
    12051205 
     1206try: 
     1207    import fcntl 
     1208except ImportError: 
     1209    try: 
     1210        from ctypes import windll, WinError 
     1211    except ImportError: 
     1212        def prevent_socket_inheritance(sock): 
     1213            """Dummy function, since neither fcntl nor ctypes are available.""" 
     1214            pass 
     1215    else: 
     1216        def prevent_socket_inheritance(sock): 
     1217            """Mark the given socket fd as non-inheritable (Windows).""" 
     1218            if not windll.kernel32.SetHandleInformation(sock.fileno(), 1, 0): 
     1219                raise WinError() 
     1220else: 
     1221    def prevent_socket_inheritance(sock): 
     1222        """Mark the given socket fd as non-inheritable (POSIX).""" 
     1223        fd = sock.fileno() 
     1224        old_flags = fcntl.fcntl(fd, fcntl.F_GETFD) 
     1225        fcntl.fcntl(fd, fcntl.F_SETFD, old_flags | fcntl.FD_CLOEXEC) 
     1226 
    12061227 
    12071228class CherryPyWSGIServer(object): 
     
    13971418        """Create (or recreate) the actual socket object.""" 
    13981419        self.socket = socket.socket(family, type, proto) 
     1420        prevent_socket_inheritance(self.socket) 
    13991421        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
    14001422        if self.nodelay: 
     
    14161438        try: 
    14171439            s, addr = self.socket.accept() 
     1440            prevent_socket_inheritance(s) 
    14181441            if not self.ready: 
    14191442                return 

Hosted by WebFaction

Log in as guest/cpguest to create tickets