Skip to content

Commit

Permalink
Support Linux's abstract sockets
Browse files Browse the repository at this point in the history
Closes puma#2526
  • Loading branch information
MSP-Greg committed Mar 15, 2021
1 parent 5e6e073 commit c4424be
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
14 changes: 14 additions & 0 deletions lib/puma.rb
Expand Up @@ -39,6 +39,20 @@ def self.ssl?
HAS_SSL
end

def self.abstract_unix_socket?
@abstract_unix ||=
if HAS_UNIX_SOCKET
begin
::UNIXServer.new("\0puma.temp.unix").close
true
rescue ArgumentError # darwin
false
end
else
false
end
end

# @!attribute [rw] stats_object=
def self.stats_object=(val)
@get_stats = val
Expand Down
27 changes: 16 additions & 11 deletions lib/puma/binder.rb
Expand Up @@ -177,11 +177,19 @@ def parse(binds, logger, log_msg = 'Listening')
@listeners << [str, io] if io
when "unix"
path = "#{uri.host}#{uri.path}".gsub("%20", " ")
abstract = false
if str.start_with? 'unix://@'
raise "OS does not support abstract UNIXSockets" unless Puma.abstract_unix_socket?
abstract = true
path = "@#{path}"
end

if fd = @inherited_fds.delete(str)
@unix_paths << path unless abstract
io = inherit_unix_listener path, fd
logger.log "* Inherited #{str}"
elsif sock = @activated_sockets.delete([ :unix, path ])
@unix_paths << path unless abstract || File.exist?(path)
io = inherit_unix_listener path, sock
logger.log "* Activated #{str}"
else
Expand All @@ -205,6 +213,7 @@ def parse(binds, logger, log_msg = 'Listening')
end
end

@unix_paths << path unless abstract || File.exist?(path)
io = add_unix_listener path, umask, mode, backlog
logger.log "* #{log_msg} on #{str}"
end
Expand Down Expand Up @@ -355,8 +364,6 @@ def inherit_ssl_listener(fd, ctx)
# Tell the server to listen on +path+ as a UNIX domain socket.
#
def add_unix_listener(path, umask=nil, mode=nil, backlog=1024)
@unix_paths << path unless File.exist? path

# Let anyone connect by default
umask ||= 0

Expand All @@ -374,7 +381,7 @@ def add_unix_listener(path, umask=nil, mode=nil, backlog=1024)
end
end

s = UNIXServer.new(path)
s = UNIXServer.new path.sub(/\A@/, "\0")
s.listen backlog
@ios << s
ensure
Expand All @@ -393,8 +400,6 @@ def add_unix_listener(path, umask=nil, mode=nil, backlog=1024)
end

def inherit_unix_listener(path, fd)
@unix_paths << path unless File.exist? path

s = fd.kind_of?(::TCPServer) ? fd : ::UNIXServer.for_fd(fd)

@ios << s
Expand All @@ -407,24 +412,24 @@ def inherit_unix_listener(path, fd)
end

def close_listeners
listeners.each do |l, io|
io.close unless io.closed? # Ruby 2.2 issue
uri = URI.parse(l)
@listeners.each do |l, io|
io.close unless io.closed?
uri = URI.parse l
next unless uri.scheme == 'unix'
unix_path = "#{uri.host}#{uri.path}"
File.unlink unix_path if unix_paths.include? unix_path
File.unlink unix_path if @unix_paths.include?(unix_path) && File.exist?(unix_path)
end
end

def redirects_for_restart
redirects = listeners.map { |a| [a[1].to_i, a[1].to_i] }.to_h
redirects = @listeners.map { |a| [a[1].to_i, a[1].to_i] }.to_h
redirects[:close_others] = true
redirects
end

# @version 5.0.0
def redirects_for_restart_env
listeners.each_with_object({}).with_index do |(listen, memo), i|
@listeners.each_with_object({}).with_index do |(listen, memo), i|
memo["PUMA_INHERIT_#{i}"] = "#{listen[1].to_i}:#{listen[0]}"
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/puma/control_cli.rb
Expand Up @@ -176,7 +176,8 @@ def send_request
when 'tcp'
TCPSocket.new uri.host, uri.port
when 'unix'
UNIXSocket.new "#{uri.host}#{uri.path}"
UNIXSocket.new(@control_url.start_with?('unix://@') ?
"\0#{uri.host}#{uri.path}" : "#{uri.host}#{uri.path}")
else
raise "Invalid scheme: #{uri.scheme}"
end
Expand Down

0 comments on commit c4424be

Please sign in to comment.