msgq.py.in 31 KB

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