]>
Commit | Line | Data |
---|---|---|
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 <systemd/sd-daemon.h></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 | ||
8d1ab18a LP |
127 | <listitem><para>Tells the service manager that service startup is finished, or the service finished loading its |
128 | configuration. This is only used by systemd if the service definition file has <varname>Type=notify</varname> | |
129 | set. Since there is little value in signaling non-readiness, the only value services should send is | |
130 | <literal>READY=1</literal> (i.e. <literal>READY=0</literal> is not defined).</para></listitem> | |
798d3a52 ZJS |
131 | </varlistentry> |
132 | ||
133 | <varlistentry> | |
134 | <term>RELOADING=1</term> | |
135 | ||
136 | <listitem><para>Tells the service manager that the service is | |
137 | reloading its configuration. This is useful to allow the | |
138 | service manager to track the service's internal state, and | |
139 | present it to the user. Note that a service that sends this | |
140 | notification must also send a <literal>READY=1</literal> | |
141 | notification when it completed reloading its | |
15d167f8 JW |
142 | configuration. Reloads are propagated in the same way as they |
143 | are when initiated by the user.</para></listitem> | |
798d3a52 ZJS |
144 | </varlistentry> |
145 | ||
146 | <varlistentry> | |
147 | <term>STOPPING=1</term> | |
148 | ||
149 | <listitem><para>Tells the service manager that the service is | |
150 | beginning its shutdown. This is useful to allow the service | |
151 | manager to track the service's internal state, and present it | |
152 | to the user.</para></listitem> | |
153 | </varlistentry> | |
154 | ||
155 | <varlistentry> | |
1eecafb8 | 156 | <term>STATUS=…</term> |
798d3a52 ZJS |
157 | |
158 | <listitem><para>Passes a single-line UTF-8 status string back | |
159 | to the service manager that describes the service state. This | |
160 | is free-form and can be used for various purposes: general | |
161 | state feedback, fsck-like programs could pass completion | |
b938cb90 | 162 | percentages and failing programs could pass a human-readable |
798d3a52 | 163 | error message. Example: <literal>STATUS=Completed 66% of file |
1eecafb8 | 164 | system check…</literal></para></listitem> |
798d3a52 ZJS |
165 | </varlistentry> |
166 | ||
167 | <varlistentry> | |
1eecafb8 | 168 | <term>ERRNO=…</term> |
798d3a52 ZJS |
169 | |
170 | <listitem><para>If a service fails, the errno-style error | |
171 | code, formatted as string. Example: <literal>ERRNO=2</literal> | |
172 | for ENOENT.</para></listitem> | |
173 | </varlistentry> | |
174 | ||
175 | <varlistentry> | |
1eecafb8 | 176 | <term>BUSERROR=…</term> |
798d3a52 ZJS |
177 | |
178 | <listitem><para>If a service fails, the D-Bus error-style | |
179 | error code. Example: | |
180 | <literal>BUSERROR=org.freedesktop.DBus.Error.TimedOut</literal></para></listitem> | |
181 | </varlistentry> | |
182 | ||
183 | <varlistentry> | |
1eecafb8 | 184 | <term>MAINPID=…</term> |
798d3a52 ZJS |
185 | |
186 | <listitem><para>The main process ID (PID) of the service, in | |
187 | case the service manager did not fork off the process itself. | |
188 | Example: <literal>MAINPID=4711</literal></para></listitem> | |
189 | </varlistentry> | |
190 | ||
191 | <varlistentry> | |
192 | <term>WATCHDOG=1</term> | |
193 | ||
194 | <listitem><para>Tells the service manager to update the | |
195 | watchdog timestamp. This is the keep-alive ping that services | |
196 | need to issue in regular intervals if | |
197 | <varname>WatchdogSec=</varname> is enabled for it. See | |
198 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> | |
199 | for information how to enable this functionality and | |
200 | <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry> | |
1d3eaa93 | 201 | for the details of how the service can check whether the |
798d3a52 ZJS |
202 | watchdog is enabled. </para></listitem> |
203 | </varlistentry> | |
204 | ||
c45d11cb LP |
205 | <varlistentry> |
206 | <term>WATCHDOG_USEC=…</term> | |
207 | ||
208 | <listitem><para>Reset <varname>watchdog_usec</varname> value during runtime. | |
209 | Notice that this is not available when using <function>sd_event_set_watchdog()</function> | |
210 | or <function>sd_watchdog_enabled()</function>. | |
211 | Example : <literal>WATCHDOG_USEC=20000000</literal></para></listitem> | |
212 | </varlistentry> | |
798d3a52 | 213 | |
a327431b DB |
214 | <varlistentry> |
215 | <term>EXTEND_TIMEOUT_USEC=…</term> | |
216 | ||
217 | <listitem><para>Tells the service manager to extend the startup, runtime or shutdown service timeout | |
218 | corresponding the current state. The value specified is a time in microseconds during which the service must | |
219 | send a new message. A service timeout will occur if the message isn't received, but only if the runtime of the | |
220 | current state is beyond the original maximium times of <varname>TimeoutStartSec=</varname>, <varname>RuntimeMaxSec=</varname>, | |
221 | and <varname>TimeoutStopSec=</varname>. | |
222 | See <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> | |
223 | for effects on the service timeouts.</para></listitem> | |
224 | </varlistentry> | |
225 | ||
798d3a52 ZJS |
226 | <varlistentry> |
227 | <term>FDSTORE=1</term> | |
228 | ||
3ceb72e5 LP |
229 | <listitem><para>Stores additional file descriptors in the service manager. File descriptors sent this way will |
230 | be maintained per-service by the service manager and will later be handed back using the usual file descriptor | |
231 | passing logic at the next invocation of the service, see | |
232 | <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>. This is | |
233 | useful for implementing services that can restart after an explicit request or a crash without losing | |
234 | state. Any open sockets and other file descriptors which should not be closed during the restart may be stored | |
235 | this way. Application state can either be serialized to a file in <filename>/run</filename>, or better, stored | |
236 | in a <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry> memory | |
237 | file descriptor. Note that the service manager will accept messages for a service only if its | |
238 | <varname>FileDescriptorStoreMax=</varname> setting is non-zero (defaults to zero, see | |
239 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>). If file | |
240 | descriptors sent are pollable (see | |
241 | <citerefentry><refentrytitle>epoll_ctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>), then any | |
242 | <constant>EPOLLHUP</constant> or <constant>EPOLLERR</constant> event seen on them will result in their | |
243 | automatic removal from the store. Multiple arrays of file descriptors may be sent in separate messages, in | |
244 | which case the arrays are combined. Note that the service manager removes duplicate (pointing to the same | |
245 | object) file descriptors before passing them to the service. Use <function>sd_pid_notify_with_fds()</function> | |
246 | to send messages with <literal>FDSTORE=1</literal>, see below.</para></listitem> | |
798d3a52 ZJS |
247 | </varlistentry> |
248 | ||
e78ee06d LP |
249 | <varlistentry> |
250 | <term>FDSTOREREMOVE=1</term> | |
251 | ||
252 | <listitem><para>Removes file descriptors from the file descriptor store. This field needs to be combined with | |
253 | <varname>FDNAME=</varname> to specify the name of the file descriptors to remove.</para></listitem> | |
254 | </varlistentry> | |
255 | ||
8dd4c05b | 256 | <varlistentry> |
1eecafb8 | 257 | <term>FDNAME=…</term> |
8dd4c05b | 258 | |
e78ee06d LP |
259 | <listitem><para>When used in combination with <varname>FDSTORE=1</varname>, specifies a name for the submitted |
260 | file descriptors. When used with <varname>FDSTOREREMOVE=1</varname>, specifies the name for the file | |
261 | descriptors to remove. This name is passed to the service during activation, and may be queried using | |
8dd4c05b | 262 | <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>. File |
e78ee06d LP |
263 | descriptors submitted without this field set, will implicitly get the name <literal>stored</literal> |
264 | assigned. Note that, if multiple file descriptors are submitted at once, the specified name will be assigned to | |
265 | all of them. In order to assign different names to submitted file descriptors, submit them in separate | |
266 | invocations of <function>sd_pid_notify_with_fds()</function>. The name may consist of arbitrary ASCII | |
267 | characters except control characters or <literal>:</literal>. It may not be longer than 255 characters. If a | |
268 | submitted name does not follow these restrictions, it is ignored.</para></listitem> | |
8dd4c05b LP |
269 | </varlistentry> |
270 | ||
798d3a52 ZJS |
271 | </variablelist> |
272 | ||
273 | <para>It is recommended to prefix variable names that are not | |
274 | listed above with <varname>X_</varname> to avoid namespace | |
275 | clashes.</para> | |
276 | ||
277 | <para>Note that systemd will accept status data sent from a | |
278 | service only if the <varname>NotifyAccess=</varname> option is | |
279 | correctly set in the service definition file. See | |
280 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> | |
281 | for details.</para> | |
282 | ||
b3bb6476 LP |
283 | <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only if either |
284 | the sending process is still around at the time PID 1 processes the message, or if the sending process is | |
285 | explicitly runtime-tracked by the service manager. The latter is the case if the service manager originally forked | |
286 | off the process, i.e. on all processes that match <varname>NotifyAccess=</varname><option>main</option> or | |
287 | <varname>NotifyAccess=</varname><option>exec</option>. Conversely, if an auxiliary process of the unit sends an | |
288 | <function>sd_notify()</function> message and immediately exits, the service manager might not be able to properly | |
289 | attribute the message to the unit, and thus will ignore it, even if | |
290 | <varname>NotifyAccess=</varname><option>all</option> is set for it.</para> | |
291 | ||
798d3a52 ZJS |
292 | <para><function>sd_notifyf()</function> is similar to |
293 | <function>sd_notify()</function> but takes a | |
294 | <function>printf()</function>-like format string plus | |
295 | arguments.</para> | |
296 | ||
297 | <para><function>sd_pid_notify()</function> and | |
298 | <function>sd_pid_notifyf()</function> are similar to | |
299 | <function>sd_notify()</function> and | |
300 | <function>sd_notifyf()</function> but take a process ID (PID) to | |
301 | use as originating PID for the message as first argument. This is | |
302 | useful to send notification messages on behalf of other processes, | |
303 | provided the appropriate privileges are available. If the PID | |
b938cb90 | 304 | argument is specified as 0, the process ID of the calling process |
798d3a52 ZJS |
305 | is used, in which case the calls are fully equivalent to |
306 | <function>sd_notify()</function> and | |
307 | <function>sd_notifyf()</function>.</para> | |
308 | ||
309 | <para><function>sd_pid_notify_with_fds()</function> is similar to | |
310 | <function>sd_pid_notify()</function> but takes an additional array | |
311 | of file descriptors. These file descriptors are sent along the | |
312 | notification message to the service manager. This is particularly | |
313 | useful for sending <literal>FDSTORE=1</literal> messages, as | |
314 | described above. The additional arguments are a pointer to the | |
315 | file descriptor array plus the number of file descriptors in the | |
316 | array. If the number of file descriptors is passed as 0, the call | |
317 | is fully equivalent to <function>sd_pid_notify()</function>, i.e. | |
318 | no file descriptors are passed. Note that sending file descriptors | |
319 | to the service manager on messages that do not expect them (i.e. | |
320 | without <literal>FDSTORE=1</literal>) they are immediately closed | |
321 | on reception.</para> | |
322 | </refsect1> | |
323 | ||
324 | <refsect1> | |
325 | <title>Return Value</title> | |
326 | ||
3ceb72e5 LP |
327 | <para>On failure, these calls return a negative errno-style error code. If <varname>$NOTIFY_SOCKET</varname> was |
328 | not set and hence no status message could be sent, 0 is returned. If the status was sent, these functions return a | |
329 | positive value. In order to support both service managers that implement this scheme and those which do not, it is | |
330 | generally recommended to ignore the return value of this call. Note that the return value simply indicates whether | |
331 | the notification message was enqueued properly, it does not reflect whether the message could be processed | |
332 | successfully. Specifically, no error is returned when a file descriptor is attempted to be stored using | |
333 | <varname>FDSTORE=1</varname> but the service is not actually configured to permit storing of file descriptors (see | |
334 | above).</para> | |
798d3a52 ZJS |
335 | </refsect1> |
336 | ||
337 | <refsect1> | |
338 | <title>Notes</title> | |
339 | ||
340 | <xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/> | |
341 | ||
3122633a | 342 | <para>These functions send a single datagram with the |
798d3a52 ZJS |
343 | state string as payload to the <constant>AF_UNIX</constant> socket |
344 | referenced in the <varname>$NOTIFY_SOCKET</varname> environment | |
345 | variable. If the first character of | |
346 | <varname>$NOTIFY_SOCKET</varname> is <literal>@</literal>, the | |
347 | string is understood as Linux abstract namespace socket. The | |
348 | datagram is accompanied by the process credentials of the sending | |
349 | service, using SCM_CREDENTIALS.</para> | |
350 | </refsect1> | |
351 | ||
352 | <refsect1> | |
353 | <title>Environment</title> | |
354 | ||
355 | <variablelist class='environment-variables'> | |
356 | <varlistentry> | |
357 | <term><varname>$NOTIFY_SOCKET</varname></term> | |
358 | ||
359 | <listitem><para>Set by the service manager for supervised | |
360 | processes for status and start-up completion notification. | |
361 | This environment variable specifies the socket | |
362 | <function>sd_notify()</function> talks to. See above for | |
363 | details.</para></listitem> | |
364 | </varlistentry> | |
365 | </variablelist> | |
366 | </refsect1> | |
367 | ||
368 | <refsect1> | |
369 | <title>Examples</title> | |
370 | ||
371 | <example> | |
372 | <title>Start-up Notification</title> | |
373 | ||
374 | <para>When a service finished starting up, it might issue the | |
375 | following call to notify the service manager:</para> | |
376 | ||
377 | <programlisting>sd_notify(0, "READY=1");</programlisting> | |
378 | </example> | |
379 | ||
380 | <example> | |
381 | <title>Extended Start-up Notification</title> | |
382 | ||
383 | <para>A service could send the following after completing | |
384 | initialization:</para> | |
385 | ||
386 | <programlisting>sd_notifyf(0, "READY=1\n" | |
1eecafb8 | 387 | "STATUS=Processing requests…\n" |
798d3a52 ZJS |
388 | "MAINPID=%lu", |
389 | (unsigned long) getpid());</programlisting> | |
390 | </example> | |
391 | ||
392 | <example> | |
393 | <title>Error Cause Notification</title> | |
394 | ||
395 | <para>A service could send the following shortly before exiting, on failure:</para> | |
396 | ||
397 | <programlisting>sd_notifyf(0, "STATUS=Failed to start up: %s\n" | |
398 | "ERRNO=%i", | |
399 | strerror(errno), | |
400 | errno);</programlisting> | |
401 | </example> | |
402 | ||
403 | <example> | |
404 | <title>Store a File Descriptor in the Service Manager</title> | |
405 | ||
406 | <para>To store an open file descriptor in the service manager, | |
407 | in order to continue operation after a service restart without | |
b938cb90 | 408 | losing state, use <literal>FDSTORE=1</literal>:</para> |
798d3a52 | 409 | |
8dd4c05b | 410 | <programlisting>sd_pid_notify_with_fds(0, 0, "FDSTORE=1\nFDNAME=foobar", &fd, 1);</programlisting> |
798d3a52 ZJS |
411 | </example> |
412 | </refsect1> | |
413 | ||
414 | <refsect1> | |
415 | <title>See Also</title> | |
416 | <para> | |
417 | <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, | |
418 | <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
8dd4c05b LP |
419 | <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>, |
420 | <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
421 | <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
798d3a52 | 422 | <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>, |
8dd4c05b | 423 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> |
798d3a52 ZJS |
424 | </para> |
425 | </refsect1> | |
f9378423 LP |
426 | |
427 | </refentry> |