1SD_NOTIFY(3) sd_notify SD_NOTIFY(3)
2
3
4
6 sd_notify, sd_notifyf - Notify init system about start-up completion
7 and other daemon status changes
8
10 #include "sd-daemon.h"
11
12 int sd_notify(int unset_environment, const char *state);
13
14 int sd_notifyf(int unset_environment, const char *format, ...);
15
17 sd_notify() shall be called by a daemon to notify the init system about
18 status changes. It can be used to send arbitrary information, encoded
19 in an environment-block-like string. Most importantly it can be used
20 for start-up completion notification.
21
22 If the unset_environment parameter is non-zero sd_notify() will unset
23 the $NOTIFY_SOCKET environment variable before returning (regardless
24 whether the function call itself succeeded or not). Further calls to
25 sd_notify() will then fail, but the variable is no longer inherited by
26 child processes.
27
28 The state parameter should contain an newline-separated list of
29 variable assignments, similar in style to an environment block. A
30 trailing newline is implied if none is specified. The string may
31 contain any kind of variable assignments, but the following shall be
32 considered well-known:
33
34 READY=1
35 Tells the init system that daemon startup is finished. This is only
36 used by systemd if the service definition file has Type=notify set.
37 The passed argument is a boolean "1" or "0". Since there is little
38 value in signalling non-readiness, the only value daemons should
39 send is "READY=1".
40
41 STATUS=...
42 Passes a single-line status string back to the init system that
43 describes the daemon state. This is free-form and can be used for
44 various purposes: general state feedback, fsck-like programs could
45 pass completion percentages and failing programs could pass a human
46 readable error message. Example: "STATUS=Completed 66% of file
47 system check..."
48
49 ERRNO=...
50 If a daemon fails, the errno-style error code, formatted as string.
51 Example: "ERRNO=2" for ENOENT.
52
53 BUSERROR=...
54 If a daemon fails, the D-Bus error-style error code. Example:
55 "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
56
57 MAINPID=...
58 The main pid of the daemon, in case the init system did not fork
59 off the process itself. Example: "MAINPID=4711"
60
61 It is recommended to prefix variable names that are not shown in the
62 list above with X_ to avoid namespace clashes.
63
64 Note that systemd will accept status data sent from a daemon only if
65 the NotifyAccess= option is correctly set in the service definition
66 file. See systemd.service(5) for details.
67
68 sd_notifyf() is similar to sd_notifyf() but takes a printf()-like
69 format string plus arguments.
70
72 On failure, these calls return a negative errno-style error code. If
73 $NOTIFY_SOCKET was not set and hence no status data could be sent, 0 is
74 returned. If the status was sent these functions return with a positive
75 return value. In order to support both, init systems that implement
76 this scheme and those which donĀ“t, it is generally recommended to
77 ignore the return value of this call.
78
80 These functions are provided by the reference implementation of APIs
81 for new-style daemons and distributed with the systemd package. The
82 algorithms they implement are simple, and can easily be reimplemented
83 in daemons if it is important to support this interface without using
84 the reference implementation.
85
86 Internally, these functions send a single datagram with the state
87 string as payload to the AF_UNIX socket referenced in the
88 $NOTIFY_SOCKET environment variable. If the first character of
89 $NOTIFY_SOCKET is @ the string is understood as Linux abstract
90 namespace socket. The datagram is accompanied by the process
91 credentials of the sending daemon, using SCM_CREDENTIALS.
92
93 For details about the algorithm check the liberally licensed reference
94 implementation sources:
95 http://cgit.freedesktop.org/systemd/tree/src/sd-daemon.c resp.
96 http://cgit.freedesktop.org/systemd/tree/src/sd-daemon.h
97
98 sd_notify() and sd_notifyf() are implemented in the reference
99 implementationĀ“s drop-in sd-daemon.c and sd-daemon.h files. It is
100 recommended that applications consuming these APIs copy the
101 implementation into their source tree. For more details about the
102 reference implementation see sd_daemon(7)
103
104 If -DDISABLE_SYSTEMD is set during compilation this function will
105 always return 0 and otherwise become a NOP.
106
108 $NOTIFY_SOCKET
109 Set by the init system for supervised processes for status and
110 start-up completion notification. This environment variable
111 specifies the socket sd_notify() talks to. See above for details.
112
114 Example 1. Start-up Notification
115
116 When a daemon finished starting up, it might issue the following call
117 to notify the init system:
118
119 sd_notify(0, "READY=1");
120
121 Example 2. Extended Start-up Notification
122
123 A daemon could send the following after completing initialization:
124
125 sd_notifyf(0, "READY=1\n"
126 "STATUS=Processing requests...\n"
127 "MAINPID=%lu",
128 (unsigned long) getpid());
129
130 Example 3. Error Cause Notification
131
132 A daemon could send the following shortly before exiting, on failure
133
134 sd_notifyf(0, "STATUS=Failed to start up: %s\n"
135 "ERRNO=%i",
136 strerror(errno),
137 errno);
138
140 systemd(1), sd_daemon(7), daemon(7), systemd.service(5)
141
143 Lennart Poettering <lennart@poettering.net>
144 Developer
145
146
147
148systemd 09/14/2010 SD_NOTIFY(3)