special_component.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. from isc.bind10.component import Component, BaseComponent
  16. import isc.bind10.sockcreator
  17. from bind10_config import LIBEXECDIR
  18. import os
  19. import posix
  20. import isc.log
  21. from isc.log_messages.bind10_messages import *
  22. logger = isc.log.Logger("boss")
  23. class SockCreator(BaseComponent):
  24. """
  25. The socket creator component. Will start and stop the socket creator
  26. accordingly.
  27. Note: _creator shouldn't be reset explicitly once created. The
  28. underlying Popen object would then wait() the child process internally,
  29. which breaks the assumption of the boss, who is expecting to see
  30. the process die in waitpid().
  31. """
  32. def __init__(self, process, boss, kind, address=None, params=None):
  33. BaseComponent.__init__(self, boss, kind)
  34. self.__creator = None
  35. def _start_internal(self):
  36. self._boss.curproc = 'b10-sockcreator'
  37. self.__creator = isc.bind10.sockcreator.Creator(LIBEXECDIR + ':' +
  38. os.environ['PATH'])
  39. self._boss.register_process(self.pid(), self)
  40. self._boss.log_started(self.pid())
  41. def _stop_internal(self):
  42. self.__creator.terminate()
  43. def name(self):
  44. return "Socket creator"
  45. def pid(self):
  46. """
  47. Pid of the socket creator. It is provided differently from a usual
  48. component.
  49. """
  50. return self.__creator.pid() if self.__creator else None
  51. def kill(self, forcefull=False):
  52. # We don't really care about forcefull here
  53. if self.__creator:
  54. self.__creator.kill()
  55. class Msgq(Component):
  56. """
  57. The message queue. Starting is passed to boss, stopping is not supported
  58. and we leave the boss kill it by signal.
  59. """
  60. def __init__(self, process, boss, kind, address=None, params=None):
  61. Component.__init__(self, process, boss, kind, None, None,
  62. boss.start_msgq)
  63. def _stop_internal(self):
  64. """
  65. We can't really stop the message queue, as many processes may need
  66. it for their shutdown and it doesn't have a shutdown command anyway.
  67. But as it is stateless, it's OK to kill it.
  68. So we disable this method (as the only time it could be called is
  69. during shutdown) and wait for the boss to kill it in the next shutdown
  70. step.
  71. This actually breaks the recommendation at Component we shouldn't
  72. override its methods one by one. This is a special case, because
  73. we don't provide a different implementation, we completely disable
  74. the method by providing an empty one. This can't hurt the internals.
  75. """
  76. pass
  77. class CfgMgr(Component):
  78. def __init__(self, process, boss, kind, address=None, params=None):
  79. Component.__init__(self, process, boss, kind, 'ConfigManager',
  80. None, boss.start_cfgmgr)
  81. class Auth(Component):
  82. def __init__(self, process, boss, kind, address=None, params=None):
  83. Component.__init__(self, process, boss, kind, 'Auth', None,
  84. boss.start_auth)
  85. class Resolver(Component):
  86. def __init__(self, process, boss, kind, address=None, params=None):
  87. Component.__init__(self, process, boss, kind, 'Resolver', None,
  88. boss.start_resolver)
  89. class CmdCtl(Component):
  90. def __init__(self, process, boss, kind, address=None, params=None):
  91. Component.__init__(self, process, boss, kind, 'Cmdctl', None,
  92. boss.start_cmdctl)
  93. class XfrIn(Component):
  94. def __init__(self, process, boss, kind, address=None, params=None):
  95. Component.__init__(self, process, boss, kind, 'Xfrin', None,
  96. boss.start_xfrin)
  97. class SetUID(BaseComponent):
  98. """
  99. This is a pseudo-component which drops root privileges when started
  100. and sets the uid stored in boss.
  101. This component does nothing when stopped.
  102. """
  103. def __init__(self, process, boss, kind, address=None, params=None):
  104. BaseComponent.__init__(self, boss, kind)
  105. self.uid = boss.uid
  106. def _start_internal(self):
  107. if self.uid is not None:
  108. logger.info(BIND10_SETUID, self.uid)
  109. posix.setuid(self.uid)
  110. def _stop_internal(self): pass
  111. def kill(self, forefull=False): pass
  112. def name(self):
  113. return "Set UID"
  114. def pid(self):
  115. return None
  116. def get_specials():
  117. """
  118. List of specially started components. Each one should be the class than can
  119. be created for that component.
  120. """
  121. return {
  122. 'sockcreator': SockCreator,
  123. 'msgq': Msgq,
  124. 'cfgmgr': CfgMgr,
  125. # TODO: Should these be replaced by configuration in config manager only?
  126. # They should not have any parameters anyway
  127. 'auth': Auth,
  128. 'resolver': Resolver,
  129. 'cmdctl': CmdCtl,
  130. # FIXME: Temporary workaround before #1292 is done
  131. 'xfrin': XfrIn,
  132. # TODO: Remove when not needed, workaround before sockcreator works
  133. 'setuid': SetUID
  134. }