qmemmand.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. import ConfigParser
  24. import SocketServer
  25. import logging
  26. import logging.handlers
  27. import os
  28. import socket
  29. import sys
  30. import thread
  31. import xen.lowlevel.xs
  32. import qubes.qmemman
  33. import qubes.qmemman.algo
  34. import qubes.utils
  35. SOCK_PATH = '/var/run/qubes/qmemman.sock'
  36. LOG_PATH = '/var/log/qubes/qmemman.log'
  37. system_state = qubes.qmemman.SystemState()
  38. global_lock = thread.allocate_lock()
  39. def only_in_first_list(l1, l2):
  40. ret = []
  41. for i in l1:
  42. if not i in l2:
  43. ret.append(i)
  44. return ret
  45. def get_domain_meminfo_key(domain_id):
  46. return '/local/domain/'+domain_id+'/memory/meminfo'
  47. class WatchType(object):
  48. def __init__(self, fn, param):
  49. self.fn = fn
  50. self.param = param
  51. class XS_Watcher(object):
  52. def __init__(self):
  53. self.log = logging.getLogger('qmemman.daemon.xswatcher')
  54. self.log.debug('XS_Watcher()')
  55. self.handle = xen.lowlevel.xs.xs()
  56. self.handle.watch('@introduceDomain', WatchType(XS_Watcher.domain_list_changed, None))
  57. self.handle.watch('@releaseDomain', WatchType(XS_Watcher.domain_list_changed, None))
  58. self.watch_token_dict = {}
  59. def domain_list_changed(self, param):
  60. self.log.debug('domain_list_changed(param={!r})'.format(param))
  61. curr = self.handle.ls('', '/local/domain')
  62. self.log.debug('curr={!r}'.format(curr))
  63. if curr == None:
  64. return
  65. self.log.debug('acquiring global_lock')
  66. global_lock.acquire()
  67. self.log.debug('global_lock acquired')
  68. for i in only_in_first_list(curr, self.watch_token_dict.keys()):
  69. #new domain has been created
  70. watch = WatchType(XS_Watcher.meminfo_changed, i)
  71. self.watch_token_dict[i] = watch
  72. self.handle.watch(get_domain_meminfo_key(i), watch)
  73. system_state.add_domain(i)
  74. for i in only_in_first_list(self.watch_token_dict.keys(), curr):
  75. #domain destroyed
  76. self.handle.unwatch(get_domain_meminfo_key(i), self.watch_token_dict[i])
  77. self.watch_token_dict.pop(i)
  78. system_state.del_domain(i)
  79. global_lock.release()
  80. self.log.debug('global_lock released')
  81. system_state.do_balance()
  82. def meminfo_changed(self, domain_id):
  83. self.log.debug('meminfo_changed(domain_id={!r})'.format(domain_id))
  84. untrusted_meminfo_key = self.handle.read(
  85. '', get_domain_meminfo_key(domain_id))
  86. if untrusted_meminfo_key == None or untrusted_meminfo_key == '':
  87. return
  88. self.log.debug('acquiring global_lock')
  89. global_lock.acquire()
  90. self.log.debug('global_lock acquired')
  91. system_state.refresh_meminfo(domain_id, untrusted_meminfo_key)
  92. global_lock.release()
  93. self.log.debug('global_lock released')
  94. def watch_loop(self):
  95. self.log.debug('watch_loop()')
  96. while True:
  97. result = self.handle.read_watch()
  98. self.log.debug('watch_loop result={!r}'.format(result))
  99. token = result[1]
  100. token.fn(self, token.param)
  101. class QMemmanReqHandler(SocketServer.BaseRequestHandler):
  102. """
  103. The RequestHandler class for our server.
  104. It is instantiated once per connection to the server, and must
  105. override the handle() method to implement communication to the
  106. client.
  107. """
  108. def handle(self):
  109. self.log = logging.getLogger('qmemman.daemon.reqhandler')
  110. got_lock = False
  111. # self.request is the TCP socket connected to the client
  112. while True:
  113. self.data = self.request.recv(1024).strip()
  114. self.log.debug('data={!r}'.format(self.data))
  115. if len(self.data) == 0:
  116. self.log.info('EOF')
  117. if got_lock:
  118. global_lock.release()
  119. self.log.debug('global_lock released')
  120. return
  121. # XXX something is wrong here: return without release?
  122. if got_lock:
  123. self.log.warning('Second request over qmemman.sock?')
  124. return
  125. self.log.debug('acquiring global_lock')
  126. global_lock.acquire()
  127. self.log.debug('global_lock acquired')
  128. got_lock = True
  129. if system_state.do_balloon(int(self.data)):
  130. resp = "OK\n"
  131. else:
  132. resp = "FAIL\n"
  133. self.log.debug('resp={!r}'.format(resp))
  134. self.request.send(resp)
  135. # XXX no release of lock?
  136. parser = qubes.tools.get_parser_base()
  137. parser.add_argument('--config', '-c', metavar='FILE',
  138. action='store', default='/etc/qubes/qmemman.conf',
  139. help='qmemman config file')
  140. parser.add_argument('--foreground',
  141. action='store_true', default=False,
  142. help='do not close stdio')
  143. def main():
  144. args = parser.parse_args()
  145. # setup logging
  146. ha_syslog = logging.handlers.SysLogHandler('/dev/log')
  147. ha_syslog.setFormatter(
  148. logging.Formatter('%(name)s[%(process)d]: %(message)s'))
  149. logging.root.addHandler(ha_syslog)
  150. # leave log for backwards compatibility
  151. ha_file = logging.FileHandler(LOG_PATH)
  152. ha_file.setFormatter(
  153. logging.Formatter('%(asctime)s %(name)s[%(process)d]: %(message)s'))
  154. logging.root.addHandler(ha_file)
  155. if args.foreground:
  156. ha_stderr = logging.StreamHandler(sys.stderr)
  157. ha_file.setFormatter(
  158. logging.Formatter('%(asctime)s %(name)s[%(process)d]: %(message)s'))
  159. logging.root.addHandler(ha_stderr)
  160. else:
  161. # close io
  162. sys.stdout.close()
  163. sys.stderr.close()
  164. sys.stdin.close()
  165. logging.root.setLevel((args.quiet - args.verbose) * 10 + logging.WARNING)
  166. log = logging.getLogger('qmemman.daemon')
  167. config = ConfigParser.SafeConfigParser({
  168. 'vm-min-mem': str(qubes.qmemman.algo.MIN_PREFMEM),
  169. 'dom0-mem-boost': str(qubes.qmemman.algo.DOM0_MEM_BOOST),
  170. 'cache-margin-factor': str(qubes.qmemman.algo.CACHE_FACTOR)
  171. })
  172. config.read(args.config)
  173. if config.has_section('global'):
  174. qubes.qmemman.algo.MIN_PREFMEM = \
  175. qubes.utils.parse_size(config.get('global', 'vm-min-mem'))
  176. qubes.qmemman.algo.DOM0_MEM_BOOST = \
  177. qubes.utils.parse_size(config.get('global', 'dom0-mem-boost'))
  178. qubes.qmemman.algo.CACHE_FACTOR = \
  179. config.getfloat('global', 'cache-margin-factor')
  180. log.info('MIN_PREFMEM={algo.MIN_PREFMEM}'
  181. ' DOM0_MEM_BOOST={algo.DOM0_MEM_BOOST}'
  182. ' CACHE_FACTOR={algo.CACHE_FACTOR}'.format(
  183. algo=qubes.qmemman.algo))
  184. try:
  185. os.unlink(SOCK_PATH)
  186. except:
  187. pass
  188. log.debug('instantiating server')
  189. os.umask(0)
  190. server = SocketServer.UnixStreamServer(SOCK_PATH, QMemmanReqHandler)
  191. os.umask(077)
  192. # notify systemd
  193. nofity_socket = os.getenv('NOTIFY_SOCKET')
  194. if nofity_socket:
  195. log.debug('notifying systemd')
  196. s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
  197. if nofity_socket.startswith('@'):
  198. nofity_socket = '\0%s' % nofity_socket[1:]
  199. s.connect(nofity_socket)
  200. s.sendall("READY=1")
  201. s.close()
  202. thread.start_new_thread(server.serve_forever, ())
  203. XS_Watcher().watch_loop()