80x24.org misc. Free Software, open data formats/protocols discussion
 help / 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; 1+ messages 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
      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:

^ permalink raw reply	[flat|threaded] 1+ messages in thread

only message in thread, back to index

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

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

Newsgroups are available over NNTP:

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

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