1 <?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 SPDX-License-Identifier: LGPL-2.1+
8 Copyright 2010 Lennart Poettering
11 <refentry id="sd_listen_fds"
12 xmlns:xi="http://www.w3.org/2001/XInclude">
15 <title>sd_listen_fds</title>
16 <productname>systemd</productname>
20 <contrib>Developer</contrib>
21 <firstname>Lennart</firstname>
22 <surname>Poettering</surname>
23 <email>lennart@poettering.net</email>
29 <refentrytitle>sd_listen_fds</refentrytitle>
30 <manvolnum>3</manvolnum>
34 <refname>sd_listen_fds</refname>
35 <refname>sd_listen_fds_with_names</refname>
36 <refname>SD_LISTEN_FDS_START</refname>
37 <refpurpose>Check for file descriptors passed by the system manager</refpurpose>
42 <funcsynopsisinfo>#include <systemd/sd-daemon.h></funcsynopsisinfo>
44 <funcsynopsisinfo>#define SD_LISTEN_FDS_START 3</funcsynopsisinfo>
47 <funcdef>int <function>sd_listen_fds</function></funcdef>
48 <paramdef>int <parameter>unset_environment</parameter></paramdef>
52 <funcdef>int <function>sd_listen_fds_with_names</function></funcdef>
53 <paramdef>int <parameter>unset_environment</parameter></paramdef>
54 <paramdef>char*** <parameter>names</parameter></paramdef>
60 <title>Description</title>
62 <para><function>sd_listen_fds()</function> may be invoked by a
63 daemon to check for file descriptors passed by the service manager as
64 part of the socket-based activation logic. It returns the number
65 of received file descriptors. If no file descriptors have been
66 received, zero is returned. The first file descriptor may be found
67 at file descriptor number 3
68 (i.e. <constant>SD_LISTEN_FDS_START</constant>), the remaining
69 descriptors follow at 4, 5, 6, …, if any.</para>
71 <para>If a daemon receives more than one file descriptor, they
72 will be passed in the same order as configured in the systemd
74 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>
75 for details). Nonetheless, it is recommended to verify the correct
76 socket types before using them. To simplify this checking, the
78 <citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
79 <citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
80 <citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
81 <citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry>
82 are provided. In order to maximize flexibility, it is recommended
83 to make these checks as loose as possible without allowing
84 incorrect setups. i.e. often, the actual port number a socket is
85 bound to matters little for the service to work, hence it should
86 not be verified. On the other hand, whether a socket is a datagram
87 or stream socket matters a lot for the most common program logics
88 and should be checked.</para>
90 <para>This function call will set the FD_CLOEXEC flag for all
91 passed file descriptors to avoid further inheritance to children
92 of the calling process.</para>
94 <para>If multiple socket units activate the same service, the order
95 of the file descriptors passed to its main process is undefined.
96 If additional file descriptors have been passed to the service
98 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s
99 <literal>FDSTORE=1</literal> messages, these file descriptors are
100 passed last, in arbitrary order, and with duplicates
103 <para>If the <parameter>unset_environment</parameter> parameter is
104 non-zero, <function>sd_listen_fds()</function> will unset the
105 <varname>$LISTEN_FDS</varname>, <varname>$LISTEN_PID</varname> and
106 <varname>$LISTEN_FDNAMES</varname> environment variables before
107 returning (regardless of whether the function call itself
108 succeeded or not). Further calls to
109 <function>sd_listen_fds()</function> will then return zero, but the
110 variables are no longer inherited by child processes.</para>
112 <para><function>sd_listen_fds_with_names()</function> is like
113 <function>sd_listen_fds()</function>, but optionally also returns
114 an array of strings with identification names for the passed file
115 descriptors, if that is available and the
116 <parameter>names</parameter> parameter is non-NULL. This
117 information is read from the <varname>$LISTEN_FDNAMES</varname>
118 variable, which may contain a colon-separated list of names. For
119 socket-activated services, these names may be configured with the
120 <varname>FileDescriptorName=</varname> setting in socket unit
122 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>
123 for details. For file descriptors pushed into the file descriptor
124 store (see above), the name is set via the
125 <varname>FDNAME=</varname> field transmitted via
126 <function>sd_pid_notify_with_fds()</function>. The primary usecase
127 for these names are services which accept a variety of file
128 descriptors which are not recognizable with functions like
129 <function>sd_is_socket()</function> alone, and thus require
130 identification via a name. It is recommended to rely on named file
131 descriptors only if identification via
132 <function>sd_is_socket()</function> and related calls is not
133 sufficient. Note that the names used are not unique in any
134 way. The returned array of strings has as many entries as file
135 descriptors have been received, plus a final NULL pointer
136 terminating the array. The caller needs to free the array itself
137 and each of its elements with libc's <function>free()</function>
138 call after use. If the <parameter>names</parameter> parameter is
139 NULL, the call is entirely equivalent to
140 <function>sd_listen_fds()</function>.</para>
142 <para>Under specific conditions, the following automatic file
143 descriptor names are returned:
147 <command>Special names</command>
154 <entry>Description</entry>
159 <entry><literal>unknown</literal></entry>
160 <entry>The process received no name for the specific file descriptor from the service manager.</entry>
164 <entry><literal>stored</literal></entry>
165 <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>
169 <entry><literal>connection</literal></entry>
170 <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>
179 <title>Return Value</title>
181 <para>On failure, these calls returns a negative errno-style error
183 <varname>$LISTEN_FDS</varname>/<varname>$LISTEN_PID</varname> was
184 not set or was not correctly set for this daemon and hence no file
185 descriptors were received, 0 is returned. Otherwise, the number of
186 file descriptors passed is returned. The application may find them
187 starting with file descriptor SD_LISTEN_FDS_START, i.e. file
194 <xi:include href="libelogind-pkgconfig.xml" xpointer="pkgconfig-text"/>
196 <para>Internally, <function>sd_listen_fds()</function> checks
197 whether the <varname>$LISTEN_PID</varname> environment variable
198 equals the daemon PID. If not, it returns immediately. Otherwise,
199 it parses the number passed in the <varname>$LISTEN_FDS</varname>
200 environment variable, then sets the FD_CLOEXEC flag for the parsed
201 number of file descriptors starting from SD_LISTEN_FDS_START.
202 Finally, it returns the parsed
203 number. <function>sd_listen_fds_with_names()</function> does the
204 same but also parses <varname>$LISTEN_FDNAMES</varname> if
209 <title>Environment</title>
211 <variablelist class='environment-variables'>
213 <term><varname>$LISTEN_PID</varname></term>
214 <term><varname>$LISTEN_FDS</varname></term>
215 <term><varname>$LISTEN_FDNAMES</varname></term>
217 <listitem><para>Set by the service manager for supervised
218 processes that use socket-based activation. This environment
219 variable specifies the data
220 <function>sd_listen_fds()</function> and
221 <function>sd_listen_fds_with_names()</function> parses. See
222 above for details.</para></listitem>
228 <title>See Also</title>
231 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
232 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
233 <citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
234 <citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
235 <citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
236 <citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
237 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
238 <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
239 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
240 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>