msgq.py.in 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. #!@PYTHON@
  2. # Copyright (C) 2010 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. import sys; sys.path.append ('@@PYTHONPATH@@')
  17. """This code implements the msgq daemon."""
  18. import subprocess
  19. import signal
  20. import os
  21. import socket
  22. import sys
  23. import struct
  24. import errno
  25. import time
  26. import select
  27. import random
  28. import threading
  29. import isc.config.ccsession
  30. from optparse import OptionParser, OptionValueError
  31. import isc.util.process
  32. import isc.log
  33. from isc.log_messages.msgq_messages import *
  34. import isc.cc
  35. isc.util.process.rename()
  36. isc.log.init("b10-msgq", buffer=True)
  37. # Logger that is used in the actual msgq handling - startup, shutdown and the
  38. # poller thread.
  39. logger = isc.log.Logger("msgq")
  40. # A separate copy for the master/config thread when the poller thread runs.
  41. # We use a separate instance, since the logger itself doesn't have to be
  42. # thread safe.
  43. config_logger = isc.log.Logger("msgq")
  44. TRACE_START = logger.DBGLVL_START_SHUT
  45. TRACE_BASIC = logger.DBGLVL_TRACE_BASIC
  46. TRACE_DETAIL = logger.DBGLVL_TRACE_DETAIL
  47. # This is the version that gets displayed to the user.
  48. # The VERSION string consists of the module name, the module version
  49. # number, and the overall BIND 10 version number (set in configure.ac).
  50. VERSION = "b10-msgq 20110127 (BIND 10 @PACKAGE_VERSION@)"
  51. # If B10_FROM_BUILD is set in the environment, we use data files
  52. # from a directory relative to that, otherwise we use the ones
  53. # installed on the system
  54. if "B10_FROM_BUILD" in os.environ:
  55. SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/msgq"
  56. else:
  57. PREFIX = "@prefix@"
  58. DATAROOTDIR = "@datarootdir@"
  59. SPECFILE_PATH = "@datadir@/@PACKAGE@".replace("${datarootdir}", DATAROOTDIR).replace("${prefix}", PREFIX)
  60. SPECFILE_LOCATION = SPECFILE_PATH + "/msgq.spec"
  61. class MsgQReceiveError(Exception): pass
  62. class MsgQCloseOnReceive(Exception):
  63. """Exception raised when reading data from a socket results in 'shutdown'.
  64. This happens when msgq received 0-length data. This class holds whether
  65. it happens in the middle of reading (i.e. after reading some) via
  66. partial_read parameter, which is set to True if and only if so.
  67. This will be used by an upper layer catching the exception to distinguish
  68. the severity of the event.
  69. """
  70. def __init__(self, reason, partial_read):
  71. self.partial_read = partial_read
  72. self.__reason = reason
  73. def __str__(self):
  74. return self.__reason
  75. class SubscriptionManager:
  76. def __init__(self, cfgmgr_ready):
  77. """
  78. Initialize the subscription manager.
  79. parameters:
  80. * cfgmgr_ready: A callable object run once the config manager
  81. subscribes. This is a hackish solution, but we can't read
  82. the configuration sooner.
  83. """
  84. self.subscriptions = {}
  85. self.__cfgmgr_ready = cfgmgr_ready
  86. self.__cfgmgr_ready_called = False
  87. def subscribe(self, group, instance, socket):
  88. """Add a subscription."""
  89. target = ( group, instance )
  90. if target in self.subscriptions:
  91. logger.debug(TRACE_BASIC, MSGQ_SUBS_APPEND_TARGET, group, instance)
  92. if socket not in self.subscriptions[target]:
  93. self.subscriptions[target].append(socket)
  94. else:
  95. logger.debug(TRACE_BASIC, MSGQ_SUBS_NEW_TARGET, group, instance)
  96. self.subscriptions[target] = [ socket ]
  97. if group == "ConfigManager" and not self.__cfgmgr_ready_called:
  98. logger.debug(TRACE_BASIC, MSGQ_CFGMGR_SUBSCRIBED)
  99. self.__cfgmgr_ready_called = True
  100. self.__cfgmgr_ready()
  101. def unsubscribe(self, group, instance, socket):
  102. """Remove the socket from the one specific subscription."""
  103. target = ( group, instance )
  104. if target in self.subscriptions:
  105. if socket in self.subscriptions[target]:
  106. self.subscriptions[target].remove(socket)
  107. def unsubscribe_all(self, socket):
  108. """Remove the socket from all subscriptions."""
  109. for socklist in self.subscriptions.values():
  110. if socket in socklist:
  111. socklist.remove(socket)
  112. def find_sub(self, group, instance):
  113. """Return an array of sockets which want this specific group,
  114. instance."""
  115. target = (group, instance)
  116. if target in self.subscriptions:
  117. return self.subscriptions[target]
  118. else:
  119. return []
  120. def find(self, group, instance):
  121. """Return an array of sockets who should get something sent to
  122. this group, instance pair. This includes wildcard subscriptions."""
  123. target = (group, instance)
  124. partone = self.find_sub(group, instance)
  125. parttwo = self.find_sub(group, "*")
  126. return list(set(partone + parttwo))
  127. class MsgQ:
  128. """Message Queue class."""
  129. # did we find a better way to do this?
  130. SOCKET_FILE = os.path.join("@localstatedir@",
  131. "@PACKAGE_NAME@",
  132. "msgq_socket").replace("${prefix}",
  133. "@prefix@")
  134. def __init__(self, socket_file=None, verbose=False):
  135. """Initialize the MsgQ master.
  136. The socket_file specifies the path to the UNIX domain socket
  137. that the msgq process listens on. If it is None, the
  138. environment variable BIND10_MSGQ_SOCKET_FILE is used. If that
  139. is not set, it will default to
  140. @localstatedir@/@PACKAGE_NAME@/msg_socket.
  141. If verbose is True, then the MsgQ reports
  142. what it is doing.
  143. """
  144. if socket_file is None:
  145. if "BIND10_MSGQ_SOCKET_FILE" in os.environ:
  146. self.socket_file = os.environ["BIND10_MSGQ_SOCKET_FILE"]
  147. else:
  148. self.socket_file = self.SOCKET_FILE
  149. else:
  150. self.socket_file = socket_file
  151. self.verbose = verbose
  152. self.poller = None
  153. self.kqueue = None
  154. self.runnable = False
  155. self.listen_socket = False
  156. self.sockets = {}
  157. self.connection_counter = random.random()
  158. self.hostname = socket.gethostname()
  159. self.subs = SubscriptionManager(self.cfgmgr_ready)
  160. self.lnames = {}
  161. self.sendbuffs = {}
  162. self.running = False
  163. self.__cfgmgr_ready = None
  164. self.__cfgmgr_ready_cond = threading.Condition()
  165. # A lock used when the message queue does anything more complicated.
  166. # It is mostly a safety measure, the threads doing so should be mostly
  167. # independent, and the one with config session should be read only,
  168. # but with threads, one never knows. We use threads for concurrency,
  169. # not for performance, so we use wide lock scopes to be on the safe
  170. # side.
  171. self.__lock = threading.Lock()
  172. def cfgmgr_ready(self, ready=True):
  173. """Notify that the config manager is either subscribed, or
  174. that the msgq is shutting down and it won't connect, but
  175. anybody waiting for it should stop anyway.
  176. The ready parameter signifies if the config manager is subscribed.
  177. This method can be called multiple times, but second and any
  178. following call is simply ignored. This means the "abort" version
  179. of the call can be used on any stop unconditionally, even when
  180. the config manager already connected.
  181. """
  182. with self.__cfgmgr_ready_cond:
  183. if self.__cfgmgr_ready is not None:
  184. # This is a second call to this method. In that case it does
  185. # nothing.
  186. return
  187. self.__cfgmgr_ready = ready
  188. self.__cfgmgr_ready_cond.notify_all()
  189. def wait_cfgmgr(self):
  190. """Wait for msgq to subscribe.
  191. When this returns, the config manager is either subscribed, or
  192. msgq gave up waiting for it. Success is signified by the return
  193. value.
  194. """
  195. with self.__cfgmgr_ready_cond:
  196. # Wait until it either aborts or subscribes
  197. while self.__cfgmgr_ready is None:
  198. self.__cfgmgr_ready_cond.wait()
  199. return self.__cfgmgr_ready
  200. def setup_poller(self):
  201. """Set up the poll thing. Internal function."""
  202. try:
  203. self.kqueue = select.kqueue()
  204. except AttributeError:
  205. self.poller = select.poll()
  206. def add_kqueue_socket(self, socket, write_filter=False):
  207. """Add a kqueue filter for a socket. By default the read
  208. filter is used; if write_filter is set to True, the write
  209. filter is used. We use a boolean value instead of a specific
  210. filter constant, because kqueue filter values do not seem to
  211. be defined on some systems. The use of boolean makes the
  212. interface restrictive because there are other filters, but this
  213. method is mostly only for our internal use, so it should be
  214. acceptable at least for now."""
  215. filter_type = select.KQ_FILTER_WRITE if write_filter else \
  216. select.KQ_FILTER_READ
  217. event = select.kevent(socket.fileno(), filter_type,
  218. select.KQ_EV_ADD | select.KQ_EV_ENABLE)
  219. self.kqueue.control([event], 0)
  220. def delete_kqueue_socket(self, socket, write_filter=False):
  221. """Delete a kqueue filter for socket. See add_kqueue_socket()
  222. for the semantics and notes about write_filter."""
  223. filter_type = select.KQ_FILTER_WRITE if write_filter else \
  224. select.KQ_FILTER_READ
  225. event = select.kevent(socket.fileno(), filter_type,
  226. select.KQ_EV_DELETE)
  227. self.kqueue.control([event], 0)
  228. def setup_listener(self):
  229. """Set up the listener socket. Internal function."""
  230. logger.debug(TRACE_BASIC, MSGQ_LISTENER_SETUP, self.socket_file)
  231. self.listen_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  232. if os.path.exists(self.socket_file):
  233. os.remove(self.socket_file)
  234. try:
  235. self.listen_socket.bind(self.socket_file)
  236. self.listen_socket.listen(1024)
  237. except Exception as e:
  238. # remove the file again if something goes wrong
  239. # (note this is a catch-all, but we reraise it)
  240. if os.path.exists(self.socket_file):
  241. os.remove(self.socket_file)
  242. self.listen_socket.close()
  243. logger.fatal(MSGQ_LISTENER_FAILED, self.socket_file, e)
  244. raise e
  245. if self.poller:
  246. self.poller.register(self.listen_socket, select.POLLIN)
  247. else:
  248. self.add_kqueue_socket(self.listen_socket)
  249. def setup_signalsock(self):
  250. """Create a socket pair used to signal when we want to finish.
  251. Using a socket is easy and thread/signal safe way to signal
  252. the termination.
  253. """
  254. # The __poller_sock will be the end in the poller. When it is
  255. # closed, we should shut down.
  256. (self.__poller_sock, self.__control_sock) = socket.socketpair()
  257. if self.poller:
  258. self.poller.register(self.__poller_sock, select.POLLIN)
  259. else:
  260. self.add_kqueue_socket(self.__poller_sock)
  261. def setup(self):
  262. """Configure listener socket, polling, etc.
  263. Raises a socket.error if the socket_file cannot be
  264. created.
  265. """
  266. self.setup_poller()
  267. self.setup_signalsock()
  268. self.setup_listener()
  269. logger.debug(TRACE_START, MSGQ_LISTENER_STARTED);
  270. self.runnable = True
  271. def process_accept(self):
  272. """Process an accept on the listening socket."""
  273. newsocket, ipaddr = self.listen_socket.accept()
  274. # TODO: When we have logging, we might want
  275. # to add a debug message here that a new connection
  276. # was made
  277. self.register_socket(newsocket)
  278. def register_socket(self, newsocket):
  279. """
  280. Internal function to insert a socket. Used by process_accept and some tests.
  281. """
  282. self.sockets[newsocket.fileno()] = newsocket
  283. lname = self.newlname()
  284. self.lnames[lname] = newsocket
  285. logger.debug(TRACE_BASIC, MSGQ_SOCKET_REGISTERED, newsocket.fileno(),
  286. lname)
  287. if self.poller:
  288. self.poller.register(newsocket, select.POLLIN)
  289. else:
  290. self.add_kqueue_socket(newsocket)
  291. def kill_socket(self, fd, sock):
  292. """Fully close down the socket."""
  293. # Unregister events on the socket. Note that we don't have to do
  294. # this for kqueue because the registered events are automatically
  295. # deleted when the corresponding socket is closed.
  296. if self.poller:
  297. self.poller.unregister(sock)
  298. self.subs.unsubscribe_all(sock)
  299. lname = [ k for k, v in self.lnames.items() if v == sock ][0]
  300. del self.lnames[lname]
  301. sock.close()
  302. del self.sockets[fd]
  303. if fd in self.sendbuffs:
  304. del self.sendbuffs[fd]
  305. logger.debug(TRACE_BASIC, MSGQ_SOCK_CLOSE, fd)
  306. def __getbytes(self, fd, sock, length, continued):
  307. """Get exactly the requested bytes, or raise an exception if
  308. EOF.
  309. continued is set to True if this method is called to complete
  310. already read data.
  311. """
  312. received = b''
  313. while len(received) < length:
  314. try:
  315. data = sock.recv(length - len(received))
  316. except socket.error as err:
  317. # This case includes ECONNRESET, which seems to happen when
  318. # the remote client has closed its socket at some subtle
  319. # timing (it should normally result in receiving empty data).
  320. # Since we didn't figure out how exactly that could happen,
  321. # we treat it just like other really-unexpected socket errors.
  322. raise MsgQReceiveError(str(err))
  323. if len(data) == 0:
  324. raise MsgQCloseOnReceive("EOF", continued)
  325. received += data
  326. continued = True
  327. return received
  328. def read_packet(self, fd, sock):
  329. """Read a correctly formatted packet. Will raise exceptions if
  330. something fails."""
  331. lengths = self.__getbytes(fd, sock, 6, False)
  332. overall_length, routing_length = struct.unpack(">IH", lengths)
  333. if overall_length < 2:
  334. raise MsgQReceiveError("overall_length < 2")
  335. overall_length -= 2
  336. if routing_length > overall_length:
  337. raise MsgQReceiveError("routing_length > overall_length")
  338. if routing_length == 0:
  339. raise MsgQReceiveError("routing_length == 0")
  340. data_length = overall_length - routing_length
  341. # probably need to sanity check lengths here...
  342. routing = self.__getbytes(fd, sock, routing_length, True)
  343. if data_length > 0:
  344. data = self.__getbytes(fd, sock, data_length, True)
  345. else:
  346. data = None
  347. return (routing, data)
  348. def process_packet(self, fd, sock):
  349. """Process one packet."""
  350. try:
  351. routing, data = self.read_packet(fd, sock)
  352. except (MsgQReceiveError, MsgQCloseOnReceive) as err:
  353. # If it's MsgQCloseOnReceive and that happens without reading
  354. # any data, it basically means the remote clinet has closed the
  355. # socket, so we log it as debug information. Otherwise, it's
  356. # a somewhat unexpected event, so we consider it an "error".
  357. if isinstance(err, MsgQCloseOnReceive) and not err.partial_read:
  358. logger.debug(TRACE_BASIC, MSGQ_CLOSE_ON_RECV, fd)
  359. else:
  360. logger.error(MSGQ_RECV_ERROR, fd, err)
  361. self.kill_socket(fd, sock)
  362. return
  363. try:
  364. routingmsg = isc.cc.message.from_wire(routing)
  365. except DecodeError as err:
  366. self.kill_socket(fd, sock)
  367. logger.error(MSGQ_HDR_DECODE_ERROR, fd, err)
  368. return
  369. self.process_command(fd, sock, routingmsg, data)
  370. def process_command(self, fd, sock, routing, data):
  371. """Process a single command. This will split out into one of the
  372. other functions."""
  373. logger.debug(TRACE_DETAIL, MSGQ_RECV_HDR, routing)
  374. cmd = routing["type"]
  375. if cmd == 'send':
  376. self.process_command_send(sock, routing, data)
  377. elif cmd == 'subscribe':
  378. self.process_command_subscribe(sock, routing, data)
  379. elif cmd == 'unsubscribe':
  380. self.process_command_unsubscribe(sock, routing, data)
  381. elif cmd == 'getlname':
  382. self.process_command_getlname(sock, routing, data)
  383. elif cmd == 'ping':
  384. # Command for testing purposes
  385. self.process_command_ping(sock, routing, data)
  386. elif cmd == 'stop':
  387. self.stop()
  388. else:
  389. logger.error(MSGQ_INVALID_CMD, cmd)
  390. def preparemsg(self, env, msg = None):
  391. if type(env) == dict:
  392. env = isc.cc.message.to_wire(env)
  393. if type(msg) == dict:
  394. msg = isc.cc.message.to_wire(msg)
  395. length = 2 + len(env);
  396. if msg:
  397. length += len(msg)
  398. ret = struct.pack("!IH", length, len(env))
  399. ret += env
  400. if msg:
  401. ret += msg
  402. return ret
  403. def sendmsg(self, sock, env, msg = None):
  404. self.send_prepared_msg(sock, self.preparemsg(env, msg))
  405. def _send_data(self, sock, data):
  406. """
  407. Send a piece of data to the given socket. This method is
  408. essentially "private" to MsgQ, but defined as if it were "protected"
  409. for easier access from tests.
  410. Parameters:
  411. sock: The socket to send to
  412. data: The list of bytes to send
  413. Returns:
  414. An integer or None. If an integer (which can be 0), it signals
  415. the number of bytes sent. If None, the socket appears to have
  416. been closed on the other end, and it has been killed on this
  417. side too.
  418. """
  419. try:
  420. # We set the socket nonblocking, MSG_DONTWAIT doesn't exist
  421. # on some OSes
  422. sock.setblocking(0)
  423. return sock.send(data)
  424. except socket.error as e:
  425. if e.errno in [ errno.EAGAIN, errno.EWOULDBLOCK, errno.EINTR ]:
  426. return 0
  427. elif e.errno in [ errno.EPIPE, errno.ECONNRESET, errno.ENOBUFS ]:
  428. # EPIPE happens if the remote module has terminated by the time
  429. # of this send; its severity can vary, but in many cases it
  430. # shouldn't be critical, so we log it separately as a warning.
  431. if e.errno == errno.EPIPE:
  432. logger.warn(MSGQ_CLOSE_ON_SEND, sock.fileno())
  433. else:
  434. logger.error(MSGQ_SEND_ERROR, sock.fileno(),
  435. errno.errorcode[e.errno])
  436. self.kill_socket(sock.fileno(), sock)
  437. return None
  438. else:
  439. raise e
  440. finally:
  441. # And set it back again
  442. sock.setblocking(1)
  443. def send_prepared_msg(self, sock, msg):
  444. # Try to send the data, but only if there's nothing waiting
  445. fileno = sock.fileno()
  446. if fileno in self.sendbuffs:
  447. amount_sent = 0
  448. else:
  449. amount_sent = self._send_data(sock, msg)
  450. if amount_sent is None:
  451. # Socket has been killed, drop the send
  452. return
  453. # Still something to send, add it to outgoing queue
  454. if amount_sent < len(msg):
  455. now = time.clock()
  456. # Append it to buffer (but check the data go away)
  457. if fileno in self.sendbuffs:
  458. (last_sent, buff) = self.sendbuffs[fileno]
  459. if now - last_sent > 0.1:
  460. self.kill_socket(fileno, sock)
  461. return
  462. buff += msg
  463. else:
  464. buff = msg[amount_sent:]
  465. last_sent = now
  466. if self.poller:
  467. self.poller.register(fileno, select.POLLIN |
  468. select.POLLOUT)
  469. else:
  470. self.add_kqueue_socket(sock, True)
  471. self.sendbuffs[fileno] = (last_sent, buff)
  472. def __process_write(self, fileno):
  473. # Try to send some data from the buffer
  474. (_, msg) = self.sendbuffs[fileno]
  475. sock = self.sockets[fileno]
  476. amount_sent = self._send_data(sock, msg)
  477. if amount_sent is not None:
  478. # Keep the rest
  479. msg = msg[amount_sent:]
  480. if len(msg) == 0:
  481. # If there's no more, stop requesting for write availability
  482. if self.poller:
  483. self.poller.register(fileno, select.POLLIN)
  484. else:
  485. self.delete_kqueue_socket(sock, True)
  486. del self.sendbuffs[fileno]
  487. else:
  488. self.sendbuffs[fileno] = (time.clock(), msg)
  489. def newlname(self):
  490. """Generate a unique connection identifier for this socket.
  491. This is done by using an increasing counter and the current
  492. time."""
  493. self.connection_counter += 1
  494. return "%x_%x@%s" % (time.time(), self.connection_counter, self.hostname)
  495. def process_command_ping(self, sock, routing, data):
  496. self.sendmsg(sock, { "type" : "pong" }, data)
  497. def process_command_getlname(self, sock, routing, data):
  498. lname = [ k for k, v in self.lnames.items() if v == sock ][0]
  499. self.sendmsg(sock, { "type" : "getlname" }, { "lname" : lname })
  500. def process_command_send(self, sock, routing, data):
  501. group = routing["group"]
  502. instance = routing["instance"]
  503. to = routing["to"]
  504. if group == None or instance == None:
  505. return # ignore invalid packets entirely
  506. if to == "*":
  507. sockets = self.subs.find(group, instance)
  508. else:
  509. if to in self.lnames:
  510. sockets = [ self.lnames[to] ]
  511. else:
  512. return # recipient doesn't exist
  513. msg = self.preparemsg(routing, data)
  514. if sock in sockets:
  515. sockets.remove(sock)
  516. for socket in sockets:
  517. self.send_prepared_msg(socket, msg)
  518. def process_command_subscribe(self, sock, routing, data):
  519. group = routing["group"]
  520. instance = routing["instance"]
  521. if group == None or instance == None:
  522. return # ignore invalid packets entirely
  523. self.subs.subscribe(group, instance, sock)
  524. def process_command_unsubscribe(self, sock, routing, data):
  525. group = routing["group"]
  526. instance = routing["instance"]
  527. if group == None or instance == None:
  528. return # ignore invalid packets entirely
  529. self.subs.unsubscribe(group, instance, sock)
  530. def run(self):
  531. """Process messages. Forever. Mostly."""
  532. self.running = True
  533. if self.poller:
  534. self.run_poller()
  535. else:
  536. self.run_kqueue()
  537. def run_poller(self):
  538. while self.running:
  539. try:
  540. # Poll with a timeout so that every once in a while,
  541. # the loop checks for self.running.
  542. events = self.poller.poll()
  543. except select.error as err:
  544. if err.args[0] == errno.EINTR:
  545. events = []
  546. else:
  547. logger.fatal(MSGQ_POLL_ERROR, err)
  548. break
  549. with self.__lock:
  550. for (fd, event) in events:
  551. if fd == self.listen_socket.fileno():
  552. self.process_accept()
  553. elif fd == self.__poller_sock.fileno():
  554. # If it's the signal socket, we should terminate now.
  555. self.running = False
  556. break
  557. else:
  558. writable = event & select.POLLOUT
  559. # Note: it may be okay to read data if available
  560. # immediately after write some, but due to unexpected
  561. # regression (see comments on the kqueue version below)
  562. # we restrict one operation per iteration for now.
  563. # In future we may clarify the point and enable the
  564. # "read/write" mode.
  565. readable = not writable and (event & select.POLLIN)
  566. if not writable and not readable:
  567. logger.error(MSGQ_POLL_UNKNOWN_EVENT, fd, event)
  568. self._process_fd(fd, writable, readable, False)
  569. def run_kqueue(self):
  570. while self.running:
  571. # Check with a timeout so that every once in a while,
  572. # the loop checks for self.running.
  573. events = self.kqueue.control(None, 10)
  574. if not events:
  575. raise RuntimeError('serve: kqueue returned no events')
  576. with self.__lock:
  577. for event in events:
  578. if event.ident == self.listen_socket.fileno():
  579. self.process_accept()
  580. elif event.ident == self.__poller_sock.fileno():
  581. # If it's the signal socket, we should terminate now.
  582. self.running = False
  583. break;
  584. else:
  585. fd = event.ident
  586. writable = event.filter == select.KQ_FILTER_WRITE
  587. readable = (event.filter == select.KQ_FILTER_READ and
  588. event.data > 0)
  589. # It seems to break some of our test cases if we
  590. # immediately close the socket on EOF after reading
  591. # some data. It may be possible to avoid by tweaking
  592. # the test, but unless we can be sure we'll hold off.
  593. closed = (not readable and
  594. (event.flags & select.KQ_EV_EOF))
  595. self._process_fd(fd, writable, readable, closed)
  596. def _process_fd(self, fd, writable, readable, closed):
  597. '''Process a single FD: unified subroutine of run_kqueue/poller.
  598. closed can be True only in the case of kqueue. This is essentially
  599. private but is defined as if it were "protected" so it's callable
  600. from tests.
  601. '''
  602. # We need to check if FD is still in the sockets dict, because
  603. # it's possible that the socket has been "killed" while processing
  604. # other FDs; it's even possible it's killed within this method.
  605. if writable and fd in self.sockets:
  606. self.__process_write(fd)
  607. if readable and fd in self.sockets:
  608. self.process_packet(fd, self.sockets[fd])
  609. if closed and fd in self.sockets:
  610. self.kill_socket(fd, self.sockets[fd])
  611. def stop(self):
  612. # Signal it should terminate.
  613. self.__control_sock.close()
  614. self.__control_sock = None
  615. # Abort anything waiting on the condition, just to make sure it's not
  616. # blocked forever
  617. self.cfgmgr_ready(False)
  618. def cleanup_signalsock(self):
  619. """Close the signal sockets. We could do it directly in shutdown,
  620. but this part is reused in tests.
  621. """
  622. if self.__poller_sock:
  623. self.__poller_sock.close()
  624. self.__poller_sock = None
  625. if self.__control_sock:
  626. self.__control_sock.close()
  627. self.__control_sock = None
  628. def shutdown(self):
  629. """Stop the MsgQ master."""
  630. logger.debug(TRACE_START, MSGQ_SHUTDOWN)
  631. self.listen_socket.close()
  632. self.cleanup_signalsock()
  633. # Close all the sockets too. In real life, there should be none now,
  634. # as Msgq should be the last one. But some tests don't adhere to this
  635. # and create a new Msgq for each test, which led to huge socket leaks.
  636. # Some other threads put some other things in instead of sockets, so
  637. # we catch whatever exceptions there we can. This should be safe,
  638. # because in real operation, we will terminate now anyway, implicitly
  639. # closing anything anyway.
  640. for sock in self.sockets.values():
  641. try:
  642. sock.close()
  643. except Exception:
  644. pass
  645. if os.path.exists(self.socket_file):
  646. os.remove(self.socket_file)
  647. def config_handler(self, new_config):
  648. """The configuration handler (run in a separate thread).
  649. Not tested, currently effectively empty.
  650. """
  651. config_logger.debug(TRACE_DETAIL, MSGQ_CONFIG_DATA, new_config)
  652. with self.__lock:
  653. if not self.running:
  654. return
  655. # TODO: Any config handlig goes here.
  656. return isc.config.create_answer(0)
  657. def command_handler(self, command, args):
  658. """The command handler (run in a separate thread).
  659. Not tested, currently effectively empty.
  660. """
  661. config_logger.debug(TRACE_DETAIL, MSGQ_COMMAND, command, args)
  662. with self.__lock:
  663. if not self.running:
  664. return
  665. # TODO: Any commands go here
  666. config_logger.error(MSGQ_COMMAND_UNKNOWN, command)
  667. return isc.config.create_answer(1, 'unknown command: ' + command)
  668. def signal_handler(msgq, signal, frame):
  669. if msgq:
  670. msgq.stop()
  671. if __name__ == "__main__":
  672. def check_port(option, opt_str, value, parser):
  673. """Function to insure that the port we are passed is actually
  674. a valid port number. Used by OptionParser() on startup."""
  675. intval = int(value)
  676. if (intval < 0) or (intval > 65535):
  677. raise OptionValueError("%s requires a port number (0-65535)" % opt_str)
  678. parser.values.msgq_port = intval
  679. # Parse any command-line options.
  680. parser = OptionParser(version=VERSION)
  681. # TODO: Should we remove the option?
  682. parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
  683. help="display more about what is going on")
  684. parser.add_option("-s", "--socket-file", dest="msgq_socket_file",
  685. type="string", default=None,
  686. help="UNIX domain socket file the msgq daemon will use")
  687. (options, args) = parser.parse_args()
  688. # Announce startup.
  689. logger.debug(TRACE_START, MSGQ_START, VERSION)
  690. msgq = MsgQ(options.msgq_socket_file, options.verbose)
  691. signal.signal(signal.SIGTERM,
  692. lambda signal, frame: signal_handler(msgq, signal, frame))
  693. try:
  694. msgq.setup()
  695. except Exception as e:
  696. logger.fatal(MSGQ_START_FAIL, e)
  697. sys.exit(1)
  698. # We run the processing in a separate thread. This is because we want to
  699. # connect to the msgq ourself. But the cc library is unfortunately blocking
  700. # in many places and waiting for the processing part to answer, it would
  701. # deadlock.
  702. poller_thread = threading.Thread(target=msgq.run)
  703. poller_thread.daemon = True
  704. try:
  705. poller_thread.start()
  706. if msgq.wait_cfgmgr():
  707. # Once we get the config manager, we can read our own config.
  708. session = isc.config.ModuleCCSession(SPECFILE_LOCATION,
  709. msgq.config_handler,
  710. msgq.command_handler,
  711. None, True,
  712. msgq.socket_file)
  713. session.start()
  714. # And we create a thread that'll just wait for commands and
  715. # handle them. We don't terminate the thread, we set it to
  716. # daemon. Once the main thread terminates, it'll just die.
  717. def run_session():
  718. while True:
  719. session.check_command(False)
  720. background_thread = threading.Thread(target=run_session)
  721. background_thread.daemon = True
  722. background_thread.start()
  723. poller_thread.join()
  724. except KeyboardInterrupt:
  725. pass
  726. msgq.shutdown()
  727. logger.info(MSGQ_EXITING)