Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zargony/444901 to your computer and use it in GitHub Desktop.
Save zargony/444901 to your computer and use it in GitHub Desktop.
From 1d3f5e1c64f96ceede990ad6bdc07ea6f8f925ce Mon Sep 17 00:00:00 2001
From: Andreas Neuhaus <zargony@zargony.com>
Date: Sat, 19 Jun 2010 15:21:28 +0200
Subject: [PATCH] Workaround for unicorn not being able to fchown unlinked files in Ruby 1.9.2-preview3 (which makes it unable to switch workers to a different user; Ruby bug #3451)
---
lib/unicorn.rb | 6 ++++--
lib/unicorn/util.rb | 3 ++-
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/lib/unicorn.rb b/lib/unicorn.rb
index a7b0646..c6dffd6 100644
--- a/lib/unicorn.rb
+++ b/lib/unicorn.rb
@@ -186,7 +186,7 @@ module Unicorn
uid = Etc.getpwnam(user).uid
gid = Etc.getgrnam(group).gid if group
Unicorn::Util.chown_logs(uid, gid)
- tmp.chown(uid, gid)
+ #tmp.chown(uid, gid)
if gid && Process.egid != gid
Process.initgroups(user, gid)
Process::GID.change_privilege(gid)
@@ -588,9 +588,11 @@ module Unicorn
end
def spawn_missing_workers
+ uid = user && user[0] ? Etc.getpwnam(user[0]).uid : nil
+ gid = user && user[1] ? Etc.getgrnam(user[1]).gid : nil
(0...worker_processes).each do |worker_nr|
WORKERS.values.include?(worker_nr) and next
- worker = Worker.new(worker_nr, Unicorn::Util.tmpio)
+ worker = Worker.new(worker_nr, Unicorn::Util.tmpio(uid, gid))
before_fork.call(self, worker)
WORKERS[fork {
ready_pipe.close if ready_pipe
diff --git a/lib/unicorn/util.rb b/lib/unicorn/util.rb
index e8c09d0..5560a18 100644
--- a/lib/unicorn/util.rb
+++ b/lib/unicorn/util.rb
@@ -68,13 +68,14 @@ module Unicorn
# creates and returns a new File object. The File is unlinked
# immediately, switched to binary mode, and userspace output
# buffering is disabled
- def tmpio
+ def tmpio (user = nil, group = nil)
fp = begin
TmpIO.open("#{Dir::tmpdir}/#{rand}",
File::RDWR|File::CREAT|File::EXCL, 0600)
rescue Errno::EEXIST
retry
end
+ fp.chown user, group if user
File.unlink(fp.path)
fp.binmode
fp.sync = true
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment