chiark / gitweb /
src/systemd/meson.build: Fix add_install_script() call
[elogind.git] / man / sd_notify.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   This file is part of elogind.
7
8   Copyright 2010 Lennart Poettering
9
10   elogind 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   elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
22 -->
23
24 <refentry id="sd_notify"
25   xmlns:xi="http://www.w3.org/2001/XInclude">
26
27   <refentryinfo>
28     <title>sd_notify</title>
29     <productname>elogind</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_notify</refentrytitle>
43     <manvolnum>3</manvolnum>
44   </refmeta>
45
46   <refnamediv>
47     <refname>sd_notify</refname>
48     <refname>sd_notifyf</refname>
49     <refname>sd_pid_notify</refname>
50     <refname>sd_pid_notifyf</refname>
51     <refname>sd_pid_notify_with_fds</refname>
52     <refpurpose>Notify service manager about start-up completion and other service status changes</refpurpose>
53   </refnamediv>
54
55   <refsynopsisdiv>
56     <funcsynopsis>
57       <funcsynopsisinfo>#include &lt;elogind/sd-daemon.h&gt;</funcsynopsisinfo>
58
59       <funcprototype>
60         <funcdef>int <function>sd_notify</function></funcdef>
61         <paramdef>int <parameter>unset_environment</parameter></paramdef>
62         <paramdef>const char *<parameter>state</parameter></paramdef>
63       </funcprototype>
64
65       <funcprototype>
66         <funcdef>int <function>sd_notifyf</function></funcdef>
67         <paramdef>int <parameter>unset_environment</parameter></paramdef>
68         <paramdef>const char *<parameter>format</parameter></paramdef>
69         <paramdef>…</paramdef>
70       </funcprototype>
71
72       <funcprototype>
73         <funcdef>int <function>sd_pid_notify</function></funcdef>
74         <paramdef>pid_t <parameter>pid</parameter></paramdef>
75         <paramdef>int <parameter>unset_environment</parameter></paramdef>
76         <paramdef>const char *<parameter>state</parameter></paramdef>
77       </funcprototype>
78
79       <funcprototype>
80         <funcdef>int <function>sd_pid_notifyf</function></funcdef>
81         <paramdef>pid_t <parameter>pid</parameter></paramdef>
82         <paramdef>int <parameter>unset_environment</parameter></paramdef>
83         <paramdef>const char *<parameter>format</parameter></paramdef>
84         <paramdef>…</paramdef>
85       </funcprototype>
86
87       <funcprototype>
88         <funcdef>int <function>sd_pid_notify_with_fds</function></funcdef>
89         <paramdef>pid_t <parameter>pid</parameter></paramdef>
90         <paramdef>int <parameter>unset_environment</parameter></paramdef>
91         <paramdef>const char *<parameter>state</parameter></paramdef>
92         <paramdef>const int *<parameter>fds</parameter></paramdef>
93         <paramdef>unsigned <parameter>n_fds</parameter></paramdef>
94       </funcprototype>
95     </funcsynopsis>
96   </refsynopsisdiv>
97
98   <refsect1>
99     <title>Description</title>
100     <para><function>sd_notify()</function> may be called by a service
101     to notify the service manager about state changes. It can be used
102     to send arbitrary information, encoded in an
103     environment-block-like string. Most importantly, it can be used for
104     start-up completion notification.</para>
105
106     <para>If the <parameter>unset_environment</parameter> parameter is
107     non-zero, <function>sd_notify()</function> will unset the
108     <varname>$NOTIFY_SOCKET</varname> environment variable before
109     returning (regardless of whether the function call itself
110     succeeded or not). Further calls to
111     <function>sd_notify()</function> will then fail, but the variable
112     is no longer inherited by child processes.</para>
113
114     <para>The <parameter>state</parameter> parameter should contain a
115     newline-separated list of variable assignments, similar in style
116     to an environment block. A trailing newline is implied if none is
117     specified. The string may contain any kind of variable
118     assignments, but the following shall be considered
119     well-known:</para>
120
121     <variablelist>
122       <varlistentry>
123         <term>READY=1</term>
124
125         <listitem><para>Tells the service manager that service startup
126         is finished. This is only used by elogind if the service
127         definition file has Type=notify set. Since there is little
128         value in signaling non-readiness, the only value services
129         should send is <literal>READY=1</literal> (i.e.
130         <literal>READY=0</literal> is not defined).</para></listitem>
131       </varlistentry>
132
133       <varlistentry>
134         <term>RELOADING=1</term>
135
136         <listitem><para>Tells the service manager that the service is
137         reloading its configuration. This is useful to allow the
138         service manager to track the service's internal state, and
139         present it to the user. Note that a service that sends this
140         notification must also send a <literal>READY=1</literal>
141         notification when it completed reloading its
142         configuration. Reloads are propagated in the same way as they
143         are when initiated by the user.</para></listitem>
144       </varlistentry>
145
146       <varlistentry>
147         <term>STOPPING=1</term>
148
149         <listitem><para>Tells the service manager that the service is
150         beginning its shutdown. This is useful to allow the service
151         manager to track the service's internal state, and present it
152         to the user.</para></listitem>
153       </varlistentry>
154
155       <varlistentry>
156         <term>STATUS=…</term>
157
158         <listitem><para>Passes a single-line UTF-8 status string back
159         to the service manager that describes the service state. This
160         is free-form and can be used for various purposes: general
161         state feedback, fsck-like programs could pass completion
162         percentages and failing programs could pass a human-readable
163         error message. Example: <literal>STATUS=Completed 66% of file
164         system check…</literal></para></listitem>
165       </varlistentry>
166
167       <varlistentry>
168         <term>ERRNO=…</term>
169
170         <listitem><para>If a service fails, the errno-style error
171         code, formatted as string. Example: <literal>ERRNO=2</literal>
172         for ENOENT.</para></listitem>
173       </varlistentry>
174
175       <varlistentry>
176         <term>BUSERROR=…</term>
177
178         <listitem><para>If a service fails, the D-Bus error-style
179         error code. Example:
180         <literal>BUSERROR=org.freedesktop.DBus.Error.TimedOut</literal></para></listitem>
181       </varlistentry>
182
183       <varlistentry>
184         <term>MAINPID=…</term>
185
186         <listitem><para>The main process ID (PID) of the service, in
187         case the service manager did not fork off the process itself.
188         Example: <literal>MAINPID=4711</literal></para></listitem>
189       </varlistentry>
190
191       <varlistentry>
192         <term>WATCHDOG=1</term>
193
194         <listitem><para>Tells the service manager to update the
195         watchdog timestamp. This is the keep-alive ping that services
196         need to issue in regular intervals if
197         <varname>WatchdogSec=</varname> is enabled for it. See
198         <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
199         for information how to enable this functionality and
200         <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
201         for the details of how the service can check whether the
202         watchdog is enabled. </para></listitem>
203       </varlistentry>
204
205
206       <varlistentry>
207         <term>FDSTORE=1</term>
208
209         <listitem><para>Stores additional file descriptors in the service manager. File descriptors sent this way will
210         be maintained per-service by the service manager and will later be handed back using the usual file descriptor
211         passing logic at the next invocation of the service, see
212         <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>.  This is
213         useful for implementing services that can restart after an explicit request or a crash without losing
214         state. Any open sockets and other file descriptors which should not be closed during the restart may be stored
215         this way. Application state can either be serialized to a file in <filename>/run</filename>, or better, stored
216         in a <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry> memory
217         file descriptor. Note that the service manager will accept messages for a service only if its
218         <varname>FileDescriptorStoreMax=</varname> setting is non-zero (defaults to zero, see
219         <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>). If file
220         descriptors sent are pollable (see
221         <citerefentry><refentrytitle>epoll_ctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>), then any
222         <constant>EPOLLHUP</constant> or <constant>EPOLLERR</constant> event seen on them will result in their
223         automatic removal from the store. Multiple arrays of file descriptors may be sent in separate messages, in
224         which case the arrays are combined. Note that the service manager removes duplicate (pointing to the same
225         object) file descriptors before passing them to the service. Use <function>sd_pid_notify_with_fds()</function>
226         to send messages with <literal>FDSTORE=1</literal>, see below.</para></listitem>
227       </varlistentry>
228
229       <varlistentry>
230         <term>FDNAME=…</term>
231
232         <listitem><para>When used in combination with
233         <varname>FDSTORE=1</varname>, specifies a name for the
234         submitted file descriptors. This name is passed to the service
235         during activation, and may be queried using
236         <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>. File
237         descriptors submitted without this field set, will implicitly
238         get the name <literal>stored</literal> assigned. Note that, if
239         multiple file descriptors are submitted at once, the specified
240         name will be assigned to all of them. In order to assign
241         different names to submitted file descriptors, submit them in
242         separate invocations of
243         <function>sd_pid_notify_with_fds()</function>. The name may
244         consist of any ASCII character, but must not contain control
245         characters or <literal>:</literal>. It may not be longer than
246         255 characters. If a submitted name does not follow these
247         restrictions, it is ignored.</para></listitem>
248       </varlistentry>
249
250       <varlistentry>
251         <term>WATCHDOG_USEC=…</term>
252
253         <listitem><para>Reset <varname>watchdog_usec</varname> value during runtime.
254         Notice that this is not available when using <function>sd_event_set_watchdog()</function>
255         or <function>sd_watchdog_enabled()</function>.
256         Example : <literal>WATCHDOG_USEC=20000000</literal></para></listitem>
257       </varlistentry>
258
259     </variablelist>
260
261     <para>It is recommended to prefix variable names that are not
262     listed above with <varname>X_</varname> to avoid namespace
263     clashes.</para>
264
265     <para>Note that elogind will accept status data sent from a
266     service only if the <varname>NotifyAccess=</varname> option is
267     correctly set in the service definition file. See
268     <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
269     for details.</para>
270
271     <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only if either
272     the sending process is still around at the time PID 1 processes the message, or if the sending process is
273     explicitly runtime-tracked by the service manager. The latter is the case if the service manager originally forked
274     off the process, i.e. on all processes that match <varname>NotifyAccess=</varname><option>main</option> or
275     <varname>NotifyAccess=</varname><option>exec</option>. Conversely, if an auxiliary process of the unit sends an
276     <function>sd_notify()</function> message and immediately exits, the service manager might not be able to properly
277     attribute the message to the unit, and thus will ignore it, even if
278     <varname>NotifyAccess=</varname><option>all</option> is set for it.</para>
279
280     <para><function>sd_notifyf()</function> is similar to
281     <function>sd_notify()</function> but takes a
282     <function>printf()</function>-like format string plus
283     arguments.</para>
284
285     <para><function>sd_pid_notify()</function> and
286     <function>sd_pid_notifyf()</function> are similar to
287     <function>sd_notify()</function> and
288     <function>sd_notifyf()</function> but take a process ID (PID) to
289     use as originating PID for the message as first argument. This is
290     useful to send notification messages on behalf of other processes,
291     provided the appropriate privileges are available. If the PID
292     argument is specified as 0, the process ID of the calling process
293     is used, in which case the calls are fully equivalent to
294     <function>sd_notify()</function> and
295     <function>sd_notifyf()</function>.</para>
296
297     <para><function>sd_pid_notify_with_fds()</function> is similar to
298     <function>sd_pid_notify()</function> but takes an additional array
299     of file descriptors. These file descriptors are sent along the
300     notification message to the service manager. This is particularly
301     useful for sending <literal>FDSTORE=1</literal> messages, as
302     described above. The additional arguments are a pointer to the
303     file descriptor array plus the number of file descriptors in the
304     array. If the number of file descriptors is passed as 0, the call
305     is fully equivalent to <function>sd_pid_notify()</function>, i.e.
306     no file descriptors are passed. Note that sending file descriptors
307     to the service manager on messages that do not expect them (i.e.
308     without <literal>FDSTORE=1</literal>) they are immediately closed
309     on reception.</para>
310   </refsect1>
311
312   <refsect1>
313     <title>Return Value</title>
314
315     <para>On failure, these calls return a negative errno-style error code. If <varname>$NOTIFY_SOCKET</varname> was
316     not set and hence no status message could be sent, 0 is returned. If the status was sent, these functions return a
317     positive value. In order to support both service managers that implement this scheme and those which do not, it is
318     generally recommended to ignore the return value of this call. Note that the return value simply indicates whether
319     the notification message was enqueued properly, it does not reflect whether the message could be processed
320     successfully. Specifically, no error is returned when a file descriptor is attempted to be stored using
321     <varname>FDSTORE=1</varname> but the service is not actually configured to permit storing of file descriptors (see
322     above).</para>
323   </refsect1>
324
325   <refsect1>
326     <title>Notes</title>
327
328     <xi:include href="libelogind-pkgconfig.xml" xpointer="pkgconfig-text"/>
329
330     <para>These functions send a single datagram with the
331     state string as payload to the <constant>AF_UNIX</constant> socket
332     referenced in the <varname>$NOTIFY_SOCKET</varname> environment
333     variable. If the first character of
334     <varname>$NOTIFY_SOCKET</varname> is <literal>@</literal>, the
335     string is understood as Linux abstract namespace socket. The
336     datagram is accompanied by the process credentials of the sending
337     service, using SCM_CREDENTIALS.</para>
338   </refsect1>
339
340   <refsect1>
341     <title>Environment</title>
342
343     <variablelist class='environment-variables'>
344       <varlistentry>
345         <term><varname>$NOTIFY_SOCKET</varname></term>
346
347         <listitem><para>Set by the service manager for supervised
348         processes for status and start-up completion notification.
349         This environment variable specifies the socket
350         <function>sd_notify()</function> talks to. See above for
351         details.</para></listitem>
352       </varlistentry>
353     </variablelist>
354   </refsect1>
355
356   <refsect1>
357     <title>Examples</title>
358
359     <example>
360       <title>Start-up Notification</title>
361
362       <para>When a service finished starting up, it might issue the
363       following call to notify the service manager:</para>
364
365       <programlisting>sd_notify(0, "READY=1");</programlisting>
366     </example>
367
368     <example>
369       <title>Extended Start-up Notification</title>
370
371       <para>A service could send the following after completing
372       initialization:</para>
373
374       <programlisting>sd_notifyf(0, "READY=1\n"
375         "STATUS=Processing requests…\n"
376         "MAINPID=%lu",
377         (unsigned long) getpid());</programlisting>
378     </example>
379
380     <example>
381       <title>Error Cause Notification</title>
382
383       <para>A service could send the following shortly before exiting, on failure:</para>
384
385       <programlisting>sd_notifyf(0, "STATUS=Failed to start up: %s\n"
386         "ERRNO=%i",
387         strerror(errno),
388         errno);</programlisting>
389     </example>
390
391     <example>
392       <title>Store a File Descriptor in the Service Manager</title>
393
394       <para>To store an open file descriptor in the service manager,
395       in order to continue operation after a service restart without
396       losing state, use <literal>FDSTORE=1</literal>:</para>
397
398       <programlisting>sd_pid_notify_with_fds(0, 0, "FDSTORE=1\nFDNAME=foobar", &amp;fd, 1);</programlisting>
399     </example>
400   </refsect1>
401
402   <refsect1>
403     <title>See Also</title>
404     <para>
405       <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
406       <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
407       <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
408       <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
409       <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
410       <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
411       <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
412     </para>
413   </refsect1>
414
415 </refentry>