From c421dc2a95d286ed84bd9de63c13949b34f6d555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Mon, 20 Apr 2015 05:41:03 +0200 Subject: [PATCH] Prevent concurrent qvm-sync-clock calls In some cases qvm-sync-clock can take a long time (for example in case of network problems, or when some do not responds). This can lead to multiple qvm-sync-clock hanging for the same reason (blocking vchan resources). To prevent that create a lock file and simply abort when one instance is already running. --- qvm-tools/qvm-sync-clock | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/qvm-tools/qvm-sync-clock b/qvm-tools/qvm-sync-clock index 52f1ba76..9d98f3a7 100755 --- a/qvm-tools/qvm-sync-clock +++ b/qvm-tools/qvm-sync-clock @@ -20,6 +20,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # +import fcntl from qubes.qubes import QubesVmCollection import os.path @@ -43,6 +44,19 @@ def main(): verbose = False if len(sys.argv) > 1 and sys.argv[1] in [ '--verbose', '-v' ]: verbose = True + + lockfile_name = "/var/run/qubes/qvm-sync-clock.lock" + if os.path.exists(lockfile_name): + lockfile = open(lockfile_name, "r") + else: + lockfile = open(lockfile_name, "w") + + fcntl.fcntl(lockfile.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC) + try: + fcntl.flock(lockfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + except IOError: + print >>sys.stderr, "qvm-sync-clock already running, aborting" + sys.exit(1) qvm_collection = QubesVmCollection() qvm_collection.lock_db_for_reading() @@ -98,5 +112,9 @@ def main(): print >> sys.stderr, "ERROR syncing time in VM '%s': %s" % (vm.name, str(e)) pass + # order is important! + os.unlink(lockfile_name) + lockfile.close() + main()