1<?xml version='1.0'?>
2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3  "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
5
6<refentry id="sd_listen_fds"
7  xmlns:xi="http://www.w3.org/2001/XInclude">
8
9  <refentryinfo>
10    <title>sd_listen_fds</title>
11    <productname>systemd</productname>
12  </refentryinfo>
13
14  <refmeta>
15    <refentrytitle>sd_listen_fds</refentrytitle>
16    <manvolnum>3</manvolnum>
17  </refmeta>
18
19  <refnamediv>
20    <refname>sd_listen_fds</refname>
21    <refname>sd_listen_fds_with_names</refname>
22    <refname>SD_LISTEN_FDS_START</refname>
23    <refpurpose>Check for file descriptors passed by the system manager</refpurpose>
24  </refnamediv>
25
26  <refsynopsisdiv>
27    <funcsynopsis>
28      <funcsynopsisinfo>#include &lt;systemd/sd-daemon.h&gt;</funcsynopsisinfo>
29
30      <funcsynopsisinfo>#define SD_LISTEN_FDS_START 3</funcsynopsisinfo>
31
32      <funcprototype>
33        <funcdef>int <function>sd_listen_fds</function></funcdef>
34        <paramdef>int <parameter>unset_environment</parameter></paramdef>
35      </funcprototype>
36
37      <funcprototype>
38        <funcdef>int <function>sd_listen_fds_with_names</function></funcdef>
39        <paramdef>int <parameter>unset_environment</parameter></paramdef>
40        <paramdef>char*** <parameter>names</parameter></paramdef>
41      </funcprototype>
42    </funcsynopsis>
43  </refsynopsisdiv>
44
45  <refsect1>
46    <title>Description</title>
47
48    <para><function>sd_listen_fds()</function> may be invoked by a daemon to check for file descriptors
49    passed by the service manager as part of the socket-based activation logic. It returns the number of
50    received file descriptors. If no file descriptors have been received, zero is returned. The first file
51    descriptor may be found at file descriptor number 3 (i.e. <constant>SD_LISTEN_FDS_START</constant>), the
52    remaining descriptors follow at 4, 5, 6, …, if any.</para>
53
54    <para>The file descriptors passed this way may be closed at will by the processes receiving them: it's up
55    to the processes themselves to close them after use or whether to leave them open until the process exits
56    (in which case the kernel closes them automatically). Note that the file descriptors received by daemons
57    are duplicates of the file descriptors the service manager originally allocated and bound and of which it
58    continuously keeps a copy (except if <varname>Accept=yes</varname> is used). This means any socket option
59    changes and other changes made to the sockets will be visible to the service manager too. Most
60    importantly this means it's generally not a good idea to invoke <citerefentry
61    project='man-pages'><refentrytitle>shutdown</refentrytitle><manvolnum>2</manvolnum></citerefentry> on
62    such sockets, since it will shut down communication on the file descriptor the service manager holds for
63    the same socket too. Also note that if a daemon is restarted (and its associated sockets are not) it will
64    receive file descriptors to the very same sockets as the earlier invocations, thus all socket options
65    applied then will still apply.</para>
66
67    <para>If a daemon receives more than one file descriptor, they will be passed in the same order as
68    configured in the systemd socket unit file (see
69    <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
70    details) — if there's only one such file (see below).  Nonetheless, it is recommended to verify the
71    correct socket types before using them. To simplify this checking, the functions
72    <citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
73    <citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
74    <citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
75    <citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry> are
76    provided. In order to maximize flexibility, it is recommended to make these checks as loose as possible
77    without allowing incorrect setups. i.e. often, the actual port number a socket is bound to matters little
78    for the service to work, hence it should not be verified. On the other hand, whether a socket is a
79    datagram or stream socket matters a lot for the most common program logics and should be checked.</para>
80
81    <para>This function call will set the FD_CLOEXEC flag for all
82    passed file descriptors to avoid further inheritance to children
83    of the calling process.</para>
84
85    <para>If multiple socket units activate the same service, the order
86    of the file descriptors passed to its main process is undefined.
87    If additional file descriptors have been passed to the service
88    manager using
89    <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s
90    <literal>FDSTORE=1</literal> messages, these file descriptors are
91    passed last, in arbitrary order, and with duplicates
92    removed.</para>
93
94    <para>If the <parameter>unset_environment</parameter> parameter is
95    non-zero, <function>sd_listen_fds()</function> will unset the
96    <varname>$LISTEN_FDS</varname>, <varname>$LISTEN_PID</varname> and
97    <varname>$LISTEN_FDNAMES</varname> environment variables before
98    returning (regardless of whether the function call itself
99    succeeded or not). Further calls to
100    <function>sd_listen_fds()</function> will then return zero, but the
101    variables are no longer inherited by child processes.</para>
102
103    <para><function>sd_listen_fds_with_names()</function> is like
104    <function>sd_listen_fds()</function>, but optionally also returns
105    an array of strings with identification names for the passed file
106    descriptors, if that is available and the
107    <parameter>names</parameter> parameter is non-<constant>NULL</constant>. This
108    information is read from the <varname>$LISTEN_FDNAMES</varname>
109    variable, which may contain a colon-separated list of names. For
110    socket-activated services, these names may be configured with the
111    <varname>FileDescriptorName=</varname> setting in socket unit
112    files, see
113    <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>
114    for details. For file descriptors pushed into the file descriptor
115    store (see above), the name is set via the
116    <varname>FDNAME=</varname> field transmitted via
117    <function>sd_pid_notify_with_fds()</function>. The primary usecase
118    for these names are services which accept a variety of file
119    descriptors which are not recognizable with functions like
120    <function>sd_is_socket()</function> alone, and thus require
121    identification via a name. It is recommended to rely on named file
122    descriptors only if identification via
123    <function>sd_is_socket()</function> and related calls is not
124    sufficient. Note that the names used are not unique in any
125    way. The returned array of strings has as many entries as file
126    descriptors have been received, plus a final <constant>NULL</constant> pointer
127    terminating the array. The caller needs to free the array itself
128    and each of its elements with libc's <function>free()</function>
129    call after use. If the <parameter>names</parameter> parameter is
130    <constant>NULL</constant>, the call is entirely equivalent to
131    <function>sd_listen_fds()</function>.</para>
132
133    <para>Under specific conditions, the following automatic file
134    descriptor names are returned:
135
136    <table>
137      <title>
138         <command>Special names</command>
139      </title>
140
141      <tgroup cols='2'>
142        <thead>
143          <row>
144            <entry>Name</entry>
145            <entry>Description</entry>
146          </row>
147        </thead>
148        <tbody>
149          <row>
150            <entry><literal>unknown</literal></entry>
151            <entry>The process received no name for the specific file descriptor from the service manager.</entry>
152          </row>
153
154          <row>
155            <entry><literal>stored</literal></entry>
156            <entry>The file descriptor originates in the service manager's per-service file descriptor store, and the <varname>FDNAME=</varname> field was absent when the file descriptor was submitted to the service manager.</entry>
157          </row>
158
159          <row>
160            <entry><literal>connection</literal></entry>
161            <entry>The service was activated in per-connection style using <varname>Accept=yes</varname> in the socket unit file, and the file descriptor is the connection socket.</entry>
162          </row>
163        </tbody>
164      </tgroup>
165    </table>
166    </para>
167  </refsect1>
168
169  <refsect1>
170    <title>Return Value</title>
171
172    <para>On failure, these calls returns a negative errno-style error
173    code. If
174    <varname>$LISTEN_FDS</varname>/<varname>$LISTEN_PID</varname> was
175    not set or was not correctly set for this daemon and hence no file
176    descriptors were received, 0 is returned. Otherwise, the number of
177    file descriptors passed is returned. The application may find them
178    starting with file descriptor SD_LISTEN_FDS_START, i.e. file
179    descriptor 3.</para>
180  </refsect1>
181
182  <refsect1>
183    <title>Notes</title>
184
185    <xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
186
187    <para>Internally, <function>sd_listen_fds()</function> checks
188    whether the <varname>$LISTEN_PID</varname> environment variable
189    equals the daemon PID. If not, it returns immediately. Otherwise,
190    it parses the number passed in the <varname>$LISTEN_FDS</varname>
191    environment variable, then sets the FD_CLOEXEC flag for the parsed
192    number of file descriptors starting from SD_LISTEN_FDS_START.
193    Finally, it returns the parsed
194    number. <function>sd_listen_fds_with_names()</function> does the
195    same but also parses <varname>$LISTEN_FDNAMES</varname> if
196    set.</para>
197  </refsect1>
198
199  <refsect1>
200    <title>Environment</title>
201
202    <variablelist class='environment-variables'>
203      <varlistentry>
204        <term><varname>$LISTEN_PID</varname></term>
205        <term><varname>$LISTEN_FDS</varname></term>
206        <term><varname>$LISTEN_FDNAMES</varname></term>
207
208        <listitem><para>Set by the service manager for supervised
209        processes that use socket-based activation. This environment
210        variable specifies the data
211        <function>sd_listen_fds()</function> and
212        <function>sd_listen_fds_with_names()</function> parses. See
213        above for details.</para></listitem>
214      </varlistentry>
215    </variablelist>
216  </refsect1>
217
218  <refsect1>
219    <title>See Also</title>
220
221    <para>
222      <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
223      <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
224      <citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
225      <citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
226      <citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
227      <citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
228      <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
229      <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
230      <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
231      <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>
232    </para>
233  </refsect1>
234
235</refentry>
236