Example #1
0
def init(*configFiles):
    global _svr
    if _svr:
        raise RuntimeError, "already initialized"

    loadConfig(*configFiles)
    configDefaults()
    updateConfig()
    configLogging()    

    # set effective user/group, if necessary
    isroot=os.getuid()==0
    run_user=Configuration.run_user
    run_group=Configuration.run_group
    if isroot and run_user is None:
        raise RuntimeError, "won't run as root with out run_user being defined"
    if run_group is not None:
        gid=grp.getnrnam(run_group)[2]
        if hasattr(os, 'setegid'):
            os.setegid(gid)
        #else?
    if run_user is not None:
        uid=pwd.getpwnam(run_user)[2]
        if hasattr(os, 'seteuid'):
            os.seteuid(uid)
        #else?

    loadServices()
    
    # revv 'er up
    _svr=Server(configFiles)
    _svr.mainloop()
Example #2
0
 def reload(self):
     super(Server, self).reload()
     loadServices()
     loadConfig(*self.configFiles)
     configDefaults()
     updateConfig()
     configLogging()        
def processRequest(sock, addr, protocol):
    """ the actual handler for requestHandler-managed requests."""
    requestData, responseData, ctxt = None, None, {}
    # capture the socket address (ip & port, or path, for a unix
    # socket) on which the request came, for scoping of configuration
    # data on their basis
    ip, port, unixpath = None, None, None
    try:
        addr = sock.getsockname()
        if isinstance(addr, tuple):
            ip, port = addr
        else:
            unixpath = addr
    except:
        exception("failed to read addr off socket!")
        raise

    if ip and port:
        ctxt['IP'] = ip
        ctxt['PORT'] = port
    else:
        ctxt['UNIX_SOCKET_PATH'] = unixpath

    # load configuration data for the job
    updateConfig(ctxt)

    # loop until connection is closed
    while 1:
        try:
            blockTERM()
            signal.signal(signal.SIGALRM, SIGALRMHandler)
            signal.alarm(Configuration.DocumentTimeout)
            try:
                rawResponse = HandleRequest(requestData, ctxt)
                responseData = protocol.marshalResponse(rawResponse, ctxt)
            except:
                try:
                    responseData = protocol.marshalException(ctxt)
                except:
                    exception("ironic exception in marshalling exception!")
                    raise
            try:
                sock.sendall(responseData)
            except IOError, en:
                # ignore broken pipes
                if en != errno.EPIPE:
                    exception("IO error")
            except:
                exception("error sending response")

            # reset alarm
            signal.alarm(Configuration.PostResponseTimeout)

            try:
                PostResponse(requestData, ctxt)
            except:
                exception("error in PostResponse")

        finally:
def processRequest(sock, addr, protocol):
    """ the actual handler for requestHandler-managed requests."""
    requestData, responseData, ctxt=None, None, {}
    # capture the socket address (ip & port, or path, for a unix
    # socket) on which the request came, for scoping of configuration
    # data on their basis
    ip, port, unixpath=None, None, None
    try:      
        addr=sock.getsockname()
        if isinstance(addr, tuple):
            ip, port=addr
        else:
            unixpath=addr
    except:
        exception("failed to read addr off socket!")
        raise

    if ip and port:
        ctxt['IP']=ip
        ctxt['PORT']=port
    else:
        ctxt['UNIX_SOCKET_PATH']=unixpath
    
    # load configuration data for the job
    updateConfig(ctxt)
    
    # loop until connection is closed
    while 1:
        try:
            blockTERM()
            signal.signal(signal.SIGALRM, SIGALRMHandler)
            signal.alarm(Configuration.DocumentTimeout)
            try:
                rawResponse=HandleRequest(requestData, ctxt)
                responseData=protocol.marshalResponse(rawResponse, ctxt)
            except:
                try:
                    responseData=protocol.marshalException(ctxt)
                except:
                    exception("ironic exception in marshalling exception!")
                    raise
            try:
                sock.sendall(responseData)
            except IOError, en:
                # ignore broken pipes
                if en != errno.EPIPE:
                    exception("IO error")
            except:
                exception("error sending response")
 
            # reset alarm
            signal.alarm(Configuration.PostResponseTimeout)
    
            try:
                PostResponse(requestData, ctxt)
            except:
                exception("error in PostResponse")

        finally:
def SIGALRMHandler(*args):
    updateConfig()
    error('Throwing timeout exception')
    # in case this exception is caught, raise another in one second
    signal.alarm(1)
    raise DocumentTimeout, "timeout reached"
        timeout = protocol.keepAliveTimeout(ctxt)
        ending = timeout < 0
        if not ending:
            try:
                ending = not can_read(sock, timeout)
            except socket.error, e:
                ending = 1
                if not e.args or e.args[0] != errno.ECONNRESET:
                    exception('problem with socket, ending connection')
            except:
                ending = 1
                exception("problem testing socket")

        if ending:
            updateConfig()
            return


def SIGALRMHandler(*args):
    updateConfig()
    error('Throwing timeout exception')
    # in case this exception is caught, raise another in one second
    signal.alarm(1)
    raise DocumentTimeout, "timeout reached"


class RequestHandler(object):
    """
    an object, the creation of which adds a service
    to the server on the specified ports with the specified
def SIGALRMHandler(*args):
    updateConfig()
    error('Throwing timeout exception')
    # in case this exception is caught, raise another in one second
    signal.alarm(1) 
    raise DocumentTimeout, "timeout reached"
            
        timeout=protocol.keepAliveTimeout(ctxt)
        ending=timeout<0
        if not ending:
            try:
                ending=not can_read(sock, timeout)
            except socket.error, e:
                ending=1
                if not e.args or e.args[0] != errno.ECONNRESET:
                    exception('problem with socket, ending connection')
            except:
                ending=1
                exception("problem testing socket")

        if ending:
            updateConfig()
            return

def SIGALRMHandler(*args):
    updateConfig()
    error('Throwing timeout exception')
    # in case this exception is caught, raise another in one second
    signal.alarm(1) 
    raise DocumentTimeout, "timeout reached"


class RequestHandler(object):
    """
    an object, the creation of which adds a service
    to the server on the specified ports with the specified
    protocol.