| 1 |
<?xml version='1.0'?> |
| 2 |
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
| 3 |
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
| 4 |
<!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
| 5 |
|
| 6 |
<refentry id="sd_listen_fds" |
| 7 |
xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 8 |
|
| 9 |
<refentryinfo> |
| 10 |
<title>sd_listen_fds</title> |
| 11 |
<productname>systemd</productname> |
| 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> |
| 21 |
<refname>sd_listen_fds_with_names</refname> |
| 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> |
| 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> |
| 42 |
</funcsynopsis> |
| 43 |
</refsynopsisdiv> |
| 44 |
|
| 45 |
<refsect1> |
| 46 |
<title>Description</title> |
| 47 |
|
| 48 |
<para><function>sd_listen_fds()</function> may be invoked by a daemon to check for file descriptors |
| 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> |
| 54 |
|
| 55 |
<para>The file descriptors passed this way may be closed at will by the processes receiving them: it is 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 |
| 59 |
continuously keeps a copy (except if <varname>Accept=yes</varname> is used). This means any socket option |
| 60 |
changes and other changes made to the sockets will be visible to the service manager too. Most |
| 61 |
importantly this means it is generally not a good idea to invoke <citerefentry |
| 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 |
| 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> |
| 67 |
|
| 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 |
| 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>, |
| 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> |
| 81 |
|
| 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> |
| 84 |
|
| 85 |
<para>If multiple socket units activate the same service, the order |
| 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> |
| 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>, |
| 97 |
<varname>$LISTEN_PIDFDID</varname>, and <varname>$LISTEN_FDNAMES</varname> |
| 98 |
environment variables before returning (regardless of whether the function |
| 99 |
call itself 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 |
| 104 |
<function>sd_listen_fds()</function>, but optionally also returns |
| 105 |
an array of strings with identification names for the passed file |
| 106 |
descriptors, if that is available and the |
| 107 |
<parameter>names</parameter> parameter is non-<constant>NULL</constant>. This |
| 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 |
| 115 |
store (see above), the name is set via the |
| 116 |
<varname>FDNAME=</varname> field transmitted via |
| 117 |
<function>sd_pid_notify_with_fds()</function>. The primary use case |
| 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 |
| 126 |
descriptors have been received, plus a final <constant>NULL</constant> pointer |
| 127 |
terminating the array. The caller needs to free the array itself |
| 128 |
and each of its elements with libc's <function>free()</function> |
| 129 |
call after use. If the <parameter>names</parameter> parameter is |
| 130 |
<constant>NULL</constant>, the call is entirely equivalent to |
| 131 |
<function>sd_listen_fds()</function>.</para> |
| 132 |
|
| 133 |
<para>Under specific conditions, the following automatic file |
| 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> |
| 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> |
| 170 |
</refsect1> |
| 171 |
|
| 172 |
<refsect1> |
| 173 |
<title>Return Value</title> |
| 174 |
|
| 175 |
<para>On failure, these calls returns a negative errno-style error |
| 176 |
code. If |
| 177 |
<varname>$LISTEN_FDS</varname>/<varname>$LISTEN_PID</varname> was |
| 178 |
not set, or one of those variables or <varname>$LISTEN_PIDFDID</varname> |
| 179 |
was not correctly set for this daemon, no file |
| 180 |
descriptors were received and 0 is returned. Otherwise, the number of |
| 181 |
file descriptors passed is returned. The application may find them |
| 182 |
starting with file descriptor SD_LISTEN_FDS_START, i.e. file |
| 183 |
descriptor 3.</para> |
| 184 |
</refsect1> |
| 185 |
|
| 186 |
<refsect1> |
| 187 |
<title>Notes</title> |
| 188 |
|
| 189 |
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/> |
| 190 |
<xi:include href="threads-aware.xml" xpointer="getenv"/> |
| 191 |
|
| 192 |
<para>Internally, <function>sd_listen_fds()</function> checks |
| 193 |
whether the <varname>$LISTEN_PID</varname> environment variable |
| 194 |
equals the daemon PID. If not, it returns immediately. Since version 259, |
| 195 |
if the <varname>$LISTEN_PIDFDID</varname> environment variable is set, |
| 196 |
it also checks whether it matches the inode of this process' pidfd. |
| 197 |
If both of these checks pass, |
| 198 |
it parses the number passed in the <varname>$LISTEN_FDS</varname> |
| 199 |
environment variable, then sets the FD_CLOEXEC flag for the parsed |
| 200 |
number of file descriptors starting from SD_LISTEN_FDS_START. |
| 201 |
Finally, it returns the parsed |
| 202 |
number. <function>sd_listen_fds_with_names()</function> does the |
| 203 |
same but also parses <varname>$LISTEN_FDNAMES</varname> if |
| 204 |
set.</para> |
| 205 |
|
| 206 |
<para>These functions are not designed for services that specify <varname>StandardInput=socket</varname> |
| 207 |
as the <varname>$LISTEN_FDS</varname> variable is not set in their environment.</para> |
| 208 |
</refsect1> |
| 209 |
|
| 210 |
<refsect1> |
| 211 |
<title>Environment</title> |
| 212 |
|
| 213 |
<variablelist class='environment-variables'> |
| 214 |
<varlistentry> |
| 215 |
<term><varname>$LISTEN_PID</varname></term> |
| 216 |
<term><varname>$LISTEN_PIDFDID</varname></term> |
| 217 |
<term><varname>$LISTEN_FDS</varname></term> |
| 218 |
<term><varname>$LISTEN_FDNAMES</varname></term> |
| 219 |
|
| 220 |
<listitem><para>Set by the service manager for supervised |
| 221 |
processes that use socket-based activation. This environment |
| 222 |
variable specifies the data |
| 223 |
<function>sd_listen_fds()</function> and |
| 224 |
<function>sd_listen_fds_with_names()</function> parses. See |
| 225 |
above for details.</para></listitem> |
| 226 |
</varlistentry> |
| 227 |
</variablelist> |
| 228 |
</refsect1> |
| 229 |
|
| 230 |
<refsect1> |
| 231 |
<title>History</title> |
| 232 |
<para><function>sd_listen_fds_with_names()</function> was added in version 227.</para> |
| 233 |
</refsect1> |
| 234 |
|
| 235 |
<refsect1> |
| 236 |
<title>See Also</title> |
| 237 |
|
| 238 |
<para><simplelist type="inline"> |
| 239 |
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> |
| 240 |
<member><citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 241 |
<member><citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 242 |
<member><citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 243 |
<member><citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 244 |
<member><citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 245 |
<member><citerefentry><refentrytitle>sd_pid_notify_with_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 246 |
<member><citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry></member> |
| 247 |
<member><citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry></member> |
| 248 |
<member><citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry></member> |
| 249 |
</simplelist></para> |
| 250 |
</refsect1> |
| 251 |
|
| 252 |
</refentry> |