]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/sd_notify.xml
service: split out sd_notify() message authorization code into a function of its own
[thirdparty/systemd.git] / man / sd_notify.xml
CommitLineData
8dd4c05b 1<?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
f9378423 2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
12b42c76 3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
f9378423
LP
4
5<!--
572eb058
ZJS
6 SPDX-License-Identifier: LGPL-2.1+
7
f9378423
LP
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
5430f7f2
LP
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
f9378423
LP
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
5430f7f2 20 Lesser General Public License for more details.
f9378423 21
5430f7f2 22 You should have received a copy of the GNU Lesser General Public License
f9378423
LP
23 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24-->
25
6a70f3aa 26<refentry id="sd_notify"
798d3a52
ZJS
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>
1eecafb8 71 <paramdef>…</paramdef>
798d3a52
ZJS
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>
1eecafb8 86 <paramdef>…</paramdef>
798d3a52
ZJS
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
b938cb90 105 environment-block-like string. Most importantly, it can be used for
798d3a52
ZJS
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
15d167f8
JW
144 configuration. Reloads are propagated in the same way as they
145 are when initiated by the user.</para></listitem>
798d3a52
ZJS
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>
1eecafb8 158 <term>STATUS=…</term>
798d3a52
ZJS
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
b938cb90 164 percentages and failing programs could pass a human-readable
798d3a52 165 error message. Example: <literal>STATUS=Completed 66% of file
1eecafb8 166 system check…</literal></para></listitem>
798d3a52
ZJS
167 </varlistentry>
168
169 <varlistentry>
1eecafb8 170 <term>ERRNO=…</term>
798d3a52
ZJS
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>
1eecafb8 178 <term>BUSERROR=…</term>
798d3a52
ZJS
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>
1eecafb8 186 <term>MAINPID=…</term>
798d3a52
ZJS
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>
1d3eaa93 203 for the details of how the service can check whether the
798d3a52
ZJS
204 watchdog is enabled. </para></listitem>
205 </varlistentry>
206
207
208 <varlistentry>
209 <term>FDSTORE=1</term>
210
3ceb72e5
LP
211 <listitem><para>Stores additional file descriptors in the service manager. File descriptors sent this way will
212 be maintained per-service by the service manager and will later be handed back using the usual file descriptor
213 passing logic at the next invocation of the service, see
214 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>. This is
215 useful for implementing services that can restart after an explicit request or a crash without losing
216 state. Any open sockets and other file descriptors which should not be closed during the restart may be stored
217 this way. Application state can either be serialized to a file in <filename>/run</filename>, or better, stored
218 in a <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry> memory
219 file descriptor. Note that the service manager will accept messages for a service only if its
220 <varname>FileDescriptorStoreMax=</varname> setting is non-zero (defaults to zero, see
221 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>). If file
222 descriptors sent are pollable (see
223 <citerefentry><refentrytitle>epoll_ctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>), then any
224 <constant>EPOLLHUP</constant> or <constant>EPOLLERR</constant> event seen on them will result in their
225 automatic removal from the store. Multiple arrays of file descriptors may be sent in separate messages, in
226 which case the arrays are combined. Note that the service manager removes duplicate (pointing to the same
227 object) file descriptors before passing them to the service. Use <function>sd_pid_notify_with_fds()</function>
228 to send messages with <literal>FDSTORE=1</literal>, see below.</para></listitem>
798d3a52
ZJS
229 </varlistentry>
230
8dd4c05b 231 <varlistentry>
1eecafb8 232 <term>FDNAME=…</term>
8dd4c05b
LP
233
234 <listitem><para>When used in combination with
b938cb90 235 <varname>FDSTORE=1</varname>, specifies a name for the
8dd4c05b
LP
236 submitted file descriptors. This name is passed to the service
237 during activation, and may be queried using
238 <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>. File
239 descriptors submitted without this field set, will implicitly
b938cb90
JE
240 get the name <literal>stored</literal> assigned. Note that, if
241 multiple file descriptors are submitted at once, the specified
8dd4c05b
LP
242 name will be assigned to all of them. In order to assign
243 different names to submitted file descriptors, submit them in
7f3fdb7f 244 separate invocations of
8dd4c05b 245 <function>sd_pid_notify_with_fds()</function>. The name may
a8eaaee7 246 consist of any ASCII character, but must not contain control
8dd4c05b
LP
247 characters or <literal>:</literal>. It may not be longer than
248 255 characters. If a submitted name does not follow these
b938cb90 249 restrictions, it is ignored.</para></listitem>
8dd4c05b
LP
250 </varlistentry>
251
2787d83c 252 <varlistentry>
1eecafb8 253 <term>WATCHDOG_USEC=…</term>
2787d83c
M
254
255 <listitem><para>Reset <varname>watchdog_usec</varname> value during runtime.
256 Notice that this is not available when using <function>sd_event_set_watchdog()</function>
257 or <function>sd_watchdog_enabled()</function>.
258 Example : <literal>WATCHDOG_USEC=20000000</literal></para></listitem>
259 </varlistentry>
260
798d3a52
ZJS
261 </variablelist>
262
263 <para>It is recommended to prefix variable names that are not
264 listed above with <varname>X_</varname> to avoid namespace
265 clashes.</para>
266
267 <para>Note that systemd will accept status data sent from a
268 service only if the <varname>NotifyAccess=</varname> option is
269 correctly set in the service definition file. See
270 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
271 for details.</para>
272
b3bb6476
LP
273 <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only if either
274 the sending process is still around at the time PID 1 processes the message, or if the sending process is
275 explicitly runtime-tracked by the service manager. The latter is the case if the service manager originally forked
276 off the process, i.e. on all processes that match <varname>NotifyAccess=</varname><option>main</option> or
277 <varname>NotifyAccess=</varname><option>exec</option>. Conversely, if an auxiliary process of the unit sends an
278 <function>sd_notify()</function> message and immediately exits, the service manager might not be able to properly
279 attribute the message to the unit, and thus will ignore it, even if
280 <varname>NotifyAccess=</varname><option>all</option> is set for it.</para>
281
798d3a52
ZJS
282 <para><function>sd_notifyf()</function> is similar to
283 <function>sd_notify()</function> but takes a
284 <function>printf()</function>-like format string plus
285 arguments.</para>
286
287 <para><function>sd_pid_notify()</function> and
288 <function>sd_pid_notifyf()</function> are similar to
289 <function>sd_notify()</function> and
290 <function>sd_notifyf()</function> but take a process ID (PID) to
291 use as originating PID for the message as first argument. This is
292 useful to send notification messages on behalf of other processes,
293 provided the appropriate privileges are available. If the PID
b938cb90 294 argument is specified as 0, the process ID of the calling process
798d3a52
ZJS
295 is used, in which case the calls are fully equivalent to
296 <function>sd_notify()</function> and
297 <function>sd_notifyf()</function>.</para>
298
299 <para><function>sd_pid_notify_with_fds()</function> is similar to
300 <function>sd_pid_notify()</function> but takes an additional array
301 of file descriptors. These file descriptors are sent along the
302 notification message to the service manager. This is particularly
303 useful for sending <literal>FDSTORE=1</literal> messages, as
304 described above. The additional arguments are a pointer to the
305 file descriptor array plus the number of file descriptors in the
306 array. If the number of file descriptors is passed as 0, the call
307 is fully equivalent to <function>sd_pid_notify()</function>, i.e.
308 no file descriptors are passed. Note that sending file descriptors
309 to the service manager on messages that do not expect them (i.e.
310 without <literal>FDSTORE=1</literal>) they are immediately closed
311 on reception.</para>
312 </refsect1>
313
314 <refsect1>
315 <title>Return Value</title>
316
3ceb72e5
LP
317 <para>On failure, these calls return a negative errno-style error code. If <varname>$NOTIFY_SOCKET</varname> was
318 not set and hence no status message could be sent, 0 is returned. If the status was sent, these functions return a
319 positive value. In order to support both service managers that implement this scheme and those which do not, it is
320 generally recommended to ignore the return value of this call. Note that the return value simply indicates whether
321 the notification message was enqueued properly, it does not reflect whether the message could be processed
322 successfully. Specifically, no error is returned when a file descriptor is attempted to be stored using
323 <varname>FDSTORE=1</varname> but the service is not actually configured to permit storing of file descriptors (see
324 above).</para>
798d3a52
ZJS
325 </refsect1>
326
327 <refsect1>
328 <title>Notes</title>
329
330 <xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
331
3122633a 332 <para>These functions send a single datagram with the
798d3a52
ZJS
333 state string as payload to the <constant>AF_UNIX</constant> socket
334 referenced in the <varname>$NOTIFY_SOCKET</varname> environment
335 variable. If the first character of
336 <varname>$NOTIFY_SOCKET</varname> is <literal>@</literal>, the
337 string is understood as Linux abstract namespace socket. The
338 datagram is accompanied by the process credentials of the sending
339 service, using SCM_CREDENTIALS.</para>
340 </refsect1>
341
342 <refsect1>
343 <title>Environment</title>
344
345 <variablelist class='environment-variables'>
346 <varlistentry>
347 <term><varname>$NOTIFY_SOCKET</varname></term>
348
349 <listitem><para>Set by the service manager for supervised
350 processes for status and start-up completion notification.
351 This environment variable specifies the socket
352 <function>sd_notify()</function> talks to. See above for
353 details.</para></listitem>
354 </varlistentry>
355 </variablelist>
356 </refsect1>
357
358 <refsect1>
359 <title>Examples</title>
360
361 <example>
362 <title>Start-up Notification</title>
363
364 <para>When a service finished starting up, it might issue the
365 following call to notify the service manager:</para>
366
367 <programlisting>sd_notify(0, "READY=1");</programlisting>
368 </example>
369
370 <example>
371 <title>Extended Start-up Notification</title>
372
373 <para>A service could send the following after completing
374 initialization:</para>
375
376 <programlisting>sd_notifyf(0, "READY=1\n"
1eecafb8 377 "STATUS=Processing requests…\n"
798d3a52
ZJS
378 "MAINPID=%lu",
379 (unsigned long) getpid());</programlisting>
380 </example>
381
382 <example>
383 <title>Error Cause Notification</title>
384
385 <para>A service could send the following shortly before exiting, on failure:</para>
386
387 <programlisting>sd_notifyf(0, "STATUS=Failed to start up: %s\n"
388 "ERRNO=%i",
389 strerror(errno),
390 errno);</programlisting>
391 </example>
392
393 <example>
394 <title>Store a File Descriptor in the Service Manager</title>
395
396 <para>To store an open file descriptor in the service manager,
397 in order to continue operation after a service restart without
b938cb90 398 losing state, use <literal>FDSTORE=1</literal>:</para>
798d3a52 399
8dd4c05b 400 <programlisting>sd_pid_notify_with_fds(0, 0, "FDSTORE=1\nFDNAME=foobar", &amp;fd, 1);</programlisting>
798d3a52
ZJS
401 </example>
402 </refsect1>
403
404 <refsect1>
405 <title>See Also</title>
406 <para>
407 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
408 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
8dd4c05b
LP
409 <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
410 <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
411 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
798d3a52 412 <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
8dd4c05b 413 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
798d3a52
ZJS
414 </para>
415 </refsect1>
f9378423
LP
416
417</refentry>