msgq.py.in 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. from optparse import OptionParser, OptionValueError
  29. import isc.util.process
  30. import isc.log
  31. from isc.log_messages.msgq_messages import *
  32. import isc.cc
  33. isc.util.process.rename()
  34. logger = isc.log.Logger("msgq")
  35. TRACE_START = logger.DBGLVL_START_SHUT
  36. TRACE_BASIC = logger.DBGLVL_TRACE_BASIC
  37. TRACE_DETAIL = logger.DBGLVL_TRACE_DETAIL
  38. # This is the version that gets displayed to the user.
  39. # The VERSION string consists of the module name, the module version
  40. # number, and the overall BIND 10 version number (set in configure.ac).
  41. VERSION = "b10-msgq 20110127 (BIND 10 @PACKAGE_VERSION@)"
  42. class MsgQReceiveError(Exception): pass
  43. class SubscriptionManager:
  44. def __init__(self):
  45. self.subscriptions = {}
  46. def subscribe(self, group, instance, socket):
  47. """Add a subscription."""
  48. target = ( group, instance )
  49. if target in self.subscriptions:
  50. logger.debug(TRACE_BASIC, MSGQ_SUBS_APPEND_TARGET, group, instance)
  51. if socket not in self.subscriptions[target]:
  52. self.subscriptions[target].append(socket)
  53. else:
  54. logger.debug(TRACE_BASIC, MSGQ_SUBS_NEW_TARGET, group, instance)
  55. self.subscriptions[target] = [ socket ]
  56. def unsubscribe(self, group, instance, socket):
  57. """Remove the socket from the one specific subscription."""
  58. target = ( group, instance )
  59. if target in self.subscriptions:
  60. if socket in self.subscriptions[target]:
  61. self.subscriptions[target].remove(socket)
  62. def unsubscribe_all(self, socket):
  63. """Remove the socket from all subscriptions."""
  64. for socklist in self.subscriptions.values():
  65. if socket in socklist:
  66. socklist.remove(socket)
  67. def find_sub(self, group, instance):
  68. """Return an array of sockets which want this specific group,
  69. instance."""
  70. target = (group, instance)
  71. if target in self.subscriptions:
  72. return self.subscriptions[target]
  73. else:
  74. return []
  75. def find(self, group, instance):
  76. """Return an array of sockets who should get something sent to
  77. this group, instance pair. This includes wildcard subscriptions."""
  78. target = (group, instance)
  79. partone = self.find_sub(group, instance)
  80. parttwo = self.find_sub(group, "*")
  81. return list(set(partone + parttwo))
  82. class MsgQ:
  83. """Message Queue class."""
  84. # did we find a better way to do this?
  85. SOCKET_FILE = os.path.join("@localstatedir@",
  86. "@PACKAGE_NAME@",
  87. "msgq_socket").replace("${prefix}",
  88. "@prefix@")
  89. def __init__(self, socket_file=None, verbose=False):
  90. """Initialize the MsgQ master.
  91. The socket_file specifies the path to the UNIX domain socket
  92. that the msgq process listens on. If it is None, the
  93. environment variable BIND10_MSGQ_SOCKET_FILE is used. If that
  94. is not set, it will default to
  95. @localstatedir@/@PACKAGE_NAME@/msg_socket.
  96. If verbose is True, then the MsgQ reports
  97. what it is doing.
  98. """
  99. if socket_file is None:
  100. if "BIND10_MSGQ_SOCKET_FILE" in os.environ:
  101. self.socket_file = os.environ["BIND10_MSGQ_SOCKET_FILE"]
  102. else:
  103. self.socket_file = self.SOCKET_FILE
  104. else:
  105. self.socket_file = socket_file
  106. self.verbose = verbose
  107. self.poller = None
  108. self.kqueue = None
  109. self.runnable = False
  110. self.listen_socket = False
  111. self.sockets = {}
  112. self.connection_counter = random.random()
  113. self.hostname = socket.gethostname()
  114. self.subs = SubscriptionManager()
  115. self.lnames = {}
  116. self.sendbuffs = {}
  117. self.running = False
  118. def setup_poller(self):
  119. """Set up the poll thing. Internal function."""
  120. try:
  121. self.kqueue = select.kqueue()
  122. except AttributeError:
  123. self.poller = select.poll()
  124. def add_kqueue_socket(self, socket, write_filter=False):
  125. """Add a kquque filter for a socket. By default the read
  126. filter is used; if write_filter is set to True, the write
  127. filter is used. We use a boolean value instead of a specific
  128. filter constant, because kqueue filter values do not seem to
  129. be defined on some systems. The use of boolean makes the
  130. interface restrictive because there are other filters, but this
  131. method is mostly only for our internal use, so it should be
  132. acceptable at least for now."""
  133. filter_type = select.KQ_FILTER_WRITE if write_filter else \
  134. select.KQ_FILTER_READ
  135. event = select.kevent(socket.fileno(), filter_type,
  136. select.KQ_EV_ADD | select.KQ_EV_ENABLE)
  137. self.kqueue.control([event], 0)
  138. def delete_kqueue_socket(self, socket, write_filter=False):
  139. """Delete a kqueue filter for socket. See add_kqueue_socket()
  140. for the semantics and notes about write_filter."""
  141. filter_type = select.KQ_FILTER_WRITE if write_filter else \
  142. select.KQ_FILTER_READ
  143. event = select.kevent(socket.fileno(), filter_type,
  144. select.KQ_EV_DELETE)
  145. self.kqueue.control([event], 0)
  146. def setup_listener(self):
  147. """Set up the listener socket. Internal function."""
  148. logger.debug(TRACE_BASIC, MSGQ_LISTENER_SETUP, self.socket_file)
  149. self.listen_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  150. if os.path.exists(self.socket_file):
  151. os.remove(self.socket_file)
  152. try:
  153. self.listen_socket.bind(self.socket_file)
  154. self.listen_socket.listen(1024)
  155. except Exception as e:
  156. # remove the file again if something goes wrong
  157. # (note this is a catch-all, but we reraise it)
  158. if os.path.exists(self.socket_file):
  159. os.remove(self.socket_file)
  160. self.listen_socket.close()
  161. logger.fatal(MSGQ_LISTENER_FAILED, self.socket_file, e)
  162. raise e
  163. if self.poller:
  164. self.poller.register(self.listen_socket, select.POLLIN)
  165. else:
  166. self.add_kqueue_socket(self.listen_socket)
  167. def setup(self):
  168. """Configure listener socket, polling, etc.
  169. Raises a socket.error if the socket_file cannot be
  170. created.
  171. """
  172. self.setup_poller()
  173. self.setup_listener()
  174. logger.debug(TRACE_START, MSGQ_LISTENER_STARTED);
  175. self.runnable = True
  176. def process_accept(self):
  177. """Process an accept on the listening socket."""
  178. newsocket, ipaddr = self.listen_socket.accept()
  179. # TODO: When we have logging, we might want
  180. # to add a debug message here that a new connection
  181. # was made
  182. self.register_socket(newsocket)
  183. def register_socket(self, newsocket):
  184. """
  185. Internal function to insert a socket. Used by process_accept and some tests.
  186. """
  187. self.sockets[newsocket.fileno()] = newsocket
  188. lname = self.newlname()
  189. self.lnames[lname] = newsocket
  190. if self.poller:
  191. self.poller.register(newsocket, select.POLLIN)
  192. else:
  193. self.add_kqueue_socket(newsocket)
  194. def process_socket(self, fd):
  195. """Process a read on a socket."""
  196. if not fd in self.sockets:
  197. logger.error(MSGQ_READ_UNKNOWN_FD, fd)
  198. return
  199. sock = self.sockets[fd]
  200. # sys.stderr.write("[b10-msgq] Got read on fd %d\n" %fd)
  201. self.process_packet(fd, sock)
  202. def kill_socket(self, fd, sock):
  203. """Fully close down the socket."""
  204. if self.poller:
  205. self.poller.unregister(sock)
  206. self.subs.unsubscribe_all(sock)
  207. lname = [ k for k, v in self.lnames.items() if v == sock ][0]
  208. del self.lnames[lname]
  209. sock.close()
  210. del self.sockets[fd]
  211. if fd in self.sendbuffs:
  212. del self.sendbuffs[fd]
  213. logger.debug(TRACE_BASIC, MSGQ_SOCK_CLOSE, fd)
  214. def getbytes(self, fd, sock, length):
  215. """Get exactly the requested bytes, or raise an exception if
  216. EOF."""
  217. received = b''
  218. while len(received) < length:
  219. try:
  220. data = sock.recv(length - len(received))
  221. except socket.error:
  222. raise MsgQReceiveError(socket.error)
  223. if len(data) == 0:
  224. raise MsgQReceiveError("EOF")
  225. received += data
  226. return received
  227. def read_packet(self, fd, sock):
  228. """Read a correctly formatted packet. Will raise exceptions if
  229. something fails."""
  230. lengths = self.getbytes(fd, sock, 6)
  231. overall_length, routing_length = struct.unpack(">IH", lengths)
  232. if overall_length < 2:
  233. raise MsgQReceiveError("overall_length < 2")
  234. overall_length -= 2
  235. if routing_length > overall_length:
  236. raise MsgQReceiveError("routing_length > overall_length")
  237. if routing_length == 0:
  238. raise MsgQReceiveError("routing_length == 0")
  239. data_length = overall_length - routing_length
  240. # probably need to sanity check lengths here...
  241. routing = self.getbytes(fd, sock, routing_length)
  242. if data_length > 0:
  243. data = self.getbytes(fd, sock, data_length)
  244. else:
  245. data = None
  246. return (routing, data)
  247. def process_packet(self, fd, sock):
  248. """Process one packet."""
  249. try:
  250. routing, data = self.read_packet(fd, sock)
  251. except MsgQReceiveError as err:
  252. logger.error(MSGQ_RECV_ERR, fd, err)
  253. self.kill_socket(fd, sock)
  254. return
  255. try:
  256. routingmsg = isc.cc.message.from_wire(routing)
  257. except DecodeError as err:
  258. self.kill_socket(fd, sock)
  259. logger.error(MSGQ_HDR_DECODE_ERR, fd, err)
  260. return
  261. self.process_command(fd, sock, routingmsg, data)
  262. def process_command(self, fd, sock, routing, data):
  263. """Process a single command. This will split out into one of the
  264. other functions."""
  265. logger.debug(TRACE_DETAIL, MSGQ_RECV_HDR, routing)
  266. cmd = routing["type"]
  267. if cmd == 'send':
  268. self.process_command_send(sock, routing, data)
  269. elif cmd == 'subscribe':
  270. self.process_command_subscribe(sock, routing, data)
  271. elif cmd == 'unsubscribe':
  272. self.process_command_unsubscribe(sock, routing, data)
  273. elif cmd == 'getlname':
  274. self.process_command_getlname(sock, routing, data)
  275. elif cmd == 'ping':
  276. # Command for testing purposes
  277. self.process_command_ping(sock, routing, data)
  278. elif cmd == 'stop':
  279. self.stop()
  280. else:
  281. logger.error(MSGQ_INVALID_CMD, cmd)
  282. def preparemsg(self, env, msg = None):
  283. if type(env) == dict:
  284. env = isc.cc.message.to_wire(env)
  285. if type(msg) == dict:
  286. msg = isc.cc.message.to_wire(msg)
  287. length = 2 + len(env);
  288. if msg:
  289. length += len(msg)
  290. ret = struct.pack("!IH", length, len(env))
  291. ret += env
  292. if msg:
  293. ret += msg
  294. return ret
  295. def sendmsg(self, sock, env, msg = None):
  296. self.send_prepared_msg(sock, self.preparemsg(env, msg))
  297. def __send_data(self, sock, data):
  298. """
  299. Send a piece of data to the given socket.
  300. Parameters:
  301. sock: The socket to send to
  302. data: The list of bytes to send
  303. Returns:
  304. An integer or None. If an integer (which can be 0), it signals
  305. the number of bytes sent. If None, the socket appears to have
  306. been closed on the other end, and it has been killed on this
  307. side too.
  308. """
  309. try:
  310. # We set the socket nonblocking, MSG_DONTWAIT doesn't exist
  311. # on some OSes
  312. sock.setblocking(0)
  313. return sock.send(data)
  314. except socket.error as e:
  315. if e.errno in [ errno.EAGAIN,
  316. errno.EWOULDBLOCK,
  317. errno.EINTR ]:
  318. return 0
  319. elif e.errno in [ errno.EPIPE,
  320. errno.ECONNRESET,
  321. errno.ENOBUFS ]:
  322. logger.error(MSGQ_SEND_ERR, sock.fileno(),
  323. errno.errorcode[e.errno])
  324. self.kill_socket(sock.fileno(), sock)
  325. return None
  326. else:
  327. raise e
  328. finally:
  329. # And set it back again
  330. sock.setblocking(1)
  331. def send_prepared_msg(self, sock, msg):
  332. # Try to send the data, but only if there's nothing waiting
  333. fileno = sock.fileno()
  334. if fileno in self.sendbuffs:
  335. amount_sent = 0
  336. else:
  337. amount_sent = self.__send_data(sock, msg)
  338. if amount_sent is None:
  339. # Socket has been killed, drop the send
  340. return
  341. # Still something to send, add it to outgoing queue
  342. if amount_sent < len(msg):
  343. now = time.clock()
  344. # Append it to buffer (but check the data go away)
  345. if fileno in self.sendbuffs:
  346. (last_sent, buff) = self.sendbuffs[fileno]
  347. if now - last_sent > 0.1:
  348. self.kill_socket(fileno, sock)
  349. return
  350. buff += msg
  351. else:
  352. buff = msg[amount_sent:]
  353. last_sent = now
  354. if self.poller:
  355. self.poller.register(fileno, select.POLLIN |
  356. select.POLLOUT)
  357. else:
  358. self.add_kqueue_socket(sock, True)
  359. self.sendbuffs[fileno] = (last_sent, buff)
  360. def __process_write(self, fileno):
  361. # Try to send some data from the buffer
  362. (_, msg) = self.sendbuffs[fileno]
  363. sock = self.sockets[fileno]
  364. amount_sent = self.__send_data(sock, msg)
  365. if amount_sent is not None:
  366. # Keep the rest
  367. msg = msg[amount_sent:]
  368. if len(msg) == 0:
  369. # If there's no more, stop requesting for write availability
  370. if self.poller:
  371. self.poller.register(fileno, select.POLLIN)
  372. else:
  373. self.delete_kqueue_socket(sock, True)
  374. del self.sendbuffs[fileno]
  375. else:
  376. self.sendbuffs[fileno] = (time.clock(), msg)
  377. def newlname(self):
  378. """Generate a unique connection identifier for this socket.
  379. This is done by using an increasing counter and the current
  380. time."""
  381. self.connection_counter += 1
  382. return "%x_%x@%s" % (time.time(), self.connection_counter, self.hostname)
  383. def process_command_ping(self, sock, routing, data):
  384. self.sendmsg(sock, { "type" : "pong" }, data)
  385. def process_command_getlname(self, sock, routing, data):
  386. lname = [ k for k, v in self.lnames.items() if v == sock ][0]
  387. self.sendmsg(sock, { "type" : "getlname" }, { "lname" : lname })
  388. def process_command_send(self, sock, routing, data):
  389. group = routing["group"]
  390. instance = routing["instance"]
  391. to = routing["to"]
  392. if group == None or instance == None:
  393. return # ignore invalid packets entirely
  394. if to == "*":
  395. sockets = self.subs.find(group, instance)
  396. else:
  397. if to in self.lnames:
  398. sockets = [ self.lnames[to] ]
  399. else:
  400. return # recipient doesn't exist
  401. msg = self.preparemsg(routing, data)
  402. if sock in sockets:
  403. sockets.remove(sock)
  404. for socket in sockets:
  405. self.send_prepared_msg(socket, msg)
  406. def process_command_subscribe(self, sock, routing, data):
  407. group = routing["group"]
  408. instance = routing["instance"]
  409. if group == None or instance == None:
  410. return # ignore invalid packets entirely
  411. self.subs.subscribe(group, instance, sock)
  412. def process_command_unsubscribe(self, sock, routing, data):
  413. group = routing["group"]
  414. instance = routing["instance"]
  415. if group == None or instance == None:
  416. return # ignore invalid packets entirely
  417. self.subs.unsubscribe(group, instance, sock)
  418. def run(self):
  419. """Process messages. Forever. Mostly."""
  420. self.running = True
  421. if self.poller:
  422. self.run_poller()
  423. else:
  424. self.run_kqueue()
  425. def run_poller(self):
  426. while self.running:
  427. try:
  428. # Poll with a timeout so that every once in a while,
  429. # the loop checks for self.running.
  430. events = self.poller.poll()
  431. except select.error as err:
  432. if err.args[0] == errno.EINTR:
  433. events = []
  434. else:
  435. logger.fatal(MSGQ_POLL_ERR, err)
  436. break
  437. for (fd, event) in events:
  438. if fd == self.listen_socket.fileno():
  439. self.process_accept()
  440. else:
  441. if event & select.POLLOUT:
  442. self.__process_write(fd)
  443. elif event & select.POLLIN:
  444. self.process_socket(fd)
  445. else:
  446. logger.error(MSGQ_POLL_UNKNOWN_EVENT, fd, event)
  447. def run_kqueue(self):
  448. while self.running:
  449. # Check with a timeout so that every once in a while,
  450. # the loop checks for self.running.
  451. events = self.kqueue.control(None, 10)
  452. if not events:
  453. raise RuntimeError('serve: kqueue returned no events')
  454. for event in events:
  455. if event.ident == self.listen_socket.fileno():
  456. self.process_accept()
  457. else:
  458. if event.filter == select.KQ_FILTER_WRITE:
  459. self.__process_write(event.ident)
  460. if event.filter == select.KQ_FILTER_READ and \
  461. event.data > 0:
  462. self.process_socket(event.ident)
  463. elif event.flags & select.KQ_EV_EOF:
  464. self.kill_socket(event.ident,
  465. self.sockets[event.ident])
  466. def stop(self):
  467. self.running = False
  468. def shutdown(self):
  469. """Stop the MsgQ master."""
  470. if self.verbose:
  471. sys.stdout.write("[b10-msgq] Stopping the server.\n")
  472. self.listen_socket.close()
  473. if os.path.exists(self.socket_file):
  474. os.remove(self.socket_file)
  475. # can signal handling and calling a destructor be done without a
  476. # global variable?
  477. msgq = None
  478. def signal_handler(signal, frame):
  479. if msgq:
  480. msgq.shutdown()
  481. sys.exit(0)
  482. if __name__ == "__main__":
  483. def check_port(option, opt_str, value, parser):
  484. """Function to insure that the port we are passed is actually
  485. a valid port number. Used by OptionParser() on startup."""
  486. intval = int(value)
  487. if (intval < 0) or (intval > 65535):
  488. raise OptionValueError("%s requires a port number (0-65535)" % opt_str)
  489. parser.values.msgq_port = intval
  490. # Parse any command-line options.
  491. parser = OptionParser(version=VERSION)
  492. parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
  493. help="display more about what is going on")
  494. parser.add_option("-s", "--socket-file", dest="msgq_socket_file",
  495. type="string", default=None,
  496. help="UNIX domain socket file the msgq daemon will use")
  497. (options, args) = parser.parse_args()
  498. # Init logging, according to the parameters.
  499. # FIXME: Do proper logger configuration, this is just a hack
  500. # This is #2582
  501. sev = 'INFO'
  502. if options.verbose:
  503. sev = 'DEBUG'
  504. isc.log.init("b10-msgq", buffer=False, severity=sev, debuglevel=99)
  505. signal.signal(signal.SIGTERM, signal_handler)
  506. # Announce startup.
  507. logger.debug(TRACE_START, MSGQ_START, VERSION)
  508. msgq = MsgQ(options.msgq_socket_file, options.verbose)
  509. try:
  510. msgq.setup()
  511. except Exception as e:
  512. logger.fatal(MSGQ_START_FAIL, e)
  513. sys.exit(1)
  514. try:
  515. msgq.run()
  516. except KeyboardInterrupt:
  517. pass
  518. msgq.shutdown()