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