1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
9 Maintainer: Andrew Bennetts
11 Future Plans: Bugfixes. Specifically for UDP and Sun-RPC, which don't work
17 from twisted.internet import process, reactor, fdesc
18 from twisted.internet.protocol import Protocol, ServerFactory
19 from twisted.protocols import wire
21 # A dict of known 'internal' services (i.e. those that don't involve spawning
25 'chargen': wire.Chargen,
26 'discard': wire.Discard,
27 'daytime': wire.Daytime,
32 class InetdProtocol(Protocol):
33 """Forks a child process on connectionMade, passing the socket as fd 0."""
34 def connectionMade(self):
35 sockFD = self.transport.fileno()
36 childFDs = {0: sockFD, 1: sockFD}
37 if self.factory.stderrFile:
38 childFDs[2] = self.factory.stderrFile.fileno()
40 # processes run by inetd expect blocking sockets
41 # FIXME: maybe this should be done in process.py? are other uses of
42 # Process possibly affected by this?
43 fdesc.setBlocking(sockFD)
44 if childFDs.has_key(2):
45 fdesc.setBlocking(childFDs[2])
47 service = self.factory.service
51 # don't tell Process to change our UID/GID if it's what we
53 if uid == os.getuid():
55 if gid == os.getgid():
58 process.Process(None, service.program, service.programArgs, os.environ,
59 None, None, uid, gid, childFDs)
61 reactor.removeReader(self.transport)
62 reactor.removeWriter(self.transport)
65 class InetdFactory(ServerFactory):
66 protocol = InetdProtocol
69 def __init__(self, service):
70 self.service = service