diff --git a/bin/oversip b/bin/oversip index cfaf4bf..9b2a25a 100755 --- a/bin/oversip +++ b/bin/oversip @@ -35,7 +35,8 @@ module OverSIP # Options by default. options = { - :colorize => true + :colorize => true, + :daemonize => true } OptionParser.new("", 28, " ") do |opts| @@ -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 @@ -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 diff --git a/lib/oversip/launcher.rb b/lib/oversip/launcher.rb index 078ea6c..38baf7b 100644 --- a/lib/oversip/launcher.rb +++ b/lib/oversip/launcher.rb @@ -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 @@ -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. @@ -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 @@ -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). @@ -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..." @@ -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 diff --git a/lib/oversip/logger.rb b/lib/oversip/logger.rb index 26da54b..f184c34 100644 --- a/lib/oversip/logger.rb +++ b/lib/oversip/logger.rb @@ -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]] @@ -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) @@ -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 "