bind10.py.in 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. else:
  40. PREFIX = "@prefix@"
  41. DATAROOTDIR = "@datarootdir@"
  42. SPECFILE_LOCATION = "@datadir@/@PACKAGE@/bob.spec".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  43. import subprocess
  44. import signal
  45. import re
  46. import errno
  47. import time
  48. import select
  49. import random
  50. import socket
  51. from optparse import OptionParser, OptionValueError
  52. import io
  53. import pwd
  54. import posix
  55. import isc.cc
  56. import isc.util.process
  57. import isc.net.parse
  58. # Assign this process some longer name
  59. isc.util.process.rename(sys.argv[0])
  60. # This is the version that gets displayed to the user.
  61. # The VERSION string consists of the module name, the module version
  62. # number, and the overall BIND 10 version number (set in configure.ac).
  63. VERSION = "bind10 20110223 (BIND 10 @PACKAGE_VERSION@)"
  64. # This is for bind10.boottime of stats module
  65. _BASETIME = time.gmtime()
  66. class RestartSchedule:
  67. """
  68. Keeps state when restarting something (in this case, a process).
  69. When a process dies unexpectedly, we need to restart it. However, if
  70. it fails to restart for some reason, then we should not simply keep
  71. restarting it at high speed.
  72. A more sophisticated algorithm can be developed, but for now we choose
  73. a simple set of rules:
  74. * If a process was been running for >=10 seconds, we restart it
  75. right away.
  76. * If a process was running for <10 seconds, we wait until 10 seconds
  77. after it was started.
  78. To avoid programs getting into lockstep, we use a normal distribution
  79. to avoid being restarted at exactly 10 seconds."""
  80. def __init__(self, restart_frequency=10.0):
  81. self.restart_frequency = restart_frequency
  82. self.run_start_time = None
  83. self.run_stop_time = None
  84. self.restart_time = None
  85. def set_run_start_time(self, when=None):
  86. if when is None:
  87. when = time.time()
  88. self.run_start_time = when
  89. sigma = self.restart_frequency * 0.05
  90. self.restart_time = when + random.normalvariate(self.restart_frequency,
  91. sigma)
  92. def set_run_stop_time(self, when=None):
  93. """We don't actually do anything with stop time now, but it
  94. might be useful for future algorithms."""
  95. if when is None:
  96. when = time.time()
  97. self.run_stop_time = when
  98. def get_restart_time(self, when=None):
  99. if when is None:
  100. when = time.time()
  101. return max(when, self.restart_time)
  102. class ProcessInfoError(Exception): pass
  103. class ProcessInfo:
  104. """Information about a process"""
  105. dev_null = open(os.devnull, "w")
  106. def __init__(self, name, args, env={}, dev_null_stdout=False,
  107. dev_null_stderr=False, uid=None, username=None):
  108. self.name = name
  109. self.args = args
  110. self.env = env
  111. self.dev_null_stdout = dev_null_stdout
  112. self.dev_null_stderr = dev_null_stderr
  113. self.restart_schedule = RestartSchedule()
  114. self.uid = uid
  115. self.username = username
  116. self.process = None
  117. self.pid = None
  118. def _preexec_work(self):
  119. """Function used before running a program that needs to run as a
  120. different user."""
  121. # First, put us into a separate process group so we don't get
  122. # SIGINT signals on Ctrl-C (the boss will shut everthing down by
  123. # other means).
  124. os.setpgrp()
  125. # Second, set the user ID if one has been specified
  126. if self.uid is not None:
  127. try:
  128. posix.setuid(self.uid)
  129. except OSError as e:
  130. if e.errno == errno.EPERM:
  131. # if we failed to change user due to permission report that
  132. raise ProcessInfoError("Unable to change to user %s (uid %d)" % (self.username, self.uid))
  133. else:
  134. # otherwise simply re-raise whatever error we found
  135. raise
  136. def _spawn(self):
  137. if self.dev_null_stdout:
  138. spawn_stdout = self.dev_null
  139. else:
  140. spawn_stdout = None
  141. if self.dev_null_stderr:
  142. spawn_stderr = self.dev_null
  143. else:
  144. spawn_stderr = None
  145. # Environment variables for the child process will be a copy of those
  146. # of the boss process with any additional specific variables given
  147. # on construction (self.env).
  148. spawn_env = os.environ
  149. spawn_env.update(self.env)
  150. if 'B10_FROM_SOURCE' not in os.environ:
  151. spawn_env['PATH'] = "@@LIBEXECDIR@@:" + spawn_env['PATH']
  152. self.process = subprocess.Popen(self.args,
  153. stdin=subprocess.PIPE,
  154. stdout=spawn_stdout,
  155. stderr=spawn_stderr,
  156. close_fds=True,
  157. env=spawn_env,
  158. preexec_fn=self._preexec_work)
  159. self.pid = self.process.pid
  160. self.restart_schedule.set_run_start_time()
  161. # spawn() and respawn() are the same for now, but in the future they
  162. # may have different functionality
  163. def spawn(self):
  164. self._spawn()
  165. def respawn(self):
  166. self._spawn()
  167. class CChannelConnectError(Exception): pass
  168. class BoB:
  169. """Boss of BIND class."""
  170. def __init__(self, msgq_socket_file=None, nocache=False, verbose=False,
  171. setuid=None, username=None):
  172. """
  173. Initialize the Boss of BIND. This is a singleton (only one can run).
  174. The msgq_socket_file specifies the UNIX domain socket file that the
  175. msgq process listens on. If verbose is True, then the boss reports
  176. what it is doing.
  177. """
  178. self.cc_session = None
  179. self.ccs = None
  180. self.cfg_start_auth = True
  181. self.cfg_start_resolver = False
  182. self.started_auth_family = False
  183. self.started_resolver_family = False
  184. self.curproc = None
  185. self.dead_processes = {}
  186. self.msgq_socket_file = msgq_socket_file
  187. self.nocache = nocache
  188. self.processes = {}
  189. self.expected_shutdowns = {}
  190. self.runnable = False
  191. self.uid = setuid
  192. self.username = username
  193. self.verbose = verbose
  194. def config_handler(self, new_config):
  195. # If this is initial update, don't do anything now, leave it to startup
  196. if not self.runnable:
  197. return
  198. # Now we declare few functions used only internally here. Besides the
  199. # benefit of not polluting the name space, they are closures, so we
  200. # don't need to pass some variables
  201. def start_stop(name, started, start, stop):
  202. if not'start_' + name in new_config:
  203. return
  204. if new_config['start_' + name]:
  205. if not started:
  206. if self.uid is not None:
  207. sys.stderr.write("[bind10] Starting " + name + " as " +
  208. "a user, not root. This might fail.\n")
  209. start()
  210. else:
  211. stop()
  212. # These four functions are passed to start_stop (smells like functional
  213. # programming little bit)
  214. def resolver_on():
  215. self.start_resolver(self.c_channel_env)
  216. self.started_resolver_family = True
  217. def resolver_off():
  218. self.stop_resolver()
  219. self.started_resolver_family = False
  220. def auth_on():
  221. self.start_auth(self.c_channel_env)
  222. self.start_xfrout(self.c_channel_env)
  223. self.start_xfrin(self.c_channel_env)
  224. self.start_zonemgr(self.c_channel_env)
  225. self.started_auth_family = True
  226. def auth_off():
  227. self.stop_zonemgr()
  228. self.stop_xfrin()
  229. self.stop_xfrout()
  230. self.stop_auth()
  231. self.started_auth_family = False
  232. # The real code of the config handler function follows here
  233. if self.verbose:
  234. sys.stdout.write("[bind10] Handling new configuration: " +
  235. str(new_config) + "\n")
  236. start_stop('resolver', self.started_resolver_family, resolver_on,
  237. resolver_off)
  238. start_stop('auth', self.started_auth_family, auth_on, auth_off)
  239. answer = isc.config.ccsession.create_answer(0)
  240. return answer
  241. def get_processes(self):
  242. pids = list(self.processes.keys())
  243. pids.sort()
  244. process_list = [ ]
  245. for pid in pids:
  246. process_list.append([pid, self.processes[pid].name])
  247. return process_list
  248. def command_handler(self, command, args):
  249. if self.verbose:
  250. sys.stdout.write("[bind10] Boss got command: " + command + "\n")
  251. answer = isc.config.ccsession.create_answer(1, "command not implemented")
  252. if type(command) != str:
  253. answer = isc.config.ccsession.create_answer(1, "bad command")
  254. else:
  255. if command == "shutdown":
  256. self.runnable = False
  257. answer = isc.config.ccsession.create_answer(0)
  258. elif command == "ping":
  259. answer = isc.config.ccsession.create_answer(0, "pong")
  260. elif command == "show_processes":
  261. answer = isc.config.ccsession. \
  262. create_answer(0, self.get_processes())
  263. else:
  264. answer = isc.config.ccsession.create_answer(1,
  265. "Unknown command")
  266. return answer
  267. def kill_started_processes(self):
  268. """
  269. Called as part of the exception handling when a process fails to
  270. start, this runs through the list of started processes, killing
  271. each one. It then clears that list.
  272. """
  273. if self.verbose:
  274. sys.stdout.write("[bind10] killing started processes:\n")
  275. for pid in self.processes:
  276. if self.verbose:
  277. sys.stdout.write("[bind10] - %s\n" % self.processes[pid].name)
  278. self.processes[pid].process.kill()
  279. self.processes = {}
  280. def read_bind10_config(self):
  281. """
  282. Reads the parameters associated with the BoB module itself.
  283. At present these are the components to start although arguably this
  284. information should be in the configuration for the appropriate
  285. module itself. (However, this would cause difficulty in the case of
  286. xfrin/xfrout and zone manager as we don't need to start those if we
  287. are not running the authoritative server.)
  288. """
  289. if self.verbose:
  290. sys.stdout.write("[bind10] Reading Boss configuration:\n")
  291. config_data = self.ccs.get_full_config()
  292. self.cfg_start_auth = config_data.get("start_auth")
  293. self.cfg_start_resolver = config_data.get("start_resolver")
  294. if self.verbose:
  295. sys.stdout.write("[bind10] - start_auth: %s\n" %
  296. str(self.cfg_start_auth))
  297. sys.stdout.write("[bind10] - start_resolver: %s\n" %
  298. str(self.cfg_start_resolver))
  299. def log_starting(self, process, port = None, address = None):
  300. """
  301. A convenience function to output a "Starting xxx" message if the
  302. verbose option is set. Putting this into a separate method ensures
  303. that the output form is consistent across all processes.
  304. The process name (passed as the first argument) is put into
  305. self.curproc, and is used to indicate which process failed to
  306. start if there is an error (and is used in the "Started" message
  307. on success). The optional port and address information are
  308. appended to the message (if present).
  309. """
  310. self.curproc = process
  311. if self.verbose:
  312. sys.stdout.write("[bind10] Starting %s" % self.curproc)
  313. if port is not None:
  314. sys.stdout.write(" on port %d" % port)
  315. if address is not None:
  316. sys.stdout.write(" (address %s)" % str(address))
  317. sys.stdout.write("\n")
  318. def log_started(self, pid = None):
  319. """
  320. A convenience function to output a 'Started xxxx (PID yyyy)'
  321. message. As with starting_message(), this ensures a consistent
  322. format.
  323. """
  324. if self.verbose:
  325. sys.stdout.write("[bind10] Started %s" % self.curproc)
  326. if pid is not None:
  327. sys.stdout.write(" (PID %d)" % pid)
  328. sys.stdout.write("\n")
  329. # The next few methods start the individual processes of BIND-10. They
  330. # are called via start_all_processes(). If any fail, an exception is
  331. # raised which is caught by the caller of start_all_processes(); this kills
  332. # processes started up to that point before terminating the program.
  333. def start_msgq(self, c_channel_env):
  334. """
  335. Start the message queue and connect to the command channel.
  336. """
  337. self.log_starting("b10-msgq")
  338. c_channel = ProcessInfo("b10-msgq", ["b10-msgq"], c_channel_env,
  339. True, not self.verbose, uid=self.uid,
  340. username=self.username)
  341. c_channel.spawn()
  342. self.processes[c_channel.pid] = c_channel
  343. self.log_started(c_channel.pid)
  344. # Now connect to the c-channel
  345. cc_connect_start = time.time()
  346. while self.cc_session is None:
  347. # if we have been trying for "a while" give up
  348. if (time.time() - cc_connect_start) > 5:
  349. raise CChannelConnectError("Unable to connect to c-channel after 5 seconds")
  350. # try to connect, and if we can't wait a short while
  351. try:
  352. self.cc_session = isc.cc.Session(self.msgq_socket_file)
  353. except isc.cc.session.SessionError:
  354. time.sleep(0.1)
  355. def start_cfgmgr(self, c_channel_env):
  356. """
  357. Starts the configuration manager process
  358. """
  359. self.log_starting("b10-cfgmgr")
  360. bind_cfgd = ProcessInfo("b10-cfgmgr", ["b10-cfgmgr"],
  361. c_channel_env, uid=self.uid,
  362. username=self.username)
  363. bind_cfgd.spawn()
  364. self.processes[bind_cfgd.pid] = bind_cfgd
  365. self.log_started(bind_cfgd.pid)
  366. # sleep until b10-cfgmgr is fully up and running, this is a good place
  367. # to have a (short) timeout on synchronized groupsend/receive
  368. # TODO: replace the sleep by a listen for ConfigManager started
  369. # message
  370. time.sleep(1)
  371. def start_ccsession(self, c_channel_env):
  372. """
  373. Start the CC Session
  374. The argument c_channel_env is unused but is supplied to keep the
  375. argument list the same for all start_xxx methods.
  376. """
  377. self.log_starting("ccsession")
  378. self.ccs = isc.config.ModuleCCSession(SPECFILE_LOCATION,
  379. self.config_handler, self.command_handler)
  380. self.ccs.start()
  381. self.log_started()
  382. # A couple of utility methods for starting processes...
  383. def start_process(self, name, args, c_channel_env, port=None, address=None):
  384. """
  385. Given a set of command arguments, start the process and output
  386. appropriate log messages. If the start is successful, the process
  387. is added to the list of started processes.
  388. The port and address arguments are for log messages only.
  389. """
  390. self.log_starting(name, port, address)
  391. newproc = ProcessInfo(name, args, c_channel_env)
  392. newproc.spawn()
  393. self.processes[newproc.pid] = newproc
  394. self.log_started(newproc.pid)
  395. def start_simple(self, name, c_channel_env, port=None, address=None):
  396. """
  397. Most of the BIND-10 processes are started with the command:
  398. <process-name> [-v]
  399. ... where -v is appended if verbose is enabled. This method
  400. generates the arguments from the name and starts the process.
  401. The port and address arguments are for log messages only.
  402. """
  403. # Set up the command arguments.
  404. args = [name]
  405. if self.verbose:
  406. args += ['-v']
  407. # ... and start the process
  408. self.start_process(name, args, c_channel_env, port, address)
  409. # The next few methods start up the rest of the BIND-10 processes.
  410. # Although many of these methods are little more than a call to
  411. # start_simple, they are retained (a) for testing reasons and (b) as a place
  412. # where modifications can be made if the process start-up sequence changes
  413. # for a given process.
  414. def start_auth(self, c_channel_env):
  415. """
  416. Start the Authoritative server
  417. """
  418. authargs = ['b10-auth']
  419. if self.nocache:
  420. authargs += ['-n']
  421. if self.uid:
  422. authargs += ['-u', str(self.uid)]
  423. if self.verbose:
  424. authargs += ['-v']
  425. # ... and start
  426. self.start_process("b10-auth", authargs, c_channel_env)
  427. def start_resolver(self, c_channel_env):
  428. """
  429. Start the Resolver. At present, all these arguments and switches
  430. are pure speculation. As with the auth daemon, they should be
  431. read from the configuration database.
  432. """
  433. self.curproc = "b10-resolver"
  434. # XXX: this must be read from the configuration manager in the future
  435. resargs = ['b10-resolver']
  436. if self.uid:
  437. resargs += ['-u', str(self.uid)]
  438. if self.verbose:
  439. resargs += ['-v']
  440. # ... and start
  441. self.start_process("b10-resolver", resargs, c_channel_env)
  442. def start_xfrout(self, c_channel_env):
  443. self.start_simple("b10-xfrout", c_channel_env)
  444. def start_xfrin(self, c_channel_env):
  445. self.start_simple("b10-xfrin", c_channel_env)
  446. def start_zonemgr(self, c_channel_env):
  447. self.start_simple("b10-zonemgr", c_channel_env)
  448. def start_stats(self, c_channel_env):
  449. self.start_simple("b10-stats", c_channel_env)
  450. def start_cmdctl(self, c_channel_env):
  451. # XXX: we hardcode port 8080
  452. self.start_simple("b10-cmdctl", c_channel_env, 8080)
  453. def start_all_processes(self):
  454. """
  455. Starts up all the processes. Any exception generated during the
  456. starting of the processes is handled by the caller.
  457. """
  458. c_channel_env = self.c_channel_env
  459. self.start_msgq(c_channel_env)
  460. self.start_cfgmgr(c_channel_env)
  461. self.start_ccsession(c_channel_env)
  462. # Extract the parameters associated with Bob. This can only be
  463. # done after the CC Session is started.
  464. self.read_bind10_config()
  465. # Continue starting the processes. The authoritative server (if
  466. # selected):
  467. if self.cfg_start_auth:
  468. self.start_auth(c_channel_env)
  469. # ... and resolver (if selected):
  470. if self.cfg_start_resolver:
  471. self.start_resolver(c_channel_env)
  472. self.started_resolver_family = True
  473. # Everything after the main components can run as non-root.
  474. # TODO: this is only temporary - once the privileged socket creator is
  475. # fully working, nothing else will run as root.
  476. if self.uid is not None:
  477. posix.setuid(self.uid)
  478. # xfrin/xfrout and the zone manager are only meaningful if the
  479. # authoritative server has been started.
  480. if self.cfg_start_auth:
  481. self.start_xfrout(c_channel_env)
  482. self.start_xfrin(c_channel_env)
  483. self.start_zonemgr(c_channel_env)
  484. self.started_auth_family = True
  485. # ... and finally start the remaining processes
  486. self.start_stats(c_channel_env)
  487. self.start_cmdctl(c_channel_env)
  488. def startup(self):
  489. """
  490. Start the BoB instance.
  491. Returns None if successful, otherwise an string describing the
  492. problem.
  493. """
  494. # Try to connect to the c-channel daemon, to see if it is already
  495. # running
  496. c_channel_env = {}
  497. if self.msgq_socket_file is not None:
  498. c_channel_env["BIND10_MSGQ_SOCKET_FILE"] = self.msgq_socket_file
  499. if self.verbose:
  500. sys.stdout.write("[bind10] Checking for already running b10-msgq\n")
  501. # try to connect, and if we can't wait a short while
  502. try:
  503. self.cc_session = isc.cc.Session(self.msgq_socket_file)
  504. return "b10-msgq already running, or socket file not cleaned , cannot start"
  505. except isc.cc.session.SessionError:
  506. # this is the case we want, where the msgq is not running
  507. pass
  508. # Start all processes. If any one fails to start, kill all started
  509. # processes and exit with an error indication.
  510. try:
  511. self.c_channel_env = c_channel_env
  512. self.start_all_processes()
  513. except Exception as e:
  514. self.kill_started_processes()
  515. return "Unable to start " + self.curproc + ": " + str(e)
  516. # Started successfully
  517. self.runnable = True
  518. return None
  519. def stop_all_processes(self):
  520. """Stop all processes."""
  521. cmd = { "command": ['shutdown']}
  522. self.cc_session.group_sendmsg(cmd, 'Cmdctl', 'Cmdctl')
  523. self.cc_session.group_sendmsg(cmd, "ConfigManager", "ConfigManager")
  524. self.cc_session.group_sendmsg(cmd, "Auth", "Auth")
  525. self.cc_session.group_sendmsg(cmd, "Resolver", "Resolver")
  526. self.cc_session.group_sendmsg(cmd, "Xfrout", "Xfrout")
  527. self.cc_session.group_sendmsg(cmd, "Xfrin", "Xfrin")
  528. self.cc_session.group_sendmsg(cmd, "Zonemgr", "Zonemgr")
  529. self.cc_session.group_sendmsg(cmd, "Stats", "Stats")
  530. def stop_process(self, process, recipient):
  531. """
  532. Stop the given process, friendly-like. The process is the name it has
  533. (in logs, etc), the recipient is the address on msgq.
  534. """
  535. if self.verbose:
  536. sys.stdout.write("[bind10] Asking %s to terminate\n" % process)
  537. # TODO: Some timeout to solve processes that don't want to die would
  538. # help. We can even store it in the dict, it is used only as a set
  539. self.expected_shutdowns[process] = 1
  540. # Ask the process to die willingly
  541. self.cc_session.group_sendmsg({'command': ['shutdown']}, recipient,
  542. recipient)
  543. # Series of stop_process wrappers
  544. def stop_resolver(self):
  545. self.stop_process('b10-resolver', 'Resolver')
  546. def stop_auth(self):
  547. self.stop_process('b10-auth', 'Auth')
  548. def stop_xfrout(self):
  549. self.stop_process('b10-xfrout', 'Xfrout')
  550. def stop_xfrin(self):
  551. self.stop_process('b10-xfrin', 'Xfrin')
  552. def stop_zonemgr(self):
  553. self.stop_process('b10-zonemgr', 'Zonemgr')
  554. def shutdown(self):
  555. """Stop the BoB instance."""
  556. if self.verbose:
  557. sys.stdout.write("[bind10] Stopping the server.\n")
  558. # first try using the BIND 10 request to stop
  559. try:
  560. self.stop_all_processes()
  561. except:
  562. pass
  563. # XXX: some delay probably useful... how much is uncertain
  564. # I have changed the delay from 0.5 to 1, but sometime it's
  565. # still not enough.
  566. time.sleep(1)
  567. self.reap_children()
  568. # next try sending a SIGTERM
  569. processes_to_stop = list(self.processes.values())
  570. for proc_info in processes_to_stop:
  571. if self.verbose:
  572. sys.stdout.write("[bind10] Sending SIGTERM to %s (PID %d).\n" %
  573. (proc_info.name, proc_info.pid))
  574. try:
  575. proc_info.process.terminate()
  576. except OSError:
  577. # ignore these (usually ESRCH because the child
  578. # finally exited)
  579. pass
  580. # finally, send SIGKILL (unmaskable termination) until everybody dies
  581. while self.processes:
  582. # XXX: some delay probably useful... how much is uncertain
  583. time.sleep(0.1)
  584. self.reap_children()
  585. processes_to_stop = list(self.processes.values())
  586. for proc_info in processes_to_stop:
  587. if self.verbose:
  588. sys.stdout.write("[bind10] Sending SIGKILL to %s (PID %d).\n" %
  589. (proc_info.name, proc_info.pid))
  590. try:
  591. proc_info.process.kill()
  592. except OSError:
  593. # ignore these (usually ESRCH because the child
  594. # finally exited)
  595. pass
  596. if self.verbose:
  597. sys.stdout.write("[bind10] All processes ended, server done.\n")
  598. def reap_children(self):
  599. """Check to see if any of our child processes have exited,
  600. and note this for later handling.
  601. """
  602. while True:
  603. try:
  604. (pid, exit_status) = os.waitpid(-1, os.WNOHANG)
  605. except OSError as o:
  606. if o.errno == errno.ECHILD: break
  607. # XXX: should be impossible to get any other error here
  608. raise
  609. if pid == 0: break
  610. if pid in self.processes:
  611. # One of the processes we know about. Get information on it.
  612. proc_info = self.processes.pop(pid)
  613. proc_info.restart_schedule.set_run_stop_time()
  614. self.dead_processes[proc_info.pid] = proc_info
  615. # Write out message, but only if in the running state:
  616. # During startup and shutdown, these messages are handled
  617. # elsewhere.
  618. if self.runnable:
  619. if exit_status is None:
  620. sys.stdout.write(
  621. "[bind10] Process %s (PID %d) died: exit status not available" %
  622. (proc_info.name, proc_info.pid))
  623. else:
  624. sys.stdout.write(
  625. "[bind10] Process %s (PID %d) terminated, exit status = %d\n" %
  626. (proc_info.name, proc_info.pid, exit_status))
  627. # Was it a special process?
  628. if proc_info.name == "b10-msgq":
  629. sys.stdout.write(
  630. "[bind10] The b10-msgq process died, shutting down.\n")
  631. self.runnable = False
  632. else:
  633. sys.stdout.write("[bind10] Unknown child pid %d exited.\n" % pid)
  634. def restart_processes(self):
  635. """
  636. Restart any dead processes:
  637. * Returns the time when the next process is ready to be restarted.
  638. * If the server is shutting down, returns 0.
  639. * If there are no processes, returns None.
  640. The values returned can be safely passed into select() as the
  641. timeout value.
  642. """
  643. next_restart = None
  644. # if we're shutting down, then don't restart
  645. if not self.runnable:
  646. return 0
  647. # otherwise look through each dead process and try to restart
  648. still_dead = {}
  649. now = time.time()
  650. for proc_info in self.dead_processes.values():
  651. if proc_info.name in self.expected_shutdowns:
  652. # We don't restart, we wanted it to die
  653. del self.expected_shutdowns[proc_info.name]
  654. continue
  655. restart_time = proc_info.restart_schedule.get_restart_time(now)
  656. if restart_time > now:
  657. if (next_restart is None) or (next_restart > restart_time):
  658. next_restart = restart_time
  659. still_dead[proc_info.pid] = proc_info
  660. else:
  661. if self.verbose:
  662. sys.stdout.write("[bind10] Resurrecting dead %s process...\n" %
  663. proc_info.name)
  664. try:
  665. proc_info.respawn()
  666. self.processes[proc_info.pid] = proc_info
  667. sys.stdout.write("[bind10] Resurrected %s (PID %d)\n" %
  668. (proc_info.name, proc_info.pid))
  669. except:
  670. still_dead[proc_info.pid] = proc_info
  671. # remember any processes that refuse to be resurrected
  672. self.dead_processes = still_dead
  673. # return the time when the next process is ready to be restarted
  674. return next_restart
  675. # global variables, needed for signal handlers
  676. options = None
  677. boss_of_bind = None
  678. def reaper(signal_number, stack_frame):
  679. """A child process has died (SIGCHLD received)."""
  680. # don't do anything...
  681. # the Python signal handler has been set up to write
  682. # down a pipe, waking up our select() bit
  683. pass
  684. def get_signame(signal_number):
  685. """Return the symbolic name for a signal."""
  686. for sig in dir(signal):
  687. if sig.startswith("SIG") and sig[3].isalnum():
  688. if getattr(signal, sig) == signal_number:
  689. return sig
  690. return "Unknown signal %d" % signal_number
  691. # XXX: perhaps register atexit() function and invoke that instead
  692. def fatal_signal(signal_number, stack_frame):
  693. """We need to exit (SIGINT or SIGTERM received)."""
  694. global options
  695. global boss_of_bind
  696. if options.verbose:
  697. sys.stdout.write("[bind10] Received %s.\n" % get_signame(signal_number))
  698. signal.signal(signal.SIGCHLD, signal.SIG_DFL)
  699. boss_of_bind.runnable = False
  700. def process_rename(option, opt_str, value, parser):
  701. """Function that renames the process if it is requested by a option."""
  702. isc.util.process.rename(value)
  703. def main():
  704. global options
  705. global boss_of_bind
  706. # Enforce line buffering on stdout, even when not a TTY
  707. sys.stdout = io.TextIOWrapper(sys.stdout.detach(), line_buffering=True)
  708. # Parse any command-line options.
  709. parser = OptionParser(version=VERSION)
  710. parser.add_option("-m", "--msgq-socket-file", dest="msgq_socket_file",
  711. type="string", default=None,
  712. help="UNIX domain socket file the b10-msgq daemon will use")
  713. parser.add_option("-n", "--no-cache", action="store_true", dest="nocache",
  714. default=False, help="disable hot-spot cache in authoritative DNS server")
  715. parser.add_option("-u", "--user", dest="user", type="string", default=None,
  716. help="Change user after startup (must run as root)")
  717. parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
  718. help="display more about what is going on")
  719. parser.add_option("--pretty-name", type="string", action="callback",
  720. callback=process_rename,
  721. help="Set the process name (displayed in ps, top, ...)")
  722. (options, args) = parser.parse_args()
  723. if args:
  724. parser.print_help()
  725. sys.exit(1)
  726. # Check user ID.
  727. setuid = None
  728. username = None
  729. if options.user:
  730. # Try getting information about the user, assuming UID passed.
  731. try:
  732. pw_ent = pwd.getpwuid(int(options.user))
  733. setuid = pw_ent.pw_uid
  734. username = pw_ent.pw_name
  735. except ValueError:
  736. pass
  737. except KeyError:
  738. pass
  739. # Next try getting information about the user, assuming user name
  740. # passed.
  741. # If the information is both a valid user name and user number, we
  742. # prefer the name because we try it second. A minor point, hopefully.
  743. try:
  744. pw_ent = pwd.getpwnam(options.user)
  745. setuid = pw_ent.pw_uid
  746. username = pw_ent.pw_name
  747. except KeyError:
  748. pass
  749. if setuid is None:
  750. sys.stderr.write("bind10: invalid user: '%s'\n" % options.user)
  751. sys.exit(1)
  752. # Announce startup.
  753. if options.verbose:
  754. sys.stdout.write("%s\n" % VERSION)
  755. # Create wakeup pipe for signal handlers
  756. wakeup_pipe = os.pipe()
  757. signal.set_wakeup_fd(wakeup_pipe[1])
  758. # Set signal handlers for catching child termination, as well
  759. # as our own demise.
  760. signal.signal(signal.SIGCHLD, reaper)
  761. signal.siginterrupt(signal.SIGCHLD, False)
  762. signal.signal(signal.SIGINT, fatal_signal)
  763. signal.signal(signal.SIGTERM, fatal_signal)
  764. # Block SIGPIPE, as we don't want it to end this process
  765. signal.signal(signal.SIGPIPE, signal.SIG_IGN)
  766. # Go bob!
  767. boss_of_bind = BoB(options.msgq_socket_file, options.nocache,
  768. options.verbose, setuid, username)
  769. startup_result = boss_of_bind.startup()
  770. if startup_result:
  771. sys.stderr.write("[bind10] Error on startup: %s\n" % startup_result)
  772. sys.exit(1)
  773. sys.stdout.write("[bind10] BIND 10 started\n")
  774. # send "bind10.boot_time" to b10-stats
  775. time.sleep(1) # wait a second
  776. if options.verbose:
  777. sys.stdout.write("[bind10] send \"bind10.boot_time\" to b10-stats\n")
  778. cmd = isc.config.ccsession.create_command('set',
  779. { "stats_data": {
  780. 'bind10.boot_time': time.strftime('%Y-%m-%dT%H:%M:%SZ', _BASETIME)
  781. }
  782. })
  783. boss_of_bind.cc_session.group_sendmsg(cmd, 'Stats')
  784. # In our main loop, we check for dead processes or messages
  785. # on the c-channel.
  786. wakeup_fd = wakeup_pipe[0]
  787. ccs_fd = boss_of_bind.ccs.get_socket().fileno()
  788. while boss_of_bind.runnable:
  789. # clean up any processes that exited
  790. boss_of_bind.reap_children()
  791. next_restart = boss_of_bind.restart_processes()
  792. if next_restart is None:
  793. wait_time = None
  794. else:
  795. wait_time = max(next_restart - time.time(), 0)
  796. # select() can raise EINTR when a signal arrives,
  797. # even if they are resumable, so we have to catch
  798. # the exception
  799. try:
  800. (rlist, wlist, xlist) = select.select([wakeup_fd, ccs_fd], [], [],
  801. wait_time)
  802. except select.error as err:
  803. if err.args[0] == errno.EINTR:
  804. (rlist, wlist, xlist) = ([], [], [])
  805. else:
  806. sys.stderr.write("[bind10] Error with select(); %s\n" % err)
  807. break
  808. for fd in rlist + xlist:
  809. if fd == ccs_fd:
  810. try:
  811. boss_of_bind.ccs.check_command()
  812. except isc.cc.session.ProtocolError:
  813. if options.verbose:
  814. sys.stderr.write("[bind10] msgq channel disappeared.\n")
  815. break
  816. elif fd == wakeup_fd:
  817. os.read(wakeup_fd, 32)
  818. # shutdown
  819. signal.signal(signal.SIGCHLD, signal.SIG_DFL)
  820. boss_of_bind.shutdown()
  821. sys.stdout.write("[bind10] BIND 10 exiting\n");
  822. sys.exit(0)
  823. if __name__ == "__main__":
  824. main()