unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: Christos Trochalakis <yatiohi@ideopolis.gr>
Cc: Dmitry Smirnov <onlyjob@debian.org>,
	Hleb Valoshka <375gnu@gmail.com>,
	unicorn@packages.debian.org, unicorn-public@bogomips.org
Subject: [RFC] emulate sd_listen_fds for systemd support
Date: Sat, 27 Jun 2015 04:01:36 +0000	[thread overview]
Message-ID: <20150627040136.GA13070@dcvr.yhbt.net> (raw)
In-Reply-To: <20150627030501.GA24759@dcvr.yhbt.net>

Unfortunately, testing this is tricky because FD=3
(SD_LISTEN_FDS_START) tends to be grabbed by (MRI) Ruby 1.9.3
and onwards for the internal self-pipe.

So for now, I've manually tested this with a systemd installation

Disclaimer: this is also my first experience using systemd
---
  Eric Wong <e@80x24.org> wrote:
  > OK, I'll probably add LISTEN_FDS and LISTEN_PID support to unicorn
  > directly so the wrapper is unnecessary.

 lib/unicorn/http_server.rb | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index 297b9f9..0f97516 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -766,12 +766,22 @@ def redirect_io(io, path)
   def inherit_listeners!
     # inherit sockets from parents, they need to be plain Socket objects
     # before they become Kgio::UNIXServer or Kgio::TCPServer
-    inherited = ENV['UNICORN_FD'].to_s.split(',').map do |fd|
-      io = Socket.for_fd(fd.to_i)
+    inherited = ENV['UNICORN_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
       io.autoclose = false
       io = server_cast(io)
       set_server_sockopt(io, listener_opts[sock_name(io)])
-      logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
+      logger.info "inherited addr=#{sock_name(io)} fd=#{io.fileno}"
       io
     end
 
-- 
EW

  reply	other threads:[~2015-06-27  4:01 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <16714105.fRFKlev1fp@debstor>
     [not found] ` <3137844.nsIDTWmvl4@debstor>
     [not found]   ` <CAAB-Kc=s-uSK83PcfDsybPRiYvTaLVxcu2x4eWDPaTf27AJ_XQ@mail.gmail.com>
     [not found]     ` <1810167.FXDESjsu8Z@debstor>
2015-06-24 23:26       ` unicorn: native systemd service Eric Wong
2015-06-25 23:43         ` Dmitry Smirnov
2015-06-25 23:49           ` Eric Wong
     [not found]   ` <20150625083118.GA22140@luke.ws.skroutz.gr>
2015-06-25 23:26     ` [DRE-maint] " Eric Wong
2015-06-26 11:41       ` Christos Trochalakis
2015-06-27  3:05         ` Eric Wong
2015-06-27  4:01           ` Eric Wong [this message]
2015-06-30  8:47             ` [RFC] emulate sd_listen_fds for systemd support Christos Trochalakis
2015-07-05  0:27               ` [PATCH v2] " Eric Wong
2015-06-30  9:20           ` [DRE-maint] unicorn: native systemd service Christos Trochalakis
2015-06-30 17:30             ` Eric Wong
2015-07-08 13:08               ` Christos Trochalakis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150627040136.GA13070@dcvr.yhbt.net \
    --to=e@80x24.org \
    --cc=375gnu@gmail.com \
    --cc=onlyjob@debian.org \
    --cc=unicorn-public@bogomips.org \
    --cc=unicorn@packages.debian.org \
    --cc=yatiohi@ideopolis.gr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).