1 # -*- encoding: binary -*-
4 # This class and its members can be considered a stable interface
5 # and will not change in a backwards-incompatible fashion between
6 # releases of unicorn. Knowledge of this class is generally not
7 # not needed for most users of unicorn.
9 # Some users may want to access it in the before_fork/after_fork hooks.
10 # See the Unicorn::Configurator RDoc for examples.
13 attr_accessor :nr, :switched
14 attr_reader :to_io # IO.select-compatible
17 PER_DROP = Raindrops::PAGE_SIZE / Raindrops::SIZE
20 def initialize(nr, pipe=nil)
21 drop_index = nr / PER_DROP
22 @raindrop = DROPS[drop_index] ||= Raindrops.new(PER_DROP)
23 @offset = nr % PER_DROP
24 @raindrop[@offset] = 0
27 @to_io, @master = pipe || Unicorn.pipe
30 def atfork_child # :nodoc:
31 # we _must_ close in child, parent just holds this open to signal
32 @master = @master.close
35 # master fakes SIGQUIT using this
37 @master = @master.close if @master
40 # parent does not read
41 def atfork_parent # :nodoc:
45 # call a signal handler immediately without triggering EINTR
46 # We do not use the more obvious Process.kill(sig, $$) here since
47 # that signal delivery may be deferred. We want to avoid signal delivery
48 # while the Rack app.call is running because some database drivers
49 # (e.g. ruby-pg) may cancel pending requests.
50 def fake_sig(sig) # :nodoc:
51 old_cb = trap(sig, "IGNORE")
57 # master sends fake signals to children
58 def soft_kill(sig) # :nodoc:
63 signum = Signal.list[sig.to_s] or
64 raise ArgumentError, "BUG: bad signal: #{sig.inspect}"
66 # writing and reading 4 bytes on a pipe is atomic on all POSIX platforms
67 # Do not care in the odd case the buffer is full, here.
68 @master.kgio_trywrite([signum].pack('l'))
70 # worker will be reaped soon
73 # this only runs when the Rack app.call is not running
75 def kgio_tryaccept # :nodoc:
76 case buf = @to_io.kgio_tryread(4)
78 # unpack the buffer and trigger the signal handler
79 signum = buf.unpack('l')
81 # keep looping, more signals may be queued
82 when nil # EOF: master died, but we are at a safe place to exit
84 when :wait_readable # keep waiting
86 end while true # loop, as multiple signals may be sent
89 # worker objects may be compared to just plain Integers
90 def ==(other_nr) # :nodoc:
94 # called in the worker process
95 def tick=(value) # :nodoc:
96 @raindrop[@offset] = value
99 # called in the master process
104 # called in both the master (reaping worker) and worker (SIGQUIT handler)
106 @master.close if @master
107 @to_io.close if @to_io
112 # In most cases, you should be using the Unicorn::Configurator#user
113 # directive instead. This method should only be used if you need
114 # fine-grained control of exactly when you want to change permissions
115 # in your after_fork or after_worker_ready hooks, or if you want to
116 # use the chroot support.
118 # Changes the worker process to the specified +user+ and +group+,
119 # and chroots to the current working directory if +chroot+ is set.
120 # This is only intended to be called from within the worker
121 # process from the +after_fork+ hook. This should be called in
122 # the +after_fork+ hook after any privileged functions need to be
123 # run (e.g. to set per-worker CPU affinity, niceness, etc)
125 # +group+ can be specified as a string, or as an array of two
126 # strings. If an array of two strings is given, the first string
127 # is used as the primary group of the process, and the second is
128 # used as the group of the log files.
130 # Any and all errors raised within this method will be propagated
131 # directly back to the caller (usually the +after_fork+ hook.
132 # These errors commonly include ArgumentError for specifying an
133 # invalid user/group and Errno::EPERM for insufficient privileges.
135 # chroot support is only available in unicorn 5.3.0+
136 # user and group switching appeared in unicorn 0.94.0 (2009-11-05)
137 def user(user, group = nil, chroot = false)
138 # we do not protect the caller, checking Process.euid == 0 is
139 # insufficient because modern systems have fine-grained
140 # capabilities. Let the caller handle any and all errors.
141 uid = Etc.getpwnam(user).uid
144 if group.is_a?(Array)
145 group, log_group = group
146 log_gid = Etc.getgrnam(log_group).gid
148 gid = Etc.getgrnam(group).gid
152 Unicorn::Util.chown_logs(uid, log_gid)
153 if gid && Process.egid != gid
154 Process.initgroups(user, gid)
155 Process::GID.change_privilege(gid)
158 chroot = Dir.pwd if chroot == true
162 Process.euid != uid and Process::UID.change_privilege(uid)