]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/sd_notify.xml
resolved: tweak domain routing logic 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 is finished, or the service finished loading its
128 configuration. This is only used by systemd if the service definition file has <varname>Type=notify</varname>
129 set. Since there is little value in signaling non-readiness, the only value services should send is
130 <literal>READY=1</literal> (i.e. <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 <varlistentry>
206 <term>WATCHDOG_USEC=…</term>
207
208 <listitem><para>Reset <varname>watchdog_usec</varname> value during runtime.
209 Notice that this is not available when using <function>sd_event_set_watchdog()</function>
210 or <function>sd_watchdog_enabled()</function>.
211 Example : <literal>WATCHDOG_USEC=20000000</literal></para></listitem>
212 </varlistentry>
213
214 <varlistentry>
215 <term>FDSTORE=1</term>
216
217 <listitem><para>Stores additional file descriptors in the service manager. File descriptors sent this way will
218 be maintained per-service by the service manager and will later be handed back using the usual file descriptor
219 passing logic at the next invocation of the service, see
220 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>. This is
221 useful for implementing services that can restart after an explicit request or a crash without losing
222 state. Any open sockets and other file descriptors which should not be closed during the restart may be stored
223 this way. Application state can either be serialized to a file in <filename>/run</filename>, or better, stored
224 in a <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry> memory
225 file descriptor. Note that the service manager will accept messages for a service only if its
226 <varname>FileDescriptorStoreMax=</varname> setting is non-zero (defaults to zero, see
227 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>). If file
228 descriptors sent are pollable (see
229 <citerefentry><refentrytitle>epoll_ctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>), then any
230 <constant>EPOLLHUP</constant> or <constant>EPOLLERR</constant> event seen on them will result in their
231 automatic removal from the store. Multiple arrays of file descriptors may be sent in separate messages, in
232 which case the arrays are combined. Note that the service manager removes duplicate (pointing to the same
233 object) file descriptors before passing them to the service. Use <function>sd_pid_notify_with_fds()</function>
234 to send messages with <literal>FDSTORE=1</literal>, see below.</para></listitem>
235 </varlistentry>
236
237 <varlistentry>
238 <term>FDSTOREREMOVE=1</term>
239
240 <listitem><para>Removes file descriptors from the file descriptor store. This field needs to be combined with
241 <varname>FDNAME=</varname> to specify the name of the file descriptors to remove.</para></listitem>
242 </varlistentry>
243
244 <varlistentry>
245 <term>FDNAME=…</term>
246
247 <listitem><para>When used in combination with <varname>FDSTORE=1</varname>, specifies a name for the submitted
248 file descriptors. When used with <varname>FDSTOREREMOVE=1</varname>, specifies the name for the file
249 descriptors to remove. This name is passed to the service during activation, and may be queried using
250 <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>. File
251 descriptors submitted without this field set, will implicitly get the name <literal>stored</literal>
252 assigned. Note that, if multiple file descriptors are submitted at once, the specified name will be assigned to
253 all of them. In order to assign different names to submitted file descriptors, submit them in separate
254 invocations of <function>sd_pid_notify_with_fds()</function>. The name may consist of arbitrary ASCII
255 characters except control characters or <literal>:</literal>. It may not be longer than 255 characters. If a
256 submitted name does not follow these restrictions, it is ignored.</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 systemd 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="libsystemd-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>