80x24.org misc. Free Software, open data formats/protocols discussion
 help / color / mirror / Atom feed
* how-to: inherit FDs w/o sd_listen_fds(3) in pure Ruby
@ 2015-10-30 22:26 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-10-30 22:26 UTC (permalink / raw)
  To: ruby-talk; +Cc: misc

Inevitably systemd is taking over; and socket activation is a great
idea[1].  People writing daemons might want to take advantage of socket
activation, regardless of whether they target systemd or not.

Since maintaining C or FFI extensions can lead to portability and
installation issues, I'd much rather use only standard features in Ruby.

Anyways, I consider this snippet[2] too short to release as a gem:

    # see the sd_listen_fds(3) manpage for details
    sd_pid, sd_fds = ENV.values_at('LISTEN_PID', 'LISTEN_FDS')
    if sd_pid.to_i == $$ # n.b. $$ can never be zero
      # 3 = SD_LISTEN_FDS_START
      inherited = (3...(3 + sd_fds.to_i)).map { |fd| Socket.for_fd(fd) }
    end

For non-systemd users, it is completely innocuous other than taking
around 2K of RAM for the bytecode.  It won't do anything if the
environment variables are unset.  This can conceivably work on any
POSIX/Unix-like system; including ones not supported by systemd itself.

You can see that even on non-systemd hosts, you can take advantage of
using LISTEN_PID and LISTEN_FDS in the environment to share FDs for
socket activation.

You may choose to delete the LISTEN_PID/LISTEN_FDS variables from ENV
after using them, but I prefer to leave them for debugging.

Keep in mind old versions of the sd_listen_fds(3) manpage stated this:

	> This function is provided by the reference
	> implementation of APIs for new-style daemons and
	> distributed with the systemd package. The algorithm it
	> implements is simple, and can easily be reimplemented
	> in daemons if it is important to support this
	> interface without using the reference
	> implementation.

But the systemd developers nowadays prefer people link to the shared
library; which may not be easy or practical for scripting languages.

CC0 / Public domain declaration:

  To the extent possible under law, Eric Wong has waived all copyright
  and related or neighboring rights to the contents of this message.
  If anybody actually wants this released as a gem, they can do
  it themselves; preferably without mentioning or crediting me at all.

[1] Disclaimer: Do not consider this an endorsement of systemd.
[2] Disclaimer #2: Any code snippet you use may eat your hard drive
    (or children).  There is no warranty.

P.S. You can probably link to this post via:
http://mid.gmane.org/20151030-inherit-fds-w-o-sd_listen_fds-in-pure@ruby
(or link via the Message-ID via any other mail archival service)

P.P.S: Ruby 2.3 fixes a bug which prevented Ruby itself from
       testing this feature independently of systemd:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/70390

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-10-30 22:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-30 22:26 how-to: inherit FDs w/o sd_listen_fds(3) in pure Ruby Eric Wong

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).