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.
This commit is contained in:
Marek Marczykowski-Górecki 2015-04-20 05:41:03 +02:00
parent 7652137854
commit c421dc2a95

View File

@ -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()