]>
Commit | Line | Data |
---|---|---|
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"> |
db9ecf05 | 4 | <!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
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 <systemd/sd-daemon.h></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 | ||
c896eb7a | 48 | <para><function>sd_listen_fds()</function> may be invoked by a daemon to check for file descriptors |
0959847a LP |
49 | passed by the service manager as part of the socket-based activation and file descriptor store logic. It |
50 | returns the number of received file descriptors. If no file descriptors have been received, zero is | |
51 | returned. The first file descriptor may be found at file descriptor number 3 | |
52 | (i.e. <constant>SD_LISTEN_FDS_START</constant>), the remaining descriptors follow at 4, 5, 6, …, if | |
53 | any.</para> | |
c896eb7a LP |
54 | |
55 | <para>The file descriptors passed this way may be closed at will by the processes receiving them: it's up | |
56 | to the processes themselves to close them after use or whether to leave them open until the process exits | |
57 | (in which case the kernel closes them automatically). Note that the file descriptors received by daemons | |
58 | are duplicates of the file descriptors the service manager originally allocated and bound and of which it | |
a6f44d61 | 59 | continuously keeps a copy (except if <varname>Accept=yes</varname> is used). This means any socket option |
77e289ab ZJS |
60 | changes and other changes made to the sockets will be visible to the service manager too. Most |
61 | importantly this means it's generally not a good idea to invoke <citerefentry | |
c896eb7a LP |
62 | project='man-pages'><refentrytitle>shutdown</refentrytitle><manvolnum>2</manvolnum></citerefentry> on |
63 | such sockets, since it will shut down communication on the file descriptor the service manager holds for | |
77e289ab ZJS |
64 | the same socket too. Also note that if a daemon is restarted (and its associated sockets are not) it will |
65 | receive file descriptors to the very same sockets as the earlier invocations, thus all socket options | |
66 | applied then will still apply.</para> | |
798d3a52 | 67 | |
df2f5817 LP |
68 | <para>If a daemon receives more than one file descriptor, they will be passed in the same order as |
69 | configured in the systemd socket unit file (see | |
70 | <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry> for | |
71 | details) — if there's only one such file (see below). Nonetheless, it is recommended to verify the | |
72 | correct socket types before using them. To simplify this checking, the functions | |
798d3a52 ZJS |
73 | <citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry>, |
74 | <citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
75 | <citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
df2f5817 LP |
76 | <citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry> are |
77 | provided. In order to maximize flexibility, it is recommended to make these checks as loose as possible | |
78 | without allowing incorrect setups. i.e. often, the actual port number a socket is bound to matters little | |
79 | for the service to work, hence it should not be verified. On the other hand, whether a socket is a | |
80 | datagram or stream socket matters a lot for the most common program logics and should be checked.</para> | |
798d3a52 | 81 | |
0959847a LP |
82 | <para>This function call will set the <constant>FD_CLOEXEC</constant> flag for all passed file |
83 | descriptors to avoid further inheritance to children of the calling process.</para> | |
798d3a52 | 84 | |
b938cb90 | 85 | <para>If multiple socket units activate the same service, the order |
798d3a52 ZJS |
86 | of the file descriptors passed to its main process is undefined. |
87 | If additional file descriptors have been passed to the service | |
88 | manager using | |
89 | <citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>'s | |
90 | <literal>FDSTORE=1</literal> messages, these file descriptors are | |
91 | passed last, in arbitrary order, and with duplicates | |
92 | removed.</para> | |
8dd4c05b LP |
93 | |
94 | <para>If the <parameter>unset_environment</parameter> parameter is | |
95 | non-zero, <function>sd_listen_fds()</function> will unset the | |
96 | <varname>$LISTEN_FDS</varname>, <varname>$LISTEN_PID</varname> and | |
97 | <varname>$LISTEN_FDNAMES</varname> environment variables before | |
98 | returning (regardless of whether the function call itself | |
99 | succeeded or not). Further calls to | |
100 | <function>sd_listen_fds()</function> will then return zero, but the | |
101 | variables are no longer inherited by child processes.</para> | |
102 | ||
103 | <para><function>sd_listen_fds_with_names()</function> is like | |
b938cb90 | 104 | <function>sd_listen_fds()</function>, but optionally also returns |
8dd4c05b | 105 | an array of strings with identification names for the passed file |
b938cb90 | 106 | descriptors, if that is available and the |
6b44ad0b | 107 | <parameter>names</parameter> parameter is non-<constant>NULL</constant>. This |
8dd4c05b LP |
108 | information is read from the <varname>$LISTEN_FDNAMES</varname> |
109 | variable, which may contain a colon-separated list of names. For | |
110 | socket-activated services, these names may be configured with the | |
111 | <varname>FileDescriptorName=</varname> setting in socket unit | |
112 | files, see | |
113 | <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry> | |
114 | for details. For file descriptors pushed into the file descriptor | |
b938cb90 | 115 | store (see above), the name is set via the |
8dd4c05b | 116 | <varname>FDNAME=</varname> field transmitted via |
7227dd81 | 117 | <function>sd_pid_notify_with_fds()</function>. The primary use case |
8dd4c05b LP |
118 | for these names are services which accept a variety of file |
119 | descriptors which are not recognizable with functions like | |
120 | <function>sd_is_socket()</function> alone, and thus require | |
121 | identification via a name. It is recommended to rely on named file | |
122 | descriptors only if identification via | |
123 | <function>sd_is_socket()</function> and related calls is not | |
124 | sufficient. Note that the names used are not unique in any | |
125 | way. The returned array of strings has as many entries as file | |
6b44ad0b | 126 | descriptors have been received, plus a final <constant>NULL</constant> pointer |
8dd4c05b | 127 | terminating the array. The caller needs to free the array itself |
ba6bfc0d | 128 | and each of its elements with libc's <function>free()</function> |
8dd4c05b | 129 | call after use. If the <parameter>names</parameter> parameter is |
6b44ad0b | 130 | <constant>NULL</constant>, the call is entirely equivalent to |
8dd4c05b LP |
131 | <function>sd_listen_fds()</function>.</para> |
132 | ||
b938cb90 | 133 | <para>Under specific conditions, the following automatic file |
8dd4c05b LP |
134 | descriptor names are returned: |
135 | ||
136 | <table> | |
137 | <title> | |
138 | <command>Special names</command> | |
139 | </title> | |
140 | ||
141 | <tgroup cols='2'> | |
142 | <thead> | |
143 | <row> | |
144 | <entry>Name</entry> | |
145 | <entry>Description</entry> | |
146 | </row> | |
147 | </thead> | |
148 | <tbody> | |
149 | <row> | |
150 | <entry><literal>unknown</literal></entry> | |
151 | <entry>The process received no name for the specific file descriptor from the service manager.</entry> | |
152 | </row> | |
153 | ||
154 | <row> | |
155 | <entry><literal>stored</literal></entry> | |
156 | <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> | |
157 | </row> | |
158 | ||
159 | <row> | |
160 | <entry><literal>connection</literal></entry> | |
161 | <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> | |
162 | </row> | |
163 | </tbody> | |
164 | </tgroup> | |
165 | </table> | |
166 | </para> | |
0959847a LP |
167 | |
168 | <para>For further information on the file descriptor store see the <ulink | |
169 | url="https://systemd.io/FILE_DESCRIPTOR_STORE">File Descriptor Store</ulink> overview.</para> | |
798d3a52 ZJS |
170 | </refsect1> |
171 | ||
172 | <refsect1> | |
173 | <title>Return Value</title> | |
174 | ||
8dd4c05b | 175 | <para>On failure, these calls returns a negative errno-style error |
798d3a52 ZJS |
176 | code. If |
177 | <varname>$LISTEN_FDS</varname>/<varname>$LISTEN_PID</varname> was | |
178 | not set or was not correctly set for this daemon and hence no file | |
179 | descriptors were received, 0 is returned. Otherwise, the number of | |
180 | file descriptors passed is returned. The application may find them | |
181 | starting with file descriptor SD_LISTEN_FDS_START, i.e. file | |
182 | descriptor 3.</para> | |
183 | </refsect1> | |
184 | ||
185 | <refsect1> | |
186 | <title>Notes</title> | |
187 | ||
188 | <xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/> | |
8c51e152 | 189 | <xi:include href="threads-aware.xml" xpointer="getenv"/> |
798d3a52 | 190 | |
8dd4c05b LP |
191 | <para>Internally, <function>sd_listen_fds()</function> checks |
192 | whether the <varname>$LISTEN_PID</varname> environment variable | |
193 | equals the daemon PID. If not, it returns immediately. Otherwise, | |
194 | it parses the number passed in the <varname>$LISTEN_FDS</varname> | |
798d3a52 ZJS |
195 | environment variable, then sets the FD_CLOEXEC flag for the parsed |
196 | number of file descriptors starting from SD_LISTEN_FDS_START. | |
8dd4c05b LP |
197 | Finally, it returns the parsed |
198 | number. <function>sd_listen_fds_with_names()</function> does the | |
199 | same but also parses <varname>$LISTEN_FDNAMES</varname> if | |
200 | set.</para> | |
788b7e76 MK |
201 | |
202 | <para>These functions are not designed for services that specify <varname>StandardInput=socket</varname> | |
203 | as the <varname>$LISTEN_FDS</varname> variable is not set in their environment.</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 | ||
69106f47 AK |
225 | <refsect1> |
226 | <title>History</title> | |
227 | <para><function>sd_listen_fds_with_names()</function> was added in version 227.</para> | |
228 | </refsect1> | |
229 | ||
798d3a52 ZJS |
230 | <refsect1> |
231 | <title>See Also</title> | |
232 | ||
13a69c12 DT |
233 | <para><simplelist type="inline"> |
234 | <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> | |
235 | <member><citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
236 | <member><citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
237 | <member><citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
238 | <member><citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
239 | <member><citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
240 | <member><citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
241 | <member><citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry></member> | |
242 | <member><citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry></member> | |
243 | <member><citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry></member> | |
244 | </simplelist></para> | |
798d3a52 | 245 | </refsect1> |
f9378423 LP |
246 | |
247 | </refentry> |