From 32088ec6ed30dd09069e42c10a73e9fc3ed93688 Mon Sep 17 00:00:00 2001 From: Krzysztof Urbaniak Date: Fri, 5 Apr 2013 01:41:22 +0200 Subject: [PATCH 1/2] systemd socket activation --- lib/puma/binder.rb | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/puma/binder.rb b/lib/puma/binder.rb index 548fa9d7..a1ad2ac4 100644 --- a/lib/puma/binder.rb +++ b/lib/puma/binder.rb @@ -53,6 +53,22 @@ module Puma @inherited_fds[url] = fd.to_i remove << k end + if k =~ /LISTEN_FDS/ && ENV['LISTEN_PID'].to_i == $$ + v.to_i.times do |num| + fd = num + 3 + sock = TCPServer.for_fd(fd) + begin + url = "unix://" + Socket.unpack_sockaddr_un(sock.getsockname) + rescue ArgumentError + port, addr = Socket.unpack_sockaddr_in(sock.getsockname) + if addr =~ /\:/ + addr = "[#{addr}]" + end + url = "tcp://#{addr}:#{port}" + end + @inherited_fds[url] = sock + end + end end remove.each do |k| @@ -141,7 +157,12 @@ module Puma logger.log "* Closing unused inherited connection: #{str}" begin - IO.for_fd(fd).close + if fd.kind_of? TCPServer + fd.close + else + IO.for_fd(fd).close + end + rescue SystemCallError end @@ -243,7 +264,11 @@ module Puma def inherit_unix_listener(path, fd) @unix_paths << path - s = UNIXServer.for_fd fd + if fd.kind_of? TCPServer + s = fd + else + s = UNIXServer.for_fd fd + end @ios << s s From 9b619085ff7db92c1a76ad5e84911e95cae8c9d8 Mon Sep 17 00:00:00 2001 From: Krzysztof Urbaniak Date: Fri, 5 Apr 2013 01:44:30 +0200 Subject: [PATCH 2/2] after socket activation remove LISTEN_FDS and LISTEN_PID environment variables --- lib/puma/binder.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/puma/binder.rb b/lib/puma/binder.rb index a1ad2ac4..a992425c 100644 --- a/lib/puma/binder.rb +++ b/lib/puma/binder.rb @@ -68,6 +68,8 @@ module Puma end @inherited_fds[url] = sock end + ENV.delete k + ENV.delete 'LISTEN_PID' end end