From 71a4390fdb047638cf4a5413f412f891075fb7b2 Mon Sep 17 00:00:00 2001 From: Wojtek Porczyk Date: Mon, 5 Jun 2017 16:41:14 +0200 Subject: [PATCH] qubes/tools/qubesd: properly unlink UNIX sockets at shutdown --- qubes/api/__init__.py | 4 +++- qubes/tools/qubesd.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/qubes/api/__init__.py b/qubes/api/__init__.py index 07b58076..417bfde0 100644 --- a/qubes/api/__init__.py +++ b/qubes/api/__init__.py @@ -352,5 +352,7 @@ def create_server(sockpath, handler, app, debug=False, *, loop=None): finally: os.umask(old_umask) - shutil.chown(sockpath, group='qubes') + for sock in server.sockets: + shutil.chown(sock.getsockname(), group='qubes') + return server diff --git a/qubes/tools/qubesd.py b/qubes/tools/qubesd.py index 18bc5f1b..1a700c60 100644 --- a/qubes/tools/qubesd.py +++ b/qubes/tools/qubesd.py @@ -50,6 +50,11 @@ def main(args=None): qubes.api.misc.QubesMiscAPI, app=args.app, debug=args.debug))) + socknames = [] + for server in servers: + for sock in server.sockets: + socknames.append(sock.getsockname()) + for signame in ('SIGINT', 'SIGTERM'): loop.add_signal_handler(getattr(signal, signame), sighandler, loop, signame, servers) @@ -62,6 +67,15 @@ def main(args=None): loop.run_forever() loop.run_until_complete(asyncio.wait([ server.wait_closed() for server in servers])) + for sockname in socknames: + try: + os.unlink(sockname) + except FileNotFoundError: + # XXX + # We had our socket unlinked by somebody else, possibly other + # qubesd instance. That also means we probably unlinked their + # socket when creating our server... + pass finally: loop.close()