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