qubesd.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3.6
  2. import asyncio
  3. import os
  4. import signal
  5. import libvirtaio
  6. import qubes
  7. import qubes.api
  8. import qubes.api.admin
  9. import qubes.api.internal
  10. import qubes.api.misc
  11. import qubes.utils
  12. import qubes.vm.qubesvm
  13. def sighandler(loop, signame, servers):
  14. print('caught {}, exiting'.format(signame))
  15. for server in servers:
  16. server.close()
  17. loop.stop()
  18. parser = qubes.tools.QubesArgumentParser(description='Qubes OS daemon')
  19. parser.add_argument('--debug', action='store_true', default=False,
  20. help='Enable verbose error logging (all exceptions with full '
  21. 'tracebacks) and also send tracebacks to Admin API clients')
  22. def main(args=None):
  23. loop = asyncio.get_event_loop()
  24. libvirtaio.virEventRegisterAsyncIOImpl(loop=loop)
  25. try:
  26. args = parser.parse_args(args)
  27. except:
  28. loop.close()
  29. raise
  30. args.app.vmm.register_event_handlers(args.app)
  31. servers = loop.run_until_complete(qubes.api.create_servers(
  32. qubes.api.admin.QubesAdminAPI,
  33. qubes.api.internal.QubesInternalAPI,
  34. qubes.api.misc.QubesMiscAPI,
  35. app=args.app, debug=args.debug))
  36. socknames = []
  37. for server in servers:
  38. for sock in server.sockets:
  39. socknames.append(sock.getsockname())
  40. for signame in ('SIGINT', 'SIGTERM'):
  41. loop.add_signal_handler(getattr(signal, signame),
  42. sighandler, loop, signame, servers)
  43. qubes.utils.systemd_notify()
  44. # make sure children will not inherit this
  45. os.environ.pop('NOTIFY_SOCKET', None)
  46. try:
  47. loop.run_forever()
  48. loop.run_until_complete(asyncio.wait([
  49. server.wait_closed() for server in servers]))
  50. for sockname in socknames:
  51. try:
  52. os.unlink(sockname)
  53. except FileNotFoundError:
  54. args.app.log.warning(
  55. 'socket {} got unlinked sometime before shutdown'.format(
  56. sockname))
  57. finally:
  58. loop.close()
  59. if __name__ == '__main__':
  60. main()