Skip to content

Commit

Permalink
Merge pull request Homebrew#1486 from MikeMcQuaid/lock_dir_permissions
Browse files Browse the repository at this point in the history
Check the lock directory is writable.
  • Loading branch information
MikeMcQuaid committed Nov 12, 2016
2 parents fe9ce9f + 9edf8eb commit f184eba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Library/Homebrew/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,20 @@ def check_access_site_packages
EOS
end

def check_access_lock_dir
return unless HOMEBREW_LOCK_DIR.exist?
return if HOMEBREW_LOCK_DIR.writable_real?

<<-EOS.undent
#{HOMEBREW_LOCK_DIR} isn't writable.
Homebrew writes lock files to this location.
You should change the ownership and permissions of #{HOMEBREW_LOCK_DIR}
back to your user account.
sudo chown -R $(whoami) #{HOMEBREW_LOCK_DIR}
EOS
end

def check_access_logs
return unless HOMEBREW_LOGS.exist?
return if HOMEBREW_LOGS.writable_real?
Expand Down
15 changes: 12 additions & 3 deletions Library/Homebrew/utils/lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ lock() {
local lock_dir="$HOMEBREW_PREFIX/var/homebrew/locks"
local lock_file="$lock_dir/$name"
[[ -d "$lock_dir" ]] || mkdir -p "$lock_dir"
if ! [[ -w "$lock_dir" ]]
then
odie <<EOS
Can't create $name lock in $lock_dir!
Fix permissions by running:
sudo chown -R \$(whoami) $HOMEBREW_PREFIX/var/homebrew
EOS
fi
# 200 is the file descriptor used in the lock.
# This FD should be used exclusively for lock purpose.
# Any value except 0(stdin), 1(stdout) and 2(stderr) can do the job.
Expand All @@ -17,17 +25,18 @@ lock() {
exec 200>&-
# open the lock file to FD, so the shell process can hold the lock.
exec 200>"$lock_file"
if ! _create_lock 200
if ! _create_lock 200 "$name"
then
odie <<EOS
Another active Homebrew process is already in progress.
Another active Homebrew $name process is already in progress.
Please wait for it to finish or terminate it to continue.
EOS
fi
}

_create_lock() {
local lock_fd="$1"
local name="$2"
local ruby="/usr/bin/ruby"
[[ -x "$ruby" ]] || ruby="$(which ruby 2>/dev/null)"

Expand All @@ -38,6 +47,6 @@ _create_lock() {
then
flock -n "$lock_fd"
else
onoe "Cannot create lock, please avoid running brew in parallel."
onoe "Cannot create $name lock, please avoid running Homebrew in parallel."
fi
}

0 comments on commit f184eba

Please sign in to comment.