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