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