about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-07-15 08:19:55 +0000
committerEric Wong <e@80x24.org>2015-07-15 08:19:55 +0000
commit427ee86b059228c3eb51c920d7d560432189d700 (patch)
tree77da2f88b62b633de20474219b4ca3d1c7d47972 /lib
parent5aec4a87bd571c5525eb7ffaada29eab607214f1 (diff)
downloadyahns-427ee86b059228c3eb51c920d7d560432189d700.tar.gz
systemd socket emulation shares FDs across execve, just like
the built-in SIGUSR2 upgrade process in unicorn.  Thus it is
easy to support inheriting sockets from systemd.
Diffstat (limited to 'lib')
-rw-r--r--lib/yahns/server.rb16
1 files changed, 13 insertions, 3 deletions
diff --git a/lib/yahns/server.rb b/lib/yahns/server.rb
index b6663e1..01334ca 100644
--- a/lib/yahns/server.rb
+++ b/lib/yahns/server.rb
@@ -312,12 +312,22 @@ class Yahns::Server # :nodoc:
     # because that can completely break the non-blocking one.
     # Unfortunately, there is no one-off MSG_DONTWAIT-like flag for
     # accept4(2).
-    inherited = ENV['YAHNS_FD'].to_s.split(',').map! do |fd|
-      io = Socket.for_fd(fd.to_i)
+    inherited = ENV['YAHNS_FD'].to_s.split(',')
+
+    # emulate sd_listen_fds() for systemd
+    sd_pid, sd_fds = ENV.values_at('LISTEN_PID', 'LISTEN_FDS')
+    if sd_pid && sd_pid.to_i == $$
+      # 3 = SD_LISTEN_FDS_START
+      inherited.concat((3...(3 + sd_fds.to_i)).map { |fd| Socket.for_fd(fd) })
+    end
+    # to ease debugging, we will not unset LISTEN_PID and LISTEN_FDS
+
+    inherited.map! do |fd|
+      io = String === fd ? Socket.for_fd(fd.to_i) : fd
       opts = sock_opts(io)
       io = server_cast(io, opts)
       set_server_sockopt(io, opts)
-      @logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
+      @logger.info "inherited addr=#{sock_name(io)} fd=#{io.fileno}"
       io
     end