|
Line 0
Link Here
|
|
|
1 |
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
| 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 |
|
| 30 |
#include <sys/types.h> |
| 31 |
#include <inttypes.h> |
| 32 |
|
| 33 |
#ifdef __cplusplus |
| 34 |
extern "C" { |
| 35 |
#endif |
| 36 |
|
| 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 |
| 52 |
support. This makes all those calls NOPs that are directly related to |
| 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. |
| 66 |
|
| 67 |
See sd-daemon(7) for more information. |
| 68 |
*/ |
| 69 |
|
| 70 |
#if (__GNUC__ >= 4) |
| 71 |
#define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b))) |
| 72 |
# if defined(SD_EXPORT_SYMBOLS) |
| 73 |
# define _sd_hidden_ |
| 74 |
# else |
| 75 |
# define _sd_hidden_ __attribute__ ((visibility("hidden"))) |
| 76 |
# endif |
| 77 |
#else |
| 78 |
#define _sd_printf_attr_(a,b) |
| 79 |
#define _sd_hidden_ |
| 80 |
#endif |
| 81 |
|
| 82 |
/* |
| 83 |
Log levels for usage on stderr: |
| 84 |
|
| 85 |
fprintf(stderr, SD_NOTICE "Hello World!\n"); |
| 86 |
|
| 87 |
This is similar to printk() usage in the kernel. |
| 88 |
*/ |
| 89 |
#define SD_EMERG "<0>" /* system is unusable */ |
| 90 |
#define SD_ALERT "<1>" /* action must be taken immediately */ |
| 91 |
#define SD_CRIT "<2>" /* critical conditions */ |
| 92 |
#define SD_ERR "<3>" /* error conditions */ |
| 93 |
#define SD_WARNING "<4>" /* warning conditions */ |
| 94 |
#define SD_NOTICE "<5>" /* normal but significant condition */ |
| 95 |
#define SD_INFO "<6>" /* informational */ |
| 96 |
#define SD_DEBUG "<7>" /* debug-level messages */ |
| 97 |
|
| 98 |
/* The first passed file descriptor is fd 3 */ |
| 99 |
#define SD_LISTEN_FDS_START 3 |
| 100 |
|
| 101 |
/* |
| 102 |
Returns how many file descriptors have been passed, or a negative |
| 103 |
errno code on failure. Optionally, removes the $LISTEN_FDS and |
| 104 |
$LISTEN_PID file descriptors from the environment (recommended, but |
| 105 |
problematic in threaded environments). If r is the return value of |
| 106 |
this function you'll find the file descriptors passed as fds |
| 107 |
SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative |
| 108 |
errno style error code on failure. This function call ensures that |
| 109 |
the FD_CLOEXEC flag is set for the passed file descriptors, to make |
| 110 |
sure they are not passed on to child processes. If FD_CLOEXEC shall |
| 111 |
not be set, the caller needs to unset it after this call for all file |
| 112 |
descriptors that are used. |
| 113 |
|
| 114 |
See sd_listen_fds(3) for more information. |
| 115 |
*/ |
| 116 |
int sd_listen_fds(int unset_environment) _sd_hidden_; |
| 117 |
|
| 118 |
/* |
| 119 |
Helper call for identifying a passed file descriptor. Returns 1 if |
| 120 |
the file descriptor is a FIFO in the file system stored under the |
| 121 |
specified path, 0 otherwise. If path is NULL a path name check will |
| 122 |
not be done and the call only verifies if the file descriptor |
| 123 |
refers to a FIFO. Returns a negative errno style error code on |
| 124 |
failure. |
| 125 |
|
| 126 |
See sd_is_fifo(3) for more information. |
| 127 |
*/ |
| 128 |
int sd_is_fifo(int fd, const char *path) _sd_hidden_; |
| 129 |
|
| 130 |
/* |
| 131 |
Helper call for identifying a passed file descriptor. Returns 1 if |
| 132 |
the file descriptor is a socket of the specified family (AF_INET, |
| 133 |
...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If |
| 134 |
family is 0 a socket family check will not be done. If type is 0 a |
| 135 |
socket type check will not be done and the call only verifies if |
| 136 |
the file descriptor refers to a socket. If listening is > 0 it is |
| 137 |
verified that the socket is in listening mode. (i.e. listen() has |
| 138 |
been called) If listening is == 0 it is verified that the socket is |
| 139 |
not in listening mode. If listening is < 0 no listening mode check |
| 140 |
is done. Returns a negative errno style error code on failure. |
| 141 |
|
| 142 |
See sd_is_socket(3) for more information. |
| 143 |
*/ |
| 144 |
int sd_is_socket(int fd, int family, int type, int listening) _sd_hidden_; |
| 145 |
|
| 146 |
/* |
| 147 |
Helper call for identifying a passed file descriptor. Returns 1 if |
| 148 |
the file descriptor is an Internet socket, of the specified family |
| 149 |
(either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM, |
| 150 |
SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version |
| 151 |
check is not done. If type is 0 a socket type check will not be |
| 152 |
done. If port is 0 a socket port check will not be done. The |
| 153 |
listening flag is used the same way as in sd_is_socket(). Returns a |
| 154 |
negative errno style error code on failure. |
| 155 |
|
| 156 |
See sd_is_socket_inet(3) for more information. |
| 157 |
*/ |
| 158 |
int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port) _sd_hidden_; |
| 159 |
|
| 160 |
/* |
| 161 |
Helper call for identifying a passed file descriptor. Returns 1 if |
| 162 |
the file descriptor is an AF_UNIX socket of the specified type |
| 163 |
(SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0 |
| 164 |
a socket type check will not be done. If path is NULL a socket path |
| 165 |
check will not be done. For normal AF_UNIX sockets set length to |
| 166 |
0. For abstract namespace sockets set length to the length of the |
| 167 |
socket name (including the initial 0 byte), and pass the full |
| 168 |
socket path in path (including the initial 0 byte). The listening |
| 169 |
flag is used the same way as in sd_is_socket(). Returns a negative |
| 170 |
errno style error code on failure. |
| 171 |
|
| 172 |
See sd_is_socket_unix(3) for more information. |
| 173 |
*/ |
| 174 |
int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length) _sd_hidden_; |
| 175 |
|
| 176 |
/* |
| 177 |
Informs systemd about changed daemon state. This takes a number of |
| 178 |
newline separated environment-style variable assignments in a |
| 179 |
string. The following variables are known: |
| 180 |
|
| 181 |
READY=1 Tells systemd that daemon startup is finished (only |
| 182 |
relevant for services of Type=notify). The passed |
| 183 |
argument is a boolean "1" or "0". Since there is |
| 184 |
little value in signalling non-readiness the only |
| 185 |
value daemons should send is "READY=1". |
| 186 |
|
| 187 |
STATUS=... Passes a single-line status string back to systemd |
| 188 |
that describes the daemon state. This is free-from |
| 189 |
and can be used for various purposes: general state |
| 190 |
feedback, fsck-like programs could pass completion |
| 191 |
percentages and failing programs could pass a human |
| 192 |
readable error message. Example: "STATUS=Completed |
| 193 |
66% of file system check..." |
| 194 |
|
| 195 |
ERRNO=... If a daemon fails, the errno-style error code, |
| 196 |
formatted as string. Example: "ERRNO=2" for ENOENT. |
| 197 |
|
| 198 |
BUSERROR=... If a daemon fails, the D-Bus error-style error |
| 199 |
code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut" |
| 200 |
|
| 201 |
MAINPID=... The main pid of a daemon, in case systemd did not |
| 202 |
fork off the process itself. Example: "MAINPID=4711" |
| 203 |
|
| 204 |
Daemons can choose to send additional variables. However, it is |
| 205 |
recommened to prefix variable names not listed above with X_. |
| 206 |
|
| 207 |
Returns a negative errno-style error code on failure. Returns > 0 |
| 208 |
if systemd could be notified, 0 if it couldn't possibly because |
| 209 |
systemd is not running. |
| 210 |
|
| 211 |
Example: When a daemon finished starting up, it could issue this |
| 212 |
call to notify systemd about it: |
| 213 |
|
| 214 |
sd_notify(0, "READY=1"); |
| 215 |
|
| 216 |
See sd_notifyf() for more complete examples. |
| 217 |
|
| 218 |
See sd_notify(3) for more information. |
| 219 |
*/ |
| 220 |
int sd_notify(int unset_environment, const char *state) _sd_hidden_; |
| 221 |
|
| 222 |
/* |
| 223 |
Similar to sd_notify() but takes a format string. |
| 224 |
|
| 225 |
Example 1: A daemon could send the following after initialization: |
| 226 |
|
| 227 |
sd_notifyf(0, "READY=1\n" |
| 228 |
"STATUS=Processing requests...\n" |
| 229 |
"MAINPID=%lu", |
| 230 |
(unsigned long) getpid()); |
| 231 |
|
| 232 |
Example 2: A daemon could send the following shortly before |
| 233 |
exiting, on failure: |
| 234 |
|
| 235 |
sd_notifyf(0, "STATUS=Failed to start up: %s\n" |
| 236 |
"ERRNO=%i", |
| 237 |
strerror(errno), |
| 238 |
errno); |
| 239 |
|
| 240 |
See sd_notifyf(3) for more information. |
| 241 |
*/ |
| 242 |
int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_attr_(2,3) _sd_hidden_; |
| 243 |
|
| 244 |
/* |
| 245 |
Returns > 0 if the system was booted with systemd. Returns < 0 on |
| 246 |
error. Returns 0 if the system was not booted with systemd. Note |
| 247 |
that all of the functions above handle non-systemd boots just |
| 248 |
fine. You should NOT protect them with a call to this function. Also |
| 249 |
note that this function checks whether the system, not the user |
| 250 |
session is controlled by systemd. However the functions above work |
| 251 |
for both session and system services. |
| 252 |
|
| 253 |
See sd_booted(3) for more information. |
| 254 |
*/ |
| 255 |
int sd_booted(void) _sd_hidden_; |
| 256 |
|
| 257 |
#ifdef __cplusplus |
| 258 |
} |
| 259 |
#endif |
| 260 |
|
| 261 |
#endif |