1# lock.py 2# DNF Locking Subsystem. 3# 4# Copyright (C) 2013-2016 Red Hat, Inc. 5# 6# This copyrighted material is made available to anyone wishing to use, 7# modify, copy, or redistribute it subject to the terms and conditions of 8# the GNU General Public License v.2, or (at your option) any later version. 9# This program is distributed in the hope that it will be useful, but WITHOUT 10# ANY WARRANTY expressed or implied, including the implied warranties of 11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 12# Public License for more details. You should have received a copy of the 13# GNU General Public License along with this program; if not, write to the 14# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 15# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the 16# source code or documentation are not subject to the GNU General Public 17# License and may only be used or replicated with the express permission of 18# Red Hat, Inc. 19# 20 21from __future__ import absolute_import 22from __future__ import unicode_literals 23from dnf.exceptions import ProcessLockError, ThreadLockError, LockError 24from dnf.i18n import _ 25from dnf.yum import misc 26import dnf.logging 27import dnf.util 28import errno 29import fcntl 30import hashlib 31import logging 32import os 33import threading 34import time 35 36logger = logging.getLogger("dnf") 37 38def _fit_lock_dir(dir_): 39 if not dnf.util.am_i_root(): 40 # for regular users the best we currently do is not to clash with 41 # another DNF process of the same user. Since dir_ is quite definitely 42 # not writable for us, yet significant, use its hash: 43 hexdir = hashlib.sha1(dir_.encode('utf-8')).hexdigest() 44 dir_ = os.path.join(misc.getCacheDir(), 'locks', hexdir) 45 return dir_ 46 47def build_download_lock(cachedir, exit_on_lock): 48 return ProcessLock(os.path.join(_fit_lock_dir(cachedir), 'download_lock.pid'), 49 'cachedir', not exit_on_lock) 50 51def build_metadata_lock(cachedir, exit_on_lock): 52 return ProcessLock(os.path.join(_fit_lock_dir(cachedir), 'metadata_lock.pid'), 53 'metadata', not exit_on_lock) 54 55 56def build_rpmdb_lock(persistdir, exit_on_lock): 57 return ProcessLock(os.path.join(_fit_lock_dir(persistdir), 'rpmdb_lock.pid'), 58 'RPMDB', not exit_on_lock) 59 60 61def build_log_lock(logdir, exit_on_lock): 62 return ProcessLock(os.path.join(_fit_lock_dir(logdir), 'log_lock.pid'), 63 'log', not exit_on_lock) 64 65 66class ProcessLock(object): 67 def __init__(self, target, description, blocking=False): 68 self.blocking = blocking 69 self.count = 0 70 self.description = description 71 self.target = target 72 self.thread_lock = threading.RLock() 73 74 def _lock_thread(self): 75 if not self.thread_lock.acquire(blocking=False): 76 msg = '%s already locked by a different thread' % self.description 77 raise ThreadLockError(msg) 78 self.count += 1 79 80 def _try_lock(self, pid): 81 fd = os.open(self.target, os.O_CREAT | os.O_RDWR, 0o644) 82 83 try: 84 try: 85 fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) 86 except OSError as e: 87 if e.errno == errno.EWOULDBLOCK: 88 return -1 89 raise 90 91 old_pid = os.read(fd, 20) 92 if len(old_pid) == 0: 93 # empty file, write our pid 94 os.write(fd, str(pid).encode('utf-8')) 95 return pid 96 97 try: 98 old_pid = int(old_pid) 99 except ValueError: 100 msg = _('Malformed lock file found: %s.\n' 101 'Ensure no other dnf/yum process is running and ' 102 'remove the lock file manually or run ' 103 'systemd-tmpfiles --remove dnf.conf.') % (self.target) 104 raise LockError(msg) 105 106 if old_pid == pid: 107 # already locked by this process 108 return pid 109 110 if not os.access('/proc/%d/stat' % old_pid, os.F_OK): 111 # locked by a dead process, write our pid 112 os.lseek(fd, 0, os.SEEK_SET) 113 os.ftruncate(fd, 0) 114 os.write(fd, str(pid).encode('utf-8')) 115 return pid 116 117 return old_pid 118 119 finally: 120 os.close(fd) 121 122 def _unlock_thread(self): 123 self.count -= 1 124 self.thread_lock.release() 125 126 def __enter__(self): 127 dnf.util.ensure_dir(os.path.dirname(self.target)) 128 self._lock_thread() 129 prev_pid = -1 130 my_pid = os.getpid() 131 pid = self._try_lock(my_pid) 132 while pid != my_pid: 133 if pid != -1: 134 if not self.blocking: 135 self._unlock_thread() 136 msg = '%s already locked by %d' % (self.description, pid) 137 raise ProcessLockError(msg, pid) 138 if prev_pid != pid: 139 msg = _('Waiting for process with pid %d to finish.') % (pid) 140 logger.info(msg) 141 prev_pid = pid 142 time.sleep(1) 143 pid = self._try_lock(my_pid) 144 145 def __exit__(self, *exc_args): 146 if self.count == 1: 147 os.unlink(self.target) 148 self._unlock_thread() 149