From d39358743e54b3503e09f64b7ccf38b5d9e9a501 Mon Sep 17 00:00:00 2001 From: og-orca <301901286+og-orca@users.noreply.github.com> Date: Wed, 2 Dec 2026 19:07:56 +0100 Subject: [PATCH 1/2] fluent-ctl: reject 0 and multi-line values as a PID PID 0 makes Process.kill signal the whole process group, so fluent-ctl shutdown 0 killed the shell that ran it. A value like "abc\n123" passed the check too and became 0 in to_i. Signed-off-by: og-orca <301901286+og-orca@users.noreply.github.com> --- lib/fluent/command/ctl.rb | 2 +- test/command/test_ctl.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/fluent/command/ctl.rb b/lib/fluent/command/ctl.rb index a51341578e..c63a87504d 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]*\z/.match?(@pid_or_svcname) end end end diff --git a/test/command/test_ctl.rb b/test/command/test_ctl.rb index 2e7157a1b7..fccefe725a 100644 --- a/test/command/test_ctl.rb +++ b/test/command/test_ctl.rb @@ -56,6 +56,10 @@ 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"], "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? From 8aafc8aa872e4d08757552d24451f0212690c4b3 Mon Sep 17 00:00:00 2001 From: og-orca <301901286+og-orca@users.noreply.github.com> Date: Thu, 3 Dec 2026 03:55:49 +0100 Subject: [PATCH 2/2] fluent-ctl: accept a PID that ends with a newline Signed-off-by: og-orca <301901286+og-orca@users.noreply.github.com> --- lib/fluent/command/ctl.rb | 2 +- test/command/test_ctl.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/fluent/command/ctl.rb b/lib/fluent/command/ctl.rb index c63a87504d..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 /\A[1-9][0-9]*\z/.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 fccefe725a..522e7774a9 100644 --- a/test/command/test_ctl.rb +++ b/test/command/test_ctl.rb @@ -60,6 +60,7 @@ def test_commands_with_winsvcname(data) "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? @@ -74,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?