]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/sd_notify.xml
Merge pull request #25168 from valentindavid/valentindavid/umount-move-recursive...
[thirdparty/systemd.git] / man / sd_notify.xml
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.2/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
5
6 <refentry id="sd_notify"
7 xmlns:xi="http://www.w3.org/2001/XInclude">
8
9 <refentryinfo>
10 <title>sd_notify</title>
11 <productname>systemd</productname>
12 </refentryinfo>
13
14 <refmeta>
15 <refentrytitle>sd_notify</refentrytitle>
16 <manvolnum>3</manvolnum>
17 </refmeta>
18
19 <refnamediv>
20 <refname>sd_notify</refname>
21 <refname>sd_notifyf</refname>
22 <refname>sd_pid_notify</refname>
23 <refname>sd_pid_notifyf</refname>
24 <refname>sd_pid_notify_with_fds</refname>
25 <refname>sd_notify_barrier</refname>
26 <refpurpose>Notify service manager about start-up completion and other service status changes</refpurpose>
27 </refnamediv>
28
29 <refsynopsisdiv>
30 <funcsynopsis>
31 <funcsynopsisinfo>#include &lt;systemd/sd-daemon.h&gt;</funcsynopsisinfo>
32
33 <funcprototype>
34 <funcdef>int <function>sd_notify</function></funcdef>
35 <paramdef>int <parameter>unset_environment</parameter></paramdef>
36 <paramdef>const char *<parameter>state</parameter></paramdef>
37 </funcprototype>
38
39 <funcprototype>
40 <funcdef>int <function>sd_notifyf</function></funcdef>
41 <paramdef>int <parameter>unset_environment</parameter></paramdef>
42 <paramdef>const char *<parameter>format</parameter></paramdef>
43 <paramdef></paramdef>
44 </funcprototype>
45
46 <funcprototype>
47 <funcdef>int <function>sd_pid_notify</function></funcdef>
48 <paramdef>pid_t <parameter>pid</parameter></paramdef>
49 <paramdef>int <parameter>unset_environment</parameter></paramdef>
50 <paramdef>const char *<parameter>state</parameter></paramdef>
51 </funcprototype>
52
53 <funcprototype>
54 <funcdef>int <function>sd_pid_notifyf</function></funcdef>
55 <paramdef>pid_t <parameter>pid</parameter></paramdef>
56 <paramdef>int <parameter>unset_environment</parameter></paramdef>
57 <paramdef>const char *<parameter>format</parameter></paramdef>
58 <paramdef></paramdef>
59 </funcprototype>
60
61 <funcprototype>
62 <funcdef>int <function>sd_pid_notify_with_fds</function></funcdef>
63 <paramdef>pid_t <parameter>pid</parameter></paramdef>
64 <paramdef>int <parameter>unset_environment</parameter></paramdef>
65 <paramdef>const char *<parameter>state</parameter></paramdef>
66 <paramdef>const int *<parameter>fds</parameter></paramdef>
67 <paramdef>unsigned <parameter>n_fds</parameter></paramdef>
68 </funcprototype>
69
70 <funcprototype>
71 <funcdef>int <function>sd_notify_barrier</function></funcdef>
72 <paramdef>int <parameter>unset_environment</parameter></paramdef>
73 <paramdef>uint64_t <parameter>timeout</parameter></paramdef>
74 </funcprototype>
75 </funcsynopsis>
76 </refsynopsisdiv>
77
78 <refsect1>
79 <title>Description</title>
80 <para><function>sd_notify()</function> may be called by a service
81 to notify the service manager about state changes. It can be used
82 to send arbitrary information, encoded in an
83 environment-block-like string. Most importantly, it can be used for
84 start-up completion notification.</para>
85
86 <para>If the <parameter>unset_environment</parameter> parameter is
87 non-zero, <function>sd_notify()</function> will unset the
88 <varname>$NOTIFY_SOCKET</varname> environment variable before
89 returning (regardless of whether the function call itself
90 succeeded or not). Further calls to
91 <function>sd_notify()</function> will then fail, but the variable
92 is no longer inherited by child processes.</para>
93
94 <para>The <parameter>state</parameter> parameter should contain a
95 newline-separated list of variable assignments, similar in style
96 to an environment block. A trailing newline is implied if none is
97 specified. The string may contain any kind of variable
98 assignments, but the following shall be considered
99 well-known:</para>
100
101 <variablelist>
102 <varlistentry>
103 <term>READY=1</term>
104
105 <listitem><para>Tells the service manager that service startup is finished, or the service finished
106 re-loading its configuration. This is only used by systemd if the service definition file has
107 <varname>Type=notify</varname> or <varname>Type=notify-reload</varname> set. Since there is little
108 value in signaling non-readiness, the only value services should send is <literal>READY=1</literal>
109 (i.e. <literal>READY=0</literal> is not defined).</para></listitem>
110 </varlistentry>
111
112 <varlistentry>
113 <term>RELOADING=1</term>
114
115 <listitem><para>Tells the service manager that the service is beginning to reload its
116 configuration. This is useful to allow the service manager to track the service's internal state, and
117 present it to the user. Note that a service that sends this notification must also send a
118 <literal>READY=1</literal> notification when it completed reloading its configuration. Reloads the
119 service manager is notified about with this mechanisms are propagated in the same way as they are
120 when originally initiated through the service manager. This message is particularly relevant for
121 <varname>Type=notify-reload</varname> services, to inform the service manager that the request to
122 reload the service has been received and is now being processed.</para></listitem>
123 </varlistentry>
124
125 <varlistentry>
126 <term>MONOTONIC_USEC=…</term>
127
128 <listitem><para>A field carrying the monotonic timestamp (as per
129 <constant>CLOCK_MONOTONIC</constant>) formatted in decimal in µs, when the notification message was
130 generated by the client. This is typically used in combination with <literal>RELOADING=1</literal>,
131 to allow the service manager to properly synchronize reload cycles. See
132 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
133 for details, specifically <literal>Type=notify-reload</literal>.</para></listitem>
134 </varlistentry>
135
136 <varlistentry>
137 <term>STOPPING=1</term>
138
139 <listitem><para>Tells the service manager that the service is
140 beginning its shutdown. This is useful to allow the service
141 manager to track the service's internal state, and present it
142 to the user.</para></listitem>
143 </varlistentry>
144
145 <varlistentry>
146 <term>STATUS=…</term>
147
148 <listitem><para>Passes a single-line UTF-8 status string back
149 to the service manager that describes the service state. This
150 is free-form and can be used for various purposes: general
151 state feedback, fsck-like programs could pass completion
152 percentages and failing programs could pass a human-readable
153 error message. Example: <literal>STATUS=Completed 66% of file
154 system check…</literal></para></listitem>
155 </varlistentry>
156
157 <varlistentry>
158 <term>ERRNO=…</term>
159
160 <listitem><para>If a service fails, the errno-style error
161 code, formatted as string. Example: <literal>ERRNO=2</literal>
162 for ENOENT.</para></listitem>
163 </varlistentry>
164
165 <varlistentry>
166 <term>BUSERROR=…</term>
167
168 <listitem><para>If a service fails, the D-Bus error-style
169 error code. Example:
170 <literal>BUSERROR=org.freedesktop.DBus.Error.TimedOut</literal></para></listitem>
171 </varlistentry>
172
173 <varlistentry>
174 <term>MAINPID=…</term>
175
176 <listitem><para>The main process ID (PID) of the service, in
177 case the service manager did not fork off the process itself.
178 Example: <literal>MAINPID=4711</literal></para></listitem>
179 </varlistentry>
180
181 <varlistentry>
182 <term>WATCHDOG=1</term>
183
184 <listitem><para>Tells the service manager to update the
185 watchdog timestamp. This is the keep-alive ping that services
186 need to issue in regular intervals if
187 <varname>WatchdogSec=</varname> is enabled for it. See
188 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
189 for information how to enable this functionality and
190 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
191 for the details of how the service can check whether the
192 watchdog is enabled. </para></listitem>
193 </varlistentry>
194
195 <varlistentry>
196 <term>WATCHDOG=trigger</term>
197
198 <listitem><para>Tells the service manager that the service detected an internal error that should be handled by
199 the configured watchdog options. This will trigger the same behaviour as if <varname>WatchdogSec=</varname> is
200 enabled and the service did not send <literal>WATCHDOG=1</literal> in time. Note that
201 <varname>WatchdogSec=</varname> does not need to be enabled for <literal>WATCHDOG=trigger</literal> to trigger
202 the watchdog action. See
203 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> for
204 information about the watchdog behavior. </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>EXTEND_TIMEOUT_USEC=…</term>
218
219 <listitem><para>Tells the service manager to extend the startup, runtime or shutdown service timeout
220 corresponding the current state. The value specified is a time in microseconds during which the service must
221 send a new message. A service timeout will occur if the message isn't received, but only if the runtime of the
222 current state is beyond the original maximum times of <varname>TimeoutStartSec=</varname>, <varname>RuntimeMaxSec=</varname>,
223 and <varname>TimeoutStopSec=</varname>.
224 See <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
225 for effects on the service timeouts.</para></listitem>
226 </varlistentry>
227
228 <varlistentry>
229 <term>FDSTORE=1</term>
230
231 <listitem><para>Stores additional file descriptors in the service manager. File descriptors sent this way will
232 be maintained per-service by the service manager and will later be handed back using the usual file descriptor
233 passing logic at the next invocation of the service (e.g. when it is restarted), see
234 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>. This is
235 useful for implementing services that can restart after an explicit request or a crash without losing
236 state. Any open sockets and other file descriptors which should not be closed during the restart may be stored
237 this way. Application state can either be serialized to a file in <filename>/run/</filename>, or better, stored
238 in a <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry> memory
239 file descriptor. Note that the service manager will accept messages for a service only if its
240 <varname>FileDescriptorStoreMax=</varname> setting is non-zero (defaults to zero, see
241 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>). If
242 <varname>FDPOLL=0</varname> is not set and the file descriptors sent are pollable (see
243 <citerefentry><refentrytitle>epoll_ctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>), then any
244 <constant>EPOLLHUP</constant> or <constant>EPOLLERR</constant> event seen on them will result in their
245 automatic removal from the store. Multiple arrays of file descriptors may be sent in separate messages, in
246 which case the arrays are combined. Note that the service manager removes duplicate (pointing to the same
247 object) file descriptors before passing them to the service. When a service is stopped, its file descriptor
248 store is discarded and all file descriptors in it are closed. Use <function>sd_pid_notify_with_fds()</function>
249 to send messages with <literal>FDSTORE=1</literal>, see below.</para></listitem>
250 </varlistentry>
251
252 <varlistentry>
253 <term>FDSTOREREMOVE=1</term>
254
255 <listitem><para>Removes file descriptors from the file descriptor store. This field needs to be combined with
256 <varname>FDNAME=</varname> to specify the name of the file descriptors to remove.</para></listitem>
257 </varlistentry>
258
259 <varlistentry>
260 <term>FDNAME=…</term>
261
262 <listitem><para>When used in combination with <varname>FDSTORE=1</varname>, specifies a name for the submitted
263 file descriptors. When used with <varname>FDSTOREREMOVE=1</varname>, specifies the name for the file
264 descriptors to remove. This name is passed to the service during activation, and may be queried using
265 <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>. File
266 descriptors submitted without this field set, will implicitly get the name <literal>stored</literal>
267 assigned. Note that, if multiple file descriptors are submitted at once, the specified name will be assigned to
268 all of them. In order to assign different names to submitted file descriptors, submit them in separate
269 invocations of <function>sd_pid_notify_with_fds()</function>. The name may consist of arbitrary ASCII
270 characters except control characters or <literal>:</literal>. It may not be longer than 255 characters. If a
271 submitted name does not follow these restrictions, it is ignored.</para></listitem>
272 </varlistentry>
273
274 <varlistentry>
275 <term>FDPOLL=0</term>
276
277 <listitem><para>When used in combination with <varname>FDSTORE=1</varname>, disables polling of the stored
278 file descriptors regardless of whether or not they are pollable. As this option disables automatic cleanup
279 of the stored file descriptors on EPOLLERR and EPOLLHUP, care must be taken to ensure proper manual cleanup.
280 Use of this option is not generally recommended except for when automatic cleanup has unwanted behavior such
281 as prematurely discarding file descriptors from the store.</para></listitem>
282 </varlistentry>
283
284 <varlistentry>
285 <term>BARRIER=1</term>
286
287 <listitem><para>Tells the service manager that the client is explicitly requesting synchronization by
288 means of closing the file descriptor sent with this command. The service manager guarantees that the
289 processing of a <varname>BARRIER=1</varname> command will only happen after all previous notification
290 messages sent before this command have been processed. Hence, this command accompanied with a single
291 file descriptor can be used to synchronize against reception of all previous status messages. Note
292 that this command cannot be mixed with other notifications, and has to be sent in a separate message
293 to the service manager, otherwise all assignments will be ignored. Note that sending 0 or more than 1
294 file descriptor with this command is a violation of the protocol.</para></listitem>
295 </varlistentry>
296 </variablelist>
297
298 <para>It is recommended to prefix variable names that are not
299 listed above with <varname>X_</varname> to avoid namespace
300 clashes.</para>
301
302 <para>Note that systemd will accept status data sent from a
303 service only if the <varname>NotifyAccess=</varname> option is
304 correctly set in the service definition file. See
305 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
306 for details.</para>
307
308 <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only if either
309 the sending process is still around at the time PID 1 processes the message, or if the sending process is
310 explicitly runtime-tracked by the service manager. The latter is the case if the service manager originally forked
311 off the process, i.e. on all processes that match <varname>NotifyAccess=</varname><option>main</option> or
312 <varname>NotifyAccess=</varname><option>exec</option>. Conversely, if an auxiliary process of the unit sends an
313 <function>sd_notify()</function> message and immediately exits, the service manager might not be able to properly
314 attribute the message to the unit, and thus will ignore it, even if
315 <varname>NotifyAccess=</varname><option>all</option> is set for it.</para>
316
317 <para>Hence, to eliminate all race conditions involving lookup of the client's unit and attribution of notifications
318 to units correctly, <function>sd_notify_barrier()</function> may be used. This call acts as a synchronization point
319 and ensures all notifications sent before this call have been picked up by the service manager when it returns
320 successfully. Use of <function>sd_notify_barrier()</function> is needed for clients which are not invoked by the
321 service manager, otherwise this synchronization mechanism is unnecessary for attribution of notifications to the
322 unit.</para>
323
324 <para><function>sd_notifyf()</function> is similar to
325 <function>sd_notify()</function> but takes a
326 <function>printf()</function>-like format string plus
327 arguments.</para>
328
329 <para><function>sd_pid_notify()</function> and
330 <function>sd_pid_notifyf()</function> are similar to
331 <function>sd_notify()</function> and
332 <function>sd_notifyf()</function> but take a process ID (PID) to
333 use as originating PID for the message as first argument. This is
334 useful to send notification messages on behalf of other processes,
335 provided the appropriate privileges are available. If the PID
336 argument is specified as 0, the process ID of the calling process
337 is used, in which case the calls are fully equivalent to
338 <function>sd_notify()</function> and
339 <function>sd_notifyf()</function>.</para>
340
341 <para><function>sd_pid_notify_with_fds()</function> is similar to
342 <function>sd_pid_notify()</function> but takes an additional array
343 of file descriptors. These file descriptors are sent along the
344 notification message to the service manager. This is particularly
345 useful for sending <literal>FDSTORE=1</literal> messages, as
346 described above. The additional arguments are a pointer to the
347 file descriptor array plus the number of file descriptors in the
348 array. If the number of file descriptors is passed as 0, the call
349 is fully equivalent to <function>sd_pid_notify()</function>, i.e.
350 no file descriptors are passed. Note that sending file descriptors
351 to the service manager on messages that do not expect them (i.e.
352 without <literal>FDSTORE=1</literal>) they are immediately closed
353 on reception.</para>
354
355 <para><function>sd_notify_barrier()</function> allows the caller to
356 synchronize against reception of previously sent notification messages
357 and uses the <varname>BARRIER=1</varname> command. It takes a relative
358 <varname>timeout</varname> value in microseconds which is passed to
359 <citerefentry><refentrytitle>ppoll</refentrytitle><manvolnum>2</manvolnum>
360 </citerefentry>. A value of UINT64_MAX is interpreted as infinite timeout.
361 </para>
362 </refsect1>
363
364 <refsect1>
365 <title>Return Value</title>
366
367 <para>On failure, these calls return a negative errno-style error code. If <varname>$NOTIFY_SOCKET</varname> was
368 not set and hence no status message could be sent, 0 is returned. If the status was sent, these functions return a
369 positive value. In order to support both service managers that implement this scheme and those which do not, it is
370 generally recommended to ignore the return value of this call. Note that the return value simply indicates whether
371 the notification message was enqueued properly, it does not reflect whether the message could be processed
372 successfully. Specifically, no error is returned when a file descriptor is attempted to be stored using
373 <varname>FDSTORE=1</varname> but the service is not actually configured to permit storing of file descriptors (see
374 above).</para>
375 </refsect1>
376
377 <refsect1>
378 <title>Notes</title>
379
380 <xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
381
382 <para>These functions send a single datagram with the
383 state string as payload to the socket referenced in the
384 <varname>$NOTIFY_SOCKET</varname> environment variable. If the
385 first character of <varname>$NOTIFY_SOCKET</varname> is
386 <literal>/</literal> or <literal>@</literal>, the string is understood
387 as an <constant>AF_UNIX</constant> or Linux abstract namespace socket
388 (respectively), and in both cases the datagram is accompanied by the
389 process credentials of the sending service, using SCM_CREDENTIALS. If
390 the string starts with <literal>vsock:</literal> then the string is
391 understood as an <constant>AF_VSOCK</constant> address, which is useful
392 for hypervisors/VMMs or other processes on the host to receive a
393 notification when a virtual machine has finished booting. Note that in
394 case the hypervisor does not support <constant>SOCK_DGRAM</constant>
395 over <constant>AF_VSOCK</constant>, <constant>SOCK_SEQPACKET</constant>
396 will be used instead. The address should be in the form:
397 <literal>vsock:CID:PORT</literal>. Note that unlike other uses of vsock,
398 the CID is mandatory and cannot be <literal>VMADDR_CID_ANY</literal>.
399 Note that PID1 will send the VSOCK packets from a privileged port
400 (i.e.: lower than 1024), as an attempt to address concerns that unprivileged
401 processes in the guest might try to send malicious notifications to the
402 host, driving it to make destructive decisions based on them.</para>
403 </refsect1>
404
405 <refsect1>
406 <title>Environment</title>
407
408 <variablelist class='environment-variables'>
409 <varlistentry>
410 <term><varname>$NOTIFY_SOCKET</varname></term>
411
412 <listitem><para>Set by the service manager for supervised
413 processes for status and start-up completion notification.
414 This environment variable specifies the socket
415 <function>sd_notify()</function> talks to. See above for
416 details.</para></listitem>
417 </varlistentry>
418 </variablelist>
419 </refsect1>
420
421 <refsect1>
422 <title>Examples</title>
423
424 <example>
425 <title>Start-up Notification</title>
426
427 <para>When a service finished starting up, it might issue the
428 following call to notify the service manager:</para>
429
430 <programlisting>sd_notify(0, "READY=1");</programlisting>
431 </example>
432
433 <example>
434 <title>Extended Start-up Notification</title>
435
436 <para>A service could send the following after completing
437 initialization:</para>
438
439 <programlisting>
440 sd_notifyf(0, "READY=1\n"
441 "STATUS=Processing requests…\n"
442 "MAINPID=%lu",
443 (unsigned long) getpid());</programlisting>
444 </example>
445
446 <example>
447 <title>Error Cause Notification</title>
448
449 <para>A service could send the following shortly before exiting, on failure:</para>
450
451 <programlisting>
452 sd_notifyf(0, "STATUS=Failed to start up: %s\n"
453 "ERRNO=%i",
454 strerror_r(errnum, (char[1024]){}, 1024),
455 errnum);</programlisting>
456 </example>
457
458 <example>
459 <title>Store a File Descriptor in the Service Manager</title>
460
461 <para>To store an open file descriptor in the service manager,
462 in order to continue operation after a service restart without
463 losing state, use <literal>FDSTORE=1</literal>:</para>
464
465 <programlisting>sd_pid_notify_with_fds(0, 0, "FDSTORE=1\nFDNAME=foobar", &amp;fd, 1);</programlisting>
466 </example>
467
468 <example>
469 <title>Eliminating race conditions</title>
470
471 <para>When the client sending the notifications is not spawned
472 by the service manager, it may exit too quickly and the service
473 manager may fail to attribute them correctly to the unit. To
474 prevent such races, use <function>sd_notify_barrier()</function>
475 to synchronize against reception of all notifications sent before
476 this call is made.</para>
477
478 <programlisting>
479 sd_notify(0, "READY=1");
480 /* set timeout to 5 seconds */
481 sd_notify_barrier(0, 5 * 1000000);
482 </programlisting>
483 </example>
484 </refsect1>
485
486 <refsect1>
487 <title>See Also</title>
488 <para>
489 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
490 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
491 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
492 <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
493 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
494 <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
495 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
496 </para>
497 </refsect1>
498
499 </refentry>