Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.

Adds a --no-daemonize option #91

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions bin/oversip
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ module OverSIP

# Options by default.
options = {
:colorize => true
:colorize => true,
:daemonize => true
}

OptionParser.new("", 28, " ") do |opts|
Expand Down Expand Up @@ -93,6 +94,10 @@ module OverSIP
require library
end

opts.on("--no-daemonize", "Starts OverSIP in the foreground (no fork)") do |value|
options[:daemonize] = false
end

opts.separator "\nCommon options:"

opts.on_tail("-h", "--help", "Show this message") do
Expand Down Expand Up @@ -173,7 +178,11 @@ module OverSIP
log_system_error "error increasing rlimits for 'nofiles': #{e.message} (#{e.class})"
end

::OverSIP::Launcher.daemonize!(options)
# Only fork here if the user didn't ask not to
if options[:daemonize]
::OverSIP::Launcher.daemonize!(options)
end

::OverSIP::Launcher.run(options)

end # def run
Expand Down
18 changes: 11 additions & 7 deletions lib/oversip/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def self.run options
# Create PID file.
create_pid_file(options[:pid_file])

trap_signals
trap_signals(options[:daemonize])

# Ensure the code in the next SystemEvents and SystemCallbacks are run serially.
::Fiber.new do
Expand Down Expand Up @@ -162,7 +162,7 @@ def self.run options
fatal e
end

log_system_notice "#{::OverSIP::PROGRAM_NAME} #{::OverSIP::VERSION} running in background"
log_system_notice "#{::OverSIP::PROGRAM_NAME} #{::OverSIP::VERSION} running in #{options[:daemonize] ? "background" : "foreground"}"

# Write "ok" into the ready_pipe so grandparent process (launcher)
# exits with status 0.
Expand All @@ -173,9 +173,11 @@ def self.run options
end

# Stop writting into standard output/error.
$stdout.reopen("/dev/null")
$stderr.reopen("/dev/null")
::OverSIP.daemonized = true
if options[:daemonize]
$stdout.reopen("/dev/null")
$stderr.reopen("/dev/null")
::OverSIP.daemonized = true
end

# So update the logger to stop writting into stdout.
::OverSIP::Logger.load_methods
Expand Down Expand Up @@ -409,7 +411,7 @@ def self.run_servers options
end


def self.trap_signals
def self.trap_signals(background)
# This should never occur (unless some not trapped signal is received
# and causes Ruby to exit, or maybe the user called "exit()" within its
# custom code).
Expand All @@ -425,6 +427,7 @@ def self.trap_signals

# Signals that cause OverSIP to terminate.
exit_signals = [:TERM, :QUIT]
exit_signals << :INT unless background
exit_signals.each do |signal|
trap signal do
log_system_notice "#{signal} signal received, exiting..."
Expand All @@ -433,7 +436,8 @@ def self.trap_signals
end

# Signals that must be ignored.
ignore_signals = [:ALRM, :INT, :PIPE, :POLL, :PROF, :USR2, :WINCH]
ignore_signals = [:ALRM, :PIPE, :POLL, :PROF, :USR2, :WINCH]
ignore_signals << :INT if background
ignore_signals.each do |signal|
begin
trap signal do
Expand Down
25 changes: 16 additions & 9 deletions lib/oversip/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def self.load_methods

syslog_options = ::Syslog::LOG_PID | ::Syslog::LOG_NDELAY
syslog_facility = ::OverSIP::Syslog::SYSLOG_FACILITY_MAPPING[::OverSIP.configuration[:core][:syslog_facility]] rescue ::Syslog::LOG_DAEMON
::Syslog.open(::OverSIP.master_name, syslog_options, syslog_facility)

::Syslog.open(::OverSIP.master_name, syslog_options, syslog_facility) if ::OverSIP.daemonized?

begin
@@threshold = ::OverSIP::Syslog::SYSLOG_SEVERITY_MAPPING[::OverSIP.configuration[:core][:syslog_level]]
Expand All @@ -22,16 +23,18 @@ def self.load_methods

::OverSIP::Syslog::SYSLOG_SEVERITY_MAPPING.each do |level_str, level_value|
method_str = "
def log_system_#{level_str}(msg)
def internal_log_#{level_str}(system, msg)
"

method_str << "
return false if @@threshold > #{level_value}

::OverSIP::Syslog.log #{level_value}, msg, log_id, false
"

if not ::OverSIP.daemonized?
if ::OverSIP.daemonized?
method_str << "
::OverSIP::Syslog.log #{level_value}, msg, log_id, !system
"
else
if %w{debug info notice}.include? level_str
method_str << "
puts ::OverSIP::Logger.fg_system_msg2str('#{level_str}', msg, log_id)
Expand All @@ -43,17 +46,21 @@ def log_system_#{level_str}(msg)
end
end

method_str << "end"
method_str << "
end
"

self.module_eval method_str


# User logs.
method_str = "
def log_#{level_str}(msg)
return false if @@threshold > #{level_value}
def log_system_#{level_str}(msg)
internal_log_#{level_str}(true, msg)
end

::OverSIP::Syslog.log #{level_value}, msg, log_id, true
def log_#{level_str}(msg)
internal_log_#{level_str}(false, msg)
end
"

Expand Down