]>
Commit | Line | Data |
---|---|---|
6e866b33 | 1 | <?xml version='1.0'?> |
bb4f798a | 2 | <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
e735f4d4 | 3 | "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> |
bb4f798a | 4 | <!-- SPDX-License-Identifier: LGPL-2.1+ --> |
663996b3 | 5 | |
60f067b4 | 6 | <refentry id="sd_notify" |
e735f4d4 MP |
7 | xmlns:xi="http://www.w3.org/2001/XInclude"> |
8 | ||
9 | <refentryinfo> | |
10 | <title>sd_notify</title> | |
11 | <productname>systemd</productname> | |
e735f4d4 MP |
12 | </refentryinfo> |
13 | ||
14 | <refmeta> | |
15 | <refentrytitle>sd_notify</refentrytitle> | |
16 | <manvolnum>3</manvolnum> | |
17 | </refmeta> | |
18 | ||
19 | <refnamediv> | |
20 | <refname>sd_notify</refname> | |
21 | <refname>sd_notifyf</refname> | |
22 | <refname>sd_pid_notify</refname> | |
23 | <refname>sd_pid_notifyf</refname> | |
24 | <refname>sd_pid_notify_with_fds</refname> | |
25 | <refpurpose>Notify service manager about start-up completion and other service status changes</refpurpose> | |
26 | </refnamediv> | |
27 | ||
28 | <refsynopsisdiv> | |
29 | <funcsynopsis> | |
30 | <funcsynopsisinfo>#include <systemd/sd-daemon.h></funcsynopsisinfo> | |
31 | ||
32 | <funcprototype> | |
33 | <funcdef>int <function>sd_notify</function></funcdef> | |
34 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
35 | <paramdef>const char *<parameter>state</parameter></paramdef> | |
36 | </funcprototype> | |
37 | ||
38 | <funcprototype> | |
39 | <funcdef>int <function>sd_notifyf</function></funcdef> | |
40 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
41 | <paramdef>const char *<parameter>format</parameter></paramdef> | |
2897b343 | 42 | <paramdef>…</paramdef> |
e735f4d4 MP |
43 | </funcprototype> |
44 | ||
45 | <funcprototype> | |
46 | <funcdef>int <function>sd_pid_notify</function></funcdef> | |
47 | <paramdef>pid_t <parameter>pid</parameter></paramdef> | |
48 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
49 | <paramdef>const char *<parameter>state</parameter></paramdef> | |
50 | </funcprototype> | |
51 | ||
52 | <funcprototype> | |
53 | <funcdef>int <function>sd_pid_notifyf</function></funcdef> | |
54 | <paramdef>pid_t <parameter>pid</parameter></paramdef> | |
55 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
56 | <paramdef>const char *<parameter>format</parameter></paramdef> | |
2897b343 | 57 | <paramdef>…</paramdef> |
e735f4d4 MP |
58 | </funcprototype> |
59 | ||
60 | <funcprototype> | |
61 | <funcdef>int <function>sd_pid_notify_with_fds</function></funcdef> | |
62 | <paramdef>pid_t <parameter>pid</parameter></paramdef> | |
63 | <paramdef>int <parameter>unset_environment</parameter></paramdef> | |
64 | <paramdef>const char *<parameter>state</parameter></paramdef> | |
65 | <paramdef>const int *<parameter>fds</parameter></paramdef> | |
66 | <paramdef>unsigned <parameter>n_fds</parameter></paramdef> | |
67 | </funcprototype> | |
68 | </funcsynopsis> | |
69 | </refsynopsisdiv> | |
70 | ||
71 | <refsect1> | |
72 | <title>Description</title> | |
73 | <para><function>sd_notify()</function> may be called by a service | |
74 | to notify the service manager about state changes. It can be used | |
75 | to send arbitrary information, encoded in an | |
db2df898 | 76 | environment-block-like string. Most importantly, it can be used for |
e735f4d4 MP |
77 | start-up completion notification.</para> |
78 | ||
79 | <para>If the <parameter>unset_environment</parameter> parameter is | |
80 | non-zero, <function>sd_notify()</function> will unset the | |
81 | <varname>$NOTIFY_SOCKET</varname> environment variable before | |
82 | returning (regardless of whether the function call itself | |
83 | succeeded or not). Further calls to | |
84 | <function>sd_notify()</function> will then fail, but the variable | |
85 | is no longer inherited by child processes.</para> | |
86 | ||
87 | <para>The <parameter>state</parameter> parameter should contain a | |
88 | newline-separated list of variable assignments, similar in style | |
89 | to an environment block. A trailing newline is implied if none is | |
90 | specified. The string may contain any kind of variable | |
91 | assignments, but the following shall be considered | |
92 | well-known:</para> | |
93 | ||
94 | <variablelist> | |
95 | <varlistentry> | |
96 | <term>READY=1</term> | |
97 | ||
52ad194e MB |
98 | <listitem><para>Tells the service manager that service startup is finished, or the service finished loading its |
99 | configuration. This is only used by systemd if the service definition file has <varname>Type=notify</varname> | |
100 | set. Since there is little value in signaling non-readiness, the only value services should send is | |
101 | <literal>READY=1</literal> (i.e. <literal>READY=0</literal> is not defined).</para></listitem> | |
e735f4d4 MP |
102 | </varlistentry> |
103 | ||
104 | <varlistentry> | |
105 | <term>RELOADING=1</term> | |
106 | ||
107 | <listitem><para>Tells the service manager that the service is | |
108 | reloading its configuration. This is useful to allow the | |
109 | service manager to track the service's internal state, and | |
110 | present it to the user. Note that a service that sends this | |
111 | notification must also send a <literal>READY=1</literal> | |
112 | notification when it completed reloading its | |
f5e65279 MB |
113 | configuration. Reloads are propagated in the same way as they |
114 | are when initiated by the user.</para></listitem> | |
e735f4d4 MP |
115 | </varlistentry> |
116 | ||
117 | <varlistentry> | |
118 | <term>STOPPING=1</term> | |
119 | ||
120 | <listitem><para>Tells the service manager that the service is | |
121 | beginning its shutdown. This is useful to allow the service | |
122 | manager to track the service's internal state, and present it | |
123 | to the user.</para></listitem> | |
124 | </varlistentry> | |
125 | ||
126 | <varlistentry> | |
2897b343 | 127 | <term>STATUS=…</term> |
e735f4d4 MP |
128 | |
129 | <listitem><para>Passes a single-line UTF-8 status string back | |
130 | to the service manager that describes the service state. This | |
131 | is free-form and can be used for various purposes: general | |
132 | state feedback, fsck-like programs could pass completion | |
db2df898 | 133 | percentages and failing programs could pass a human-readable |
e735f4d4 | 134 | error message. Example: <literal>STATUS=Completed 66% of file |
2897b343 | 135 | system check…</literal></para></listitem> |
e735f4d4 MP |
136 | </varlistentry> |
137 | ||
138 | <varlistentry> | |
2897b343 | 139 | <term>ERRNO=…</term> |
e735f4d4 MP |
140 | |
141 | <listitem><para>If a service fails, the errno-style error | |
142 | code, formatted as string. Example: <literal>ERRNO=2</literal> | |
143 | for ENOENT.</para></listitem> | |
144 | </varlistentry> | |
145 | ||
146 | <varlistentry> | |
2897b343 | 147 | <term>BUSERROR=…</term> |
e735f4d4 MP |
148 | |
149 | <listitem><para>If a service fails, the D-Bus error-style | |
150 | error code. Example: | |
151 | <literal>BUSERROR=org.freedesktop.DBus.Error.TimedOut</literal></para></listitem> | |
152 | </varlistentry> | |
153 | ||
154 | <varlistentry> | |
2897b343 | 155 | <term>MAINPID=…</term> |
e735f4d4 MP |
156 | |
157 | <listitem><para>The main process ID (PID) of the service, in | |
158 | case the service manager did not fork off the process itself. | |
159 | Example: <literal>MAINPID=4711</literal></para></listitem> | |
160 | </varlistentry> | |
161 | ||
162 | <varlistentry> | |
163 | <term>WATCHDOG=1</term> | |
164 | ||
165 | <listitem><para>Tells the service manager to update the | |
166 | watchdog timestamp. This is the keep-alive ping that services | |
167 | need to issue in regular intervals if | |
168 | <varname>WatchdogSec=</varname> is enabled for it. See | |
169 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> | |
170 | for information how to enable this functionality and | |
171 | <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry> | |
fb183854 | 172 | for the details of how the service can check whether the |
e735f4d4 MP |
173 | watchdog is enabled. </para></listitem> |
174 | </varlistentry> | |
175 | ||
52ad194e MB |
176 | <varlistentry> |
177 | <term>WATCHDOG_USEC=…</term> | |
178 | ||
179 | <listitem><para>Reset <varname>watchdog_usec</varname> value during runtime. | |
180 | Notice that this is not available when using <function>sd_event_set_watchdog()</function> | |
181 | or <function>sd_watchdog_enabled()</function>. | |
182 | Example : <literal>WATCHDOG_USEC=20000000</literal></para></listitem> | |
183 | </varlistentry> | |
184 | ||
185 | <varlistentry> | |
186 | <term>EXTEND_TIMEOUT_USEC=…</term> | |
187 | ||
188 | <listitem><para>Tells the service manager to extend the startup, runtime or shutdown service timeout | |
189 | corresponding the current state. The value specified is a time in microseconds during which the service must | |
190 | send a new message. A service timeout will occur if the message isn't received, but only if the runtime of the | |
191 | current state is beyond the original maximium times of <varname>TimeoutStartSec=</varname>, <varname>RuntimeMaxSec=</varname>, | |
192 | and <varname>TimeoutStopSec=</varname>. | |
193 | See <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> | |
194 | for effects on the service timeouts.</para></listitem> | |
195 | </varlistentry> | |
e735f4d4 MP |
196 | |
197 | <varlistentry> | |
198 | <term>FDSTORE=1</term> | |
199 | ||
81c58355 MB |
200 | <listitem><para>Stores additional file descriptors in the service manager. File descriptors sent this way will |
201 | be maintained per-service by the service manager and will later be handed back using the usual file descriptor | |
202 | passing logic at the next invocation of the service, see | |
203 | <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>. This is | |
204 | useful for implementing services that can restart after an explicit request or a crash without losing | |
205 | state. Any open sockets and other file descriptors which should not be closed during the restart may be stored | |
206 | this way. Application state can either be serialized to a file in <filename>/run</filename>, or better, stored | |
207 | in a <citerefentry><refentrytitle>memfd_create</refentrytitle><manvolnum>2</manvolnum></citerefentry> memory | |
208 | file descriptor. Note that the service manager will accept messages for a service only if its | |
209 | <varname>FileDescriptorStoreMax=</varname> setting is non-zero (defaults to zero, see | |
210 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry>). If file | |
211 | descriptors sent are pollable (see | |
212 | <citerefentry><refentrytitle>epoll_ctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>), then any | |
213 | <constant>EPOLLHUP</constant> or <constant>EPOLLERR</constant> event seen on them will result in their | |
214 | automatic removal from the store. Multiple arrays of file descriptors may be sent in separate messages, in | |
215 | which case the arrays are combined. Note that the service manager removes duplicate (pointing to the same | |
216 | object) file descriptors before passing them to the service. Use <function>sd_pid_notify_with_fds()</function> | |
217 | to send messages with <literal>FDSTORE=1</literal>, see below.</para></listitem> | |
e735f4d4 MP |
218 | </varlistentry> |
219 | ||
6300502b | 220 | <varlistentry> |
52ad194e | 221 | <term>FDSTOREREMOVE=1</term> |
6300502b | 222 | |
52ad194e MB |
223 | <listitem><para>Removes file descriptors from the file descriptor store. This field needs to be combined with |
224 | <varname>FDNAME=</varname> to specify the name of the file descriptors to remove.</para></listitem> | |
6300502b MP |
225 | </varlistentry> |
226 | ||
5a920b42 | 227 | <varlistentry> |
52ad194e | 228 | <term>FDNAME=…</term> |
5a920b42 | 229 | |
52ad194e MB |
230 | <listitem><para>When used in combination with <varname>FDSTORE=1</varname>, specifies a name for the submitted |
231 | file descriptors. When used with <varname>FDSTOREREMOVE=1</varname>, specifies the name for the file | |
232 | descriptors to remove. This name is passed to the service during activation, and may be queried using | |
233 | <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>. File | |
234 | descriptors submitted without this field set, will implicitly get the name <literal>stored</literal> | |
235 | assigned. Note that, if multiple file descriptors are submitted at once, the specified name will be assigned to | |
236 | all of them. In order to assign different names to submitted file descriptors, submit them in separate | |
237 | invocations of <function>sd_pid_notify_with_fds()</function>. The name may consist of arbitrary ASCII | |
238 | characters except control characters or <literal>:</literal>. It may not be longer than 255 characters. If a | |
239 | submitted name does not follow these restrictions, it is ignored.</para></listitem> | |
5a920b42 MP |
240 | </varlistentry> |
241 | ||
e735f4d4 MP |
242 | </variablelist> |
243 | ||
244 | <para>It is recommended to prefix variable names that are not | |
245 | listed above with <varname>X_</varname> to avoid namespace | |
246 | clashes.</para> | |
247 | ||
248 | <para>Note that systemd will accept status data sent from a | |
249 | service only if the <varname>NotifyAccess=</varname> option is | |
250 | correctly set in the service definition file. See | |
251 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> | |
252 | for details.</para> | |
253 | ||
2897b343 MP |
254 | <para>Note that <function>sd_notify()</function> notifications may be attributed to units correctly only if either |
255 | the sending process is still around at the time PID 1 processes the message, or if the sending process is | |
256 | explicitly runtime-tracked by the service manager. The latter is the case if the service manager originally forked | |
257 | off the process, i.e. on all processes that match <varname>NotifyAccess=</varname><option>main</option> or | |
258 | <varname>NotifyAccess=</varname><option>exec</option>. Conversely, if an auxiliary process of the unit sends an | |
259 | <function>sd_notify()</function> message and immediately exits, the service manager might not be able to properly | |
260 | attribute the message to the unit, and thus will ignore it, even if | |
261 | <varname>NotifyAccess=</varname><option>all</option> is set for it.</para> | |
262 | ||
e735f4d4 MP |
263 | <para><function>sd_notifyf()</function> is similar to |
264 | <function>sd_notify()</function> but takes a | |
265 | <function>printf()</function>-like format string plus | |
266 | arguments.</para> | |
267 | ||
268 | <para><function>sd_pid_notify()</function> and | |
269 | <function>sd_pid_notifyf()</function> are similar to | |
270 | <function>sd_notify()</function> and | |
271 | <function>sd_notifyf()</function> but take a process ID (PID) to | |
272 | use as originating PID for the message as first argument. This is | |
273 | useful to send notification messages on behalf of other processes, | |
274 | provided the appropriate privileges are available. If the PID | |
db2df898 | 275 | argument is specified as 0, the process ID of the calling process |
e735f4d4 MP |
276 | is used, in which case the calls are fully equivalent to |
277 | <function>sd_notify()</function> and | |
278 | <function>sd_notifyf()</function>.</para> | |
279 | ||
280 | <para><function>sd_pid_notify_with_fds()</function> is similar to | |
281 | <function>sd_pid_notify()</function> but takes an additional array | |
282 | of file descriptors. These file descriptors are sent along the | |
283 | notification message to the service manager. This is particularly | |
284 | useful for sending <literal>FDSTORE=1</literal> messages, as | |
285 | described above. The additional arguments are a pointer to the | |
286 | file descriptor array plus the number of file descriptors in the | |
287 | array. If the number of file descriptors is passed as 0, the call | |
288 | is fully equivalent to <function>sd_pid_notify()</function>, i.e. | |
289 | no file descriptors are passed. Note that sending file descriptors | |
290 | to the service manager on messages that do not expect them (i.e. | |
291 | without <literal>FDSTORE=1</literal>) they are immediately closed | |
292 | on reception.</para> | |
293 | </refsect1> | |
294 | ||
295 | <refsect1> | |
296 | <title>Return Value</title> | |
297 | ||
81c58355 MB |
298 | <para>On failure, these calls return a negative errno-style error code. If <varname>$NOTIFY_SOCKET</varname> was |
299 | not set and hence no status message could be sent, 0 is returned. If the status was sent, these functions return a | |
300 | positive value. In order to support both service managers that implement this scheme and those which do not, it is | |
301 | generally recommended to ignore the return value of this call. Note that the return value simply indicates whether | |
302 | the notification message was enqueued properly, it does not reflect whether the message could be processed | |
303 | successfully. Specifically, no error is returned when a file descriptor is attempted to be stored using | |
304 | <varname>FDSTORE=1</varname> but the service is not actually configured to permit storing of file descriptors (see | |
305 | above).</para> | |
e735f4d4 MP |
306 | </refsect1> |
307 | ||
308 | <refsect1> | |
309 | <title>Notes</title> | |
310 | ||
311 | <xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/> | |
312 | ||
db2df898 | 313 | <para>These functions send a single datagram with the |
e735f4d4 MP |
314 | state string as payload to the <constant>AF_UNIX</constant> socket |
315 | referenced in the <varname>$NOTIFY_SOCKET</varname> environment | |
316 | variable. If the first character of | |
317 | <varname>$NOTIFY_SOCKET</varname> is <literal>@</literal>, the | |
318 | string is understood as Linux abstract namespace socket. The | |
319 | datagram is accompanied by the process credentials of the sending | |
320 | service, using SCM_CREDENTIALS.</para> | |
321 | </refsect1> | |
322 | ||
323 | <refsect1> | |
324 | <title>Environment</title> | |
325 | ||
326 | <variablelist class='environment-variables'> | |
327 | <varlistentry> | |
328 | <term><varname>$NOTIFY_SOCKET</varname></term> | |
329 | ||
330 | <listitem><para>Set by the service manager for supervised | |
331 | processes for status and start-up completion notification. | |
332 | This environment variable specifies the socket | |
333 | <function>sd_notify()</function> talks to. See above for | |
334 | details.</para></listitem> | |
335 | </varlistentry> | |
336 | </variablelist> | |
337 | </refsect1> | |
338 | ||
339 | <refsect1> | |
340 | <title>Examples</title> | |
341 | ||
342 | <example> | |
343 | <title>Start-up Notification</title> | |
344 | ||
345 | <para>When a service finished starting up, it might issue the | |
346 | following call to notify the service manager:</para> | |
347 | ||
348 | <programlisting>sd_notify(0, "READY=1");</programlisting> | |
349 | </example> | |
350 | ||
351 | <example> | |
352 | <title>Extended Start-up Notification</title> | |
353 | ||
354 | <para>A service could send the following after completing | |
355 | initialization:</para> | |
356 | ||
357 | <programlisting>sd_notifyf(0, "READY=1\n" | |
2897b343 | 358 | "STATUS=Processing requests…\n" |
e735f4d4 MP |
359 | "MAINPID=%lu", |
360 | (unsigned long) getpid());</programlisting> | |
361 | </example> | |
362 | ||
363 | <example> | |
364 | <title>Error Cause Notification</title> | |
365 | ||
366 | <para>A service could send the following shortly before exiting, on failure:</para> | |
367 | ||
368 | <programlisting>sd_notifyf(0, "STATUS=Failed to start up: %s\n" | |
369 | "ERRNO=%i", | |
370 | strerror(errno), | |
371 | errno);</programlisting> | |
372 | </example> | |
373 | ||
374 | <example> | |
375 | <title>Store a File Descriptor in the Service Manager</title> | |
376 | ||
377 | <para>To store an open file descriptor in the service manager, | |
378 | in order to continue operation after a service restart without | |
db2df898 | 379 | losing state, use <literal>FDSTORE=1</literal>:</para> |
e735f4d4 | 380 | |
6300502b | 381 | <programlisting>sd_pid_notify_with_fds(0, 0, "FDSTORE=1\nFDNAME=foobar", &fd, 1);</programlisting> |
e735f4d4 MP |
382 | </example> |
383 | </refsect1> | |
384 | ||
385 | <refsect1> | |
386 | <title>See Also</title> | |
387 | <para> | |
388 | <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, | |
389 | <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
6300502b MP |
390 | <citerefentry><refentrytitle>sd_listen_fds</refentrytitle><manvolnum>3</manvolnum></citerefentry>, |
391 | <citerefentry><refentrytitle>sd_listen_fds_with_names</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
392 | <citerefentry><refentrytitle>sd_watchdog_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
e735f4d4 | 393 | <citerefentry><refentrytitle>daemon</refentrytitle><manvolnum>7</manvolnum></citerefentry>, |
6300502b | 394 | <citerefentry><refentrytitle>systemd.service</refentrytitle><manvolnum>5</manvolnum></citerefentry> |
e735f4d4 MP |
395 | </para> |
396 | </refsect1> | |
663996b3 MS |
397 | |
398 | </refentry> |