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">
6 SPDX-License-Identifier: LGPL-2.1+
8 This file is part of systemd.
10 Copyright 2010 Lennart Poettering
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.
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.
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/>.
26 <refentry id=
"sd_notify"
27 xmlns:
xi=
"http://www.w3.org/2001/XInclude">
30 <title>sd_notify
</title>
31 <productname>systemd
</productname>
35 <contrib>Developer
</contrib>
36 <firstname>Lennart
</firstname>
37 <surname>Poettering
</surname>
38 <email>lennart@poettering.net
</email>
44 <refentrytitle>sd_notify
</refentrytitle>
45 <manvolnum>3</manvolnum>
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>
59 <funcsynopsisinfo>#include
<systemd/sd-daemon.h
></funcsynopsisinfo>
62 <funcdef>int
<function>sd_notify
</function></funcdef>
63 <paramdef>int
<parameter>unset_environment
</parameter></paramdef>
64 <paramdef>const char *
<parameter>state
</parameter></paramdef>
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>
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>
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>
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>
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>
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>
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
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>
136 <term>RELOADING=
1</term>
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>
149 <term>STOPPING=
1</term>
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>
158 <term>STATUS=…
</term>
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>
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>
178 <term>BUSERROR=…
</term>
180 <listitem><para>If a service fails, the D-Bus error-style
182 <literal>BUSERROR=org.freedesktop.DBus.Error.TimedOut
</literal></para></listitem>
186 <term>MAINPID=…
</term>
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>
194 <term>WATCHDOG=
1</term>
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>
208 <term>WATCHDOG_USEC=…
</term>
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>
217 <term>FDSTORE=
1</term>
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>
240 <term>FDNAME=…
</term>
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>
262 <para>It is recommended to prefix variable names that are not
263 listed above with
<varname>X_
</varname> to avoid namespace
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>
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>
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
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>
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
314 <title>Return Value
</title>
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
329 <xi:include href=
"libsystemd-pkgconfig.xml" xpointer=
"pkgconfig-text"/>
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>
342 <title>Environment
</title>
344 <variablelist class='environment-variables'
>
346 <term><varname>$NOTIFY_SOCKET
</varname></term>
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>
358 <title>Examples
</title>
361 <title>Start-up Notification
</title>
363 <para>When a service finished starting up, it might issue the
364 following call to notify the service manager:
</para>
366 <programlisting>sd_notify(
0,
"READY=1");
</programlisting>
370 <title>Extended Start-up Notification
</title>
372 <para>A service could send the following after completing
373 initialization:
</para>
375 <programlisting>sd_notifyf(
0,
"READY=1\n"
376 "STATUS=Processing requests…\n"
378 (unsigned long) getpid());
</programlisting>
382 <title>Error Cause Notification
</title>
384 <para>A service could send the following shortly before exiting, on failure:
</para>
386 <programlisting>sd_notifyf(
0,
"STATUS=Failed to start up: %s\n"
389 errno);
</programlisting>
393 <title>Store a File Descriptor in the Service Manager
</title>
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>
399 <programlisting>sd_pid_notify_with_fds(
0,
0,
"FDSTORE=1\nFDNAME=foobar",
&fd,
1);
</programlisting>
404 <title>See Also
</title>
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>