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 This file is part of systemd.
10 Copyright 2010 Lennart Poettering
13 <refentry id="sd_listen_fds"
14 xmlns:xi="http://www.w3.org/2001/XInclude">
17 <title>sd_listen_fds</title>
18 <productname>systemd</productname>
22 <contrib>Developer</contrib>
23 <firstname>Lennart</firstname>
24 <surname>Poettering</surname>
25 <email>lennart@poettering.net</email>
31 <refentrytitle>sd_listen_fds</refentrytitle>
32 <manvolnum>3</manvolnum>
36 <refname>sd_listen_fds</refname>
37 <refname>sd_listen_fds_with_names</refname>
38 <refname>SD_LISTEN_FDS_START</refname>
39 <refpurpose>Check for file descriptors passed by the system manager</refpurpose>
44 <funcsynopsisinfo>#include <systemd/sd-daemon.h></funcsynopsisinfo>
46 <funcsynopsisinfo>#define SD_LISTEN_FDS_START 3</funcsynopsisinfo>
49 <funcdef>int <function>sd_listen_fds</function></funcdef>
50 <paramdef>int <parameter>unset_environment</parameter></paramdef>
54 <funcdef>int <function>sd_listen_fds_with_names</function></funcdef>
55 <paramdef>int <parameter>unset_environment</parameter></paramdef>
56 <paramdef>char*** <parameter>names</parameter></paramdef>
62 <title>Description</title>
64 <para><function>sd_listen_fds()</function> may be invoked by a
65 daemon to check for file descriptors passed by the service manager as
66 part of the socket-based activation logic. It returns the number
67 of received file descriptors. If no file descriptors have been
68 received, zero is returned. The first file descriptor may be found
69 at file descriptor number 3
70 (i.e. <constant>SD_LISTEN_FDS_START</constant>), the remaining
71 descriptors follow at 4, 5, 6, …, if any.</para>
73 <para>If a daemon receives more than one file descriptor, they
74 will be passed in the same order as configured in the systemd
76 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>
77 for details). Nonetheless, it is recommended to verify the correct
78 socket types before using them. To simplify this checking, the
80 <citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
81 <citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
82 <citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
83 <citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry>
84 are provided. In order to maximize flexibility, it is recommended
85 to make these checks as loose as possible without allowing
86 incorrect setups. i.e. often, the actual port number a socket is
87 bound to matters little for the service to work, hence it should
88 not be verified. On the other hand, whether a socket is a datagram
89 or stream socket matters a lot for the most common program logics
90 and should be checked.</para>
92 <para>This function call will set the FD_CLOEXEC flag for all
93 passed file descriptors to avoid further inheritance to children
94 of the calling process.</para>
96 <para>If multiple socket units activate the same service, the order
97 of the file descriptors passed to its main process is undefined.
98 If additional file descriptors have been passed to the service
100 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s
101 <literal>FDSTORE=1</literal> messages, these file descriptors are
102 passed last, in arbitrary order, and with duplicates
105 <para>If the <parameter>unset_environment</parameter> parameter is
106 non-zero, <function>sd_listen_fds()</function> will unset the
107 <varname>$LISTEN_FDS</varname>, <varname>$LISTEN_PID</varname> and
108 <varname>$LISTEN_FDNAMES</varname> environment variables before
109 returning (regardless of whether the function call itself
110 succeeded or not). Further calls to
111 <function>sd_listen_fds()</function> will then return zero, but the
112 variables are no longer inherited by child processes.</para>
114 <para><function>sd_listen_fds_with_names()</function> is like
115 <function>sd_listen_fds()</function>, but optionally also returns
116 an array of strings with identification names for the passed file
117 descriptors, if that is available and the
118 <parameter>names</parameter> parameter is non-NULL. This
119 information is read from the <varname>$LISTEN_FDNAMES</varname>
120 variable, which may contain a colon-separated list of names. For
121 socket-activated services, these names may be configured with the
122 <varname>FileDescriptorName=</varname> setting in socket unit
124 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>
125 for details. For file descriptors pushed into the file descriptor
126 store (see above), the name is set via the
127 <varname>FDNAME=</varname> field transmitted via
128 <function>sd_pid_notify_with_fds()</function>. The primary usecase
129 for these names are services which accept a variety of file
130 descriptors which are not recognizable with functions like
131 <function>sd_is_socket()</function> alone, and thus require
132 identification via a name. It is recommended to rely on named file
133 descriptors only if identification via
134 <function>sd_is_socket()</function> and related calls is not
135 sufficient. Note that the names used are not unique in any
136 way. The returned array of strings has as many entries as file
137 descriptors have been received, plus a final NULL pointer
138 terminating the array. The caller needs to free the array itself
139 and each of its elements with libc's <function>free()</function>
140 call after use. If the <parameter>names</parameter> parameter is
141 NULL, the call is entirely equivalent to
142 <function>sd_listen_fds()</function>.</para>
144 <para>Under specific conditions, the following automatic file
145 descriptor names are returned:
149 <command>Special names</command>
156 <entry>Description</entry>
161 <entry><literal>unknown</literal></entry>
162 <entry>The process received no name for the specific file descriptor from the service manager.</entry>
166 <entry><literal>stored</literal></entry>
167 <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>
171 <entry><literal>connection</literal></entry>
172 <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>
181 <title>Return Value</title>
183 <para>On failure, these calls returns a negative errno-style error
185 <varname>$LISTEN_FDS</varname>/<varname>$LISTEN_PID</varname> was
186 not set or was not correctly set for this daemon and hence no file
187 descriptors were received, 0 is returned. Otherwise, the number of
188 file descriptors passed is returned. The application may find them
189 starting with file descriptor SD_LISTEN_FDS_START, i.e. file
196 <xi:include href="libelogind-pkgconfig.xml" xpointer="pkgconfig-text"/>
198 <para>Internally, <function>sd_listen_fds()</function> checks
199 whether the <varname>$LISTEN_PID</varname> environment variable
200 equals the daemon PID. If not, it returns immediately. Otherwise,
201 it parses the number passed in the <varname>$LISTEN_FDS</varname>
202 environment variable, then sets the FD_CLOEXEC flag for the parsed
203 number of file descriptors starting from SD_LISTEN_FDS_START.
204 Finally, it returns the parsed
205 number. <function>sd_listen_fds_with_names()</function> does the
206 same but also parses <varname>$LISTEN_FDNAMES</varname> if
211 <title>Environment</title>
213 <variablelist class='environment-variables'>
215 <term><varname>$LISTEN_PID</varname></term>
216 <term><varname>$LISTEN_FDS</varname></term>
217 <term><varname>$LISTEN_FDNAMES</varname></term>
219 <listitem><para>Set by the service manager for supervised
220 processes that use socket-based activation. This environment
221 variable specifies the data
222 <function>sd_listen_fds()</function> and
223 <function>sd_listen_fds_with_names()</function> parses. See
224 above for details.</para></listitem>
230 <title>See Also</title>
233 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
234 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
235 <citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
236 <citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
237 <citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
238 <citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
239 <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
240 <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
241 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
242 <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>