]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/sd_listen_fds.xml
Merge pull request #8700 from keszybz/hibernation
[thirdparty/systemd.git] / man / sd_listen_fds.xml
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">
4
5 <!--
6 SPDX-License-Identifier: LGPL-2.1+
7
8 This file is part of systemd.
9
10 Copyright 2010 Lennart Poettering
11 -->
12
13 <refentry id="sd_listen_fds"
14 xmlns:xi="http://www.w3.org/2001/XInclude">
15
16 <refentryinfo>
17 <title>sd_listen_fds</title>
18 <productname>systemd</productname>
19
20 <authorgroup>
21 <author>
22 <contrib>Developer</contrib>
23 <firstname>Lennart</firstname>
24 <surname>Poettering</surname>
25 <email>lennart@poettering.net</email>
26 </author>
27 </authorgroup>
28 </refentryinfo>
29
30 <refmeta>
31 <refentrytitle>sd_listen_fds</refentrytitle>
32 <manvolnum>3</manvolnum>
33 </refmeta>
34
35 <refnamediv>
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>
40 </refnamediv>
41
42 <refsynopsisdiv>
43 <funcsynopsis>
44 <funcsynopsisinfo>#include &lt;systemd/sd-daemon.h&gt;</funcsynopsisinfo>
45
46 <funcsynopsisinfo>#define SD_LISTEN_FDS_START 3</funcsynopsisinfo>
47
48 <funcprototype>
49 <funcdef>int <function>sd_listen_fds</function></funcdef>
50 <paramdef>int <parameter>unset_environment</parameter></paramdef>
51 </funcprototype>
52
53 <funcprototype>
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>
57 </funcprototype>
58 </funcsynopsis>
59 </refsynopsisdiv>
60
61 <refsect1>
62 <title>Description</title>
63
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>
72
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
75 socket unit file (see
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
79 functions
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>
91
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>
95
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
99 manager using
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
103 removed.</para>
104
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>
113
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
123 files, see
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>
143
144 <para>Under specific conditions, the following automatic file
145 descriptor names are returned:
146
147 <table>
148 <title>
149 <command>Special names</command>
150 </title>
151
152 <tgroup cols='2'>
153 <thead>
154 <row>
155 <entry>Name</entry>
156 <entry>Description</entry>
157 </row>
158 </thead>
159 <tbody>
160 <row>
161 <entry><literal>unknown</literal></entry>
162 <entry>The process received no name for the specific file descriptor from the service manager.</entry>
163 </row>
164
165 <row>
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>
168 </row>
169
170 <row>
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>
173 </row>
174 </tbody>
175 </tgroup>
176 </table>
177 </para>
178 </refsect1>
179
180 <refsect1>
181 <title>Return Value</title>
182
183 <para>On failure, these calls returns a negative errno-style error
184 code. If
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
190 descriptor 3.</para>
191 </refsect1>
192
193 <refsect1>
194 <title>Notes</title>
195
196 <xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
197
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
207 set.</para>
208 </refsect1>
209
210 <refsect1>
211 <title>Environment</title>
212
213 <variablelist class='environment-variables'>
214 <varlistentry>
215 <term><varname>$LISTEN_PID</varname></term>
216 <term><varname>$LISTEN_FDS</varname></term>
217 <term><varname>$LISTEN_FDNAMES</varname></term>
218
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>
225 </varlistentry>
226 </variablelist>
227 </refsect1>
228
229 <refsect1>
230 <title>See Also</title>
231
232 <para>
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>
243 </para>
244 </refsect1>
245
246 </refentry>