diff --git a/lib/fluent/command/ctl.rb b/lib/fluent/command/ctl.rb index a51341578e..6a2ffe42ae 100644 --- a/lib/fluent/command/ctl.rb +++ b/lib/fluent/command/ctl.rb @@ -173,7 +173,7 @@ def parse_options! usage("PID or SVCNAME isn't specified!") if @pid_or_svcname.nil? || @pid_or_svcname.empty? else usage("PID isn't specified!") if @pid_or_svcname.nil? || @pid_or_svcname.empty? - usage("Invalid PID: #{@pid_or_svcname}") unless /^[0-9]+$/.match?(@pid_or_svcname) + usage("Invalid PID: #{@pid_or_svcname}") unless /\A[1-9][0-9]*\n?\z/.match?(@pid_or_svcname) end end end diff --git a/test/command/test_ctl.rb b/test/command/test_ctl.rb index 2e7157a1b7..522e7774a9 100644 --- a/test/command/test_ctl.rb +++ b/test/command/test_ctl.rb @@ -56,6 +56,11 @@ def test_commands_with_winsvcname(data) end data("not a number" => [["shutdown", "abc"], "Error: Invalid PID: abc"], + "zero" => [["shutdown", "0"], "Error: Invalid PID: 0"], + "leading zero" => [["shutdown", "0123"], "Error: Invalid PID: 0123"], + "digits on the first line" => [["shutdown", "123\nabc"], "Error: Invalid PID: 123\nabc"], + "digits on the second line" => [["shutdown", "abc\n123"], "Error: Invalid PID: abc\n123"], + "two trailing newlines" => [["shutdown", "123\n\n"], "Error: Invalid PID: 123\n\n"], "no PID" => [["shutdown"], "Error: PID isn't specified!"]) def test_invalid_pid(data) omit "Windows accepts a service name instead of a PID" if Fluent.windows? @@ -70,6 +75,16 @@ def test_invalid_pid(data) assert_include(out, message) end + def test_pid_with_trailing_newline + omit "Windows accepts a service name instead of a PID" if Fluent.windows? + + # The PID file that `fluentd --daemon` writes ends with a newline. + mock(Process).kill(:USR1, Process.pid) { 1 } + assert_nothing_raised do + Fluent::Ctl.new(["flush", "#{Process.pid}\n"]).call + end + end + def test_default_winsvc_name omit "Only for Windows" unless Fluent.windows?