1 <?xml version='1.0'?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 <refentry id="sd_notify">
27 <title>sd_notify</title>
28 <productname>systemd</productname>
32 <contrib>Developer</contrib>
33 <firstname>Lennart</firstname>
34 <surname>Poettering</surname>
35 <email>lennart@poettering.net</email>
41 <refentrytitle>sd_notify</refentrytitle>
42 <manvolnum>3</manvolnum>
46 <refname>sd_notify</refname>
47 <refname>sd_notifyf</refname>
48 <refpurpose>Notify service manager about start-up completion and other daemon status changes</refpurpose>
53 <funcsynopsisinfo>#include <systemd/sd-daemon.h></funcsynopsisinfo>
56 <funcdef>int <function>sd_notify</function></funcdef>
57 <paramdef>int <parameter>unset_environment</parameter></paramdef>
58 <paramdef>const char *<parameter>state</parameter></paramdef>
62 <funcdef>int <function>sd_notifyf</function></funcdef>
63 <paramdef>int <parameter>unset_environment</parameter></paramdef>
64 <paramdef>const char *<parameter>format</parameter></paramdef>
65 <paramdef>...</paramdef>
71 <title>Description</title>
72 <para><function>sd_notify()</function> shall be called
73 by a daemon to notify the init system about status
74 changes. It can be used to send arbitrary information,
75 encoded in an environment-block-like string. Most
76 importantly it can be used for start-up completion
79 <para>If the <parameter>unset_environment</parameter>
80 parameter is non-zero, <function>sd_notify()</function>
81 will unset the <varname>$NOTIFY_SOCKET</varname>
82 environment variable before returning (regardless of
83 whether the function call itself succeeded or
84 not). Further calls to
85 <function>sd_notify()</function> will then fail, but
86 the variable is no longer inherited by child
89 <para>The <parameter>state</parameter> parameter
90 should contain a newline-separated list of variable
91 assignments, similar in style to an environment
92 block. A trailing newline is implied if none is
93 specified. The string may contain any kind of variable
94 assignments, but the following shall be considered
101 <listitem><para>Tells the init system
102 that daemon startup is finished. This
103 is only used by systemd if the service
104 definition file has Type=notify
105 set. The passed argument is a boolean
106 "1" or "0". Since there is little
107 value in signaling non-readiness, the
108 only value daemons should send is
109 "READY=1".</para></listitem>
113 <term>STATUS=...</term>
115 <listitem><para>Passes a single-line
116 status string back to the init system
117 that describes the daemon state. This
118 is free-form and can be used for
119 various purposes: general state
120 feedback, fsck-like programs could
121 pass completion percentages and
122 failing programs could pass a human
123 readable error message. Example:
124 "STATUS=Completed 66% of file system
125 check..."</para></listitem>
129 <term>ERRNO=...</term>
131 <listitem><para>If a daemon fails, the
132 errno-style error code, formatted as
133 string. Example: "ERRNO=2" for
134 ENOENT.</para></listitem>
138 <term>BUSERROR=...</term>
140 <listitem><para>If a daemon fails, the
141 D-Bus error-style error code. Example:
142 "BUSERROR=org.freedesktop.DBus.Error.TimedOut"</para></listitem>
146 <term>MAINPID=...</term>
148 <listitem><para>The main pid of the
149 daemon, in case the init system did
150 not fork off the process
152 "MAINPID=4711"</para></listitem>
156 <term>WATCHDOG=1</term>
158 <listitem><para>Tells systemd to
159 update the watchdog timestamp. This is
160 the keep-alive ping that services need
161 to issue in regular intervals if
162 <varname>WatchdogSec=</varname> is
164 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
165 for details. It is recommended to send
167 <varname>$WATCHDOG_PID</varname>
168 environment variable has been set to
169 the PID of the service process, in
170 every half the time interval that is
172 <varname>$WATCHDOG_USEC</varname>
173 environment variable. See
174 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
175 for details.</para></listitem>
179 <para>It is recommended to prefix variable names that
180 are not shown in the list above with
181 <varname>X_</varname> to avoid namespace
184 <para>Note that systemd will accept status data sent
185 from a daemon only if the
186 <varname>NotifyAccess=</varname> option is correctly
187 set in the service definition file. See
188 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
191 <para><function>sd_notifyf()</function> is similar to
192 <function>sd_notify()</function> but takes a
193 <function>printf()</function>-like format string plus
198 <title>Return Value</title>
200 <para>On failure, these calls return a negative
201 errno-style error code. If
202 <varname>$NOTIFY_SOCKET</varname> was not set and
203 hence no status data could be sent, 0 is returned. If
204 the status was sent, these functions return with a
205 positive return value. In order to support both, init
206 systems that implement this scheme and those which
207 do not, it is generally recommended to ignore the return
208 value of this call.</para>
214 <para>These functions are provided by the reference
215 implementation of APIs for new-style daemons and
216 distributed with the systemd package. The algorithms
217 they implement are simple, and can easily be
218 reimplemented in daemons if it is important to support
219 this interface without using the reference
220 implementation.</para>
222 <para>Internally, these functions send a single
223 datagram with the state string as payload to the
224 <constant>AF_UNIX</constant> socket referenced in the
225 <varname>$NOTIFY_SOCKET</varname> environment
226 variable. If the first character of
227 <varname>$NOTIFY_SOCKET</varname> is <literal>@</literal>, the string is
228 understood as Linux abstract namespace socket. The
229 datagram is accompanied by the process credentials of
230 the sending daemon, using SCM_CREDENTIALS.</para>
232 <para>For details about the algorithms check the
233 liberally licensed reference implementation sources:
234 <ulink url="http://cgit.freedesktop.org/systemd/systemd/plain/src/libsystemd-daemon/sd-daemon.c"/>
236 url="http://cgit.freedesktop.org/systemd/systemd/plain/src/systemd/sd-daemon.h"/></para>
238 <para><function>sd_notify()</function> and
239 <function>sd_notifyf()</function> are implemented in
240 the reference implementation's
241 <filename>sd-daemon.c</filename> and
242 <filename>sd-daemon.h</filename> files. These
243 interfaces are available as a shared library, which can
244 be compiled and linked to with the
245 <constant>libsystemd-daemon</constant> <citerefentry><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
246 file. Alternatively, applications consuming these APIs
247 may copy the implementation into their source tree. For
248 more details about the reference implementation, see
249 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
251 <para>If the reference implementation is used as
252 drop-in files and -DDISABLE_SYSTEMD is set during
253 compilation, these functions will always return 0 and
254 otherwise become a NOP.</para>
258 <title>Environment</title>
260 <variablelist class='environment-variables'>
262 <term><varname>$NOTIFY_SOCKET</varname></term>
264 <listitem><para>Set by the init system
265 for supervised processes for status
266 and start-up completion
267 notification. This environment variable
269 <function>sd_notify()</function> talks
270 to. See above for details.</para></listitem>
276 <title>Examples</title>
279 <title>Start-up Notification</title>
281 <para>When a daemon finished starting up, it
282 might issue the following call to notify
283 the init system:</para>
285 <programlisting>sd_notify(0, "READY=1");</programlisting>
289 <title>Extended Start-up Notification</title>
291 <para>A daemon could send the following after
292 completing initialization:</para>
294 <programlisting>sd_notifyf(0, "READY=1\n"
295 "STATUS=Processing requests...\n"
297 (unsigned long) getpid());</programlisting>
301 <title>Error Cause Notification</title>
303 <para>A daemon could send the following shortly before exiting, on failure</para>
305 <programlisting>sd_notifyf(0, "STATUS=Failed to start up: %s\n"
308 errno);</programlisting>
313 <title>See Also</title>
315 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
316 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
317 <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
318 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
319 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>