documentation cleanup/reduction
[unicorn.git] / lib / unicorn / launcher.rb
blob5eafe5b86301cb48670a5421c0fbde7b863ce302
1 # -*- encoding: binary -*-
3 # :enddoc:
4 $stdout.sync = $stderr.sync = true
5 $stdin.binmode
6 $stdout.binmode
7 $stderr.binmode
9 require 'unicorn'
11 module Unicorn::Launcher
13   # We don't do a lot of standard daemonization stuff:
14   #   * umask is whatever was set by the parent process at startup
15   #     and can be set in config.ru and config_file, so making it
16   #     0000 and potentially exposing sensitive log data can be bad
17   #     policy.
18   #   * don't bother to chdir("/") here since unicorn is designed to
19   #     run inside APP_ROOT.  Unicorn will also re-chdir() to
20   #     the directory it was started in when being re-executed
21   #     to pickup code changes if the original deployment directory
22   #     is a symlink or otherwise got replaced.
23   def self.daemonize!(options)
24     cfg = Unicorn::Configurator
25     $stdin.reopen("/dev/null")
27     # We only start a new process group if we're not being reexecuted
28     # and inheriting file descriptors from our parent
29     unless ENV['UNICORN_FD']
30       # grandparent - reads pipe, exits when master is ready
31       #  \_ parent  - exits immediately ASAP
32       #      \_ unicorn master - writes to pipe when ready
34       rd, wr = IO.pipe
35       grandparent = $$
36       if fork
37         wr.close # grandparent does not write
38       else
39         rd.close # unicorn master does not read
40         Process.setsid
41         exit if fork # parent dies now
42       end
44       if grandparent == $$
45         # this will block until HttpServer#join runs (or it dies)
46         master_pid = (rd.readpartial(16) rescue nil).to_i
47         unless master_pid > 1
48           warn "master failed to start, check stderr log for details"
49           exit!(1)
50         end
51         exit 0
52       else # unicorn master process
53         options[:ready_pipe] = wr
54       end
55     end
56     # $stderr/$stderr can/will be redirected separately in the Unicorn config
57     cfg::DEFAULTS[:stderr_path] ||= "/dev/null"
58     cfg::DEFAULTS[:stdout_path] ||= "/dev/null"
59     cfg::RACKUP[:daemonized] = true
60   end
62 end