Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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 2.0KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 fdesc, process, reactor
  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(
  46. None,
  47. service.program,
  48. service.programArgs,
  49. os.environ,
  50. None,
  51. None,
  52. uid,
  53. gid,
  54. childFDs,
  55. )
  56. reactor.removeReader(self.transport)
  57. reactor.removeWriter(self.transport)
  58. class InetdFactory(ServerFactory):
  59. protocol = InetdProtocol
  60. stderrFile = None
  61. def __init__(self, service):
  62. self.service = service