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