msgq.py.in 38 KB

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