Skip to content

Commit

Permalink
apply locking patches to HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Vidal committed Oct 21, 2003
1 parent 25932bd commit 23cac0b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
37 changes: 3 additions & 34 deletions bin/yum
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,13 @@ import yumlock

uid = os.geteuid()
lockfile = '/var/run/yum.pid'
mypid = str(os.getpid())

if uid == 0:
#check out/get the lockfile
if yumlock.lock(lockfile, mypid, 0644):
pass
else:
fd = open(lockfile, 'r')
try: oldpid = int(fd.readline())
except ValueError:
# bogus data in the pid file. Throw away.
os.unlink(lockfile)
else:
try: os.kill(oldpid, 0)
except OSError, e:
import errno
if e[0] == errno.ESRCH:
# The pid doesn't exist
os.unlink(lockfile)
else:
# Whoa. What the heck happened?
print _('Unable to check if PID %s is active') % oldpid
sys.exit(1)
else:
# Another copy seems to be running.
msg = _('Existing lock %s: another copy is running. Aborting.')
print msg % lockfile
sys.exit(1)
# lock again.
yumlock.lock(lockfile, mypid, 0644)

try:
yummain.main(sys.argv[1:])
except SystemExit, e:
if uid == 0:
yumlock.unlock(lockfile)
if e.code != 200:
if uid == 0:
yumlock.unlock(lockfile)
sys.exit(e)
except KeyboardInterrupt, e:
print _('Exiting on User Cancel')
Expand All @@ -72,5 +43,3 @@ except IOError, (errno, strerror):
if uid == 0:
yumlock.unlock(lockfile)
sys.exit(1)


5 changes: 4 additions & 1 deletion yumlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ def lock(filename, contents='', mode=0777):
return 1

def unlock(filename):
os.unlink(filename)
try:
os.unlink(filename)
except OSError, msg:
pass
38 changes: 38 additions & 0 deletions yummain.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import rpm
import rpmUtils
import yumcomps
import yumlock

from logger import Logger
from config import yumconf
Expand Down Expand Up @@ -113,6 +114,38 @@ def parseCmdArgs(args):

return (log, errorlog, filelog, conf, cmds)


def lock(lockfile, mypid):
"""do the lock file work"""
#check out/get the lockfile
if yumlock.lock(lockfile, mypid, 0644):
pass
else:
fd = open(lockfile, 'r')
try: oldpid = int(fd.readline())
except ValueError:
# bogus data in the pid file. Throw away.
yumlock.unlock(lockfile)
else:
try: os.kill(oldpid, 0)
except OSError, e:
import errno
if e[0] == errno.ESRCH:
print _('Unable to find pid')
# The pid doesn't exist
yumlock.unlock(lockfile)
else:
# Whoa. What the heck happened?
print _('Unable to check if PID %s is active') % oldpid
sys.exit(200)
else:
# Another copy seems to be running.
msg = _('Existing lock %s: another copy is running. Aborting.')
print msg % lockfile
sys.exit(200)
# lock again.
yumlock.lock(lockfile, mypid, 0644)

def main(args):
"""This does all the real work"""

Expand All @@ -134,6 +167,11 @@ def main(args):
usage()
process = cmds[0]

# ok at this point lets check the lock/set the lock if we can
if conf.uid == 0:
mypid = str(os.getpid())
lock('/var/run/yum.pid', mypid)

# some misc speedups/sanity checks
if conf.uid != 0:
conf.cache=1
Expand Down

0 comments on commit 23cac0b

Please sign in to comment.