From: beatles Date: Mon, 4 Feb 2008 14:16:42 +0000 (+0000) Subject: Fixed a bug in the --pid-file option X-Git-Tag: 20170902~313 X-Git-Url: http://git.sourceforge.jp/view?p=shogi-server%2Fshogi-server.git;a=commitdiff_plain;h=499c54dc1200ce9e52f7f6debdbd3fd1f7db51b0 Fixed a bug in the --pid-file option --- diff --git a/changelog b/changelog index 9252759..9d8bfe6 100644 --- a/changelog +++ b/changelog @@ -1,16 +1,14 @@ 2008-02-04 Daigo Moriwaki * [shogi-server] - - In the daemon mode, if the specified directory was a relative - path, the server could fail to start with a permission denied - error. This issue has been fixed. The path is interpreted as - an absolute path before switching to daemon. - * [webserver] - In the daemon mode, if the specified directory was a relative path, the server could fail to start with a permission denied error. This issue has been fixed. The path is interpreted as an absolute path before switching to daemon. + - In the daemon mode, a wrong process id was written in + '--pid-file'. This issue has been fixed. Also, the pid file + will be removed when the server shuts down. 2008-02-03 Daigo Moriwaki diff --git a/shogi-server b/shogi-server index 5569a28..9a369f7 100755 --- a/shogi-server +++ b/shogi-server @@ -2140,7 +2140,7 @@ end def write_pid_file(file) open(file, "w") do |fh| - fh.print Process::pid, "\n" + fh.puts "#{$$}" end end @@ -2179,9 +2179,7 @@ def main LEAGUE.event = ARGV.shift port = ARGV.shift - write_pid_file($options["pid-file"]) if ($options["pid-file"]) - - dir = $options["daemon"] || nil + dir = $options["daemon"] dir = File.expand_path(dir) if dir if dir && ! File.exist?(dir) FileUtils.mkdir(dir) @@ -2196,6 +2194,15 @@ def main config[:Port] = port config[:ServerType] = WEBrick::Daemon if $options["daemon"] config[:Logger] = $logger + if $options["pid-file"] + pid_file = File.expand_path($options["pid-file"]) + config[:StartCallback] = Proc.new do + write_pid_file(pid_file) + end + config[:StopCallback] = Proc.new do + FileUtils.rm(pid_file, :force => true) + end + end server = WEBrick::GenericServer.new(config) ["INT", "TERM"].each do |signal| diff --git a/webserver b/webserver index d788000..edaf51e 100755 --- a/webserver +++ b/webserver @@ -21,6 +21,13 @@ require 'timeout' require 'thread' require 'webrick' require 'socket' +require 'fileutils' + +def write_pid_file(file) + open(file, "w") do |fh| + fh.puts "#{$$}" + end +end class ShogiClient CLIENT_NAME = "web" @@ -98,16 +105,18 @@ end def usage $stderr.puts <<-EOF -USAGE: #{$0} [--daemon dir] [--port port] +USAGE: #{$0} [--daemon dir] [--port port] [--pid-file file] --daemon dir Run as a daemon. Log files are put in dir --port port Listening port for HTTP server (default 4080) + --pid-file file Write the process id to the file EOF end def parse_command_line options = Hash::new - parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT], - ["--port", GetoptLong::REQUIRED_ARGUMENT]) + parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT], + ["--port", GetoptLong::REQUIRED_ARGUMENT], + ["--pid-file", GetoptLong::REQUIRED_ARGUMENT]) parser.quiet = true begin parser.each_option do |name, arg| @@ -119,16 +128,16 @@ def parse_command_line raise parser.error_message end - dir = options["daemon"] || nil + dir = options["daemon"] dir = File.expand_path(dir) if dir if dir && ! File.exist?(dir) FileUtils.mkdir(dir) end options["dir"] = dir || File.dirname(__FILE__) - options["port"] ||= 4080 options["port"] = options["port"].to_i - + options["pid-file"] = File.expand_path(options["pid-file"]) if options["pid-file"] + return options end @@ -147,6 +156,15 @@ def main http_config[:Logger] = WEBrick::Log.new(http_log_file) http_config[:AccessLog] = [[ WEBrick::Log.new(http_access_log_file), WEBrick::AccessLog::COMMON_LOG_FORMAT ]] + if $options["pid-file"] + http_config[:StartCallback] = Proc.new do + write_pid_file($options["pid-file"]) + end + http_config[:StopCallback] = Proc.new do + FileUtils.rm($options["pid-file"], :force => true) + end + end + server = WEBrick::HTTPServer.new(http_config)