Skip to content

Commit

Permalink
Remove dependency to win32-api gem
Browse files Browse the repository at this point in the history
Recently we fail to load win32-api on Ruby 3.2-dev:
https://github.com/fluent/fluentd/runs/7660210030?check_suite_focus=true

  D:/rubyinstaller-head-x64/lib/ruby/gems/3.2.0+2/gems/windows-api-0.4.5/lib/windows/api.rb:335:in `initialize': uninitialized constant Win32::API (NameError)
        @api = Win32::API.new(func, proto, rtype, dll)
                    ^^^^^
          from D:/rubyinstaller-head-x64/lib/ruby/gems/3.2.0+2/gems/windows-pr-1.2.6/lib/windows/msvcrt/string.rb:12:in `new'

but it seems that Fluentd doesn't seem depend on win32-api so much.
Using Fiddle is enough instead. It's time to drop win32-api gem.

Signed-off-by: Takuro Ashie <[email protected]>
  • Loading branch information
ashie committed Aug 8, 2022
1 parent cf241f0 commit 1bc44cb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 37 deletions.
3 changes: 0 additions & 3 deletions fluentd.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ Gem::Specification.new do |gem|
fake_platform = ENV['GEM_BUILD_FAKE_PLATFORM'].to_s
gem.platform = fake_platform unless fake_platform.empty?
if /mswin|mingw/ =~ fake_platform || (/mswin|mingw/ =~ RUBY_PLATFORM && fake_platform.empty?)
gem.add_runtime_dependency("win32-api", [">= 1.10", "< 2.0.0"])
gem.add_runtime_dependency("win32-service", ["~> 2.3.0"])
gem.add_runtime_dependency("win32-ipc", ["~> 0.7.0"])
gem.add_runtime_dependency("win32-event", ["~> 0.6.3"])
gem.add_runtime_dependency("windows-api", ["~> 0.4.5"])
gem.add_runtime_dependency("windows-pr", ["~> 1.2.6"])
gem.add_runtime_dependency("certstore_c", ["~> 0.1.7"])
end

Expand Down
7 changes: 1 addition & 6 deletions lib/fluent/command/fluentd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@
}

if Fluent.windows?
require 'windows/library'
include Windows::Library

opts.merge!(
:winsvc_name => 'fluentdwinsvc',
:winsvc_display_name => 'Fluentd Windows Service',
Expand Down Expand Up @@ -292,9 +289,7 @@
case winsvcinstmode
when 'i'
binary_path = File.join(File.dirname(__FILE__), "..")
ruby_path = "\0" * 256
GetModuleFileName.call(0,ruby_path,256)
ruby_path = ruby_path.rstrip.gsub(/\\/, '/')
ruby_path = ServerEngine.ruby_bin_path
start_type = Service::DEMAND_START
if opts[:regwinsvcautostart]
start_type = Service::AUTO_START
Expand Down
32 changes: 24 additions & 8 deletions lib/fluent/plugin/file_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,36 @@ def self.stat(path)
end
end

class WindowsFile
require 'windows/file'
require 'windows/handle'
module Win32API
require 'fiddle/import'
require 'fiddle/types'
extend Fiddle::Importer

if RUBY_PLATFORM.split('-')[-1] == "ucrt"
MSVCRT_DLL = 'ucrtbase.dll'
else
MSVCRT_DLL = 'msvcrt.dll'
end

dlload MSVCRT_DLL, "kernel32.dll"
include Fiddle::Win32Types

extern "intptr_t _get_osfhandle(int)"
extern "int GetFileInformationByHandle(HANDLE, void *)"
extern "int GetFileInformationByHandleEx(HANDLE, int, void *, DWORD)"
end

class WindowsFile
include File::Constants
include Windows::File
include Windows::Handle

attr_reader :io

INVALID_HANDLE_VALUE = -1

def initialize(path, mode='r')
@path = path
@io = File.open(path, mode2flags(mode))
@file_handle = _get_osfhandle(@io.to_i)
@file_handle = Win32API._get_osfhandle(@io.to_i)
@io.instance_variable_set(:@file_index, self.ino)
def @io.ino
@file_index
Expand All @@ -68,7 +84,7 @@ def close
def ino
by_handle_file_information = '\0'*(4+8+8+8+4+4+4+4+4+4) #72bytes

unless GetFileInformationByHandle.call(@file_handle, by_handle_file_information)
unless Win32API.GetFileInformationByHandle(@file_handle, by_handle_file_information)
return 0
end

Expand Down Expand Up @@ -122,7 +138,7 @@ def delete_pending
bufsize = 1024
buf = '\0' * bufsize

unless GetFileInformationByHandleEx.call(@file_handle, file_standard_info, buf, bufsize)
unless Win32API.GetFileInformationByHandleEx(@file_handle, file_standard_info, buf, bufsize)
return false
end

Expand Down
9 changes: 2 additions & 7 deletions lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@
require 'serverengine'

if Fluent.windows?
require 'windows/library'
require 'windows/synchronize'
require 'windows/system_info'
include Windows::Library
include Windows::Synchronize
include Windows::SystemInfo
require 'win32/ipc'
require 'win32/event'
end
Expand Down Expand Up @@ -235,7 +229,8 @@ def install_windows_event_handler
end
begin
loop do
ipc_idx = ipc.wait_any(events.map {|e| e[:win32_event]}, Windows::Synchronize::INFINITE)
infinite = 0xFFFFFFFF
ipc_idx = ipc.wait_any(events.map {|e| e[:win32_event]}, infinite)
event_idx = ipc_idx - 1

if event_idx >= 0 && event_idx < events.length
Expand Down
10 changes: 2 additions & 8 deletions lib/fluent/winsvc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
begin

require 'optparse'
require 'windows/debug'
require 'Windows/Library'
require 'win32/daemon'
require 'win32/event'

include Win32
include Windows::Library
include Windows::Debug

op = OptionParser.new
opts = {service_name: nil}
Expand All @@ -37,16 +33,14 @@
end

def read_fluentdopt(service_name)
require 'win32/Registry'
require 'win32/registry'
Win32::Registry::HKEY_LOCAL_MACHINE.open("SYSTEM\\CurrentControlSet\\Services\\#{service_name}") do |reg|
reg.read("fluentdopt")[1] rescue ""
end
end

def service_main_start(service_name)
ruby_path = 0.chr * 260
GetModuleFileName.call(0, ruby_path,260)
ruby_path = ruby_path.rstrip.gsub(/\\/, '/')
ruby_path = ServerEngine.ruby_bin_path
rubybin_dir = ruby_path[0, ruby_path.rindex("/")]
opt = read_fluentdopt(service_name)
Process.spawn("\"#{rubybin_dir}/ruby.exe\" \"#{rubybin_dir}/fluentd\" #{opt} -x #{service_name}")
Expand Down
5 changes: 0 additions & 5 deletions test/plugin/test_file_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
require 'fluent/plugin/file_wrapper'

class FileWrapperTest < Test::Unit::TestCase
require 'windows/file'
require 'windows/error'
include Windows::File
include Windows::Error

TMP_DIR = File.dirname(__FILE__) + "/../tmp/file_wrapper#{ENV['TEST_ENV_NUMBER']}"

def setup
Expand Down

0 comments on commit 1bc44cb

Please sign in to comment.