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"
25 xmlns:xi="http://www.w3.org/2001/XInclude">
28 <title>sd_notify</title>
29 <productname>systemd</productname>
33 <contrib>Developer</contrib>
34 <firstname>Lennart</firstname>
35 <surname>Poettering</surname>
36 <email>lennart@poettering.net</email>
42 <refentrytitle>sd_notify</refentrytitle>
43 <manvolnum>3</manvolnum>
47 <refname>sd_notify</refname>
48 <refname>sd_notifyf</refname>
49 <refpurpose>Notify service manager about start-up completion and other daemon status changes</refpurpose>
54 <funcsynopsisinfo>#include <systemd/sd-daemon.h></funcsynopsisinfo>
57 <funcdef>int <function>sd_notify</function></funcdef>
58 <paramdef>int <parameter>unset_environment</parameter></paramdef>
59 <paramdef>const char *<parameter>state</parameter></paramdef>
63 <funcdef>int <function>sd_notifyf</function></funcdef>
64 <paramdef>int <parameter>unset_environment</parameter></paramdef>
65 <paramdef>const char *<parameter>format</parameter></paramdef>
66 <paramdef>...</paramdef>
72 <title>Description</title>
73 <para><function>sd_notify()</function> shall be called
74 by a daemon to notify the init system about status
75 changes. It can be used to send arbitrary information,
76 encoded in an environment-block-like string. Most
77 importantly it can be used for start-up completion
80 <para>If the <parameter>unset_environment</parameter>
81 parameter is non-zero, <function>sd_notify()</function>
82 will unset the <varname>$NOTIFY_SOCKET</varname>
83 environment variable before returning (regardless of
84 whether the function call itself succeeded or
85 not). Further calls to
86 <function>sd_notify()</function> will then fail, but
87 the variable is no longer inherited by child
90 <para>The <parameter>state</parameter> parameter
91 should contain a newline-separated list of variable
92 assignments, similar in style to an environment
93 block. A trailing newline is implied if none is
94 specified. The string may contain any kind of variable
95 assignments, but the following shall be considered
102 <listitem><para>Tells the init system
103 that daemon startup is finished. This
104 is only used by systemd if the service
105 definition file has Type=notify
106 set. The passed argument is a boolean
107 "1" or "0". Since there is little
108 value in signaling non-readiness, the
109 only value daemons should send is
110 "READY=1".</para></listitem>
114 <term>STATUS=...</term>
116 <listitem><para>Passes a single-line
117 status string back to the init system
118 that describes the daemon state. This
119 is free-form and can be used for
120 various purposes: general state
121 feedback, fsck-like programs could
122 pass completion percentages and
123 failing programs could pass a human
124 readable error message. Example:
125 "STATUS=Completed 66% of file system
126 check..."</para></listitem>
130 <term>ERRNO=...</term>
132 <listitem><para>If a daemon fails, the
133 errno-style error code, formatted as
134 string. Example: "ERRNO=2" for
135 ENOENT.</para></listitem>
139 <term>BUSERROR=...</term>
141 <listitem><para>If a daemon fails, the
142 D-Bus error-style error code. Example:
143 "BUSERROR=org.freedesktop.DBus.Error.TimedOut"</para></listitem>
147 <term>MAINPID=...</term>
149 <listitem><para>The main pid of the
150 daemon, in case the init system did
151 not fork off the process
153 "MAINPID=4711"</para></listitem>
157 <term>WATCHDOG=1</term>
159 <listitem><para>Tells systemd to
160 update the watchdog timestamp. This is
161 the keep-alive ping that services need
162 to issue in regular intervals if
163 <varname>WatchdogSec=</varname> is
165 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
166 for details. It is recommended to send
168 <varname>$WATCHDOG_PID</varname>
169 environment variable has been set to
170 the PID of the service process, in
171 every half the time interval that is
173 <varname>$WATCHDOG_USEC</varname>
174 environment variable. See
175 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
176 for details.</para></listitem>
180 <para>It is recommended to prefix variable names that
181 are not shown in the list above with
182 <varname>X_</varname> to avoid namespace
185 <para>Note that systemd will accept status data sent
186 from a daemon only if the
187 <varname>NotifyAccess=</varname> option is correctly
188 set in the service definition file. See
189 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>
192 <para><function>sd_notifyf()</function> is similar to
193 <function>sd_notify()</function> but takes a
194 <function>printf()</function>-like format string plus
199 <title>Return Value</title>
201 <para>On failure, these calls return a negative
202 errno-style error code. If
203 <varname>$NOTIFY_SOCKET</varname> was not set and
204 hence no status data could be sent, 0 is returned. If
205 the status was sent, these functions return with a
206 positive return value. In order to support both, init
207 systems that implement this scheme and those which
208 do not, it is generally recommended to ignore the return
209 value of this call.</para>
215 <xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
217 <para>Internally, these functions send a single
218 datagram with the state string as payload to the
219 <constant>AF_UNIX</constant> socket referenced in the
220 <varname>$NOTIFY_SOCKET</varname> environment
221 variable. If the first character of
222 <varname>$NOTIFY_SOCKET</varname> is <literal>@</literal>, the string is
223 understood as Linux abstract namespace socket. The
224 datagram is accompanied by the process credentials of
225 the sending daemon, using SCM_CREDENTIALS.</para>
229 <title>Environment</title>
231 <variablelist class='environment-variables'>
233 <term><varname>$NOTIFY_SOCKET</varname></term>
235 <listitem><para>Set by the init system
236 for supervised processes for status
237 and start-up completion
238 notification. This environment variable
240 <function>sd_notify()</function> talks
241 to. See above for details.</para></listitem>
247 <title>Examples</title>
250 <title>Start-up Notification</title>
252 <para>When a daemon finished starting up, it
253 might issue the following call to notify
254 the init system:</para>
256 <programlisting>sd_notify(0, "READY=1");</programlisting>
260 <title>Extended Start-up Notification</title>
262 <para>A daemon could send the following after
263 completing initialization:</para>
265 <programlisting>sd_notifyf(0, "READY=1\n"
266 "STATUS=Processing requests...\n"
268 (unsigned long) getpid());</programlisting>
272 <title>Error Cause Notification</title>
274 <para>A daemon could send the following shortly before exiting, on failure</para>
276 <programlisting>sd_notifyf(0, "STATUS=Failed to start up: %s\n"
279 errno);</programlisting>
284 <title>See Also</title>
286 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
287 <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
288 <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
289 <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>,
290 <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>