about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-03-19 13:05:54 -0700
committerEric Wong <normalperson@yhbt.net>2009-03-19 13:19:33 -0700
commitd1ff8c5bdb286ae212962ec9034d6a345cf09b30 (patch)
tree182340be9f69e80156ec0b4d6ed33e79dcd78f6e /lib
parent2a2acc089fc2424f5d01ac170f29a75e090c576f (diff)
downloadunicorn-d1ff8c5bdb286ae212962ec9034d6a345cf09b30.tar.gz
The daemonization logic between unicorn and unicorn_rails
scripts can definitely be shared.

Again: our daemonization logic is slightly non-standard since
our executables are designed to run in APP_ROOT/RAILS_ROOT and
not "/" like "normal" UNIX daemons.
Diffstat (limited to 'lib')
-rw-r--r--lib/unicorn/launcher.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/unicorn/launcher.rb b/lib/unicorn/launcher.rb
new file mode 100644
index 0000000..8c96059
--- /dev/null
+++ b/lib/unicorn/launcher.rb
@@ -0,0 +1,33 @@
+$stdin.sync = $stdout.sync = $stderr.sync = true
+require 'unicorn'
+
+class Unicorn::Launcher
+
+  # We don't do a lot of standard daemonization stuff:
+  #   * umask is whatever was set by the parent process at startup
+  #     and can be set in config.ru and config_file, so making it
+  #     0000 and potentially exposing sensitive log data can be bad
+  #     policy.
+  #   * don't bother to chdir("/") here since unicorn is designed to
+  #     run inside APP_ROOT.  Unicorn will also re-chdir() to
+  #     the directory it was started in when being re-executed
+  #     to pickup code changes if the original deployment directory
+  #     is a symlink or otherwise got replaced.
+  def self.daemonize!
+    $stdin.reopen("/dev/null")
+
+    # We only start a new process group if we're not being reexecuted
+    # and inheriting file descriptors from our parent
+    unless ENV['UNICORN_FD']
+      exit if fork
+      Process.setsid
+      exit if fork
+
+      # $stderr/$stderr can/will be redirected separately in the Unicorn config
+      $stdout.reopen("/dev/null", "a")
+      $stderr.reopen("/dev/null", "a")
+    end
+    $stdin.sync = $stdout.sync = $stderr.sync = true
+  end
+
+end