]>
Commit | Line | Data |
---|---|---|
abbbea81 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
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 |
34 | extern "C" { | |
35 | #endif | |
36 | ||
abbbea81 | 37 | /* Reference implementation of a few systemd related interfaces for |
695d1ef1 LP |
38 | * writing daemons. These interfaces are trivial to implement. To |
39 | * simplify porting we provide this reference | |
40 | * implementation. Applications are welcome to reimplement the | |
41 | * algorithms described here, if they do not want to include these two | |
42 | * source files. */ | |
abbbea81 | 43 | |
706243a2 LP |
44 | #ifdef __GNUC__ |
45 | #define _sd_printf_attr_(a,b) __attribute__ ((format (printf, a, b))) | |
46 | #else | |
47 | #define _sd_printf_attr_(a,b) | |
48 | #endif | |
49 | ||
abbbea81 LP |
50 | /* |
51 | Log levels for usage on stderr: | |
52 | ||
53 | fprintf(stderr, SD_NOTICE "Hello World!"); | |
54 | ||
55 | This is similar to printk() usage in the kernel. | |
56 | */ | |
57 | ||
58 | #define SD_EMERG "<0>" /* system is unusable */ | |
59 | #define SD_ALERT "<1>" /* action must be taken immediately */ | |
60 | #define SD_CRIT "<2>" /* critical conditions */ | |
61 | #define SD_ERR "<3>" /* error conditions */ | |
62 | #define SD_WARNING "<4>" /* warning conditions */ | |
63 | #define SD_NOTICE "<5>" /* normal but significant condition */ | |
64 | #define SD_INFO "<6>" /* informational */ | |
65 | #define SD_DEBUG "<7>" /* debug-level messages */ | |
66 | ||
67 | /* The first passed file descriptor is fd 3 */ | |
68 | #define SD_LISTEN_FDS_START 3 | |
69 | ||
70 | /* Returns how many file descriptors have been passed, or a negative | |
695d1ef1 LP |
71 | * errno code on failure. Optionally, removes the $LISTEN_FDS and |
72 | * $LISTEN_PID file descriptors from the environment (recommended, but | |
73 | * problematic in threaded environments). If r is the return value of | |
74 | * this function you'll find the file descriptors passed as fds | |
75 | * SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative | |
76 | * errno style error code on failure. This function call ensures that | |
77 | * the FD_CLOEXEC flag is set for the passed file descriptors, to make | |
78 | * sure they are not passed on to child processes. If FD_CLOEXEC shall | |
79 | * not be set, the caller needs to unset it after this call for all file | |
80 | * descriptors that are used.*/ | |
abbbea81 LP |
81 | int sd_listen_fds(int unset_environment); |
82 | ||
7c394faa LP |
83 | /* Helper call for identifying a passed file descriptor. Returns 1 if |
84 | * the file descriptor is a FIFO in the file system stored under the | |
85 | * specified path, 0 otherwise. If path is NULL a path name check will | |
86 | * not be done and the call only verifies if the file descriptor | |
87 | * refers to a FIFO. Returns a negative errno style error code on | |
88 | * failure. */ | |
89 | int sd_is_fifo(int fd, const char *path); | |
90 | ||
91 | /* Helper call for identifying a passed file descriptor. Returns 1 if | |
88ce42f6 LP |
92 | * the file descriptor is a socket of the specified family (AF_INET, |
93 | * ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If | |
94 | * family is 0 a socket family check will not be done. If type is 0 a | |
95 | * socket type check will not be done and the call only verifies if | |
96 | * the file descriptor refers to a socket. If listening is > 0 it is | |
97 | * verified that the socket is in listening mode. (i.e. listen() has | |
98 | * been called) If listening is == 0 it is verified that the socket is | |
99 | * not in listening mode. If listening is < 0 no listening mode check | |
100 | * is done. Returns a negative errno style error code on failure. */ | |
101 | int sd_is_socket(int fd, int family, int type, int listening); | |
7c394faa LP |
102 | |
103 | /* Helper call for identifying a passed file descriptor. Returns 1 if | |
88ce42f6 | 104 | * the file descriptor is an Internet socket, of the specified family |
695d1ef1 | 105 | * (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM, |
88ce42f6 LP |
106 | * SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version |
107 | * check is not done. If type is 0 a socket type check will not be | |
108 | * done. If port is 0 a socket port check will not be done. The | |
109 | * listening flag is used the same way as in sd_is_socket(). Returns a | |
110 | * negative errno style error code on failure. */ | |
111 | int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port); | |
7c394faa LP |
112 | |
113 | /* Helper call for identifying a passed file descriptor. Returns 1 if | |
114 | * the file descriptor is an AF_UNIX socket of the specified type | |
115 | * (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0 | |
116 | * a socket type check will not be done. If path is NULL a socket path | |
117 | * check will not be done. For normal AF_UNIX sockets set length to | |
118 | * 0. For abstract namespace sockets set length to the length of the | |
cd250a39 LP |
119 | * socket name (including the initial 0 byte), and pass the full |
120 | * socket path in path (including the initial 0 byte). The listening | |
121 | * flag is used the same way as in sd_is_socket(). Returns a negative | |
7c394faa LP |
122 | * errno style error code on failure. */ |
123 | int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length); | |
124 | ||
8c47c732 LP |
125 | /* Informs systemd about changed daemon state. This takes a numeber of |
126 | * newline seperated environment-style variable assignments in a | |
127 | * string. The following strings are known: | |
128 | * | |
129 | * READY=1 Tells systemd that daemon startup is finished (only | |
130 | * relevant for services of Type=notify). The passed | |
131 | * argument is a boolean "1" or "0". Since there is | |
132 | * little value in signalling non-readiness the only | |
133 | * value daemons should send is "READY=1". | |
134 | * | |
08bfb810 LP |
135 | * STATUS=... Passes a single-line status string back to systemd |
136 | * that describes the daemon state. This is free-from | |
137 | * and can be used for various purposes: general state | |
8c47c732 LP |
138 | * feedback, fsck-like programs could pass completion |
139 | * percentages and failing programs could pass a human | |
140 | * readable error message. Example: "STATUS=Completed | |
141 | * 66% of file system check..." | |
142 | * | |
143 | * ERRNO=... If a daemon fails, the errno-style error code, | |
144 | * formatted as string. Example: "ERRNO=2" for ENOENT. | |
145 | * | |
146 | * BUSERROR=... If a daemon fails, the D-Bus error-style error | |
147 | * code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut" | |
148 | * | |
149 | * MAINPID=... The main pid of a daemon, in case systemd did not | |
150 | * fork off the process itself. Example: "MAINPID=4711" | |
151 | * | |
08bfb810 LP |
152 | * Daemons can choose to send additional variables. |
153 | * | |
154 | * Returns a negative errno-style error code on failure. Returns > 0 | |
155 | * if systemd could be notified, 0 if it couldn't possibly because | |
156 | * systemd is not running. | |
157 | * | |
8c47c732 LP |
158 | * See sd_notifyf() for more complete examples. |
159 | */ | |
160 | int sd_notify(int unset_environment, const char *state); | |
161 | ||
162 | /* Similar to sd_send_state() but takes a format string. | |
163 | * | |
164 | * Example 1: A daemon could send the following after initialization: | |
165 | * | |
166 | * sd_notifyf(0, "READY=1\n" | |
167 | * "STATUS=Processing requests...\n" | |
168 | * "MAINPID=%lu", | |
169 | * (unsigned long) getpid()); | |
170 | * | |
171 | * Example 2: A daemon could send the following shortly before | |
172 | * exiting, on failure: | |
173 | * | |
174 | * sd_notifyf(0, "STATUS=Failed to start up: %s\n" | |
175 | * "ERRNO=%i", | |
176 | * strerror(errno), | |
177 | * errno); | |
178 | */ | |
706243a2 | 179 | int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_attr_(2,3); |
8c47c732 LP |
180 | |
181 | #ifdef __cplusplus | |
182 | } | |
183 | #endif | |
184 | ||
abbbea81 | 185 | #endif |