chiark / gitweb /
machined: optionally, allow registration of pre-existing units (scopes
[elogind.git] / man / sd_listen_fds.xml
1 <?xml version='1.0'?> <!--*-nxml-*-->
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   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
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
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
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 -->
23
24 <refentry id="sd_listen_fds">
25
26         <refentryinfo>
27                 <title>sd_listen_fds</title>
28                 <productname>systemd</productname>
29
30                 <authorgroup>
31                         <author>
32                                 <contrib>Developer</contrib>
33                                 <firstname>Lennart</firstname>
34                                 <surname>Poettering</surname>
35                                 <email>lennart@poettering.net</email>
36                         </author>
37                 </authorgroup>
38         </refentryinfo>
39
40         <refmeta>
41                 <refentrytitle>sd_listen_fds</refentrytitle>
42                 <manvolnum>3</manvolnum>
43         </refmeta>
44
45         <refnamediv>
46                 <refname>sd_listen_fds</refname>
47                 <refname>SD_LISTEN_FDS_START</refname>
48                 <refpurpose>Check for file descriptors passed by the system manager</refpurpose>
49         </refnamediv>
50
51         <refsynopsisdiv>
52                 <funcsynopsis>
53                         <funcsynopsisinfo>#include &lt;systemd/sd-daemon.h&gt;</funcsynopsisinfo>
54
55                         <funcsynopsisinfo>#define SD_LISTEN_FDS_START 3</funcsynopsisinfo>
56
57                         <funcprototype>
58                                 <funcdef>int <function>sd_listen_fds</function></funcdef>
59                                 <paramdef>int <parameter>unset_environment</parameter></paramdef>
60                         </funcprototype>
61                 </funcsynopsis>
62         </refsynopsisdiv>
63
64         <refsect1>
65                 <title>Description</title>
66
67                 <para><function>sd_listen_fds()</function> shall be
68                 called by a daemon to check for file descriptors
69                 passed by the init system as part of the socket-based
70                 activation logic.</para>
71
72                 <para>If the <parameter>unset_environment</parameter>
73                 parameter is non-zero,
74                 <function>sd_listen_fds()</function> will unset the
75                 <varname>$LISTEN_FDS</varname>/<varname>$LISTEN_PID</varname>
76                 environment variables before returning (regardless of
77                 whether the function call itself succeeded or
78                 not). Further calls to
79                 <function>sd_listen_fds()</function> will then fail,
80                 but the variables are no longer inherited by child
81                 processes.</para>
82
83                 <para>If a daemon receives more than one file
84                 descriptor, they will be passed in the same order as
85                 configured in the systemd socket definition
86                 file. Nonetheless, it is recommended to verify the
87                 correct socket types before using them. To simplify
88                 this checking, the functions
89                 <citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
90                 <citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
91                 <citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
92                 <citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry>
93                 are provided. In order to maximize flexibility, it is
94                 recommended to make these checks as loose as possible
95                 without allowing incorrect setups. i.e. often, the
96                 actual port number a socket is bound to matters little
97                 for the service to work, hence it should not be
98                 verified. On the other hand, whether a socket is a
99                 datagram or stream socket matters a lot for the most
100                 common program logics and should be checked.</para>
101
102                 <para>This function call will set the FD_CLOEXEC flag
103                 for all passed file descriptors to avoid further
104                 inheritance to children of the calling process.</para>
105         </refsect1>
106
107         <refsect1>
108                 <title>Return Value</title>
109
110                 <para>On failure, this call returns a negative
111                 errno-style error code. If
112                 <varname>$LISTEN_FDS</varname>/<varname>$LISTEN_PID</varname>
113                 was not set or was not correctly set for this daemon and
114                 hence no file descriptors were received, 0 is
115                 returned. Otherwise, the number of file descriptors
116                 passed is returned. The application may find them
117                 starting with file descriptor SD_LISTEN_FDS_START,
118                 i.e. file descriptor 3.</para>
119         </refsect1>
120
121         <refsect1>
122                 <title>Notes</title>
123
124                 <para>This function is provided by the reference
125                 implementation of APIs for new-style daemons and
126                 distributed with the systemd package. The algorithm it
127                 implements is simple, and can easily be reimplemented
128                 in daemons if it is important to support this
129                 interface without using the reference
130                 implementation.</para>
131
132                 <para>Internally, this function checks whether the
133                 <varname>$LISTEN_PID</varname> environment variable
134                 equals the daemon PID. If not, it returns
135                 immediately. Otherwise, it parses the number passed in
136                 the <varname>$LISTEN_FDS</varname> environment
137                 variable, then sets the FD_CLOEXEC flag for the parsed
138                 number of file descriptors starting from
139                 SD_LISTEN_FDS_START. Finally, it returns the parsed
140                 number.</para>
141
142                 <para>For details about the algorithm check the
143                 liberally licensed reference implementation sources:
144                 <ulink url="http://cgit.freedesktop.org/systemd/systemd/plain/src/libsystemd-daemon/sd-daemon.c"/>
145                 and <ulink
146                 url="http://cgit.freedesktop.org/systemd/systemd/plain/src/systemd/sd-daemon.h"/></para>
147
148                 <para><function>sd_listen_fds()</function> is
149                 implemented in the reference implementation's
150                 <filename>sd-daemon.c</filename> and
151                 <filename>sd-daemon.h</filename> files. These
152                 interfaces are available as a shared library, which can
153                 be compiled and linked to with the
154                 <constant>libsystemd-daemon</constant> <citerefentry><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
155                 file. Alternatively, applications consuming these APIs
156                 may copy the implementation into their source
157                 tree. For more details about the reference
158                 implementation, see
159                 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
160
161                 <para>If the reference implementation is used as
162                 drop-in files and -DDISABLE_SYSTEMD is set during
163                 compilation, this function will always return 0 and
164                 otherwise become a NOP.</para>
165         </refsect1>
166
167         <refsect1>
168                 <title>Environment</title>
169
170                 <variablelist class='environment-variables'>
171                         <varlistentry>
172                                 <term><varname>$LISTEN_PID</varname></term>
173                                 <term><varname>$LISTEN_FDS</varname></term>
174
175                                 <listitem><para>Set by the init system
176                                 for supervised processes that use
177                                 socket-based activation. This
178                                 environment variable specifies the
179                                 data
180                                 <function>sd_listen_fds()</function>
181                                 parses. See above for
182                                 details.</para></listitem>
183                         </varlistentry>
184                 </variablelist>
185         </refsect1>
186
187         <refsect1>
188                 <title>See Also</title>
189
190                 <para>
191                         <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
192                         <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
193                         <citerefentry><refentrytitle>sd_is_fifo</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
194                         <citerefentry><refentrytitle>sd_is_socket</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
195                         <citerefentry><refentrytitle>sd_is_socket_inet</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
196                         <citerefentry><refentrytitle>sd_is_socket_unix</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
197                         <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
198                         <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
199                         <citerefentry><refentrytitle>systemd.socket</refentrytitle><manvolnum>5</manvolnum></citerefentry>
200                 </para>
201         </refsect1>
202
203 </refentry>