You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inetd.py 1.9KB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. #
  4. """
  5. Twisted inetd.
  6. Maintainer: Andrew Bennetts
  7. Future Plans: Bugfixes. Specifically for UDP and Sun-RPC, which don't work
  8. correctly yet.
  9. """
  10. import os
  11. from twisted.internet import process, reactor, fdesc
  12. from twisted.internet.protocol import Protocol, ServerFactory
  13. from twisted.protocols import wire
  14. # A dict of known 'internal' services (i.e. those that don't involve spawning
  15. # another process.
  16. internalProtocols = {
  17. 'echo': wire.Echo,
  18. 'chargen': wire.Chargen,
  19. 'discard': wire.Discard,
  20. 'daytime': wire.Daytime,
  21. 'time': wire.Time,
  22. }
  23. class InetdProtocol(Protocol):
  24. """Forks a child process on connectionMade, passing the socket as fd 0."""
  25. def connectionMade(self):
  26. sockFD = self.transport.fileno()
  27. childFDs = {0: sockFD, 1: sockFD}
  28. if self.factory.stderrFile:
  29. childFDs[2] = self.factory.stderrFile.fileno()
  30. # processes run by inetd expect blocking sockets
  31. # FIXME: maybe this should be done in process.py? are other uses of
  32. # Process possibly affected by this?
  33. fdesc.setBlocking(sockFD)
  34. if 2 in childFDs:
  35. fdesc.setBlocking(childFDs[2])
  36. service = self.factory.service
  37. uid = service.user
  38. gid = service.group
  39. # don't tell Process to change our UID/GID if it's what we
  40. # already are
  41. if uid == os.getuid():
  42. uid = None
  43. if gid == os.getgid():
  44. gid = None
  45. process.Process(None, service.program, service.programArgs, os.environ,
  46. None, None, uid, gid, childFDs)
  47. reactor.removeReader(self.transport)
  48. reactor.removeWriter(self.transport)
  49. class InetdFactory(ServerFactory):
  50. protocol = InetdProtocol
  51. stderrFile = None
  52. def __init__(self, service):
  53. self.service = service