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

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

This inbox may be cloned and mirrored by anyone:

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

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 misc misc/ https://80x24.org/misc \
	public-inbox-index 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 http://ou63pmih66umazou.onion/public-inbox.git