Skip to content

Add file descriptor store support to pystemd.daemon - #130

Merged
aleivag merged 4 commits into
systemd:mainfrom
aleivag:daemon-fd-store
Nov 18, 2026
Merged

aleivag merged 4 commits into
systemd:mainfrom
aleivag:daemon-fd-store

Conversation

@aleivag

@aleivag aleivag commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Support for systemd's file descriptor store in pystemd.daemon:

  • notify(unset_environment, *states, fds=None, **kwstates): when fds is given, call sd_pid_notify_with_fds() so the fds travel with the message; without it (or with an empty list), sd_notify() exactly as before. fds is keyword-only; it takes a single fd or a list, each an int or an object with fileno(). fds=0 sends fd 0, and str/bytes raise TypeError (bytes would otherwise iterate as ints and send the wrong fds).
  • listen_fds_with_names(unset_environment=0): sd_listen_fds_with_names(), returned as {name: [fd, ...]}. This is how a restarted service gets its stored fds back (and how named socket activation fds are found). Names aren't unique, hence the lists.
  • store_fd(name, fds, unset_environment=False, **kwstates): the same as notify(unset_environment, fds=fds, fdstore=1, fdname=name, **kwstates).
  • store_fds({name: fds, ...}, unset_environment=False, **kwstates): several names, one message per name (FDNAME= covers a whole message, so it's not atomic); unset_environment and the extra states only go with the last message.

Both helpers validate names first (1-255 printable ASCII characters, no :) and raise ValueError: systemd silently ignores an invalid FDNAME=, so the fds would just never be stored.

Tested on systemd 259: a Type=notify service with FileDescriptorStoreMax=1 stores a memfd holding a SQLite database with notify(fds=...) and gets it back with listen_fds_with_names()["db"][0] across four restarts; two fds stored under the same FDNAME both come back; store_fds({"a": fd, "b": [fd1, fd2], "c": fd}, unset_environment=True, ready=1, status="stored") stores all four fds under three names and activates the unit. New unit tests in tests/test_daemon.py.

Lint: ruff 0.9.6 (as pinned in .pre-commit-config.yaml), isort and pyrefly pass. The formatting check fails on every PR right now for an unrelated reason: CI installs the latest ruff, which flags existing files in examples/.

- notify(unset_environment, *states, fds=None, **kwstates): when `fds`
  is given, call sd_pid_notify_with_fds() so the fds travel with the
  message; without it (or with an empty list), sd_notify() exactly as
  before. `fds` is keyword-only, and there is no FDS= notify state for
  it to shadow. It takes a single fd or a list of them; each fd is an int
  or an object with fileno(). fds=0 sends fd 0 (it is not "no fds"), and
  str/bytes are rejected with TypeError: bytes would otherwise iterate
  as ints and silently send the wrong fds.
  e.g. notify(False, fds=[memfd], fdstore=1, fdname="db") hands the
  memfd to systemd's file descriptor store.
- listen_fds_with_names(unset_environment=0): sd_listen_fds_with_names(),
  returned as {name: [fd, ...]}; this is how a restarted service gets
  its stored fds back (and how named socket activation fds are found).
  Names are not unique (a socket unit with several ListenStream= passes
  all its fds under one name; FDNAME= can repeat), hence the lists.

Tested on systemd 259: a Type=notify service with FileDescriptorStoreMax=1
stores a memfd holding a SQLite database with notify(fds=...) and gets
it back with listen_fds_with_names()["db"][0] across four restarts;
fds=<int> lands in the store too, and two fds stored under the same
FDNAME both come back (LISTEN_FDNAMES=same:same).
Helpers for systemd's file descriptor store, on top of notify(fds=...):

- store_fd(name, fds, unset_environment=False, **kwstates): hand a single
  fd or a list of fds to the store under `name`; the same as
  notify(unset_environment, fds=fds, fdstore=1, fdname=name, **kwstates).
- store_fds({name: fds, ...}, unset_environment=False, **kwstates): several
  names at once. FDNAME= covers a whole notify message, so this sends one
  message per name (not atomic); unset_environment and the extra states
  (e.g. ready=1) only go with the last message, since unsetting
  $NOTIFY_SOCKET earlier would make the remaining sends no-ops. Returns
  the lowest notify() result.

unset_environment can be passed positionally (like notify()) or as a
keyword, for readability: store_fds({...}, unset_environment=True).

Both validate names first (1-255 printable ASCII characters, no ':', as
LISTEN_FDNAMES is ':'-separated) and raise ValueError: systemd ignores an
invalid FDNAME= silently, so the fds would just never be stored.

Together with listen_fds_with_names() -> {name: [fd, ...]}, storing and
getting fds back is a dict both ways.

Tested on systemd 259: store_fds({"a": fd, "b": [fd1, fd2], "c": fd},
unset_environment=True, ready=1, status="stored") stores all four fds under three names and
activates the unit with the status set; store_fd() is used by a memfd,
chat-server (listening + client sockets), pty and pidfd example that all
get their fds back across restarts.
@aleivag
aleivag merged commit ebb4924 into systemd:main Nov 18, 2026
49 checks passed
@aleivag
aleivag deleted the daemon-fd-store branch November 18, 2026 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant