Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix running in non-daemonized mode. Allow injection of a logger.

  • Loading branch information...
commit a0470d1b57df1d3557a267f3441be532ec95b779 1 parent bbca69e
Roman Shterenzon authored
39 bin/received
View
@@ -4,6 +4,7 @@ require 'logger'
require 'optparse'
$:<< File.expand_path('../../lib', __FILE__)
require 'received'
+require 'daemons'
options = {}
OptionParser.new do |opts|
@@ -14,8 +15,8 @@ OptionParser.new do |opts|
opts.on('-s', '--unix-socket PATH', 'Use UNIX socket') {|v| options[:unix_socket] = v}
opts.on('-p', '--port NUM', 'Listen to TCP port') {|v| options[:port] = v.to_i}
opts.on('-i', '--host NAME', 'Bind to this IP (default: 127.0.0.1)') {|v| options[:host] = v}
- opts.on('-a', '--piddir PATH', 'Directory for pid file (default: /var/tmp)') {|v| options[:dir] = v}
- opts.on('-l', '--log FILE', 'Log file name (default: piddir/received.log)') {|v| options[:logfile] = v}
+ opts.on('-a', '--piddir PATH', 'Directory for pid file (default: /var/run/received)') {|v| options[:dir] = v}
+ opts.on('-l', '--log FILE', 'Log file name (default: /var/log/received.log)') {|v| options[:logfile] = v}
opts.on('-u', '--user NAME', 'Effective user when daemon (default: nobody)') {|v| options[:user] = v}
opts.on('-g', '--group NAME', 'Effective group when daemon (default: nobody)') {|v| options[:group] = v}
opts.on('-r', '--require FILE1,FILE2,...', 'Require ruby file(s)') {|v| options[:require] = v}
@@ -30,24 +31,28 @@ raise "Config file is required, please provide with -c config.yml" unless option
# Default backend
options[:backend] ||= 'mongodb'
+options[:dir] ||= '/var/run/received'
options[:logger] = Logger.new(options[:logfile] || $stderr).tap do |logger|
logger.level = options[:level] || Logger::INFO
end
-if options.delete(:daemon)
- require 'daemons'
- # Monkey patch to rename log file
- class Daemons::Application
- def output_logfile
- (options[:log_output] && logdir) ? File.join(logdir, @group.app_name + '.log') : nil
- end
- end
- params = {:app_name => 'received', :log_output => true, :dir_mode => :normal, :dir => options[:dir] || '/var/tmp'}
- # Drop privileges if started as superuser
- params.merge!({:user => options[:user] || 'nobody', :group => options[:group] || 'nobody'}) if Process.uid == 0
- Daemons.daemonize(params)
- File.umask(0007)
+ontop = !options.delete(:daemon)
+params = {
+ :ontop => ontop,
+ :app_name => 'received',
+ :log_output => true,
+ :dir_mode => :normal,
+ :dir => options[:dir]
+}
+
+# Drop privileges if started as superuser
+params.merge!(:user => options[:user] || 'nobody', :group => options[:group] || 'nobody') if Process.uid == 0
+Daemons.daemonize(params)
+File.umask(0007)
+
+if files = options[:require]
+ files.split(',').each { |file| require file }
end
server = Received::Server.new(options)
@@ -55,8 +60,4 @@ server = Received::Server.new(options)
Signal.trap(sig) {server.stop}
end
-if files = options[:require]
- files.split(',').each { |file| require file }
-end
-
server.serve!
7 lib/received.rb
View
@@ -1,4 +1,11 @@
module Received
+ def self.logger=(l)
+ @logger = l
+ end
+
+ def self.logger
+ @logger
+ end
end
require 'received/backend/base'
2  lib/received/connection.rb
View
@@ -52,7 +52,7 @@ def mail_received(mail)
end
def logger
- @server.logger
+ Received.logger
end
end
end
4 lib/received/lmtp.rb
View
@@ -29,7 +29,7 @@ def reset!
end
def event(ev)
- @conn.logger.debug {"state was: #{@state.inspect}"}
+ Received.logger.debug {"state was: #{@state.inspect}"}
@state = case @state
when :start
reset!
@@ -92,7 +92,7 @@ def event(ev)
else
raise "Where am I? (#{@state.inspect})"
end || @state
- @conn.logger.debug {"state now: #{@state.inspect}"}
+ Received.logger.debug {"state now: #{@state.inspect}"}
end
def banner
8 lib/received/server.rb
View
@@ -5,11 +5,11 @@
module Received
class Server
- attr_reader :logger, :options
+ attr_reader :options
def initialize(options)
@options = options
- @logger = options[:logger] || Logger.new($stderr)
+ Received.logger ||= (options[:logger] || Logger.new(STDERR))
@connections = []
# For how long the server will wait for connections to finish
@grace_period = options[:grace_period] || 10
@@ -63,6 +63,10 @@ def remove_connection(conn)
set_title
end
+ def logger
+ Received.logger
+ end
+
private
# Sets the process title as seen in ps
10 spec/lmtp_spec.rb
View
@@ -2,12 +2,16 @@
require 'logger'
describe Received::LMTP do
- let(:conn) { mock(:conn, :logger => Logger.new(STDERR)) }
+ let(:conn) { mock(:conn) }
let(:proto) { Received::LMTP.new(conn) }
+ before :all do
+ Received.logger = Logger.new(STDERR)
+ end
+
before do
conn.should_receive(:send_data).with("220 localhost LMTP server ready\r\n")
- conn.logger.debug "*** Starting test ***"
+ #conn.logger.debug "*** Starting test ***"
proto.start!
end
@@ -17,7 +21,7 @@
def begin_flow!
["LHLO", "MAIL FROM:<spec1@example.com>", "RCPT TO:<spec2@example.com>",
"RCPT TO:<spec3@example.com>", "DATA", "#{body}.", "QUIT"].each do |line|
- conn.logger.debug "client: #{line}"
+ # conn.logger.debug "client: #{line}"
proto.on_data(line + "\r\n")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.