1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3 #ifndef foosddaemonhfoo
4 #define foosddaemonhfoo
7 This file is part of systemd.
9 Copyright 2013 Lennart Poettering
11 systemd is free software; you can redistribute it and/or modify it
12 under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or
14 (at your option) any later version.
16 systemd is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
28 #include "_sd-common.h"
30 _SD_BEGIN_DECLARATIONS;
33 The following functionality is provided:
35 - Support for logging with log levels on stderr
36 - File descriptor passing for socket-based activation
37 - Daemon startup and status notification
38 - Detection of systemd boots
40 See sd-daemon(3) for more information.
44 Log levels for usage on stderr:
46 fprintf(stderr, SD_NOTICE "Hello World!\n");
48 This is similar to printk() usage in the kernel.
50 #define SD_EMERG "<0>" /* system is unusable */
51 #define SD_ALERT "<1>" /* action must be taken immediately */
52 #define SD_CRIT "<2>" /* critical conditions */
53 #define SD_ERR "<3>" /* error conditions */
54 #define SD_WARNING "<4>" /* warning conditions */
55 #define SD_NOTICE "<5>" /* normal but significant condition */
56 #define SD_INFO "<6>" /* informational */
57 #define SD_DEBUG "<7>" /* debug-level messages */
59 /* The first passed file descriptor is fd 3 */
60 #define SD_LISTEN_FDS_START 3
63 Returns how many file descriptors have been passed, or a negative
64 errno code on failure. Optionally, removes the $LISTEN_FDS and
65 $LISTEN_PID file descriptors from the environment (recommended, but
66 problematic in threaded environments). If r is the return value of
67 this function you'll find the file descriptors passed as fds
68 SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
69 errno style error code on failure. This function call ensures that
70 the FD_CLOEXEC flag is set for the passed file descriptors, to make
71 sure they are not passed on to child processes. If FD_CLOEXEC shall
72 not be set, the caller needs to unset it after this call for all file
73 descriptors that are used.
75 See sd_listen_fds(3) for more information.
77 int sd_listen_fds(int unset_environment);
80 Helper call for identifying a passed file descriptor. Returns 1 if
81 the file descriptor is a FIFO in the file system stored under the
82 specified path, 0 otherwise. If path is NULL a path name check will
83 not be done and the call only verifies if the file descriptor
84 refers to a FIFO. Returns a negative errno style error code on
87 See sd_is_fifo(3) for more information.
89 int sd_is_fifo(int fd, const char *path);
92 Helper call for identifying a passed file descriptor. Returns 1 if
93 the file descriptor is a special character device on the file
94 system stored under the specified path, 0 otherwise.
95 If path is NULL a path name check will not be done and the call
96 only verifies if the file descriptor refers to a special character.
97 Returns a negative errno style error code on failure.
99 See sd_is_special(3) for more information.
101 int sd_is_special(int fd, const char *path);
104 Helper call for identifying a passed file descriptor. Returns 1 if
105 the file descriptor is a socket of the specified family (AF_INET,
106 ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If
107 family is 0 a socket family check will not be done. If type is 0 a
108 socket type check will not be done and the call only verifies if
109 the file descriptor refers to a socket. If listening is > 0 it is
110 verified that the socket is in listening mode. (i.e. listen() has
111 been called) If listening is == 0 it is verified that the socket is
112 not in listening mode. If listening is < 0 no listening mode check
113 is done. Returns a negative errno style error code on failure.
115 See sd_is_socket(3) for more information.
117 int sd_is_socket(int fd, int family, int type, int listening);
120 Helper call for identifying a passed file descriptor. Returns 1 if
121 the file descriptor is an Internet socket, of the specified family
122 (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM,
123 SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version
124 check is not done. If type is 0 a socket type check will not be
125 done. If port is 0 a socket port check will not be done. The
126 listening flag is used the same way as in sd_is_socket(). Returns a
127 negative errno style error code on failure.
129 See sd_is_socket_inet(3) for more information.
131 int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port);
134 Helper call for identifying a passed file descriptor. Returns 1 if
135 the file descriptor is an AF_UNIX socket of the specified type
136 (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
137 a socket type check will not be done. If path is NULL a socket path
138 check will not be done. For normal AF_UNIX sockets set length to
139 0. For abstract namespace sockets set length to the length of the
140 socket name (including the initial 0 byte), and pass the full
141 socket path in path (including the initial 0 byte). The listening
142 flag is used the same way as in sd_is_socket(). Returns a negative
143 errno style error code on failure.
145 See sd_is_socket_unix(3) for more information.
147 int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length);
150 Helper call for identifying a passed file descriptor. Returns 1 if
151 the file descriptor is a POSIX Message Queue of the specified name,
152 0 otherwise. If path is NULL a message queue name check is not
153 done. Returns a negative errno style error code on failure.
155 See sd_is_mq(3) for more information.
157 int sd_is_mq(int fd, const char *path);
160 Informs systemd about changed daemon state. This takes a number of
161 newline separated environment-style variable assignments in a
162 string. The following variables are known:
164 READY=1 Tells systemd that daemon startup is finished (only
165 relevant for services of Type=notify). The passed
166 argument is a boolean "1" or "0". Since there is
167 little value in signaling non-readiness the only
168 value daemons should send is "READY=1".
170 STATUS=... Passes a single-line status string back to systemd
171 that describes the daemon state. This is free-from
172 and can be used for various purposes: general state
173 feedback, fsck-like programs could pass completion
174 percentages and failing programs could pass a human
175 readable error message. Example: "STATUS=Completed
176 66% of file system check..."
178 ERRNO=... If a daemon fails, the errno-style error code,
179 formatted as string. Example: "ERRNO=2" for ENOENT.
181 BUSERROR=... If a daemon fails, the D-Bus error-style error
182 code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
184 MAINPID=... The main pid of a daemon, in case systemd did not
185 fork off the process itself. Example: "MAINPID=4711"
187 WATCHDOG=1 Tells systemd to update the watchdog timestamp.
188 Services using this feature should do this in
189 regular intervals. A watchdog framework can use the
190 timestamps to detect failed services. Also see
191 sd_watchdog_enabled() below.
193 Daemons can choose to send additional variables. However, it is
194 recommended to prefix variable names not listed above with X_.
196 Returns a negative errno-style error code on failure. Returns > 0
197 if systemd could be notified, 0 if it couldn't possibly because
198 systemd is not running.
200 Example: When a daemon finished starting up, it could issue this
201 call to notify systemd about it:
203 sd_notify(0, "READY=1");
205 See sd_notifyf() for more complete examples.
207 See sd_notify(3) for more information.
209 int sd_notify(int unset_environment, const char *state);
212 Similar to sd_notify() but takes a format string.
214 Example 1: A daemon could send the following after initialization:
216 sd_notifyf(0, "READY=1\n"
217 "STATUS=Processing requests...\n"
219 (unsigned long) getpid());
221 Example 2: A daemon could send the following shortly before
224 sd_notifyf(0, "STATUS=Failed to start up: %s\n"
229 See sd_notifyf(3) for more information.
231 int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_(2,3);
234 Similar to sd_notify(), but send the message on behalf of another
235 process, if the appropriate permissions are available.
237 int sd_pid_notify(pid_t pid, int unset_environment, const char *state);
240 Similar to sd_notifyf(), but send the message on behalf of another
241 process, if the appropriate permissions are available.
243 int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) _sd_printf_(3,4);
246 Returns > 0 if the system was booted with systemd. Returns < 0 on
247 error. Returns 0 if the system was not booted with systemd. Note
248 that all of the functions above handle non-systemd boots just
249 fine. You should NOT protect them with a call to this function. Also
250 note that this function checks whether the system, not the user
251 session is controlled by systemd. However the functions above work
252 for both user and system services.
254 See sd_booted(3) for more information.
259 Returns > 0 if the service manager expects watchdog keep-alive
260 events to be sent regularly via sd_notify(0, "WATCHDOG=1"). Returns
261 0 if it does not expect this. If the usec argument is non-NULL
262 returns the watchdog timeout in µs after which the service manager
263 will act on a process that has not sent a watchdog keep alive
264 message. This function is useful to implement services that
265 recognize automatically if they are being run under supervision of
266 systemd with WatchdogSec= set. It is recommended for clients to
267 generate keep-alive pings via sd_notify(0, "WATCHDOG=1") every half
268 of the returned time.
270 See sd_watchdog_enabled(3) for more information.
272 int sd_watchdog_enabled(int unset_environment, uint64_t *usec);
274 _SD_END_DECLARATIONS;