def connectionMade(self):
        sockFD = self.transport.fileno()
        childFDs = {0: sockFD, 1: sockFD}
        if self.factory.stderrFile:
            childFDs[2] = self.factory.stderrFile.fileno()

        # processes run by inetd expect blocking sockets
        # FIXME: maybe this should be done in process.py?  are other uses of
        #        Process possibly affected by this?
        fdesc.setBlocking(sockFD)
        if childFDs.has_key(2):
            fdesc.setBlocking(childFDs[2])

        service = self.factory.service
        uid = service.user
        gid = service.group

        # don't tell Process to change our UID/GID if it's what we
        # already are
        if uid == os.getuid():
            uid = None
        if gid == os.getgid():
            gid = None

        process.Process(None, service.program, service.programArgs, os.environ,
                        None, None, uid, gid, childFDs)

        reactor.removeReader(self.transport)
        reactor.removeWriter(self.transport)
Example #2
0
    def connectionMade(self):
        sockFD = self.transport.fileno()
        childFDs = {0: sockFD, 1: sockFD}
        if self.factory.stderrFile:
            childFDs[2] = self.factory.stderrFile.fileno()

        # processes run by inetd expect blocking sockets
        # FIXME: maybe this should be done in process.py?  are other uses of
        #        Process possibly affected by this?
        fdesc.setBlocking(sockFD)
        if childFDs.has_key(2):
            fdesc.setBlocking(childFDs[2])

        service = self.factory.service
        uid = service.user
        gid = service.group

        # don't tell Process to change our UID/GID if it's what we
        # already are
        if uid == os.getuid():
            uid = None
        if gid == os.getgid():
            gid = None

        process.Process(None, service.program, service.programArgs, os.environ,
                        None, None, uid, gid, childFDs)

        reactor.removeReader(self.transport)
        reactor.removeWriter(self.transport)
Example #3
0
def socket_remove(id, condition=IO_READ):
    """
    Removes the IRead/WriteDescriptor object with this socket from
    the Twisted reactor.
    """
    global __sockobjs
    sockobj = __sockobjs[condition].get(id)

    if sockobj:
        if condition == IO_READ:
            reactor.removeReader(sockobj)
        elif condition == IO_WRITE:
            reactor.removeWriter(sockobj)
        del __sockobjs[condition][id]
Example #4
0
def socket_remove(id, condition=IO_READ):
    """
    Removes the IRead/WriteDescriptor object with this socket from
    the Twisted reactor.
    """
    global __sockobjs
    sockobj = __sockobjs[condition].get(id)

    if sockobj:
        if condition == IO_READ:
            reactor.removeReader(sockobj)
        elif condition == IO_WRITE:
            reactor.removeWriter(sockobj)
        del __sockobjs[condition][id]
Example #5
0
def forkPassingFD(exe, args, env, user, group, fdesc):
    """Run exe as a child process, passing fdesc as fd 0.
    
    This will also make sure that fdesc is removed from the parent's reactor.
    """
    # This is half-cannibalised from twisted.internet.process.Process
    pid = os.fork()
    if pid == 0:    # Child
        try:
            # Close stdin/stdout (we keep stderr from the parent to report
            # errors with)
            for fd in range(2):
                os.close(fd)
            
            # Make the socket be fd 0 
            # (and fd 1, although I'm not sure if that matters)
            os.dup(fdesc.fileno())
            os.dup(fdesc.fileno())

            # Close unused file descriptors
            for fd in range(3, 256):
                try: os.close(fd)
                except: pass
            
            # Set uid/gid
            os.setgid(group)
            os.setuid(user)
            
            # Start the new process
            os.execvpe(exe, args, env)
        except:
            # If anything goes wrong, just die.
            stderr = os.fdopen(2, 'w')
            stderr.write('Unable to spawn child:\n')
            traceback.print_exc(file=stderr)

            # Close the socket so the client doesn't think it's still
            # connected to a server
            try:
                s = socket.fromfd(0, socket.AF_INET, socket.SOCK_STREAM)
                s.shutdown(2)
            except:
                pass
        os._exit(1)
    else:           # Parent
        reactor.removeReader(fdesc)
        reactor.removeWriter(fdesc)
    def test_getWriters(self):
        """
        Check that L{interfaces.IReactorFDSet.getWriters} reflects the actions
        made with L{interfaces.IReactorFDSet.addWriter} and
        L{interfaces.IReactorFDSet.removeWriter}.
        """
        s = socket.socket()
        self.addCleanup(s.close)

        c = Connection(s, protocol.Protocol())
        self.assertNotIn(c, reactor.getWriters())

        reactor.addWriter(c)
        self.assertIn(c, reactor.getWriters())

        reactor.removeWriter(c)
        self.assertNotIn(c, reactor.getWriters())
Example #7
0
    def test_getWriters(self):
        """
        Check that L{interfaces.IReactorFDSet.getWriters} reflects the actions
        made with L{interfaces.IReactorFDSet.addWriter} and
        L{interfaces.IReactorFDSet.removeWriter}.
        """
        s = socket.socket()
        self.addCleanup(s.close)

        c = Connection(s, protocol.Protocol())
        self.assertNotIn(c, reactor.getWriters())

        reactor.addWriter(c)
        self.assertIn(c, reactor.getWriters())

        reactor.removeWriter(c)
        self.assertNotIn(c, reactor.getWriters())
Example #8
0
 def remove(self, descriptor):
     from twisted.internet import reactor
     reactor.removeReader(descriptor)
     reactor.removeWriter(descriptor)
Example #9
0
 def remove_descriptor(self, descriptor):
     from twisted.internet import reactor
     reactor.removeReader(descriptor)
     reactor.removeWriter(descriptor)
Example #10
0
 def endConnection(self):
     self.sock.close()
     from twisted.internet import reactor
     reactor.removeWriter(self)
Example #11
0
 def stopWriting(self):
     """Remove writer from the reactor"""
     reactor.removeWriter(self)
Example #12
0
 def doWrite(self):
     if len(self.packets) > 0:
         self.socket.sendto(self.packets.pop(0), self.dest_ip)
     if len(self.packets) == 0:
         reactor.removeWriter(self)
Example #13
0
 def connectionLost(self, reason):
     reactor.removeWriter(self)
     return reason
Example #14
0
 def stop(self):
     reactor.removeWriter(self)
Example #15
0
 def stopWriting(self):
     if self.writing:
         reactor.removeWriter(self)
         self.writing = False
Example #16
0
 def stopWriting(self):
     if self.writing:
         reactor.removeWriter(self)
         self.writing = False