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