xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Brendan Cully <brendan@cs.ubc.ca>
To: xen-devel@lists.xensource.com
Subject: [PATCH 6 of 7] Remus: add file locking and modprobe utility functions
Date: Mon, 03 May 2010 11:53:53 -0700	[thread overview]
Message-ID: <95c7229ae3f411dfe57b.1272912833@kremvax.cs.ubc.ca> (raw)
In-Reply-To: <patchbomb.1272912827@kremvax.cs.ubc.ca>

[-- Attachment #1: Type: text/plain, Size: 51 bytes --]

Signed-off-by: Brendan Cully <brendan@cs.ubc.ca>



[-- Attachment #2: xen-4.0-6.patch --]
[-- Type: text/x-patch, Size: 2143 bytes --]

# HG changeset patch
# User Brendan Cully <brendan@cs.ubc.ca>
# Date 1272694699 25200
# Node ID 95c7229ae3f411dfe57b9d92203336c75fd628e8
# Parent  c10edb7433f4546351dca7b47de0821fa0da795e
Remus: add file locking and modprobe utility functions

Signed-off-by: Brendan Cully <brendan@cs.ubc.ca>

diff --git a/tools/python/xen/remus/util.py b/tools/python/xen/remus/util.py
--- a/tools/python/xen/remus/util.py
+++ b/tools/python/xen/remus/util.py
@@ -1,6 +1,6 @@
 # utility functions
 
-import os, subprocess
+import fcntl, os, subprocess
 
 class PipeException(Exception):
     def __init__(self, message, errno):
@@ -8,9 +8,50 @@
         message = '%s: %d, %s' % (message, errno, os.strerror(errno))
         Exception.__init__(self, message)
 
+class Lock(object):
+    """advisory lock"""
+
+    def __init__(self, filename):
+        """lock using filename for synchronization"""
+        self.filename = filename + '.lock'
+
+        self.fd = None
+
+        self.lock()
+
+    def __del__(self):
+        self.unlock()
+
+    def lock(self):
+        if self.fd:
+            return
+
+        self.fd = open(self.filename, 'w')
+        fcntl.lockf(self.fd, fcntl.LOCK_EX)
+
+    def unlock(self):
+        if not self.fd:
+            return
+
+        fcntl.lockf(self.fd, fcntl.LOCK_UN)
+        self.fd = None
+        try:
+            os.remove(self.filename)
+        except OSError:
+            # harmless race
+            pass
+
 def canonifymac(mac):
     return ':'.join(['%02x' % int(field, 16) for field in mac.split(':')])
 
+def checkpid(pid):
+    """return True if pid is live"""
+    try:
+        os.kill(pid, 0)
+        return True
+    except OSError:
+        return False
+
 def runcmd(args, cwd=None):
     # TODO: stdin handling
     if type(args) == str:
@@ -29,3 +70,11 @@
         return stdout
     except (OSError, IOError), inst:
         raise PipeException('could not run %s' % args[0], inst.errno)
+
+def modprobe(modname):
+    """attempt to load kernel module modname"""
+    try:
+        runcmd(['modprobe', '-q', modname])
+        return True
+    except PipeException:
+        return False

[-- Attachment #3: Type: text/plain, Size: 138 bytes --]

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xensource.com
http://lists.xensource.com/xen-devel

  parent reply	other threads:[~2010-05-03 18:53 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-03 18:53 [PATCH 0 of 7] Remus: pvops dom0 support Brendan Cully
2010-05-03 18:53 ` [PATCH 1 of 7] Remus: python netlink fixes Brendan Cully
2010-05-03 18:53 ` [PATCH 2 of 7] Remus: remove obsolete code Brendan Cully
2010-05-03 18:53 ` [PATCH 3 of 7] Remus: move device handling into its own module Brendan Cully
2010-05-03 18:53 ` [PATCH 4 of 7] Remus: fix VM stringification Brendan Cully
2010-05-03 19:21   ` Measuring the amount of memory read/written (basically touched) by a domU Yuvraj Agarwal
2010-05-03 19:32     ` Keir Fraser
2010-05-03 18:53 ` [PATCH 5 of 7] Remus: include device name in vif objects Brendan Cully
2010-05-03 18:53 ` Brendan Cully [this message]
2010-05-03 18:53 ` [PATCH 7 of 7] Remus: use IFB for net buffer on newer kernels Brendan Cully
2010-05-03 20:05 ` [PATCH 0 of 7] Remus: pvops dom0 support Gilberto Nunes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=95c7229ae3f411dfe57b.1272912833@kremvax.cs.ubc.ca \
    --to=brendan@cs.ubc.ca \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).