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)
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]
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())
def remove(self, descriptor): from twisted.internet import reactor reactor.removeReader(descriptor) reactor.removeWriter(descriptor)
def remove_descriptor(self, descriptor): from twisted.internet import reactor reactor.removeReader(descriptor) reactor.removeWriter(descriptor)
def endConnection(self): self.sock.close() from twisted.internet import reactor reactor.removeWriter(self)
def stopWriting(self): """Remove writer from the reactor""" reactor.removeWriter(self)
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)
def connectionLost(self, reason): reactor.removeWriter(self) return reason
def stop(self): reactor.removeWriter(self)
def stopWriting(self): if self.writing: reactor.removeWriter(self) self.writing = False