lib/rack/server.rb in rack-1.3.6 vs lib/rack/server.rb in rack-1.3.7

- old
+ new

@@ -24,11 +24,11 @@ options[:warn] = true } opts.on("-I", "--include PATH", "specify $LOAD_PATH (may be used more than once)") { |path| - options[:include] = path.split(":") + (options[:include] ||= []).concat(path.split(":")) } opts.on("-r", "--require LIBRARY", "require the library, before executing your script") { |library| options[:require] = library @@ -245,15 +245,18 @@ p options[:server] pp wrapped_app pp app end + check_pid! if options[:pid] + # Touch the wrapped app, so that the config.ru is loaded before # daemonization (i.e. before chdir, etc). wrapped_app daemonize_app if options[:daemonize] + write_pid if options[:pid] trap(:INT) do if server.respond_to?(:shutdown) server.shutdown @@ -272,11 +275,11 @@ private def parse_options(args) options = default_options # Don't evaluate CGI ISINDEX parameters. - # http://hoohoo.ncsa.uiuc.edu/cgi/cl.html + # http://www.meb.uni-bonn.de/docs/cgi/cl.html args.clear if ENV.include?("REQUEST_METHOD") options.merge! opt_parser.parse!(args) options[:config] = ::File.expand_path(options[:config]) ENV["RACK_ENV"] = options[:environment] @@ -317,7 +320,30 @@ def write_pid ::File.open(options[:pid], 'w'){ |f| f.write("#{Process.pid}") } at_exit { ::File.delete(options[:pid]) if ::File.exist?(options[:pid]) } end + + def check_pid! + case pidfile_process_status + when :running, :not_owned + $stderr.puts "A server is already running. Check #{options[:pid]}." + exit(1) + when :dead + ::File.delete(options[:pid]) + end + end + + def pidfile_process_status + return :exited unless ::File.exist?(options[:pid]) + + pid = ::File.read(options[:pid]).to_i + Process.kill(0, pid) + :running + rescue Errno::ESRCH + :dead + rescue Errno::EPERM + :not_owned + end + end end