~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/runner/inetd.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
 
5
 
 
6
"""Twisted inetd.
 
7
 
 
8
Stability: semi-stable
 
9
 
 
10
Maintainer: U{Andrew Bennetts<mailto:spiv@twistedmatrix.com>}
 
11
 
 
12
Future Plans: Bugfixes.  Specifically for UDP and Sun-RPC, which don't work
 
13
correctly yet.
 
14
"""
 
15
 
 
16
import os
 
17
 
 
18
from twisted.internet import process, reactor, fdesc
 
19
from twisted.internet.protocol import Protocol, ServerFactory
 
20
from twisted.protocols import wire
 
21
 
 
22
# A dict of known 'internal' services (i.e. those that don't involve spawning
 
23
# another process.
 
24
internalProtocols = {
 
25
    'echo': wire.Echo,
 
26
    'chargen': wire.Chargen,
 
27
    'discard': wire.Discard,
 
28
    'daytime': wire.Daytime,
 
29
    'time': wire.Time,
 
30
}
 
31
            
 
32
 
 
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()
 
40
 
 
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])
 
47
 
 
48
        service = self.factory.service
 
49
        uid = service.user
 
50
        gid = service.group
 
51
 
 
52
        # don't tell Process to change our UID/GID if it's what we
 
53
        # already are
 
54
        if uid == os.getuid():
 
55
            uid = None
 
56
        if gid == os.getgid():
 
57
            gid = None
 
58
 
 
59
        process.Process(None, service.program, service.programArgs, os.environ,
 
60
                        None, None, uid, gid, childFDs)
 
61
 
 
62
        reactor.removeReader(self.transport)
 
63
        reactor.removeWriter(self.transport)
 
64
                        
 
65
 
 
66
class InetdFactory(ServerFactory):
 
67
    protocol = InetdProtocol
 
68
    stderrFile = None
 
69
    
 
70
    def __init__(self, service):
 
71
        self.service = service