80x24.org misc. Free Software, open data formats/protocols discussion
 help / color / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: ruby-talk@ruby-lang.org
Cc: misc@80x24.org
Subject: how-to: inherit FDs w/o sd_listen_fds(3) in pure Ruby
Date: Fri, 30 Oct 2015 22:26:06 +0000
Message-ID: <20151030-inherit-fds-w-o-sd_listen_fds-in-pure@ruby> (raw)

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
      inherited = (3...(3 + sd_fds.to_i)).map { |fd| Socket.for_fd(fd) }

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:
(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:

                 reply index

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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:

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

  git send-email \
    --in-reply-to=20151030-inherit-fds-w-o-sd_listen_fds-in-pure@ruby \
    --to=normalperson@yhbt.net \
    --cc=misc@80x24.org \
    --cc=ruby-talk@ruby-lang.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

80x24.org misc. Free Software, open data formats/protocols discussion

Archives are clonable:
	git clone --mirror https://80x24.org/misc
	git clone --mirror http://ou63pmih66umazou.onion/misc

Example config snippet for mirrors

Newsgroups are available over NNTP:

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git