Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yum: check whether the lock file disappeared #58581

Merged
merged 1 commit into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/ansible/module_utils/yumdnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ def __init__(self, module):
# default isn't a bad idea
self.lockfile = '/var/run/yum.pid'

@abstractmethod
def is_lockfile_pid_valid(self):
raise NotImplementedError
return

def _is_lockfile_present(self):
return (os.path.isfile(self.lockfile) or glob.glob(self.lockfile)) and self.is_lockfile_pid_valid()
Expand Down
56 changes: 30 additions & 26 deletions lib/ansible/modules/packaging/os/yum.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,38 +413,42 @@ def _enablerepos_with_error_checking(self, yumbase):

def is_lockfile_pid_valid(self):
try:
with open(self.lockfile, 'r') as f:
oldpid = int(f.readline())
except ValueError:
# invalid data
os.unlink(self.lockfile)
return False
except (IOError, OSError) as e:
self.module.fail_json(msg="Failure opening %s: %s" % (self.lockfile, to_native(e)))

if oldpid == os.getpid():
# that's us?
os.unlink(self.lockfile)
return False

try:
with open("/proc/%d/stat" % oldpid, 'r') as f:
stat = f.readline()
try:
with open(self.lockfile, 'r') as f:
oldpid = int(f.readline())
except ValueError:
# invalid data
os.unlink(self.lockfile)
return False

if stat.split()[2] == 'Z':
# Zombie
if oldpid == os.getpid():
# that's us?
os.unlink(self.lockfile)
return False
except IOError:

try:
os.kill(oldpid, 0)
except OSError as e:
if e.errno == errno.ESRCH:
# No such process
with open("/proc/%d/stat" % oldpid, 'r') as f:
stat = f.readline()

if stat.split()[2] == 'Z':
# Zombie
os.unlink(self.lockfile)
return False

self.module.fail_json(msg="Unable to check PID %s in %s: %s" % (oldpid, self.lockfile, to_native(e)))
except IOError:
# either /proc is not mounted or the process is already dead
try:
# check the state of the process
os.kill(oldpid, 0)
except OSError as e:
if e.errno == errno.ESRCH:
# No such process
os.unlink(self.lockfile)
return False

self.module.fail_json(msg="Unable to check PID %s in %s: %s" % (oldpid, self.lockfile, to_native(e)))
except (IOError, OSError) as e:
# lockfile disappeared?
return False

# another copy seems to be running
return True
Expand Down