Sha256: fdf87bd68decf144791b232884144f36c29511433c6fe1e20125659a9b7ee9af

Contents?: true

Size: 1.44 KB

Versions: 19

Compression:

Stored size: 1.44 KB

Contents

# encoding: utf-8

# This is largely based on the Daemonize library by Travis Whitton and
# Judson Lester. http://grub.ath.cx/daemonize. I cleaned it up a bit to
# meet Adhearsion's quality standards.
module Adhearsion
  module CustomDaemonizer

    # Try to fork if at all possible retrying every 5 sec if the
    # maximum process limit for the system has been reached
    def self.safefork
      begin
        pid = fork
        return pid if pid
      rescue Errno::EWOULDBLOCK
        sleep 5
        retry
      end
    end

    # This method causes the current running process to become a daemon
    def self.daemonize(log_file = '/dev/null')
      srand # Split rand streams between spawning and daemonized process

      # Fork, then exit when the child has exited below
      if pid = safefork
        ::Process.wait pid
        exit
      end

      # Detach from the controlling terminal
      raise 'Cannot detach from controlled terminal' unless sess_id = ::Process.setsid

      # Prevent the possibility of acquiring a controlling terminal
      # Fork again, allow a PID file to be written, then exit
      trap 'SIGHUP', 'IGNORE'

      if pid = safefork
        yield pid if block_given?
        exit
      end

      Dir.chdir "/"   # Release old working directory
      File.umask 0000 # Ensure sensible umask

      STDIN.reopen "/dev/null"
      STDOUT.reopen '/dev/null', "a"
      STDERR.reopen log_file, "a"
      return sess_id
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
adhearsion-2.6.4 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.6.3 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.6.2 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.6.1 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.6.0 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.5.4 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.5.3 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.5.2 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.5.0 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.4.0 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.4.0.beta3 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.4.0.beta2 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.4.0.beta1 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.3.5 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.3.4 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.3.3 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.3.2 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.3.1 lib/adhearsion/foundation/custom_daemonizer.rb
adhearsion-2.3.0 lib/adhearsion/foundation/custom_daemonizer.rb