1 # -*- encoding: binary -*-
4 $stdout.sync = $stderr.sync = true
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
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
37 wr.close # grandparent does not write
39 rd.close # unicorn master does not read
41 exit if fork # parent dies now
45 # this will block until HttpServer#join runs (or it dies)
46 master_pid = (rd.readpartial(16) rescue nil).to_i
48 warn "master failed to start, check stderr log for details"
52 else # unicorn master process
53 options[:ready_pipe] = wr
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