bind10_src.py.in 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. #!@PYTHON@
  2. # Copyright (C) 2010,2011 Internet Systems Consortium.
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  9. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """
  17. This file implements the Boss of Bind (BoB, or bob) program.
  18. Its purpose is to start up the BIND 10 system, and then manage the
  19. processes, by starting and stopping processes, plus restarting
  20. processes that exit.
  21. To start the system, it first runs the c-channel program (msgq), then
  22. connects to that. It then runs the configuration manager, and reads
  23. its own configuration. Then it proceeds to starting other modules.
  24. The Python subprocess module is used for starting processes, but
  25. because this is not efficient for managing groups of processes,
  26. SIGCHLD signals are caught and processed using the signal module.
  27. Most of the logic is contained in the BoB class. However, since Python
  28. requires that signal processing happen in the main thread, we do
  29. signal handling outside of that class, in the code running for
  30. __main__.
  31. """
  32. import sys; sys.path.append ('@@PYTHONPATH@@')
  33. import os
  34. # If B10_FROM_SOURCE is set in the environment, we use data files
  35. # from a directory relative to that, otherwise we use the ones
  36. # installed on the system
  37. if "B10_FROM_SOURCE" in os.environ:
  38. SPECFILE_LOCATION = os.environ["B10_FROM_SOURCE"] + "/src/bin/bind10/bob.spec"
  39. ADD_LIBEXEC_PATH = False
  40. else:
  41. PREFIX = "@prefix@"
  42. DATAROOTDIR = "@datarootdir@"
  43. SPECFILE_LOCATION = "@datadir@/@PACKAGE@/bob.spec".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  44. ADD_LIBEXEC_PATH = True
  45. import subprocess
  46. import signal
  47. import re
  48. import errno
  49. import time
  50. import select
  51. import random
  52. import socket
  53. from optparse import OptionParser, OptionValueError
  54. import io
  55. import pwd
  56. import posix
  57. import copy
  58. import isc.cc
  59. import isc.util.process
  60. import isc.net.parse
  61. import isc.log
  62. from isc.log_messages.bind10_messages import *
  63. import isc.bind10.component
  64. import isc.bind10.special_component
  65. isc.log.init("b10-boss")
  66. logger = isc.log.Logger("boss")
  67. # Pending system-wide debug level definitions, the ones we
  68. # use here are hardcoded for now
  69. DBG_PROCESS = logger.DBGLVL_TRACE_BASIC
  70. DBG_COMMANDS = logger.DBGLVL_TRACE_DETAIL
  71. # Assign this process some longer name
  72. isc.util.process.rename(sys.argv[0])
  73. # This is the version that gets displayed to the user.
  74. # The VERSION string consists of the module name, the module version
  75. # number, and the overall BIND 10 version number (set in configure.ac).
  76. VERSION = "bind10 20110223 (BIND 10 @PACKAGE_VERSION@)"
  77. # This is for boot_time of Boss
  78. _BASETIME = time.gmtime()
  79. class ProcessInfoError(Exception): pass
  80. class ProcessInfo:
  81. """Information about a process"""
  82. dev_null = open(os.devnull, "w")
  83. def __init__(self, name, args, env={}, dev_null_stdout=False,
  84. dev_null_stderr=False, uid=None, username=None):
  85. self.name = name
  86. self.args = args
  87. self.env = env
  88. self.dev_null_stdout = dev_null_stdout
  89. self.dev_null_stderr = dev_null_stderr
  90. self.uid = uid
  91. self.username = username
  92. self.process = None
  93. self.pid = None
  94. def _preexec_work(self):
  95. """Function used before running a program that needs to run as a
  96. different user."""
  97. # First, put us into a separate process group so we don't get
  98. # SIGINT signals on Ctrl-C (the boss will shut everthing down by
  99. # other means).
  100. os.setpgrp()
  101. # Second, set the user ID if one has been specified
  102. if self.uid is not None:
  103. try:
  104. posix.setuid(self.uid)
  105. except OSError as e:
  106. if e.errno == errno.EPERM:
  107. # if we failed to change user due to permission report that
  108. raise ProcessInfoError("Unable to change to user %s (uid %d)" % (self.username, self.uid))
  109. else:
  110. # otherwise simply re-raise whatever error we found
  111. raise
  112. def _spawn(self):
  113. if self.dev_null_stdout:
  114. spawn_stdout = self.dev_null
  115. else:
  116. spawn_stdout = None
  117. if self.dev_null_stderr:
  118. spawn_stderr = self.dev_null
  119. else:
  120. spawn_stderr = None
  121. # Environment variables for the child process will be a copy of those
  122. # of the boss process with any additional specific variables given
  123. # on construction (self.env).
  124. spawn_env = copy.deepcopy(os.environ)
  125. spawn_env.update(self.env)
  126. if ADD_LIBEXEC_PATH:
  127. spawn_env['PATH'] = "@@LIBEXECDIR@@:" + spawn_env['PATH']
  128. self.process = subprocess.Popen(self.args,
  129. stdin=subprocess.PIPE,
  130. stdout=spawn_stdout,
  131. stderr=spawn_stderr,
  132. close_fds=True,
  133. env=spawn_env,
  134. preexec_fn=self._preexec_work)
  135. self.pid = self.process.pid
  136. # spawn() and respawn() are the same for now, but in the future they
  137. # may have different functionality
  138. def spawn(self):
  139. self._spawn()
  140. def respawn(self):
  141. self._spawn()
  142. class CChannelConnectError(Exception): pass
  143. class ProcessStartError(Exception): pass
  144. class BoB:
  145. """Boss of BIND class."""
  146. def __init__(self, msgq_socket_file=None, data_path=None,
  147. config_filename=None, nocache=False, verbose=False, setuid=None,
  148. username=None, cmdctl_port=None, wait_time=10):
  149. """
  150. Initialize the Boss of BIND. This is a singleton (only one can run).
  151. The msgq_socket_file specifies the UNIX domain socket file that the
  152. msgq process listens on. If verbose is True, then the boss reports
  153. what it is doing.
  154. Data path and config filename are passed through to config manager
  155. (if provided) and specify the config file to be used.
  156. The cmdctl_port is passed to cmdctl and specify on which port it
  157. should listen.
  158. wait_time controls the amount of time (in seconds) that Boss waits
  159. for selected processes to initialize before continuing with the
  160. initialization. Currently this is only the configuration manager.
  161. """
  162. self.cc_session = None
  163. self.ccs = None
  164. self.curproc = None
  165. self.msgq_socket_file = msgq_socket_file
  166. self.nocache = nocache
  167. self.component_config = {}
  168. # Some time in future, it may happen that a single component has
  169. # multple processes. If so happens, name "components" may be
  170. # inapropriate. But as the code isn't probably completely ready
  171. # for it, we leave it at components for now.
  172. self.components = {}
  173. # Simply list of components that died and need to wait for a
  174. # restart. Components manage their own restart schedule now
  175. self.components_to_restart = []
  176. self.runnable = False
  177. self.uid = setuid
  178. self.username = username
  179. self.verbose = verbose
  180. self.data_path = data_path
  181. self.config_filename = config_filename
  182. self.cmdctl_port = cmdctl_port
  183. self.wait_time = wait_time
  184. self._component_configurator = isc.bind10.component.Configurator(self,
  185. isc.bind10.special_component.get_specials())
  186. # The priorities here make them start in the correct order. First
  187. # the socket creator (which would drop root privileges by then),
  188. # then message queue and after that the config manager (which uses
  189. # the config manager)
  190. self.__core_components = {
  191. 'sockcreator': {
  192. 'kind': 'core',
  193. 'special': 'sockcreator',
  194. 'priority': 200
  195. },
  196. 'msgq': {
  197. 'kind': 'core',
  198. 'special': 'msgq',
  199. 'priority': 199
  200. },
  201. 'cfgmgr': {
  202. 'kind': 'core',
  203. 'special': 'cfgmgr',
  204. 'priority': 198
  205. }
  206. }
  207. self.__started = False
  208. self.exitcode = 0
  209. # If -v was set, enable full debug logging.
  210. if self.verbose:
  211. logger.set_severity("DEBUG", 99)
  212. def __propagate_component_config(self, config):
  213. comps = dict(config)
  214. # Fill in the core components, so they stay alive
  215. for comp in self.__core_components:
  216. if comp in comps:
  217. raise Exception(comp + " is core component managed by " +
  218. "bind10 boss, do not set it")
  219. comps[comp] = self.__core_components[comp]
  220. # Update the configuration
  221. self._component_configurator.reconfigure(comps)
  222. def config_handler(self, new_config):
  223. # If this is initial update, don't do anything now, leave it to startup
  224. if not self.runnable:
  225. return
  226. logger.debug(DBG_COMMANDS, BIND10_RECEIVED_NEW_CONFIGURATION,
  227. new_config)
  228. try:
  229. if 'components' in new_config:
  230. self.__propagate_component_config(new_config['components'])
  231. return isc.config.ccsession.create_answer(0)
  232. except Exception as e:
  233. return isc.config.ccsession.create_answer(1, str(e))
  234. def get_processes(self):
  235. pids = list(self.components.keys())
  236. pids.sort()
  237. process_list = [ ]
  238. for pid in pids:
  239. process_list.append([pid, self.components[pid].name()])
  240. return process_list
  241. def _get_stats_data(self):
  242. return { "owner": "Boss",
  243. "data": { 'boot_time':
  244. time.strftime('%Y-%m-%dT%H:%M:%SZ', _BASETIME)
  245. }
  246. }
  247. def command_handler(self, command, args):
  248. logger.debug(DBG_COMMANDS, BIND10_RECEIVED_COMMAND, command)
  249. answer = isc.config.ccsession.create_answer(1, "command not implemented")
  250. if type(command) != str:
  251. answer = isc.config.ccsession.create_answer(1, "bad command")
  252. else:
  253. if command == "shutdown":
  254. self.runnable = False
  255. answer = isc.config.ccsession.create_answer(0)
  256. elif command == "getstats":
  257. answer = isc.config.ccsession.create_answer(0, self._get_stats_data())
  258. elif command == "sendstats":
  259. # send statistics data to the stats daemon immediately
  260. stats_data = self._get_stats_data()
  261. valid = self.ccs.get_module_spec().validate_statistics(
  262. True, stats_data["data"])
  263. if valid:
  264. cmd = isc.config.ccsession.create_command('set', stats_data)
  265. seq = self.cc_session.group_sendmsg(cmd, 'Stats')
  266. # Consume the answer, in case it becomes a orphan message.
  267. try:
  268. self.cc_session.group_recvmsg(False, seq)
  269. except isc.cc.session.SessionTimeout:
  270. pass
  271. answer = isc.config.ccsession.create_answer(0)
  272. else:
  273. logger.fatal(BIND10_INVALID_STATISTICS_DATA);
  274. answer = isc.config.ccsession.create_answer(
  275. 1, "specified statistics data is invalid")
  276. elif command == "ping":
  277. answer = isc.config.ccsession.create_answer(0, "pong")
  278. elif command == "show_processes":
  279. answer = isc.config.ccsession. \
  280. create_answer(0, self.get_processes())
  281. else:
  282. answer = isc.config.ccsession.create_answer(1,
  283. "Unknown command")
  284. return answer
  285. def kill_started_components(self):
  286. """
  287. Called as part of the exception handling when a process fails to
  288. start, this runs through the list of started processes, killing
  289. each one. It then clears that list.
  290. """
  291. logger.info(BIND10_KILLING_ALL_PROCESSES)
  292. for pid in self.components:
  293. logger.info(BIND10_KILL_PROCESS, self.components[pid].name())
  294. self.components[pid].kill(True)
  295. self.components = {}
  296. def _read_bind10_config(self):
  297. """
  298. Reads the parameters associated with the BoB module itself.
  299. This means the list of components we should start now.
  300. This could easily be combined into start_all_processes, but
  301. it stays because of historical reasons and because the tests
  302. replace the method sometimes.
  303. """
  304. logger.info(BIND10_READING_BOSS_CONFIGURATION)
  305. config_data = self.ccs.get_full_config()
  306. self.__propagate_component_config(config_data['components'])
  307. def log_starting(self, process, port = None, address = None):
  308. """
  309. A convenience function to output a "Starting xxx" message if the
  310. logging is set to DEBUG with debuglevel DBG_PROCESS or higher.
  311. Putting this into a separate method ensures
  312. that the output form is consistent across all processes.
  313. The process name (passed as the first argument) is put into
  314. self.curproc, and is used to indicate which process failed to
  315. start if there is an error (and is used in the "Started" message
  316. on success). The optional port and address information are
  317. appended to the message (if present).
  318. """
  319. self.curproc = process
  320. if port is None and address is None:
  321. logger.info(BIND10_STARTING_PROCESS, self.curproc)
  322. elif address is None:
  323. logger.info(BIND10_STARTING_PROCESS_PORT, self.curproc,
  324. port)
  325. else:
  326. logger.info(BIND10_STARTING_PROCESS_PORT_ADDRESS,
  327. self.curproc, address, port)
  328. def log_started(self, pid = None):
  329. """
  330. A convenience function to output a 'Started xxxx (PID yyyy)'
  331. message. As with starting_message(), this ensures a consistent
  332. format.
  333. """
  334. if pid is None:
  335. logger.debug(DBG_PROCESS, BIND10_STARTED_PROCESS, self.curproc)
  336. else:
  337. logger.debug(DBG_PROCESS, BIND10_STARTED_PROCESS_PID, self.curproc, pid)
  338. def process_running(self, msg, who):
  339. """
  340. Some processes return a message to the Boss after they have
  341. started to indicate that they are running. The form of the
  342. message is a dictionary with contents {"running:", "<process>"}.
  343. This method checks the passed message and returns True if the
  344. "who" process is contained in the message (so is presumably
  345. running). It returns False for all other conditions and will
  346. log an error if appropriate.
  347. """
  348. if msg is not None:
  349. try:
  350. if msg["running"] == who:
  351. return True
  352. else:
  353. logger.error(BIND10_STARTUP_UNEXPECTED_MESSAGE, msg)
  354. except:
  355. logger.error(BIND10_STARTUP_UNRECOGNISED_MESSAGE, msg)
  356. return False
  357. # The next few methods start the individual processes of BIND-10. They
  358. # are called via start_all_processes(). If any fail, an exception is
  359. # raised which is caught by the caller of start_all_processes(); this kills
  360. # processes started up to that point before terminating the program.
  361. def start_msgq(self):
  362. """
  363. Start the message queue and connect to the command channel.
  364. """
  365. self.log_starting("b10-msgq")
  366. msgq_proc = ProcessInfo("b10-msgq", ["b10-msgq"], self.c_channel_env,
  367. True, not self.verbose, uid=self.uid,
  368. username=self.username)
  369. msgq_proc.spawn()
  370. self.log_started(msgq_proc.pid)
  371. # Now connect to the c-channel
  372. cc_connect_start = time.time()
  373. while self.cc_session is None:
  374. # if we have been trying for "a while" give up
  375. if (time.time() - cc_connect_start) > 5:
  376. raise CChannelConnectError("Unable to connect to c-channel after 5 seconds")
  377. # try to connect, and if we can't wait a short while
  378. try:
  379. self.cc_session = isc.cc.Session(self.msgq_socket_file)
  380. except isc.cc.session.SessionError:
  381. time.sleep(0.1)
  382. # Subscribe to the message queue. The only messages we expect to receive
  383. # on this channel are once relating to process startup.
  384. self.cc_session.group_subscribe("Boss")
  385. return msgq_proc
  386. def start_cfgmgr(self):
  387. """
  388. Starts the configuration manager process
  389. """
  390. self.log_starting("b10-cfgmgr")
  391. args = ["b10-cfgmgr"]
  392. if self.data_path is not None:
  393. args.append("--data-path=" + self.data_path)
  394. if self.config_filename is not None:
  395. args.append("--config-filename=" + self.config_filename)
  396. bind_cfgd = ProcessInfo("b10-cfgmgr", args,
  397. self.c_channel_env, uid=self.uid,
  398. username=self.username)
  399. bind_cfgd.spawn()
  400. self.log_started(bind_cfgd.pid)
  401. # Wait for the configuration manager to start up as subsequent initialization
  402. # cannot proceed without it. The time to wait can be set on the command line.
  403. time_remaining = self.wait_time
  404. msg, env = self.cc_session.group_recvmsg()
  405. while time_remaining > 0 and not self.process_running(msg, "ConfigManager"):
  406. logger.debug(DBG_PROCESS, BIND10_WAIT_CFGMGR)
  407. time.sleep(1)
  408. time_remaining = time_remaining - 1
  409. msg, env = self.cc_session.group_recvmsg()
  410. if not self.process_running(msg, "ConfigManager"):
  411. raise ProcessStartError("Configuration manager process has not started")
  412. return bind_cfgd
  413. def start_ccsession(self, c_channel_env):
  414. """
  415. Start the CC Session
  416. The argument c_channel_env is unused but is supplied to keep the
  417. argument list the same for all start_xxx methods.
  418. With regards to logging, note that as the CC session is not a
  419. process, the log_starting/log_started methods are not used.
  420. """
  421. logger.info(BIND10_STARTING_CC)
  422. self.ccs = isc.config.ModuleCCSession(SPECFILE_LOCATION,
  423. self.config_handler,
  424. self.command_handler,
  425. socket_file = self.msgq_socket_file)
  426. self.ccs.start()
  427. logger.debug(DBG_PROCESS, BIND10_STARTED_CC)
  428. # A couple of utility methods for starting processes...
  429. def start_process(self, name, args, c_channel_env, port=None, address=None):
  430. """
  431. Given a set of command arguments, start the process and output
  432. appropriate log messages. If the start is successful, the process
  433. is added to the list of started processes.
  434. The port and address arguments are for log messages only.
  435. """
  436. self.log_starting(name, port, address)
  437. newproc = ProcessInfo(name, args, c_channel_env)
  438. newproc.spawn()
  439. self.log_started(newproc.pid)
  440. return newproc
  441. def register_process(self, pid, component):
  442. """
  443. Put another process into boss to watch over it. When the process
  444. dies, the component.failed() is called with the exit code.
  445. It is expected the info is a isc.bind10.component.BaseComponent
  446. subclass (or anything having the same interface).
  447. """
  448. self.components[pid] = component
  449. def start_simple(self, name):
  450. """
  451. Most of the BIND-10 processes are started with the command:
  452. <process-name> [-v]
  453. ... where -v is appended if verbose is enabled. This method
  454. generates the arguments from the name and starts the process.
  455. The port and address arguments are for log messages only.
  456. """
  457. # Set up the command arguments.
  458. args = [name]
  459. if self.verbose:
  460. args += ['-v']
  461. # ... and start the process
  462. return self.start_process(name, args, self.c_channel_env)
  463. # The next few methods start up the rest of the BIND-10 processes.
  464. # Although many of these methods are little more than a call to
  465. # start_simple, they are retained (a) for testing reasons and (b) as a place
  466. # where modifications can be made if the process start-up sequence changes
  467. # for a given process.
  468. def start_auth(self):
  469. """
  470. Start the Authoritative server
  471. """
  472. if self.uid is not None and self.__started:
  473. logger.warn(BIND10_START_AS_NON_ROOT_AUTH)
  474. authargs = ['b10-auth']
  475. if self.nocache:
  476. authargs += ['-n']
  477. if self.uid:
  478. authargs += ['-u', str(self.uid)]
  479. if self.verbose:
  480. authargs += ['-v']
  481. # ... and start
  482. return self.start_process("b10-auth", authargs, self.c_channel_env)
  483. def start_resolver(self):
  484. """
  485. Start the Resolver. At present, all these arguments and switches
  486. are pure speculation. As with the auth daemon, they should be
  487. read from the configuration database.
  488. """
  489. if self.uid is not None and self.__started:
  490. logger.warn(BIND10_START_AS_NON_ROOT_RESOLVER)
  491. self.curproc = "b10-resolver"
  492. # XXX: this must be read from the configuration manager in the future
  493. resargs = ['b10-resolver']
  494. if self.uid:
  495. resargs += ['-u', str(self.uid)]
  496. if self.verbose:
  497. resargs += ['-v']
  498. # ... and start
  499. return self.start_process("b10-resolver", resargs, self.c_channel_env)
  500. def __ld_path_hack(self):
  501. # XXX: a quick-hack workaround. xfrin/out will implicitly use
  502. # dynamically loadable data source modules, which will be installed in
  503. # $(libdir).
  504. # On some OSes (including MacOS X and *BSDs) the main process (python)
  505. # cannot find the modules unless they are located in a common shared
  506. # object path or a path in the (DY)LD_LIBRARY_PATH. We should seek
  507. # a cleaner solution, but for a short term workaround we specify the
  508. # path here, unconditionally, and without even bothering which
  509. # environment variable should be used.
  510. #
  511. # We reuse the ADD_LIBEXEC_PATH variable to see whether we need to
  512. # do this, as the conditions that make this workaround needed are
  513. # the same as for the libexec path addition
  514. # TODO: Once #1292 is finished, remove this method and the special
  515. # component, use it as normal component.
  516. env = dict(self.c_channel_env)
  517. if ADD_LIBEXEC_PATH:
  518. cur_path = os.getenv('DYLD_LIBRARY_PATH')
  519. cur_path = '' if cur_path is None else ':' + cur_path
  520. env['DYLD_LIBRARY_PATH'] = "@@LIBDIR@@" + cur_path
  521. cur_path = os.getenv('LD_LIBRARY_PATH')
  522. cur_path = '' if cur_path is None else ':' + cur_path
  523. env['LD_LIBRARY_PATH'] = "@@LIBDIR@@" + cur_path
  524. return env
  525. def start_cmdctl(self):
  526. """
  527. Starts the command control process
  528. """
  529. args = ["b10-cmdctl"]
  530. if self.cmdctl_port is not None:
  531. args.append("--port=" + str(self.cmdctl_port))
  532. if self.verbose:
  533. args.append("-v")
  534. return self.start_process("b10-cmdctl", args, self.c_channel_env,
  535. self.cmdctl_port)
  536. def start_xfrin(self):
  537. # Set up the command arguments.
  538. args = ['b10-xfrin']
  539. if self.verbose:
  540. args += ['-v']
  541. return self.start_process("b10-xfrin", args, self.__ld_path_hack())
  542. def start_xfrout(self):
  543. # Set up the command arguments.
  544. args = ['b10-xfrout']
  545. if self.verbose:
  546. args += ['-v']
  547. return self.start_process("b10-xfrout", args, self.__ld_path_hack())
  548. def start_all_components(self):
  549. """
  550. Starts up all the components. Any exception generated during the
  551. starting of the components is handled by the caller.
  552. """
  553. # Start the real core (sockcreator, msgq, cfgmgr)
  554. self._component_configurator.startup(self.__core_components)
  555. # Connect to the msgq. This is not a process, so it's not handled
  556. # inside the configurator.
  557. self.start_ccsession(self.c_channel_env)
  558. # Extract the parameters associated with Bob. This can only be
  559. # done after the CC Session is started. Note that the logging
  560. # configuration may override the "-v" switch set on the command line.
  561. self._read_bind10_config()
  562. # TODO: Return the dropping of privileges
  563. def startup(self):
  564. """
  565. Start the BoB instance.
  566. Returns None if successful, otherwise an string describing the
  567. problem.
  568. """
  569. # Try to connect to the c-channel daemon, to see if it is already
  570. # running
  571. c_channel_env = {}
  572. if self.msgq_socket_file is not None:
  573. c_channel_env["BIND10_MSGQ_SOCKET_FILE"] = self.msgq_socket_file
  574. logger.debug(DBG_PROCESS, BIND10_CHECK_MSGQ_ALREADY_RUNNING)
  575. # try to connect, and if we can't wait a short while
  576. try:
  577. self.cc_session = isc.cc.Session(self.msgq_socket_file)
  578. logger.fatal(BIND10_MSGQ_ALREADY_RUNNING)
  579. return "b10-msgq already running, or socket file not cleaned , cannot start"
  580. except isc.cc.session.SessionError:
  581. # this is the case we want, where the msgq is not running
  582. pass
  583. # Start all components. If any one fails to start, kill all started
  584. # components and exit with an error indication.
  585. try:
  586. self.c_channel_env = c_channel_env
  587. self.start_all_components()
  588. except Exception as e:
  589. self.kill_started_components()
  590. return "Unable to start " + self.curproc + ": " + str(e)
  591. # Started successfully
  592. self.runnable = True
  593. self.__started = True
  594. return None
  595. def stop_process(self, process, recipient):
  596. """
  597. Stop the given process, friendly-like. The process is the name it has
  598. (in logs, etc), the recipient is the address on msgq.
  599. """
  600. logger.info(BIND10_STOP_PROCESS, process)
  601. self.cc_session.group_sendmsg({'command': ['shutdown']}, recipient,
  602. recipient)
  603. def component_shutdown(self, exitcode=0):
  604. """
  605. Stop the Boss instance from a components' request. The exitcode
  606. indicates the desired exit code.
  607. If we did not start yet, it raises an exception, which is meant
  608. to propagate through the component and configurator to the startup
  609. routine and abort the startup imediatelly. If it is started up already,
  610. we just mark it so we terminate soon.
  611. It does set the exit code in both cases.
  612. """
  613. self.exitcode = exitcode
  614. if not self.__started:
  615. raise Exception("Component failed during startup");
  616. else:
  617. self.runnable = False
  618. def shutdown(self):
  619. """Stop the BoB instance."""
  620. logger.info(BIND10_SHUTDOWN)
  621. # first try using the BIND 10 request to stop
  622. try:
  623. self._component_configurator.shutdown()
  624. except:
  625. pass
  626. # XXX: some delay probably useful... how much is uncertain
  627. # I have changed the delay from 0.5 to 1, but sometime it's
  628. # still not enough.
  629. time.sleep(1)
  630. self.reap_children()
  631. # next try sending a SIGTERM
  632. components_to_stop = list(self.components.values())
  633. for component in components_to_stop:
  634. logger.info(BIND10_SEND_SIGTERM, component.name(), component.pid())
  635. try:
  636. component.kill()
  637. except OSError:
  638. # ignore these (usually ESRCH because the child
  639. # finally exited)
  640. pass
  641. # finally, send SIGKILL (unmaskable termination) until everybody dies
  642. while self.components:
  643. # XXX: some delay probably useful... how much is uncertain
  644. time.sleep(0.1)
  645. self.reap_children()
  646. components_to_stop = list(self.components.values())
  647. for component in components_to_stop:
  648. logger.info(BIND10_SEND_SIGKILL, component.name(),
  649. component.pid())
  650. try:
  651. component.kill(True)
  652. except OSError:
  653. # ignore these (usually ESRCH because the child
  654. # finally exited)
  655. pass
  656. logger.info(BIND10_SHUTDOWN_COMPLETE)
  657. def _get_process_exit_status(self):
  658. return os.waitpid(-1, os.WNOHANG)
  659. def reap_children(self):
  660. """Check to see if any of our child processes have exited,
  661. and note this for later handling.
  662. """
  663. while True:
  664. try:
  665. (pid, exit_status) = self._get_process_exit_status()
  666. except OSError as o:
  667. if o.errno == errno.ECHILD: break
  668. # XXX: should be impossible to get any other error here
  669. raise
  670. if pid == 0: break
  671. if pid in self.components:
  672. # One of the components we know about. Get information on it.
  673. component = self.components.pop(pid)
  674. logger.info(BIND10_PROCESS_ENDED, component.name(), pid,
  675. exit_status)
  676. if component.running() and self.runnable:
  677. # Tell it it failed. But only if it matters (we are
  678. # not shutting down and the component considers itself
  679. # to be running.
  680. component_restarted = component.failed(exit_status);
  681. # if the process wants to be restarted, but not just yet,
  682. # it returns False
  683. if not component_restarted:
  684. self.components_to_restart.append(component)
  685. else:
  686. logger.info(BIND10_UNKNOWN_CHILD_PROCESS_ENDED, pid)
  687. def restart_processes(self):
  688. """
  689. Restart any dead processes:
  690. * Returns the time when the next process is ready to be restarted.
  691. * If the server is shutting down, returns 0.
  692. * If there are no processes, returns None.
  693. The values returned can be safely passed into select() as the
  694. timeout value.
  695. """
  696. if not self.runnable:
  697. return 0
  698. still_dead = []
  699. # keep track of the first time we need to check this queue again,
  700. # if at all
  701. next_restart_time = None
  702. now = time.time()
  703. for component in self.components_to_restart:
  704. if not component.restart(now):
  705. still_dead.append(component)
  706. if next_restart_time is None or\
  707. next_restart_time > component.get_restart_time():
  708. next_restart_time = component.get_restart_time()
  709. self.components_to_restart = still_dead
  710. return next_restart_time
  711. # global variables, needed for signal handlers
  712. options = None
  713. boss_of_bind = None
  714. def reaper(signal_number, stack_frame):
  715. """A child process has died (SIGCHLD received)."""
  716. # don't do anything...
  717. # the Python signal handler has been set up to write
  718. # down a pipe, waking up our select() bit
  719. pass
  720. def get_signame(signal_number):
  721. """Return the symbolic name for a signal."""
  722. for sig in dir(signal):
  723. if sig.startswith("SIG") and sig[3].isalnum():
  724. if getattr(signal, sig) == signal_number:
  725. return sig
  726. return "Unknown signal %d" % signal_number
  727. # XXX: perhaps register atexit() function and invoke that instead
  728. def fatal_signal(signal_number, stack_frame):
  729. """We need to exit (SIGINT or SIGTERM received)."""
  730. global options
  731. global boss_of_bind
  732. logger.info(BIND10_RECEIVED_SIGNAL, get_signame(signal_number))
  733. signal.signal(signal.SIGCHLD, signal.SIG_DFL)
  734. boss_of_bind.runnable = False
  735. def process_rename(option, opt_str, value, parser):
  736. """Function that renames the process if it is requested by a option."""
  737. isc.util.process.rename(value)
  738. def parse_args(args=sys.argv[1:], Parser=OptionParser):
  739. """
  740. Function for parsing command line arguments. Returns the
  741. options object from OptionParser.
  742. """
  743. parser = Parser(version=VERSION)
  744. parser.add_option("-m", "--msgq-socket-file", dest="msgq_socket_file",
  745. type="string", default=None,
  746. help="UNIX domain socket file the b10-msgq daemon will use")
  747. parser.add_option("-n", "--no-cache", action="store_true", dest="nocache",
  748. default=False, help="disable hot-spot cache in authoritative DNS server")
  749. parser.add_option("-u", "--user", dest="user", type="string", default=None,
  750. help="Change user after startup (must run as root)")
  751. parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
  752. help="display more about what is going on")
  753. parser.add_option("--pretty-name", type="string", action="callback",
  754. callback=process_rename,
  755. help="Set the process name (displayed in ps, top, ...)")
  756. parser.add_option("-c", "--config-file", action="store",
  757. dest="config_file", default=None,
  758. help="Configuration database filename")
  759. parser.add_option("-p", "--data-path", dest="data_path",
  760. help="Directory to search for configuration files",
  761. default=None)
  762. parser.add_option("--cmdctl-port", dest="cmdctl_port", type="int",
  763. default=None, help="Port of command control")
  764. parser.add_option("--pid-file", dest="pid_file", type="string",
  765. default=None,
  766. help="file to dump the PID of the BIND 10 process")
  767. parser.add_option("-w", "--wait", dest="wait_time", type="int",
  768. default=10, help="Time (in seconds) to wait for config manager to start up")
  769. (options, args) = parser.parse_args(args)
  770. if options.cmdctl_port is not None:
  771. try:
  772. isc.net.parse.port_parse(options.cmdctl_port)
  773. except ValueError as e:
  774. parser.error(e)
  775. if args:
  776. parser.print_help()
  777. sys.exit(1)
  778. return options
  779. def dump_pid(pid_file):
  780. """
  781. Dump the PID of the current process to the specified file. If the given
  782. file is None this function does nothing. If the file already exists,
  783. the existing content will be removed. If a system error happens in
  784. creating or writing to the file, the corresponding exception will be
  785. propagated to the caller.
  786. """
  787. if pid_file is None:
  788. return
  789. f = open(pid_file, "w")
  790. f.write('%d\n' % os.getpid())
  791. f.close()
  792. def unlink_pid_file(pid_file):
  793. """
  794. Remove the given file, which is basically expected to be the PID file
  795. created by dump_pid(). The specified may or may not exist; if it
  796. doesn't this function does nothing. Other system level errors in removing
  797. the file will be propagated as the corresponding exception.
  798. """
  799. if pid_file is None:
  800. return
  801. try:
  802. os.unlink(pid_file)
  803. except OSError as error:
  804. if error.errno is not errno.ENOENT:
  805. raise
  806. def main():
  807. global options
  808. global boss_of_bind
  809. # Enforce line buffering on stdout, even when not a TTY
  810. sys.stdout = io.TextIOWrapper(sys.stdout.detach(), line_buffering=True)
  811. options = parse_args()
  812. # Check user ID.
  813. setuid = None
  814. username = None
  815. if options.user:
  816. # Try getting information about the user, assuming UID passed.
  817. try:
  818. pw_ent = pwd.getpwuid(int(options.user))
  819. setuid = pw_ent.pw_uid
  820. username = pw_ent.pw_name
  821. except ValueError:
  822. pass
  823. except KeyError:
  824. pass
  825. # Next try getting information about the user, assuming user name
  826. # passed.
  827. # If the information is both a valid user name and user number, we
  828. # prefer the name because we try it second. A minor point, hopefully.
  829. try:
  830. pw_ent = pwd.getpwnam(options.user)
  831. setuid = pw_ent.pw_uid
  832. username = pw_ent.pw_name
  833. except KeyError:
  834. pass
  835. if setuid is None:
  836. logger.fatal(BIND10_INVALID_USER, options.user)
  837. sys.exit(1)
  838. # Announce startup.
  839. logger.info(BIND10_STARTING, VERSION)
  840. # Create wakeup pipe for signal handlers
  841. wakeup_pipe = os.pipe()
  842. signal.set_wakeup_fd(wakeup_pipe[1])
  843. # Set signal handlers for catching child termination, as well
  844. # as our own demise.
  845. signal.signal(signal.SIGCHLD, reaper)
  846. signal.siginterrupt(signal.SIGCHLD, False)
  847. signal.signal(signal.SIGINT, fatal_signal)
  848. signal.signal(signal.SIGTERM, fatal_signal)
  849. # Block SIGPIPE, as we don't want it to end this process
  850. signal.signal(signal.SIGPIPE, signal.SIG_IGN)
  851. # Go bob!
  852. boss_of_bind = BoB(options.msgq_socket_file, options.data_path,
  853. options.config_file, options.nocache, options.verbose,
  854. setuid, username, options.cmdctl_port,
  855. options.wait_time)
  856. startup_result = boss_of_bind.startup()
  857. if startup_result:
  858. logger.fatal(BIND10_STARTUP_ERROR, startup_result)
  859. sys.exit(1)
  860. logger.info(BIND10_STARTUP_COMPLETE)
  861. dump_pid(options.pid_file)
  862. # In our main loop, we check for dead processes or messages
  863. # on the c-channel.
  864. wakeup_fd = wakeup_pipe[0]
  865. ccs_fd = boss_of_bind.ccs.get_socket().fileno()
  866. while boss_of_bind.runnable:
  867. # clean up any processes that exited
  868. boss_of_bind.reap_children()
  869. next_restart = boss_of_bind.restart_processes()
  870. if next_restart is None:
  871. wait_time = None
  872. else:
  873. wait_time = max(next_restart - time.time(), 0)
  874. # select() can raise EINTR when a signal arrives,
  875. # even if they are resumable, so we have to catch
  876. # the exception
  877. try:
  878. (rlist, wlist, xlist) = select.select([wakeup_fd, ccs_fd], [], [],
  879. wait_time)
  880. except select.error as err:
  881. if err.args[0] == errno.EINTR:
  882. (rlist, wlist, xlist) = ([], [], [])
  883. else:
  884. logger.fatal(BIND10_SELECT_ERROR, err)
  885. break
  886. for fd in rlist + xlist:
  887. if fd == ccs_fd:
  888. try:
  889. boss_of_bind.ccs.check_command()
  890. except isc.cc.session.ProtocolError:
  891. logger.fatal(BIND10_MSGQ_DISAPPEARED)
  892. self.runnable = False
  893. break
  894. elif fd == wakeup_fd:
  895. os.read(wakeup_fd, 32)
  896. # shutdown
  897. signal.signal(signal.SIGCHLD, signal.SIG_DFL)
  898. boss_of_bind.shutdown()
  899. unlink_pid_file(options.pid_file)
  900. sys.exit(0)
  901. if __name__ == "__main__":
  902. main()