Skip to content

Commit

Permalink
msi: guard duplicated instance
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kenhys committed Feb 20, 2024
1 parent fdc9475 commit 9fe4c9e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
34 changes: 34 additions & 0 deletions fluent-package/msi/assets/fluentd.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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 /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 /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

Expand Down
17 changes: 17 additions & 0 deletions fluent-package/msi/update-from-v4-test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,20 @@ $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: The previous config works as before
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"

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"

0 comments on commit 9fe4c9e

Please sign in to comment.