Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use given encoding when RUBYOPT has -E #2814

Merged
merged 3 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -661,25 +661,7 @@ def supervise
Process.setproctitle("supervisor:#{@system_config.process_name}") if @system_config.process_name
$log.info "starting fluentd-#{Fluent::VERSION}", pid: Process.pid, ruby: RUBY_VERSION

rubyopt = ENV["RUBYOPT"]
fluentd_spawn_cmd = [ServerEngine.ruby_bin_path, "-Eascii-8bit:ascii-8bit"]
if rubyopt
fluentd_spawn_cmd.concat(rubyopt.split(' '))
end

# Adding `-h` so that it can avoid ruby's command blocking
# e.g. `ruby -Eascii-8bit:ascii-8bit` will block. but `ruby -Eascii-8bit:ascii-8bit -h` won't.
cmd = fluentd_spawn_cmd.join(' ')
_, e, s = Open3.capture3("#{cmd} -h")
if s.exitstatus != 0
$log.error('Invalid option is passed to RUBYOPT', command: cmd, error: e)
exit s.exitstatus
end

fluentd_spawn_cmd << $0
fluentd_spawn_cmd += $fluentdargv
fluentd_spawn_cmd << "--under-supervisor"

fluentd_spawn_cmd = build_spawn_command
$log.info "spawn command to main: ", cmdline: fluentd_spawn_cmd

params = {
Expand Down Expand Up @@ -888,5 +870,37 @@ def build_system_config(conf)
system_config.overwrite_variables(**opt)
system_config
end

RUBY_ENCODING_OPTIONS_REGEX = %r{\A(-E|--encoding=|--internal-encoding=|--external-encoding=)}.freeze

def build_spawn_command
fluentd_spawn_cmd = [ServerEngine.ruby_bin_path]

rubyopt = ENV['RUBYOPT']
if rubyopt
encodes, others = rubyopt.split(' ').partition { |e| e.match?(RUBY_ENCODING_OPTIONS_REGEX) }
fluentd_spawn_cmd.concat(others)

adopted_encodes = encodes.empty? ? ['-Eascii-8bit:ascii-8bit'] : encodes
fluentd_spawn_cmd.concat(adopted_encodes)
else
fluentd_spawn_cmd << '-Eascii-8bit:ascii-8bit'
end

# Adding `-h` so that it can avoid ruby's command blocking
# e.g. `ruby -Eascii-8bit:ascii-8bit` will block. but `ruby -Eascii-8bit:ascii-8bit -h` won't.
cmd = fluentd_spawn_cmd.join(' ')
_, e, s = Open3.capture3("#{cmd} -h")
if s.exitstatus != 0
$log.error('Invalid option is passed to RUBYOPT', command: cmd, error: e)
exit s.exitstatus
end

fluentd_spawn_cmd << $0
fluentd_spawn_cmd += $fluentdargv
fluentd_spawn_cmd << '--under-supervisor'

fluentd_spawn_cmd
end
end
end
39 changes: 39 additions & 0 deletions test/command/test_fluentd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,45 @@ def multi_workers_ready?
)
end

data(
'-E' => '-Eutf-8',
'-encoding' => '--encoding=utf-8',
'-external-encoding' => '--external-encoding=utf-8',
'-internal-encoding' => '--internal-encoding=utf-8',
)
test "-E option is set to RUBYOPT3" do |opt|
conf = <<CONF
<source>
@type dummy
tag dummy
</source>
<match>
@type null
</match>
CONF
conf_path = create_conf_file('rubyopt_test.conf', conf)
assert_log_matches(
create_cmdline(conf_path),
*opt.split(' '),
patterns_not_match: ['-Eascii-8bit:ascii-8bit'],
env: { 'RUBYOPT' => opt },
)
end

test "without RUBYOPT" do
conf = <<CONF
<source>
@type dummy
tag dummy
</source>
<match>
@type null
</match>
CONF
conf_path = create_conf_file('rubyopt_test.conf', conf)
assert_log_matches(create_cmdline(conf_path), '-Eascii-8bit:ascii-8bit')
end

test 'invalid values are set to RUBYOPT' do
conf = <<CONF
<source>
Expand Down