1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3 #ifndef foosddaemonhfoo
4 #define foosddaemonhfoo
7 Copyright 2010 Lennart Poettering
9 Permission is hereby granted, free of charge, to any person
10 obtaining a copy of this software and associated documentation files
11 (the "Software"), to deal in the Software without restriction,
12 including without limitation the rights to use, copy, modify, merge,
13 publish, distribute, sublicense, and/or sell copies of the Software,
14 and to permit persons to whom the Software is furnished to do so,
15 subject to the following conditions:
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <sys/types.h>
33 #include "_sd-common.h"
35 _SD_BEGIN_DECLARATIONS;
38 The following functionality is provided:
40 - Support for logging with log levels on stderr
41 - File descriptor passing for socket-based activation
42 - Daemon startup and status notification
43 - Detection of systemd boots
45 See sd-daemon(3) for more information.
49 Log levels for usage on stderr:
51 fprintf(stderr, SD_NOTICE "Hello World!\n");
53 This is similar to printk() usage in the kernel.
55 #define SD_EMERG "<0>" /* system is unusable */
56 #define SD_ALERT "<1>" /* action must be taken immediately */
57 #define SD_CRIT "<2>" /* critical conditions */
58 #define SD_ERR "<3>" /* error conditions */
59 #define SD_WARNING "<4>" /* warning conditions */
60 #define SD_NOTICE "<5>" /* normal but significant condition */
61 #define SD_INFO "<6>" /* informational */
62 #define SD_DEBUG "<7>" /* debug-level messages */
64 /* The first passed file descriptor is fd 3 */
65 #define SD_LISTEN_FDS_START 3
68 Returns how many file descriptors have been passed, or a negative
69 errno code on failure. Optionally, removes the $LISTEN_FDS and
70 $LISTEN_PID file descriptors from the environment (recommended, but
71 problematic in threaded environments). If r is the return value of
72 this function you'll find the file descriptors passed as fds
73 SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
74 errno style error code on failure. This function call ensures that
75 the FD_CLOEXEC flag is set for the passed file descriptors, to make
76 sure they are not passed on to child processes. If FD_CLOEXEC shall
77 not be set, the caller needs to unset it after this call for all file
78 descriptors that are used.
80 See sd_listen_fds(3) for more information.
82 int sd_listen_fds(int unset_environment);
85 Helper call for identifying a passed file descriptor. Returns 1 if
86 the file descriptor is a FIFO in the file system stored under the
87 specified path, 0 otherwise. If path is NULL a path name check will
88 not be done and the call only verifies if the file descriptor
89 refers to a FIFO. Returns a negative errno style error code on
92 See sd_is_fifo(3) for more information.
94 int sd_is_fifo(int fd, const char *path);
97 Helper call for identifying a passed file descriptor. Returns 1 if
98 the file descriptor is a special character device on the file
99 system stored under the specified path, 0 otherwise.
100 If path is NULL a path name check will not be done and the call
101 only verifies if the file descriptor refers to a special character.
102 Returns a negative errno style error code on failure.
104 See sd_is_special(3) for more information.
106 int sd_is_special(int fd, const char *path);
109 Helper call for identifying a passed file descriptor. Returns 1 if
110 the file descriptor is a socket of the specified family (AF_INET,
111 ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If
112 family is 0 a socket family check will not be done. If type is 0 a
113 socket type check will not be done and the call only verifies if
114 the file descriptor refers to a socket. If listening is > 0 it is
115 verified that the socket is in listening mode. (i.e. listen() has
116 been called) If listening is == 0 it is verified that the socket is
117 not in listening mode. If listening is < 0 no listening mode check
118 is done. Returns a negative errno style error code on failure.
120 See sd_is_socket(3) for more information.
122 int sd_is_socket(int fd, int family, int type, int listening);
125 Helper call for identifying a passed file descriptor. Returns 1 if
126 the file descriptor is an Internet socket, of the specified family
127 (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM,
128 SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version
129 check is not done. If type is 0 a socket type check will not be
130 done. If port is 0 a socket port check will not be done. The
131 listening flag is used the same way as in sd_is_socket(). Returns a
132 negative errno style error code on failure.
134 See sd_is_socket_inet(3) for more information.
136 int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port);
139 Helper call for identifying a passed file descriptor. Returns 1 if
140 the file descriptor is an AF_UNIX socket of the specified type
141 (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
142 a socket type check will not be done. If path is NULL a socket path
143 check will not be done. For normal AF_UNIX sockets set length to
144 0. For abstract namespace sockets set length to the length of the
145 socket name (including the initial 0 byte), and pass the full
146 socket path in path (including the initial 0 byte). The listening
147 flag is used the same way as in sd_is_socket(). Returns a negative
148 errno style error code on failure.
150 See sd_is_socket_unix(3) for more information.
152 int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length);
155 Helper call for identifying a passed file descriptor. Returns 1 if
156 the file descriptor is a POSIX Message Queue of the specified name,
157 0 otherwise. If path is NULL a message queue name check is not
158 done. Returns a negative errno style error code on failure.
160 See sd_is_mq(3) for more information.
162 int sd_is_mq(int fd, const char *path);
165 Informs systemd about changed daemon state. This takes a number of
166 newline separated environment-style variable assignments in a
167 string. The following variables are known:
169 READY=1 Tells systemd that daemon startup is finished (only
170 relevant for services of Type=notify). The passed
171 argument is a boolean "1" or "0". Since there is
172 little value in signaling non-readiness the only
173 value daemons should send is "READY=1".
175 STATUS=... Passes a single-line status string back to systemd
176 that describes the daemon state. This is free-from
177 and can be used for various purposes: general state
178 feedback, fsck-like programs could pass completion
179 percentages and failing programs could pass a human
180 readable error message. Example: "STATUS=Completed
181 66% of file system check..."
183 ERRNO=... If a daemon fails, the errno-style error code,
184 formatted as string. Example: "ERRNO=2" for ENOENT.
186 BUSERROR=... If a daemon fails, the D-Bus error-style error
187 code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
189 MAINPID=... The main pid of a daemon, in case systemd did not
190 fork off the process itself. Example: "MAINPID=4711"
192 WATCHDOG=1 Tells systemd to update the watchdog timestamp.
193 Services using this feature should do this in
194 regular intervals. A watchdog framework can use the
195 timestamps to detect failed services. Also see
196 sd_watchdog_enabled() below.
198 Daemons can choose to send additional variables. However, it is
199 recommended to prefix variable names not listed above with X_.
201 Returns a negative errno-style error code on failure. Returns > 0
202 if systemd could be notified, 0 if it couldn't possibly because
203 systemd is not running.
205 Example: When a daemon finished starting up, it could issue this
206 call to notify systemd about it:
208 sd_notify(0, "READY=1");
210 See sd_notifyf() for more complete examples.
212 See sd_notify(3) for more information.
214 int sd_notify(int unset_environment, const char *state);
217 Similar to sd_notify() but takes a format string.
219 Example 1: A daemon could send the following after initialization:
221 sd_notifyf(0, "READY=1\n"
222 "STATUS=Processing requests...\n"
224 (unsigned long) getpid());
226 Example 2: A daemon could send the following shortly before
229 sd_notifyf(0, "STATUS=Failed to start up: %s\n"
234 See sd_notifyf(3) for more information.
236 int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_(2,3);
239 Returns > 0 if the system was booted with systemd. Returns < 0 on
240 error. Returns 0 if the system was not booted with systemd. Note
241 that all of the functions above handle non-systemd boots just
242 fine. You should NOT protect them with a call to this function. Also
243 note that this function checks whether the system, not the user
244 session is controlled by systemd. However the functions above work
245 for both user and system services.
247 See sd_booted(3) for more information.
252 Returns > 0 if the service manager expects watchdog keep-alive
253 events to be sent regularly via sd_notify(0, "WATCHDOG=1"). Returns
254 0 if it does not expect this. If the usec argument is non-NULL
255 returns the watchdog timeout in µs after which the service manager
256 will act on a process that has not sent a watchdog keep alive
257 message. This function is useful to implement services that
258 recognize automatically if they are being run under supervision of
259 systemd with WatchdogSec= set. It is recommended for clients to
260 generate keep-alive pings via sd_notify(0, "WATCHDOG=1") every half
261 of the returned time.
263 See sd_watchdog_enabled(3) for more information.
265 int sd_watchdog_enabled(int unset_environment, uint64_t *usec);
267 _SD_END_DECLARATIONS;