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