[entropy] simplify entropy locks management code, drop non-atomic functions

This commit is contained in:
Fabio Erculiani
2011-10-05 10:44:42 +02:00
parent 1df7743540
commit cd8d70728d
9 changed files with 47 additions and 138 deletions
-5
View File
@@ -466,11 +466,6 @@ def _database_spmsync(entropy_client):
print_info(red(" %s." % (_("Entropy locked, giving up"),)))
return 2
acquired = entropy_client.lock_resources()
if not acquired:
print_info(red(" %s." % (_("Entropy locked during lock acquire"),)))
return 2
if to_be_removed:
mytxt = blue("%s. %s:") % (
_("Someone removed these packages"),
+2 -47
View File
@@ -1589,51 +1589,6 @@ class MiscMixin:
f_obj.close()
MiscMixin.RESOURCES_LOCK_F_REF = None
def resources_locked(self):
"""
Determine whether Entropy resources are locked (in use).
@return: True, if resources are locked
@rtype: bool
"""
return self._check_pid_file_lock(etpConst['locks']['using_resources'])
def _check_pid_file_lock(self, pidfile):
pid_f = None
flags = fcntl.LOCK_EX | fcntl.LOCK_NB
pid = os.getpid()
try:
pid_f = open(pidfile, "r")
fcntl.flock(pid_f.fileno(), flags)
# unlock now, file is not locked
fcntl.flock(pid_f.fileno(), fcntl.LOCK_UN)
return False
except IOError as err:
if err.errno == errno.ENOENT:
# file doesn't exist, not locked
return False
elif err.errno in (errno.EACCES, errno.EAGAIN):
# locked? our pid?
if pid_f is None:
# wtf? not locked for me
return False
# check if same pid
s_pid = pid_f.read(128).strip()
try:
s_pid = int(s_pid)
except ValueError:
return True # locked anyway
# locked?
return s_pid != pid
else:
# ouch, wtf?
raise
finally:
if pid_f is not None:
pid_f.close()
def _create_pid_file_lock(self, pidfile, blocking = False):
if MiscMixin.RESOURCES_LOCK_F_REF is not None:
@@ -1703,8 +1658,8 @@ class MiscMixin:
lock_count = 0
# check lock file
while True:
locked = self.resources_locked()
if not locked:
acquired = self.lock_resources(blocking=False)
if acquired:
if lock_count > 0:
self.output(
blue(_("Resources unlocked, let's go!")),
+5 -14
View File
@@ -3640,10 +3640,6 @@ class Package:
def run(self, xterm_header = None):
self._error_on_not_prepared()
gave_up = self._entropy.wait_resources()
if gave_up:
return 20
locked = self._entropy.another_entropy_running()
if locked:
self._entropy.output(
@@ -3654,16 +3650,11 @@ class Package:
)
return 21
# lock
acquired = self._entropy.lock_resources()
if not acquired:
self._entropy.output(
blue(_("Cannot acquire Entropy resources lock.")),
importance = 2,
level = "error",
header = darkred(" ## ")
)
return 4 # app locked during lock acquire
gave_up = self._entropy.wait_resources()
if gave_up:
return 20
# resources acquired
try:
rc = self._stepper(xterm_header)
finally:
@@ -278,20 +278,7 @@ class Repository:
if gave_up:
return 3
locked = self._entropy.another_entropy_running()
if locked:
self._entropy.output(
red(_("Another Entropy is currently running.")),
importance = 1,
level = "error",
header = darkred(" @@ ")
)
return 4
# lock
acquired = self._entropy.lock_resources()
if not acquired:
return 4 # app locked during lock acquire
# locked
try:
rc = self._run_sync()
if rc:
+4 -8
View File
@@ -1031,10 +1031,6 @@ class System:
header = red(" @@ ")
)
gave_up = self._entropy.wait_resources()
if gave_up:
return 7
locked = self._entropy.another_entropy_running()
if locked:
self._entropy.output(
@@ -1045,11 +1041,11 @@ class System:
)
return 4
# lock
acquired = self._entropy.lock_resources()
if not acquired:
return 4 # app locked during lock acquire
gave_up = self._entropy.wait_resources()
if gave_up:
return 7
# acquired
try:
rc_lock = self.__run_fetch(force = force)
if rc_lock != 0:
+4 -21
View File
@@ -3028,24 +3028,6 @@ def release_lock(lock_file, lock_map):
raise
def acquire_entropy_locks(entropy_client, blocking = False,
max_tries = 300):
"""
Acquire Entropy Client/Server file locks.
@param entropy_client: any Entropy Client based instance
@type entropy_client: entropy.client.interfaces.Client
@keyword blocking: acquire locks in blocking mode?
@type blocking: bool
@keyword max_tries: number of tries for wait_resources()
@type max_tries: int
"""
locked = entropy_client.another_entropy_running()
if locked:
return False
return acquire_entropy_resources_locks(entropy_client,
blocking = blocking, max_tries = max_tries)
def acquire_entropy_resources_locks(entropy_client, blocking = False,
max_tries = 300):
"""
Acquire Entropy Resources General Lock.
@@ -3064,12 +3046,13 @@ def acquire_entropy_resources_locks(entropy_client, blocking = False,
gave_up = entropy_client.wait_resources(max_lock_count = max_tries)
if gave_up:
return False
# acquired
return True
# acquire resources lock
acquired = entropy_client.lock_resources(blocking = blocking)
# acquire resources lock in blocking mode
acquired = entropy_client.lock_resources(blocking = True)
if not acquired:
return False
return True
def release_entropy_locks(entropy_client):
+24 -26
View File
@@ -462,22 +462,12 @@ class UpdatesDaemon(dbus.service.Object):
return False
# entropy resources locked?
locked = entropy.resources_locked()
if locked:
if DAEMON_DEBUG:
write_output(
"__acquire_entropy_locks: resources locked, skipping")
return False
# acquire resources lock
acquired = entropy.lock_resources()
acquired = entropy.lock_resources(blocking = False)
if not acquired:
if DAEMON_DEBUG:
write_output(
"__acquire_entropy_locks: resources locked during acquire")
return False
return True
"__acquire_entropy_locks: resources locked, skipping")
return acquired
def __release_entropy_locks(self, entropy, pid_lock = True):
# unlock resources
@@ -666,21 +656,29 @@ class UpdatesDaemon(dbus.service.Object):
try:
entropy = Entropy()
locked = entropy.resources_locked()
if locked:
if DAEMON_DEBUG:
write_output("_is_system_changed: resources locked!")
return False # resources are locked, nothing changed yet :P
acquired = False
try:
acquired = entropy.lock_resources(blocking = False)
if not acquired:
if DAEMON_DEBUG:
write_output(
"_is_system_changed: resources locked!")
# resources are locked, nothing changed yet :P
return False
last_checksum = self.__last_system_repo_checksum
cur_checksum = entropy.installed_repository().checksum(
strict = False, strings = True)
last_checksum = self.__last_system_repo_checksum
cur_checksum = entropy.installed_repository().checksum(
strict = False, strings = True)
changed = last_checksum != cur_checksum
if DAEMON_DEBUG and changed:
write_output("_is_system_changed: system db checksum changed!")
self.__last_system_repo_checksum = cur_checksum
return changed
changed = last_checksum != cur_checksum
if DAEMON_DEBUG and changed:
write_output(
"_is_system_changed: system db checksum changed!")
self.__last_system_repo_checksum = cur_checksum
return changed
finally:
if acquired:
entropy.unlock_resources()
finally:
if entropy is not None:
+1 -1
View File
@@ -120,7 +120,7 @@ class EntropyResourceLock(object):
self.__inside_with_stmt = 0
def acquire(self):
acquired = entropy.tools.acquire_entropy_resources_locks(self._entropy,
acquired = entropy.tools.acquire_entropy_locks(self._entropy,
blocking = self._blocking)
if not acquired:
raise EntropyResourceLock.NotAcquired("unable to acquire lock")
+6 -2
View File
@@ -93,8 +93,12 @@ class SulfurApplication(Controller, SulfurApplicationEventsMixin):
if resources_locked:
locked = True
else:
locked = not entropy.tools.acquire_entropy_locks(self._entropy,
max_tries = 5)
# we don't want to interleave equo or entropy services with
# sulfur. People just cannot deal with it.
locked = self._entropy.another_entropy_running()
if not locked:
locked = not entropy.tools.acquire_entropy_locks(self._entropy,
max_tries = 5)
self._effective_root = os.getuid() == 0
if self._effective_root:
self._privileges.drop()