]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/sd-daemon.h
missing: add IP_TRANSPARENT
[thirdparty/systemd.git] / src / sd-daemon.h
CommitLineData
03467c88 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
abbbea81
LP
2
3#ifndef foosddaemonhfoo
4#define foosddaemonhfoo
5
6/***
7 Copyright 2010 Lennart Poettering
8
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:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
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
27 SOFTWARE.
28***/
29
8c47c732 30#include <sys/types.h>
7c394faa
LP
31#include <inttypes.h>
32
8c47c732
LP
33#ifdef __cplusplus
34extern "C" {
35#endif
36
40473a70
LP
37/*
38 Reference implementation of a few systemd related interfaces for
39 writing daemons. These interfaces are trivial to implement. To
40 simplify porting we provide this reference implementation.
41 Applications are welcome to reimplement the algorithms described
42 here if they do not want to include these two source files.
43
44 The following functionality is provided:
45
46 - Support for logging with log levels on stderr
47 - File descriptor passing for socket-based activation
48 - Daemon startup and status notification
49 - Detection of systemd boots
50
51 You may compile this with -DDISABLE_SYSTEMD to disable systemd
1d0ae74a 52 support. This makes all those calls NOPs that are directly related to
40473a70
LP
53 systemd (i.e. only sd_is_xxx() will stay useful).
54
55 Since this is drop-in code we don't want any of our symbols to be
56 exported in any case. Hence we declare hidden visibility for all of
57 them.
58
59 You may find an up-to-date version of these source files online:
60
61 http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.h
62 http://cgit.freedesktop.org/systemd/plain/src/sd-daemon.c
63
64 This should compile on non-Linux systems, too, but with the
65 exception of the sd_is_xxx() calls all functions will become NOPs.
daaa7e5a
LP
66
67 See sd-daemon(7) for more information.
40473a70
LP
68*/
69
d0b48809 70#ifndef _sd_printf_attr_
e702c2c0 71#if __GNUC__ >= 4
706243a2
LP
72#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b)))
73#else
74#define _sd_printf_attr_(a,b)
e702c2c0 75#endif
d0b48809 76#endif
e702c2c0 77
d0b48809 78#ifndef _sd_hidden_
e702c2c0
RG
79#if (__GNUC__ >= 4) && !defined(SD_EXPORT_SYMBOLS)
80#define _sd_hidden_ __attribute__ ((visibility("hidden")))
81#else
40473a70 82#define _sd_hidden_
706243a2 83#endif
d0b48809 84#endif
706243a2 85
abbbea81
LP
86/*
87 Log levels for usage on stderr:
88
f9378423 89 fprintf(stderr, SD_NOTICE "Hello World!\n");
abbbea81
LP
90
91 This is similar to printk() usage in the kernel.
92*/
abbbea81
LP
93#define SD_EMERG "<0>" /* system is unusable */
94#define SD_ALERT "<1>" /* action must be taken immediately */
95#define SD_CRIT "<2>" /* critical conditions */
96#define SD_ERR "<3>" /* error conditions */
97#define SD_WARNING "<4>" /* warning conditions */
98#define SD_NOTICE "<5>" /* normal but significant condition */
99#define SD_INFO "<6>" /* informational */
100#define SD_DEBUG "<7>" /* debug-level messages */
101
102/* The first passed file descriptor is fd 3 */
103#define SD_LISTEN_FDS_START 3
104
40473a70
LP
105/*
106 Returns how many file descriptors have been passed, or a negative
107 errno code on failure. Optionally, removes the $LISTEN_FDS and
108 $LISTEN_PID file descriptors from the environment (recommended, but
109 problematic in threaded environments). If r is the return value of
110 this function you'll find the file descriptors passed as fds
111 SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
112 errno style error code on failure. This function call ensures that
113 the FD_CLOEXEC flag is set for the passed file descriptors, to make
114 sure they are not passed on to child processes. If FD_CLOEXEC shall
115 not be set, the caller needs to unset it after this call for all file
116 descriptors that are used.
daaa7e5a
LP
117
118 See sd_listen_fds(3) for more information.
40473a70
LP
119*/
120int sd_listen_fds(int unset_environment) _sd_hidden_;
121
122/*
123 Helper call for identifying a passed file descriptor. Returns 1 if
124 the file descriptor is a FIFO in the file system stored under the
125 specified path, 0 otherwise. If path is NULL a path name check will
126 not be done and the call only verifies if the file descriptor
127 refers to a FIFO. Returns a negative errno style error code on
128 failure.
daaa7e5a
LP
129
130 See sd_is_fifo(3) for more information.
40473a70
LP
131*/
132int sd_is_fifo(int fd, const char *path) _sd_hidden_;
133
134/*
135 Helper call for identifying a passed file descriptor. Returns 1 if
136 the file descriptor is a socket of the specified family (AF_INET,
137 ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If
138 family is 0 a socket family check will not be done. If type is 0 a
139 socket type check will not be done and the call only verifies if
140 the file descriptor refers to a socket. If listening is > 0 it is
141 verified that the socket is in listening mode. (i.e. listen() has
142 been called) If listening is == 0 it is verified that the socket is
143 not in listening mode. If listening is < 0 no listening mode check
144 is done. Returns a negative errno style error code on failure.
daaa7e5a
LP
145
146 See sd_is_socket(3) for more information.
40473a70
LP
147*/
148int sd_is_socket(int fd, int family, int type, int listening) _sd_hidden_;
149
150/*
151 Helper call for identifying a passed file descriptor. Returns 1 if
152 the file descriptor is an Internet socket, of the specified family
153 (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM,
154 SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version
155 check is not done. If type is 0 a socket type check will not be
156 done. If port is 0 a socket port check will not be done. The
157 listening flag is used the same way as in sd_is_socket(). Returns a
158 negative errno style error code on failure.
daaa7e5a
LP
159
160 See sd_is_socket_inet(3) for more information.
40473a70
LP
161*/
162int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) _sd_hidden_;
163
164/*
165 Helper call for identifying a passed file descriptor. Returns 1 if
166 the file descriptor is an AF_UNIX socket of the specified type
167 (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
168 a socket type check will not be done. If path is NULL a socket path
169 check will not be done. For normal AF_UNIX sockets set length to
170 0. For abstract namespace sockets set length to the length of the
171 socket name (including the initial 0 byte), and pass the full
172 socket path in path (including the initial 0 byte). The listening
173 flag is used the same way as in sd_is_socket(). Returns a negative
174 errno style error code on failure.
daaa7e5a
LP
175
176 See sd_is_socket_unix(3) for more information.
40473a70
LP
177*/
178int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) _sd_hidden_;
179
916abb21
LP
180/*
181 Helper call for identifying a passed file descriptor. Returns 1 if
182 the file descriptor is a POSIX Message Queue of the specified name,
183 0 otherwise. If path is NULL a message queue name check is not
184 done. Returns a negative errno style error code on failure.
185*/
186int sd_is_mq(int fd, const char *path) _sd_hidden_;
187
40473a70 188/*
f9378423 189 Informs systemd about changed daemon state. This takes a number of
96d4ce01 190 newline separated environment-style variable assignments in a
f9378423 191 string. The following variables are known:
40473a70
LP
192
193 READY=1 Tells systemd that daemon startup is finished (only
194 relevant for services of Type=notify). The passed
195 argument is a boolean "1" or "0". Since there is
35b8ca3a 196 little value in signaling non-readiness the only
40473a70
LP
197 value daemons should send is "READY=1".
198
199 STATUS=... Passes a single-line status string back to systemd
200 that describes the daemon state. This is free-from
201 and can be used for various purposes: general state
202 feedback, fsck-like programs could pass completion
203 percentages and failing programs could pass a human
204 readable error message. Example: "STATUS=Completed
205 66% of file system check..."
206
207 ERRNO=... If a daemon fails, the errno-style error code,
208 formatted as string. Example: "ERRNO=2" for ENOENT.
209
210 BUSERROR=... If a daemon fails, the D-Bus error-style error
211 code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
212
213 MAINPID=... The main pid of a daemon, in case systemd did not
214 fork off the process itself. Example: "MAINPID=4711"
215
f9378423 216 Daemons can choose to send additional variables. However, it is
35b8ca3a 217 recommended to prefix variable names not listed above with X_.
40473a70
LP
218
219 Returns a negative errno-style error code on failure. Returns > 0
220 if systemd could be notified, 0 if it couldn't possibly because
221 systemd is not running.
222
223 Example: When a daemon finished starting up, it could issue this
224 call to notify systemd about it:
225
226 sd_notify(0, "READY=1");
227
228 See sd_notifyf() for more complete examples.
daaa7e5a
LP
229
230 See sd_notify(3) for more information.
40473a70
LP
231*/
232int sd_notify(int unset_environment, const char *state) _sd_hidden_;
233
234/*
235 Similar to sd_notify() but takes a format string.
236
237 Example 1: A daemon could send the following after initialization:
238
239 sd_notifyf(0, "READY=1\n"
240 "STATUS=Processing requests...\n"
241 "MAINPID=%lu",
242 (unsigned long) getpid());
243
244 Example 2: A daemon could send the following shortly before
245 exiting, on failure:
246
247 sd_notifyf(0, "STATUS=Failed to start up: %s\n"
248 "ERRNO=%i",
249 strerror(errno),
250 errno);
daaa7e5a
LP
251
252 See sd_notifyf(3) for more information.
40473a70
LP
253*/
254int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_attr_(2,3) _sd_hidden_;
255
256/*
257 Returns > 0 if the system was booted with systemd. Returns < 0 on
258 error. Returns 0 if the system was not booted with systemd. Note
259 that all of the functions above handle non-systemd boots just
260 fine. You should NOT protect them with a call to this function. Also
261 note that this function checks whether the system, not the user
262 session is controlled by systemd. However the functions above work
af2d49f7 263 for both user and system services.
daaa7e5a
LP
264
265 See sd_booted(3) for more information.
40473a70
LP
266*/
267int sd_booted(void) _sd_hidden_;
8c47c732
LP
268
269#ifdef __cplusplus
270}
271#endif
272
abbbea81 273#endif