Browse Source

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.
Marek Marczykowski-Górecki 10 years ago
parent
commit
11047bf427
2 changed files with 20 additions and 6 deletions
  1. 0 3
      core-modules/000QubesVm.py
  2. 20 3
      core/qubes.py

+ 0 - 3
core-modules/000QubesVm.py

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

+ 20 - 3
core/qubes.py

@@ -28,12 +28,19 @@ import os
 import os.path
 import lxml.etree
 import xml.parsers.expat
-import fcntl
 import time
 import warnings
 import tempfile
 import grp
 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
 # This is for testing only!
@@ -579,7 +586,12 @@ class QubesVmCollection(dict):
         # still the right file
         while True:
             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(
                     self.qubes_store_filename):
                 break
@@ -591,7 +603,12 @@ class QubesVmCollection(dict):
         # still the right file
         while True:
             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(
                     self.qubes_store_filename):
                 break