From ae2df81ef9b7c8da6a7f8b201e95e01062543953 Mon Sep 17 00:00:00 2001
From: Faiyaz Ahmed <faiyaza@cs.princeton.edu>
Date: Fri, 4 Apr 2008 19:54:58 +0000
Subject: [PATCH] File locking isn't exclusive in the same process across
 threads.  Switched to regular semaphores.  The assumption is
 sliver_vs.configure is called before Vserver.start(), which sets
 disk_usage_initialized.  The disk check should be skipped during start, so
 the lock won't carry over.  ...I hope.

---
 sliver_vs.py | 5 ++++-
 tools.py     | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/sliver_vs.py b/sliver_vs.py
index 13ff990..3060b9f 100644
--- a/sliver_vs.py
+++ b/sliver_vs.py
@@ -24,6 +24,9 @@ import vserver
 import accounts
 import logger
 import tools
+from threading import BoundedSemaphore
+
+globalsem = BoundedSemaphore()
 
 # special constant that tells vserver to keep its existing settings
 KEEP_LIMIT = vserver.VC_LIM_KEEP
@@ -42,7 +45,7 @@ class Sliver_VS(accounts.Account, vserver.VServer):
 
     SHELL = '/bin/vsh'
     TYPE = 'sliver.VServer'
-    _init_disk_info_sem = tools.NMLock("/var/run/nm-disk-info.lock")
+    _init_disk_info_sem = globalsem
 
     def __init__(self, rec):
         logger.verbose ('initing Sliver_VS with name=%s'%rec['name'])
diff --git a/tools.py b/tools.py
index 49441bd..776a8d4 100644
--- a/tools.py
+++ b/tools.py
@@ -94,6 +94,7 @@ def write_temp_file(do_write, mode=None, uidgid=None):
 
 class NMLock:
     def __init__(self, file):
+        logger.log("Lock %s initialized." % file, 2)
         self.fd = os.open(file, os.O_RDWR|os.O_CREAT, 0600)
         flags = fcntl.fcntl(self.fd, fcntl.F_GETFD)
         flags |= fcntl.FD_CLOEXEC
@@ -101,6 +102,8 @@ class NMLock:
     def __del__(self):
         os.close(self.fd)
     def acquire(self):
-        fcntl.lockf(self.fd, fcntl.LOCK_EX)
+        logger.log("Lock acquired.", 2)
+        fcntl.lockf(self.fd, fcntl.LOCK_SH)
     def release(self):
+        logger.log("Lock released.", 2)
         fcntl.lockf(self.fd, fcntl.LOCK_UN)
-- 
2.47.0