doc: expand on the SELF_PIPE description
[unicorn.git] / lib / unicorn.rb
blobd63567f243ea94457a7db59ce98c74b38272adc4
1 # -*- encoding: binary -*-
3 require 'fcntl'
4 require 'unicorn/socket_helper'
5 autoload :Rack, 'rack'
7 # Unicorn module containing all of the classes (include C extensions) for running
8 # a Unicorn web server.  It contains a minimalist HTTP server with just enough
9 # functionality to service web application requests fast as possible.
10 module Unicorn
11   autoload :Const, 'unicorn/const'
12   autoload :HttpRequest, 'unicorn/http_request'
13   autoload :HttpResponse, 'unicorn/http_response'
14   autoload :Configurator, 'unicorn/configurator'
15   autoload :TeeInput, 'unicorn/tee_input'
16   autoload :Util, 'unicorn/util'
18   class << self
19     def run(app, options = {})
20       HttpServer.new(app, options).start.join
21     end
22   end
24   # This is the process manager of Unicorn. This manages worker
25   # processes which in turn handle the I/O and application process.
26   # Listener sockets are started in the master process and shared with
27   # forked worker children.
29   class HttpServer < Struct.new(:listener_opts, :timeout, :worker_processes,
30                                 :before_fork, :after_fork, :before_exec,
31                                 :logger, :pid, :app, :preload_app,
32                                 :reexec_pid, :orig_app, :init_listeners,
33                                 :master_pid, :config)
34     include ::Unicorn::SocketHelper
36     # prevents IO objects in here from being GC-ed
37     IO_PURGATORY = []
39     # all bound listener sockets
40     LISTENERS = []
42     # This hash maps PIDs to Workers
43     WORKERS = {}
45     # We use SELF_PIPE differently in the master and worker processes:
46     #
47     # * The master process never closes or reinitializes this once
48     # initialized.  Signal handlers in the master process will write to
49     # it to wake up the master from IO.select in exactly the same manner
50     # djb describes in http://cr.yp.to/docs/selfpipe.html
51     #
52     # * The workers immediately close the pipe they inherit from the
53     # master and replace it with a new pipe after forking.  This new
54     # pipe is also used to wakeup from IO.select from inside (worker)
55     # signal handlers.  However, workers *close* the pipe descriptors in
56     # the signal handlers to raise EBADF in IO.select instead of writing
57     # like we do in the master.  We cannot easily use the reader set for
58     # IO.select because LISTENERS is already that set, and it's extra
59     # work (and cycles) to distinguish the pipe FD from the reader set
60     # once IO.select returns.  So we're lazy and just close the pipe when
61     # a (rare) signal arrives in the worker and reinitialize the pipe later.
62     SELF_PIPE = []
64     # signal queue used for self-piping
65     SIG_QUEUE = []
67     # constant lookups are faster and we're single-threaded/non-reentrant
68     REQUEST = HttpRequest.new
70     # We populate this at startup so we can figure out how to reexecute
71     # and upgrade the currently running instance of Unicorn
72     # This Hash is considered a stable interface and changing its contents
73     # will allow you to switch between different installations of Unicorn
74     # or even different installations of the same applications without
75     # downtime.  Keys of this constant Hash are described as follows:
76     #
77     # * 0 - the path to the unicorn/unicorn_rails executable
78     # * :argv - a deep copy of the ARGV array the executable originally saw
79     # * :cwd - the working directory of the application, this is where
80     # you originally started Unicorn.
81     #
82     # The following example may be used in your Unicorn config file to
83     # change your working directory during a config reload (HUP) without
84     # upgrading or restarting:
85     #
86     #   Dir.chdir(Unicorn::HttpServer::START_CTX[:cwd] = path)
87     #
88     # To change your unicorn executable to a different path without downtime,
89     # you can set the following in your Unicorn config file, HUP and then
90     # continue with the traditional USR2 + QUIT upgrade steps:
91     #
92     #   Unicorn::HttpServer::START_CTX[0] = "/home/bofh/1.9.2/bin/unicorn"
93     START_CTX = {
94       :argv => ARGV.map { |arg| arg.dup },
95       # don't rely on Dir.pwd here since it's not symlink-aware, and
96       # symlink dirs are the default with Capistrano...
97       :cwd => `/bin/sh -c pwd`.chomp("\n"),
98       0 => $0.dup,
99     }
101     # This class and its members can be considered a stable interface
102     # and will not change in a backwards-incompatible fashion between
103     # releases of Unicorn.  You may need to access it in the
104     # before_fork/after_fork hooks.  See the Unicorn::Configurator RDoc
105     # for examples.
106     class Worker < Struct.new(:nr, :tmp)
108       # worker objects may be compared to just plain numbers
109       def ==(other_nr)
110         self.nr == other_nr
111       end
112     end
114     # Creates a working server on host:port (strange things happen if
115     # port isn't a Number).  Use HttpServer::run to start the server and
116     # HttpServer.run.join to join the thread that's processing
117     # incoming requests on the socket.
118     def initialize(app, options = {})
119       self.app = app
120       self.reexec_pid = 0
121       self.init_listeners = options[:listeners] ? options[:listeners].dup : []
122       self.config = Configurator.new(options.merge(:use_defaults => true))
123       self.listener_opts = {}
125       # we try inheriting listeners first, so we bind them later.
126       # we don't write the pid file until we've bound listeners in case
127       # unicorn was started twice by mistake.  Even though our #pid= method
128       # checks for stale/existing pid files, race conditions are still
129       # possible (and difficult/non-portable to avoid) and can be likely
130       # to clobber the pid if the second start was in quick succession
131       # after the first, so we rely on the listener binding to fail in
132       # that case.  Some tests (in and outside of this source tree) and
133       # monitoring tools may also rely on pid files existing before we
134       # attempt to connect to the listener(s)
135       config.commit!(self, :skip => [:listeners, :pid])
136       self.orig_app = app
137     end
139     # Runs the thing.  Returns self so you can run join on it
140     def start
141       BasicSocket.do_not_reverse_lookup = true
143       # inherit sockets from parents, they need to be plain Socket objects
144       # before they become UNIXServer or TCPServer
145       inherited = ENV['UNICORN_FD'].to_s.split(/,/).map do |fd|
146         io = Socket.for_fd(fd.to_i)
147         set_server_sockopt(io, listener_opts[sock_name(io)])
148         IO_PURGATORY << io
149         logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
150         server_cast(io)
151       end
153       config_listeners = config[:listeners].dup
154       LISTENERS.replace(inherited)
156       # we start out with generic Socket objects that get cast to either
157       # TCPServer or UNIXServer objects; but since the Socket objects
158       # share the same OS-level file descriptor as the higher-level *Server
159       # objects; we need to prevent Socket objects from being garbage-collected
160       config_listeners -= listener_names
161       if config_listeners.empty? && LISTENERS.empty?
162         config_listeners << Unicorn::Const::DEFAULT_LISTEN
163         init_listeners << Unicorn::Const::DEFAULT_LISTEN
164         START_CTX[:argv] << "-l#{Unicorn::Const::DEFAULT_LISTEN}"
165       end
166       config_listeners.each { |addr| listen(addr) }
167       raise ArgumentError, "no listeners" if LISTENERS.empty?
168       self.pid = config[:pid]
169       self.master_pid = $$
170       build_app! if preload_app
171       maintain_worker_count
172       self
173     end
175     # replaces current listener set with +listeners+.  This will
176     # close the socket if it will not exist in the new listener set
177     def listeners=(listeners)
178       cur_names, dead_names = [], []
179       listener_names.each do |name|
180         if ?/ == name[0]
181           # mark unlinked sockets as dead so we can rebind them
182           (File.socket?(name) ? cur_names : dead_names) << name
183         else
184           cur_names << name
185         end
186       end
187       set_names = listener_names(listeners)
188       dead_names.concat(cur_names - set_names).uniq!
190       LISTENERS.delete_if do |io|
191         if dead_names.include?(sock_name(io))
192           IO_PURGATORY.delete_if do |pio|
193             pio.fileno == io.fileno && (pio.close rescue nil).nil? # true
194           end
195           (io.close rescue nil).nil? # true
196         else
197           set_server_sockopt(io, listener_opts[sock_name(io)])
198           false
199         end
200       end
202       (set_names - cur_names).each { |addr| listen(addr) }
203     end
205     def stdout_path=(path); redirect_io($stdout, path); end
206     def stderr_path=(path); redirect_io($stderr, path); end
208     alias_method :set_pid, :pid=
209     undef_method :pid=
211     # sets the path for the PID file of the master process
212     def pid=(path)
213       if path
214         if x = valid_pid?(path)
215           return path if pid && path == pid && x == $$
216           raise ArgumentError, "Already running on PID:#{x} " \
217                                "(or pid=#{path} is stale)"
218         end
219       end
220       unlink_pid_safe(pid) if pid
222       if path
223         fp = begin
224           tmp = "#{File.dirname(path)}/#{rand}.#$$"
225           File.open(tmp, File::RDWR|File::CREAT|File::EXCL, 0644)
226         rescue Errno::EEXIST
227           retry
228         end
229         fp.syswrite("#$$\n")
230         File.rename(fp.path, path)
231         fp.close
232       end
233       self.set_pid(path)
234     end
236     # add a given address to the +listeners+ set, idempotently
237     # Allows workers to add a private, per-process listener via the
238     # after_fork hook.  Very useful for debugging and testing.
239     # +:tries+ may be specified as an option for the number of times
240     # to retry, and +:delay+ may be specified as the time in seconds
241     # to delay between retries.
242     # A negative value for +:tries+ indicates the listen will be
243     # retried indefinitely, this is useful when workers belonging to
244     # different masters are spawned during a transparent upgrade.
245     def listen(address, opt = {}.merge(listener_opts[address] || {}))
246       address = config.expand_addr(address)
247       return if String === address && listener_names.include?(address)
249       delay = opt[:delay] || 0.5
250       tries = opt[:tries] || 5
251       begin
252         io = bind_listen(address, opt)
253         unless TCPServer === io || UNIXServer === io
254           IO_PURGATORY << io
255           io = server_cast(io)
256         end
257         logger.info "listening on addr=#{sock_name(io)} fd=#{io.fileno}"
258         LISTENERS << io
259         return io
260       rescue Errno::EADDRINUSE => err
261         logger.error "adding listener failed addr=#{address} (in use)"
262         raise err if tries == 0
263         tries -= 1
264         logger.error "retrying in #{delay} seconds " \
265                      "(#{tries < 0 ? 'infinite' : tries} tries left)"
266         sleep(delay)
267         retry
268       end
269     end
271     # monitors children and receives signals forever
272     # (or until a termination signal is sent).  This handles signals
273     # one-at-a-time time and we'll happily drop signals in case somebody
274     # is signalling us too often.
275     def join
276       # this pipe is used to wake us up from select(2) in #join when signals
277       # are trapped.  See trap_deferred
278       init_self_pipe!
279       respawn = true
281       QUEUE_SIGS.each { |sig| trap_deferred(sig) }
282       trap(:CHLD) { |sig_nr| awaken_master }
283       proc_name 'master'
284       logger.info "master process ready" # test_exec.rb relies on this message
285       begin
286         loop do
287           reap_all_workers
288           case SIG_QUEUE.shift
289           when nil
290             murder_lazy_workers
291             maintain_worker_count if respawn
292             master_sleep
293           when :QUIT # graceful shutdown
294             break
295           when :TERM, :INT # immediate shutdown
296             stop(false)
297             break
298           when :USR1 # rotate logs
299             logger.info "master reopening logs..."
300             Unicorn::Util.reopen_logs
301             logger.info "master done reopening logs"
302             kill_each_worker(:USR1)
303           when :USR2 # exec binary, stay alive in case something went wrong
304             reexec
305           when :WINCH
306             if Process.ppid == 1 || Process.getpgrp != $$
307               respawn = false
308               logger.info "gracefully stopping all workers"
309               kill_each_worker(:QUIT)
310             else
311               logger.info "SIGWINCH ignored because we're not daemonized"
312             end
313           when :TTIN
314             self.worker_processes += 1
315           when :TTOU
316             self.worker_processes -= 1 if self.worker_processes > 0
317           when :HUP
318             respawn = true
319             if config.config_file
320               load_config!
321               redo # immediate reaping since we may have QUIT workers
322             else # exec binary and exit if there's no config file
323               logger.info "config_file not present, reexecuting binary"
324               reexec
325               break
326             end
327           end
328         end
329       rescue Errno::EINTR
330         retry
331       rescue Object => e
332         logger.error "Unhandled master loop exception #{e.inspect}."
333         logger.error e.backtrace.join("\n")
334         retry
335       end
336       stop # gracefully shutdown all workers on our way out
337       logger.info "master complete"
338       unlink_pid_safe(pid) if pid
339     end
341     # Terminates all workers, but does not exit master process
342     def stop(graceful = true)
343       self.listeners = []
344       kill_each_worker(graceful ? :QUIT : :TERM)
345       timeleft = timeout
346       step = 0.2
347       reap_all_workers
348       until WORKERS.empty?
349         sleep(step)
350         reap_all_workers
351         (timeleft -= step) > 0 and next
352         kill_each_worker(:KILL)
353       end
354     end
356     private
358     # list of signals we care about and trap in master.
359     QUEUE_SIGS = [ :WINCH, :QUIT, :INT, :TERM, :USR1, :USR2, :HUP,
360                    :TTIN, :TTOU ]
362     # defer a signal for later processing in #join (master process)
363     def trap_deferred(signal)
364       trap(signal) do |sig_nr|
365         if SIG_QUEUE.size < 5
366           SIG_QUEUE << signal
367           awaken_master
368         else
369           logger.error "ignoring SIG#{signal}, queue=#{SIG_QUEUE.inspect}"
370         end
371       end
372     end
374     # wait for a signal hander to wake us up and then consume the pipe
375     # Wake up every second anyways to run murder_lazy_workers
376     def master_sleep
377       begin
378         ready = IO.select([SELF_PIPE.first], nil, nil, 1) or return
379         ready.first && ready.first.first or return
380         loop { SELF_PIPE.first.read_nonblock(Const::CHUNK_SIZE) }
381       rescue Errno::EAGAIN, Errno::EINTR
382       end
383     end
385     def awaken_master
386       begin
387         SELF_PIPE.last.write_nonblock('.') # wakeup master process from select
388       rescue Errno::EAGAIN, Errno::EINTR
389         # pipe is full, master should wake up anyways
390         retry
391       end
392     end
394     # reaps all unreaped workers
395     def reap_all_workers
396       begin
397         loop do
398           wpid, status = Process.waitpid2(-1, Process::WNOHANG)
399           wpid or break
400           if reexec_pid == wpid
401             logger.error "reaped #{status.inspect} exec()-ed"
402             self.reexec_pid = 0
403             self.pid = pid.chomp('.oldbin') if pid
404             proc_name 'master'
405           else
406             worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
407             logger.info "reaped #{status.inspect} " \
408                         "worker=#{worker.nr rescue 'unknown'}"
409           end
410         end
411       rescue Errno::ECHILD
412       end
413     end
415     # reexecutes the START_CTX with a new binary
416     def reexec
417       if reexec_pid > 0
418         begin
419           Process.kill(0, reexec_pid)
420           logger.error "reexec-ed child already running PID:#{reexec_pid}"
421           return
422         rescue Errno::ESRCH
423           self.reexec_pid = 0
424         end
425       end
427       if pid
428         old_pid = "#{pid}.oldbin"
429         prev_pid = pid.dup
430         begin
431           self.pid = old_pid  # clear the path for a new pid file
432         rescue ArgumentError
433           logger.error "old PID:#{valid_pid?(old_pid)} running with " \
434                        "existing pid=#{old_pid}, refusing rexec"
435           return
436         rescue Object => e
437           logger.error "error writing pid=#{old_pid} #{e.class} #{e.message}"
438           return
439         end
440       end
442       self.reexec_pid = fork do
443         listener_fds = LISTENERS.map { |sock| sock.fileno }
444         ENV['UNICORN_FD'] = listener_fds.join(',')
445         Dir.chdir(START_CTX[:cwd])
446         cmd = [ START_CTX[0] ].concat(START_CTX[:argv])
448         # avoid leaking FDs we don't know about, but let before_exec
449         # unset FD_CLOEXEC, if anything else in the app eventually
450         # relies on FD inheritence.
451         (3..1024).each do |io|
452           next if listener_fds.include?(io)
453           io = IO.for_fd(io) rescue nil
454           io or next
455           IO_PURGATORY << io
456           io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
457         end
458         logger.info "executing #{cmd.inspect} (in #{Dir.pwd})"
459         before_exec.call(self)
460         exec(*cmd)
461       end
462       proc_name 'master (old)'
463     end
465     # forcibly terminate all workers that haven't checked in in timeout
466     # seconds.  The timeout is implemented using an unlinked File
467     # shared between the parent process and each worker.  The worker
468     # runs File#chmod to modify the ctime of the File.  If the ctime
469     # is stale for >timeout seconds, then we'll kill the corresponding
470     # worker.
471     def murder_lazy_workers
472       diff = stat = nil
473       WORKERS.dup.each_pair do |wpid, worker|
474         stat = begin
475           worker.tmp.stat
476         rescue => e
477           logger.warn "worker=#{worker.nr} PID:#{wpid} stat error: #{e.inspect}"
478           kill_worker(:QUIT, wpid)
479           next
480         end
481         stat.mode == 0100000 and next
482         (diff = (Time.now - stat.ctime)) <= timeout and next
483         logger.error "worker=#{worker.nr} PID:#{wpid} timeout " \
484                      "(#{diff}s > #{timeout}s), killing"
485         kill_worker(:KILL, wpid) # take no prisoners for timeout violations
486       end
487     end
489     def spawn_missing_workers
490       (0...worker_processes).each do |worker_nr|
491         WORKERS.values.include?(worker_nr) and next
492         begin
493           Dir.chdir(START_CTX[:cwd])
494         rescue Errno::ENOENT => err
495           logger.fatal "#{err.inspect} (#{START_CTX[:cwd]})"
496           SIG_QUEUE << :QUIT # forcibly emulate SIGQUIT
497           return
498         end
499         worker = Worker.new(worker_nr, Unicorn::Util.tmpio)
500         before_fork.call(self, worker)
501         WORKERS[fork { worker_loop(worker) }] = worker
502       end
503     end
505     def maintain_worker_count
506       (off = WORKERS.size - worker_processes) == 0 and return
507       off < 0 and return spawn_missing_workers
508       WORKERS.dup.each_pair { |wpid,w|
509         w.nr >= worker_processes and kill_worker(:QUIT, wpid) rescue nil
510       }
511     end
513     # once a client is accepted, it is processed in its entirety here
514     # in 3 easy steps: read request, call app, write app response
515     def process_client(client)
516       client.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
517       response = app.call(env = REQUEST.read(client))
519       if 100 == response.first.to_i
520         client.write(Const::EXPECT_100_RESPONSE)
521         env.delete(Const::HTTP_EXPECT)
522         response = app.call(env)
523       end
524       HttpResponse.write(client, response, HttpRequest::PARSER.headers?)
525     # if we get any error, try to write something back to the client
526     # assuming we haven't closed the socket, but don't get hung up
527     # if the socket is already closed or broken.  We'll always ensure
528     # the socket is closed at the end of this function
529     rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF
530       client.write_nonblock(Const::ERROR_500_RESPONSE) rescue nil
531       client.close rescue nil
532     rescue HttpParserError # try to tell the client they're bad
533       client.write_nonblock(Const::ERROR_400_RESPONSE) rescue nil
534       client.close rescue nil
535     rescue Object => e
536       client.write_nonblock(Const::ERROR_500_RESPONSE) rescue nil
537       client.close rescue nil
538       logger.error "Read error: #{e.inspect}"
539       logger.error e.backtrace.join("\n")
540     end
542     # gets rid of stuff the worker has no business keeping track of
543     # to free some resources and drops all sig handlers.
544     # traps for USR1, USR2, and HUP may be set in the after_fork Proc
545     # by the user.
546     def init_worker_process(worker)
547       QUEUE_SIGS.each { |sig| trap(sig, nil) }
548       trap(:CHLD, 'DEFAULT')
549       SIG_QUEUE.clear
550       proc_name "worker[#{worker.nr}]"
551       START_CTX.clear
552       init_self_pipe!
553       WORKERS.values.each { |other| other.tmp.close rescue nil }
554       WORKERS.clear
555       LISTENERS.each { |sock| sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
556       worker.tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
557       after_fork.call(self, worker) # can drop perms
558       self.timeout /= 2.0 # halve it for select()
559       build_app! unless preload_app
560     end
562     def reopen_worker_logs(worker_nr)
563       logger.info "worker=#{worker_nr} reopening logs..."
564       Unicorn::Util.reopen_logs
565       logger.info "worker=#{worker_nr} done reopening logs"
566       init_self_pipe!
567     end
569     # runs inside each forked worker, this sits around and waits
570     # for connections and doesn't die until the parent dies (or is
571     # given a INT, QUIT, or TERM signal)
572     def worker_loop(worker)
573       ppid = master_pid
574       init_worker_process(worker)
575       nr = 0 # this becomes negative if we need to reopen logs
576       alive = worker.tmp # tmp is our lifeline to the master process
577       ready = LISTENERS
578       t = ti = 0
580       # closing anything we IO.select on will raise EBADF
581       trap(:USR1) { nr = -65536; SELF_PIPE.first.close rescue nil }
582       trap(:QUIT) { alive = nil; LISTENERS.each { |s| s.close rescue nil } }
583       [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
584       logger.info "worker=#{worker.nr} ready"
586       begin
587         nr < 0 and reopen_worker_logs(worker.nr)
588         nr = 0
590         # we're a goner in timeout seconds anyways if alive.chmod
591         # breaks, so don't trap the exception.  Using fchmod() since
592         # futimes() is not available in base Ruby and I very strongly
593         # prefer temporary files to be unlinked for security,
594         # performance and reliability reasons, so utime is out.  No-op
595         # changes with chmod doesn't update ctime on all filesystems; so
596         # we change our counter each and every time (after process_client
597         # and before IO.select).
598         t == (ti = Time.now.to_i) or alive.chmod(t = ti)
600         ready.each do |sock|
601           begin
602             process_client(sock.accept_nonblock)
603             nr += 1
604             t == (ti = Time.now.to_i) or alive.chmod(t = ti)
605           rescue Errno::EAGAIN, Errno::ECONNABORTED
606           end
607           break if nr < 0
608         end
610         # make the following bet: if we accepted clients this round,
611         # we're probably reasonably busy, so avoid calling select()
612         # and do a speculative accept_nonblock on ready listeners
613         # before we sleep again in select().
614         redo unless nr == 0 # (nr < 0) => reopen logs
616         ppid == Process.ppid or return
617         alive.chmod(t = 0)
618         begin
619           # timeout used so we can detect parent death:
620           ret = IO.select(LISTENERS, nil, SELF_PIPE, timeout) or redo
621           ready = ret.first
622         rescue Errno::EINTR
623           ready = LISTENERS
624         rescue Errno::EBADF
625           nr < 0 or return
626         end
627       rescue Object => e
628         if alive
629           logger.error "Unhandled listen loop exception #{e.inspect}."
630           logger.error e.backtrace.join("\n")
631         end
632       end while alive
633     end
635     # delivers a signal to a worker and fails gracefully if the worker
636     # is no longer running.
637     def kill_worker(signal, wpid)
638       begin
639         Process.kill(signal, wpid)
640       rescue Errno::ESRCH
641         worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
642       end
643     end
645     # delivers a signal to each worker
646     def kill_each_worker(signal)
647       WORKERS.keys.each { |wpid| kill_worker(signal, wpid) }
648     end
650     # unlinks a PID file at given +path+ if it contains the current PID
651     # still potentially racy without locking the directory (which is
652     # non-portable and may interact badly with other programs), but the
653     # window for hitting the race condition is small
654     def unlink_pid_safe(path)
655       (File.read(path).to_i == $$ and File.unlink(path)) rescue nil
656     end
658     # returns a PID if a given path contains a non-stale PID file,
659     # nil otherwise.
660     def valid_pid?(path)
661       wpid = File.read(path).to_i
662       wpid <= 0 and return nil
663       begin
664         Process.kill(0, wpid)
665         return wpid
666       rescue Errno::ESRCH
667         # don't unlink stale pid files, racy without non-portable locking...
668       end
669       rescue Errno::ENOENT
670     end
672     def load_config!
673       begin
674         logger.info "reloading config_file=#{config.config_file}"
675         config[:listeners].replace(init_listeners)
676         config.reload
677         config.commit!(self)
678         kill_each_worker(:QUIT)
679         Unicorn::Util.reopen_logs
680         self.app = orig_app
681         build_app! if preload_app
682         logger.info "done reloading config_file=#{config.config_file}"
683       rescue Object => e
684         logger.error "error reloading config_file=#{config.config_file}: " \
685                      "#{e.class} #{e.message}"
686       end
687     end
689     # returns an array of string names for the given listener array
690     def listener_names(listeners = LISTENERS)
691       listeners.map { |io| sock_name(io) }
692     end
694     def build_app!
695       if app.respond_to?(:arity) && app.arity == 0
696         if defined?(Gem) && Gem.respond_to?(:refresh)
697           logger.info "Refreshing Gem list"
698           Gem.refresh
699         end
700         self.app = app.call
701       end
702     end
704     def proc_name(tag)
705       $0 = ([ File.basename(START_CTX[0]), tag
706             ]).concat(START_CTX[:argv]).join(' ')
707     end
709     def redirect_io(io, path)
710       File.open(path, 'ab') { |fp| io.reopen(fp) } if path
711       io.sync = true
712     end
714     def init_self_pipe!
715       SELF_PIPE.each { |io| io.close rescue nil }
716       SELF_PIPE.replace(IO.pipe)
717       SELF_PIPE.each { |io| io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
718     end
720   end