From 40b6ee5ddbacca19b214dbb5f90378837dba65a6 Mon Sep 17 00:00:00 2001 From: Kentaro Hayashi Date: Mon, 19 Feb 2024 18:20:33 +0900 Subject: [PATCH] msi: guard duplicated instance Before: Even though fluentdwinsvc is running, you can execute duplicated Fluentd instance. It may cause inconsistency of buffer or pos file. After: Guard duplicated Fluentd instance when the following conditions are met: * fluentdwinsvc service is running * fluentd.conf is specified in fluentdopt registry value and * A) duplicated fluentd.conf is specified with -c ... option or * B) fluentd.bat is executed without arguments Signed-off-by: Kentaro Hayashi --- fluent-package/msi/assets/fluentd.bat | 34 ++++++++++++++++++++++ fluent-package/msi/update-from-v4-test.ps1 | 18 ++++++++++++ 2 files changed, 52 insertions(+) diff --git a/fluent-package/msi/assets/fluentd.bat b/fluent-package/msi/assets/fluentd.bat index d3ca83a87..6220f71fe 100644 --- a/fluent-package/msi/assets/fluentd.bat +++ b/fluent-package/msi/assets/fluentd.bat @@ -14,12 +14,46 @@ set PATH=%FLUENT_PACKAGE_TOPDIR%;%PATH% set "FLUENT_CONF=%FLUENT_PACKAGE_TOPDIR%/etc/fluent/fluentd.conf" set "FLUENT_PLUGIN=%FLUENT_PACKAGE_TOPDIR%/etc/fluent/plugin" set "FLUENT_PACKAGE_VERSION=%FLUENT_PACKAGE_TOPDIR%/bin/fluent-package-version.rb" +set FLUENT_CONF_CLI= +set /a n=0 +set /a conf_index=0 for %%p in (%*) do ( if "%%p"=="--version" ( ruby "%FLUENT_PACKAGE_VERSION%" goto last ) + if "%%p"=="-c" ( + set /a conf_index=n+1 + ) + if !n! equ !conf_index! ( + set FLUENT_CONF_CLI="%%p" + ) + set /a n=n+1 ) + +@rem Abort if the fluentdwinsvc service is running and conflict with -c ...: +sc query fluentdwinsvc | findstr RUNNING > nul +if !ERRORLEVEL! neq 0 ( + goto noguard +) +@rem extract fluentdopt from registry: (e.g. fluentdopt REG_SZ -c ...) +for /f "usebackq delims=" %%v in (`reg query HKLM\SYSTEM\CurrentControlSet\Services\fluentdwinsvc /v fluentdopt ^| findstr fluentdopt`) do set FLUENTDOPT=%%v +if not "!FLUENT_CONF_CLI!"=="" ( + echo "!FLUENTDOPT:\=/!" | findstr /I /C:!FLUENT_CONF_CLI:\=/! > nul + if !ERRORLEVEL! equ 0 ( + echo Error: can't start duplicate Fluentd instance with same !FLUENT_CONF_CLI! + exit /b 2 + ) +) +if not "!FLUENT_CONF!"=="" ( + echo "!FLUENTDOPT:\=/!" | findstr /I /C:!FLUENT_CONF! > nul + if !ERRORLEVEL! equ 0 ( + echo Error: can't start duplicate Fluentd instance with same !FLUENT_CONF! + exit /b 2 + ) +) + +:noguard "%FLUENT_PACKAGE_TOPDIR%/bin/fluentd" %* endlocal diff --git a/fluent-package/msi/update-from-v4-test.ps1 b/fluent-package/msi/update-from-v4-test.ps1 index 91cf4259a..2e38cef6d 100644 --- a/fluent-package/msi/update-from-v4-test.ps1 +++ b/fluent-package/msi/update-from-v4-test.ps1 @@ -50,3 +50,21 @@ $output_files_after_sleep = Get-ChildItem "C:\\opt\\td-agent\\output" If ($output_files_after_sleep.Count -le $output_files.Count) { [Environment]::Exit(1) } + +# Test: Abort if same fluentd.conf is specified and conflict with fluentdopt (PATH is case-insensitive.) +Start-Process "C:\\opt\\fluent\\fluentd.bat" -ArgumentList "-c", "C:\\opt\\fluent\\etc\\fluent\\fluentd.conf" -Wait -NoNewWindow +$exitcode = $LASTEXITCODE +if ($exitcode -ne 2) { + Write-Host "Failed to abort when it is conflict with running fluentdwinsvc" + [Environment]::Exit(1) +} +Write-Host "Succeeded to abort if trying to launch duplicated Fluentd instance" + +# Test: Abort if FLUENT_CONF is conflict with fluentdopt +Start-Process "C:\\opt\\fluent\\fluentd.bat" -Wait -NoNewWindow +$exitcode = $LASTEXITCODE +if ($exitcode -ne 2) { + Write-Host "Failed to abort without arguments when it is conflict with running fluentdwinsvc" + [Environment]::Exit(1) +} +Write-Host "Succeeded to abort if trying to launch duplicated Fluentd instance without arguments"