]>
Commit | Line | Data |
---|---|---|
f9378423 LP |
1 | <?xml version='1.0'?> <!--*-nxml-*--> |
2 | <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" | |
681eb9cf FB |
3 | "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [ |
4 | <!ENTITY % entities SYSTEM "custom-entities.ent" > | |
5 | %entities; | |
6 | ]> | |
f9378423 LP |
7 | |
8 | <!-- | |
9 | This file is part of systemd. | |
10 | ||
11 | Copyright 2010 Lennart Poettering | |
12 | ||
13 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
14 | under the terms of the GNU Lesser General Public License as published by |
15 | the Free Software Foundation; either version 2.1 of the License, or | |
f9378423 LP |
16 | (at your option) any later version. |
17 | ||
18 | systemd is distributed in the hope that it will be useful, but | |
19 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 21 | Lesser General Public License for more details. |
f9378423 | 22 | |
5430f7f2 | 23 | You should have received a copy of the GNU Lesser General Public License |
f9378423 LP |
24 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
25 | --> | |
26 | ||
6a70f3aa | 27 | <refentry id="sd_notify" |
798d3a52 ZJS |
28 | xmlns:xi="http://www.w3.org/2001/XInclude"> |
29 | ||
30 | <refentryinfo> | |
31 | <title>sd_notify</title> | |
32 | <productname>systemd</productname> | |
33 | ||
34 | <authorgroup> | |
35 | <author> | |
36 | <contrib>Developer</contrib> | |
37 | <firstname>Lennart</firstname> | |
38 | <surname>Poettering</surname> | |
39 | <email>lennart@poettering.net</email> | |
40 | </author> | |
41 | </authorgroup> | |
42 | </refentryinfo> | |
43 | ||
44 | <refmeta> | |
45 | <refentrytitle>sd_notify</refentrytitle> | |
46 | <manvolnum>3</manvolnum> | |
47 | </refmeta> | |
48 | ||
49 | <refnamediv> | |
50 | <refname>sd_notify</refname> | |
51 | <refname>sd_notifyf</refname> | |
52 | <refname>sd_pid_notify</refname> | |
53 | <refname>sd_pid_notifyf</refname> | |
54 | <refname>sd_pid_notify_with_fds</refname> | |
55 | <refpurpose>Notify service manager about start-up completion and other service status changes</refpurpose> | |
56 | </refnamediv> | |
57 | ||
58 | <refsynopsisdiv> | |
59 | <funcsynopsis> | |
60 | <funcsynopsisinfo>#include <systemd/sd-daemon.h></funcsynopsisinfo> | |
61 | ||
62 | <funcprototype> | |
63 | <funcdef>int <function>sd_notify</function></funcdef> | |
64 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
65 | <paramdef>const char *<parameter>state</parameter></paramdef> | |
66 | </funcprototype> | |
67 | ||
68 | <funcprototype> | |
69 | <funcdef>int <function>sd_notifyf</function></funcdef> | |
70 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
71 | <paramdef>const char *<parameter>format</parameter></paramdef> | |
72 | <paramdef>...</paramdef> | |
73 | </funcprototype> | |
74 | ||
75 | <funcprototype> | |
76 | <funcdef>int <function>sd_pid_notify</function></funcdef> | |
77 | <paramdef>pid_t <parameter>pid</parameter></paramdef> | |
78 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
79 | <paramdef>const char *<parameter>state</parameter></paramdef> | |
80 | </funcprototype> | |
81 | ||
82 | <funcprototype> | |
83 | <funcdef>int <function>sd_pid_notifyf</function></funcdef> | |
84 | <paramdef>pid_t <parameter>pid</parameter></paramdef> | |
85 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
86 | <paramdef>const char *<parameter>format</parameter></paramdef> | |
87 | <paramdef>...</paramdef> | |
88 | </funcprototype> | |
89 | ||
90 | <funcprototype> | |
91 | <funcdef>int <function>sd_pid_notify_with_fds</function></funcdef> | |
92 | <paramdef>pid_t <parameter>pid</parameter></paramdef> | |
93 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
94 | <paramdef>const char *<parameter>state</parameter></paramdef> | |
95 | <paramdef>const int *<parameter>fds</parameter></paramdef> | |
96 | <paramdef>unsigned <parameter>n_fds</parameter></paramdef> | |
97 | </funcprototype> | |
98 | </funcsynopsis> | |
99 | </refsynopsisdiv> | |
100 | ||
101 | <refsect1> | |
102 | <title>Description</title> | |
103 | <para><function>sd_notify()</function> may be called by a service | |
104 | to notify the service manager about state changes. It can be used | |
105 | to send arbitrary information, encoded in an | |
106 | environment-block-like string. Most importantly it can be used for | |
107 | start-up completion notification.</para> | |
108 | ||
109 | <para>If the <parameter>unset_environment</parameter> parameter is | |
110 | non-zero, <function>sd_notify()</function> will unset the | |
111 | <varname>$NOTIFY_SOCKET</varname> environment variable before | |
112 | returning (regardless of whether the function call itself | |
113 | succeeded or not). Further calls to | |
114 | <function>sd_notify()</function> will then fail, but the variable | |
115 | is no longer inherited by child processes.</para> | |
116 | ||
117 | <para>The <parameter>state</parameter> parameter should contain a | |
118 | newline-separated list of variable assignments, similar in style | |
119 | to an environment block. A trailing newline is implied if none is | |
120 | specified. The string may contain any kind of variable | |
121 | assignments, but the following shall be considered | |
122 | well-known:</para> | |
123 | ||
124 | <variablelist> | |
125 | <varlistentry> | |
126 | <term>READY=1</term> | |
127 | ||
128 | <listitem><para>Tells the service manager that service startup | |
129 | is finished. This is only used by systemd if the service | |
130 | definition file has Type=notify set. Since there is little | |
131 | value in signaling non-readiness, the only value services | |
132 | should send is <literal>READY=1</literal> (i.e. | |
133 | <literal>READY=0</literal> is not defined).</para></listitem> | |
134 | </varlistentry> | |
135 | ||
136 | <varlistentry> | |
137 | <term>RELOADING=1</term> | |
138 | ||
139 | <listitem><para>Tells the service manager that the service is | |
140 | reloading its configuration. This is useful to allow the | |
141 | service manager to track the service's internal state, and | |
142 | present it to the user. Note that a service that sends this | |
143 | notification must also send a <literal>READY=1</literal> | |
144 | notification when it completed reloading its | |
145 | configuration.</para></listitem> | |
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> | |
158 | <term>STATUS=...</term> | |
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 | |
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> | |
167 | </varlistentry> | |
168 | ||
169 | <varlistentry> | |
170 | <term>ERRNO=...</term> | |
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> | |
178 | <term>BUSERROR=...</term> | |
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> | |
186 | <term>MAINPID=...</term> | |
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> | |
203 | for the details of how the service can check if the the | |
204 | watchdog is enabled. </para></listitem> | |
205 | </varlistentry> | |
206 | ||
207 | ||
208 | <varlistentry> | |
209 | <term>FDSTORE=1</term> | |
210 | ||
211 | <listitem><para>Stores additional file descriptors in the | |
212 | service manager. File descriptors sent this way will be | |
213 | maintained per-service by the service manager and be passed | |
214 | again using the usual file descriptor passing logic on the | |
215 | next invocation of the service (see | |
216 | <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>). | |
217 | This is useful for implementing service restart schemes where | |
218 | services serialize their state to <filename>/run</filename>, | |
219 | push their file descriptors to the system manager, and are | |
220 | then restarted, retrieving their state again via socket | |
221 | passing and <filename>/run</filename>. Note that the service | |
222 | manager will accept messages for a service only if | |
223 | <varname>FileDescriptorStoreMax=</varname> is set to non-zero | |
224 | for it (defaults to zero). See | |
225 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> | |
226 | for details. Multiple arrays of file descriptors may be sent | |
227 | in separate messages, in which case the arrays are combined. | |
228 | Note that the service manager removes duplicate file | |
229 | descriptors before passing them to the service. Use | |
230 | <function>sd_pid_notify_with_fds()</function> to send messages | |
231 | with <literal>FDSTORE=1</literal>, see | |
232 | below.</para></listitem> | |
233 | </varlistentry> | |
234 | ||
235 | </variablelist> | |
236 | ||
237 | <para>It is recommended to prefix variable names that are not | |
238 | listed above with <varname>X_</varname> to avoid namespace | |
239 | clashes.</para> | |
240 | ||
241 | <para>Note that systemd will accept status data sent from a | |
242 | service only if the <varname>NotifyAccess=</varname> option is | |
243 | correctly set in the service definition file. See | |
244 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> | |
245 | for details.</para> | |
246 | ||
247 | <para><function>sd_notifyf()</function> is similar to | |
248 | <function>sd_notify()</function> but takes a | |
249 | <function>printf()</function>-like format string plus | |
250 | arguments.</para> | |
251 | ||
252 | <para><function>sd_pid_notify()</function> and | |
253 | <function>sd_pid_notifyf()</function> are similar to | |
254 | <function>sd_notify()</function> and | |
255 | <function>sd_notifyf()</function> but take a process ID (PID) to | |
256 | use as originating PID for the message as first argument. This is | |
257 | useful to send notification messages on behalf of other processes, | |
258 | provided the appropriate privileges are available. If the PID | |
259 | argument is specified as 0 the process ID of the calling process | |
260 | is used, in which case the calls are fully equivalent to | |
261 | <function>sd_notify()</function> and | |
262 | <function>sd_notifyf()</function>.</para> | |
263 | ||
264 | <para><function>sd_pid_notify_with_fds()</function> is similar to | |
265 | <function>sd_pid_notify()</function> but takes an additional array | |
266 | of file descriptors. These file descriptors are sent along the | |
267 | notification message to the service manager. This is particularly | |
268 | useful for sending <literal>FDSTORE=1</literal> messages, as | |
269 | described above. The additional arguments are a pointer to the | |
270 | file descriptor array plus the number of file descriptors in the | |
271 | array. If the number of file descriptors is passed as 0, the call | |
272 | is fully equivalent to <function>sd_pid_notify()</function>, i.e. | |
273 | no file descriptors are passed. Note that sending file descriptors | |
274 | to the service manager on messages that do not expect them (i.e. | |
275 | without <literal>FDSTORE=1</literal>) they are immediately closed | |
276 | on reception.</para> | |
277 | </refsect1> | |
278 | ||
279 | <refsect1> | |
280 | <title>Return Value</title> | |
281 | ||
282 | <para>On failure, these calls return a negative errno-style error | |
283 | code. If <varname>$NOTIFY_SOCKET</varname> was not set and hence | |
284 | no status data could be sent, 0 is returned. If the status was | |
285 | sent, these functions return with a positive return value. In | |
286 | order to support both, init systems that implement this scheme and | |
287 | those which do not, it is generally recommended to ignore the | |
288 | return value of this call.</para> | |
289 | </refsect1> | |
290 | ||
291 | <refsect1> | |
292 | <title>Notes</title> | |
293 | ||
294 | <xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/> | |
295 | ||
296 | <para>Internally, these functions send a single datagram with the | |
297 | state string as payload to the <constant>AF_UNIX</constant> socket | |
298 | referenced in the <varname>$NOTIFY_SOCKET</varname> environment | |
299 | variable. If the first character of | |
300 | <varname>$NOTIFY_SOCKET</varname> is <literal>@</literal>, the | |
301 | string is understood as Linux abstract namespace socket. The | |
302 | datagram is accompanied by the process credentials of the sending | |
303 | service, using SCM_CREDENTIALS.</para> | |
304 | </refsect1> | |
305 | ||
306 | <refsect1> | |
307 | <title>Environment</title> | |
308 | ||
309 | <variablelist class='environment-variables'> | |
310 | <varlistentry> | |
311 | <term><varname>$NOTIFY_SOCKET</varname></term> | |
312 | ||
313 | <listitem><para>Set by the service manager for supervised | |
314 | processes for status and start-up completion notification. | |
315 | This environment variable specifies the socket | |
316 | <function>sd_notify()</function> talks to. See above for | |
317 | details.</para></listitem> | |
318 | </varlistentry> | |
319 | </variablelist> | |
320 | </refsect1> | |
321 | ||
322 | <refsect1> | |
323 | <title>Examples</title> | |
324 | ||
325 | <example> | |
326 | <title>Start-up Notification</title> | |
327 | ||
328 | <para>When a service finished starting up, it might issue the | |
329 | following call to notify the service manager:</para> | |
330 | ||
331 | <programlisting>sd_notify(0, "READY=1");</programlisting> | |
332 | </example> | |
333 | ||
334 | <example> | |
335 | <title>Extended Start-up Notification</title> | |
336 | ||
337 | <para>A service could send the following after completing | |
338 | initialization:</para> | |
339 | ||
340 | <programlisting>sd_notifyf(0, "READY=1\n" | |
341 | "STATUS=Processing requests...\n" | |
342 | "MAINPID=%lu", | |
343 | (unsigned long) getpid());</programlisting> | |
344 | </example> | |
345 | ||
346 | <example> | |
347 | <title>Error Cause Notification</title> | |
348 | ||
349 | <para>A service could send the following shortly before exiting, on failure:</para> | |
350 | ||
351 | <programlisting>sd_notifyf(0, "STATUS=Failed to start up: %s\n" | |
352 | "ERRNO=%i", | |
353 | strerror(errno), | |
354 | errno);</programlisting> | |
355 | </example> | |
356 | ||
357 | <example> | |
358 | <title>Store a File Descriptor in the Service Manager</title> | |
359 | ||
360 | <para>To store an open file descriptor in the service manager, | |
361 | in order to continue operation after a service restart without | |
362 | losing state use <literal>FDSTORE=1</literal>:</para> | |
363 | ||
364 | <programlisting>sd_pid_notify_with_fds(0, 0, "FDSTORE=1", &fd, 1);</programlisting> | |
365 | </example> | |
366 | </refsect1> | |
367 | ||
368 | <refsect1> | |
369 | <title>See Also</title> | |
370 | <para> | |
371 | <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, | |
372 | <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
373 | <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>, | |
374 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>, | |
375 | <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry> | |
376 | </para> | |
377 | </refsect1> | |
f9378423 LP |
378 | |
379 | </refentry> |