mirror of
https://github.com/xen-project/xen.git
synced 2026-10-28 10:11:27 +01:00
Remus: add file locking and modprobe utility functions
Signed-off-by: Brendan Cully <brendan@cs.ubc.ca>
This commit is contained in:
1 parent
ad195de617
commit
c7a94e935c
1 file changed
+50
-1
@@ -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 @@ class PipeException(Exception):
|
||||
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 @@ def runcmd(args, cwd=None):
|
||||
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
|
||||