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