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