Use platform specific locking method

None of found existing portable locking module does support RW locks.
Use lowlevel system locking support - both Windows and Linux support
such feature.

Drop locking code in write_firewall_conf() b/c is is called with
QubesVmCollection lock held anyway.
This commit is contained in:
Marek Marczykowski-Górecki 2013-08-27 04:00:35 +02:00
parent a5a2c0b97c
commit 11047bf427
2 changed files with 20 additions and 6 deletions

View File

@ -23,7 +23,6 @@
# #
import datetime import datetime
import fcntl
import lxml.etree import lxml.etree
import os import os
import os.path import os.path
@ -1243,9 +1242,7 @@ class QubesVm(object):
try: try:
old_umask = os.umask(002) old_umask = os.umask(002)
with open(self.firewall_conf, 'w') as f: with open(self.firewall_conf, 'w') as f:
fcntl.lockf(f, fcntl.LOCK_EX)
tree.write(f, encoding="UTF-8", pretty_print=True) tree.write(f, encoding="UTF-8", pretty_print=True)
fcntl.lockf(f, fcntl.LOCK_UN)
f.close() f.close()
os.umask(old_umask) os.umask(old_umask)
except EnvironmentError as err: except EnvironmentError as err:

View File

@ -28,12 +28,19 @@ import os
import os.path import os.path
import lxml.etree import lxml.etree
import xml.parsers.expat import xml.parsers.expat
import fcntl
import time import time
import warnings import warnings
import tempfile import tempfile
import grp import grp
import atexit import atexit
if os.name == 'posix':
import fcntl
elif os.name == 'nt':
import win32con
import win32file
import pywintypes
else:
raise RuntimeError, "Qubes works only on POSIX or WinNT systems"
# Do not use XenAPI or create/read any VM files # Do not use XenAPI or create/read any VM files
# This is for testing only! # This is for testing only!
@ -579,7 +586,12 @@ class QubesVmCollection(dict):
# still the right file # still the right file
while True: while True:
self.qubes_store_file = open (self.qubes_store_filename, 'r') self.qubes_store_file = open (self.qubes_store_filename, 'r')
fcntl.lockf(self.qubes_store_file, fcntl.LOCK_SH) if os.name == 'posix':
fcntl.lockf (self.qubes_store_file, fcntl.LOCK_SH)
elif os.name == 'nt':
overlapped = pywintypes.OVERLAPPED()
win32file.LockFileEx(win32file._get_osfhandle(self.qubes_store_file.fileno()),
0, 0, -0x10000, overlapped)
if os.fstat(self.qubes_store_file.fileno()) == os.stat( if os.fstat(self.qubes_store_file.fileno()) == os.stat(
self.qubes_store_filename): self.qubes_store_filename):
break break
@ -591,7 +603,12 @@ class QubesVmCollection(dict):
# still the right file # still the right file
while True: while True:
self.qubes_store_file = open (self.qubes_store_filename, 'r+') self.qubes_store_file = open (self.qubes_store_filename, 'r+')
fcntl.lockf(self.qubes_store_file, fcntl.LOCK_EX) if os.name == 'posix':
fcntl.lockf (self.qubes_store_file, fcntl.LOCK_EX)
elif os.name == 'nt':
overlapped = pywintypes.OVERLAPPED()
win32file.LockFileEx(win32file._get_osfhandle(self.qubes_store_file.fileno()),
win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped)
if os.fstat(self.qubes_store_file.fileno()) == os.stat( if os.fstat(self.qubes_store_file.fileno()) == os.stat(
self.qubes_store_filename): self.qubes_store_filename):
break break