From 5b6a97ff54d029d433b79eee1549e6f99464c48b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 27 Aug 2010 21:45:33 +0000 Subject: split out worker to a separate file This hopefully makes things easier to read, follow, and find since it's mostly documentation... --- lib/unicorn.rb | 44 ++++---------------------------------------- lib/unicorn/worker.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 40 deletions(-) create mode 100644 lib/unicorn/worker.rb diff --git a/lib/unicorn.rb b/lib/unicorn.rb index 2a5f493..c972f65 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -11,6 +11,7 @@ require 'unicorn/configurator' require 'unicorn/util' require 'unicorn/tee_input' require 'unicorn/http_response' +require 'unicorn/worker' # Unicorn module containing all of the classes (include C extensions) for running # a Unicorn web server. It contains a minimalist HTTP server with just enough @@ -92,6 +93,9 @@ module Unicorn include ::Unicorn::SocketHelper include ::Unicorn::HttpResponse + # backwards compatibility with 1.x + Worker = Unicorn::Worker + # prevents IO objects in here from being GC-ed IO_PURGATORY = [] @@ -159,46 +163,6 @@ module Unicorn 0 => $0.dup, } - # This class and its members can be considered a stable interface - # and will not change in a backwards-incompatible fashion between - # releases of Unicorn. You may need to access it in the - # before_fork/after_fork hooks. See the Unicorn::Configurator RDoc - # for examples. - class Worker < Struct.new(:nr, :tmp, :switched) - - # worker objects may be compared to just plain numbers - def ==(other_nr) - self.nr == other_nr - end - - # Changes the worker process to the specified +user+ and +group+ - # This is only intended to be called from within the worker - # process from the +after_fork+ hook. This should be called in - # the +after_fork+ hook after any priviledged functions need to be - # run (e.g. to set per-worker CPU affinity, niceness, etc) - # - # Any and all errors raised within this method will be propagated - # directly back to the caller (usually the +after_fork+ hook. - # These errors commonly include ArgumentError for specifying an - # invalid user/group and Errno::EPERM for insufficient priviledges - def user(user, group = nil) - # we do not protect the caller, checking Process.euid == 0 is - # insufficient because modern systems have fine-grained - # capabilities. Let the caller handle any and all errors. - uid = Etc.getpwnam(user).uid - gid = Etc.getgrnam(group).gid if group - Unicorn::Util.chown_logs(uid, gid) - tmp.chown(uid, gid) - if gid && Process.egid != gid - Process.initgroups(user, gid) - Process::GID.change_privilege(gid) - end - Process.euid != uid and Process::UID.change_privilege(uid) - self.switched = true - end - - end - # Creates a working server on host:port (strange things happen if # port isn't a Number). Use HttpServer::run to start the server and # HttpServer.run.join to join the thread that's processing diff --git a/lib/unicorn/worker.rb b/lib/unicorn/worker.rb new file mode 100644 index 0000000..fd8d20e --- /dev/null +++ b/lib/unicorn/worker.rb @@ -0,0 +1,40 @@ +# -*- encoding: binary -*- + +# This class and its members can be considered a stable interface +# and will not change in a backwards-incompatible fashion between +# releases of Unicorn. You may need to access it in the +# before_fork/after_fork hooks. See the Unicorn::Configurator RDoc +# for examples. +class Unicorn::Worker < Struct.new(:nr, :tmp, :switched) + + # worker objects may be compared to just plain numbers + def ==(other_nr) + self.nr == other_nr + end + + # Changes the worker process to the specified +user+ and +group+ + # This is only intended to be called from within the worker + # process from the +after_fork+ hook. This should be called in + # the +after_fork+ hook after any priviledged functions need to be + # run (e.g. to set per-worker CPU affinity, niceness, etc) + # + # Any and all errors raised within this method will be propagated + # directly back to the caller (usually the +after_fork+ hook. + # These errors commonly include ArgumentError for specifying an + # invalid user/group and Errno::EPERM for insufficient priviledges + def user(user, group = nil) + # we do not protect the caller, checking Process.euid == 0 is + # insufficient because modern systems have fine-grained + # capabilities. Let the caller handle any and all errors. + uid = Etc.getpwnam(user).uid + gid = Etc.getgrnam(group).gid if group + Unicorn::Util.chown_logs(uid, gid) + tmp.chown(uid, gid) + if gid && Process.egid != gid + Process.initgroups(user, gid) + Process::GID.change_privilege(gid) + end + Process.euid != uid and Process::UID.change_privilege(uid) + self.switched = true + end +end -- cgit v1.2.3-24-ge0c7